blob: 67f9c74ac033d47c30fc2d43554ca011ab0fdf97 [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"
Bob Wilsona4c22902009-04-17 19:07:39 +000026#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng078b0b02011-01-08 01:24:27 +000027#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Cheng10043e22007-01-19 07:51:42 +000028#include "llvm/CodeGen/MachineBasicBlock.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000032#include "llvm/CodeGen/MachineJumpTableInfo.h"
Bill Wendling202803e2011-10-05 00:02:33 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng10043e22007-01-19 07:51:42 +000035#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000036#include "llvm/IR/CallingConv.h"
37#include "llvm/IR/Constants.h"
38#include "llvm/IR/Function.h"
39#include "llvm/IR/GlobalValue.h"
Tim Northover037f26f22014-04-17 18:22:47 +000040#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000041#include "llvm/IR/Instruction.h"
42#include "llvm/IR/Instructions.h"
43#include "llvm/IR/Intrinsics.h"
44#include "llvm/IR/Type.h"
Bill Wendling46ffefc2010-03-09 02:46:12 +000045#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach32bb3622010-04-14 22:28:31 +000046#include "llvm/Support/CommandLine.h"
Oliver Stannardc24f2172014-05-09 14:01:47 +000047#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000048#include "llvm/Support/ErrorHandling.h"
Evan Cheng2150b922007-03-12 23:30:29 +000049#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000050#include "llvm/Target/TargetOptions.h"
David Peixottoc32e24a2013-10-17 19:49:22 +000051#include <utility>
Evan Cheng10043e22007-01-19 07:51:42 +000052using namespace llvm;
53
Chandler Carruth84e68b22014-04-22 02:41:26 +000054#define DEBUG_TYPE "arm-isel"
55
Dale Johannesend679ff72010-06-03 21:09:53 +000056STATISTIC(NumTailCalls, "Number of tail calls");
Evan Cheng68aec142011-01-19 02:16:49 +000057STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren9f911162012-06-01 02:44:42 +000058STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesend679ff72010-06-03 21:09:53 +000059
Eric Christopher347f4c32010-12-15 23:47:29 +000060cl::opt<bool>
Jim Grosbach32bb3622010-04-14 22:28:31 +000061EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng25f93642010-07-08 02:08:50 +000062 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbach32bb3622010-04-14 22:28:31 +000063 cl::init(false));
64
Evan Chengf128bdc2010-06-16 07:35:02 +000065static cl::opt<bool>
66ARMInterworking("arm-interworking", cl::Hidden,
67 cl::desc("Enable / disable ARM interworking (for debugging only)"),
68 cl::init(true));
69
Benjamin Kramer7ba71be2011-11-26 23:01:57 +000070namespace {
Cameron Zwarich89019782011-06-10 20:59:24 +000071 class ARMCCState : public CCState {
72 public:
73 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Eric Christopherb5217502014-08-06 18:45:26 +000074 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
75 ParmContext PC)
76 : CCState(CC, isVarArg, MF, locs, C) {
Cameron Zwarich89019782011-06-10 20:59:24 +000077 assert(((PC == Call) || (PC == Prologue)) &&
78 "ARMCCState users must specify whether their context is call"
79 "or prologue generation.");
80 CallOrPrologue = PC;
81 }
82 };
83}
84
Stuart Hastings45fe3c32011-04-20 16:47:52 +000085// The APCS parameter registers.
Craig Topper840beec2014-04-04 05:16:06 +000086static const MCPhysReg GPRArgRegs[] = {
Stuart Hastings45fe3c32011-04-20 16:47:52 +000087 ARM::R0, ARM::R1, ARM::R2, ARM::R3
88};
89
Craig Topper4fa625f2012-08-12 03:16:37 +000090void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
91 MVT PromotedBitwiseVT) {
Bob Wilson2e076c42009-06-22 23:27:02 +000092 if (VT != PromotedLdStVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +000093 setOperationAction(ISD::LOAD, VT, Promote);
94 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000095
Craig Topper4fa625f2012-08-12 03:16:37 +000096 setOperationAction(ISD::STORE, VT, Promote);
97 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000098 }
99
Craig Topper4fa625f2012-08-12 03:16:37 +0000100 MVT ElemTy = VT.getVectorElementType();
Owen Anderson9f944592009-08-11 20:47:22 +0000101 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Craig Topper4fa625f2012-08-12 03:16:37 +0000102 setOperationAction(ISD::SETCC, VT, Custom);
103 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
104 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000105 if (ElemTy == MVT::i32) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000106 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
107 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
108 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
109 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000110 } else {
Craig Topper4fa625f2012-08-12 03:16:37 +0000111 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
112 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
113 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
114 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson5d8cfb22009-09-16 20:20:44 +0000115 }
Craig Topper4fa625f2012-08-12 03:16:37 +0000116 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
117 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
118 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
119 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
120 setOperationAction(ISD::SELECT, VT, Expand);
121 setOperationAction(ISD::SELECT_CC, VT, Expand);
Jim Grosbach30af4422012-10-12 22:59:21 +0000122 setOperationAction(ISD::VSELECT, VT, Expand);
Craig Topper4fa625f2012-08-12 03:16:37 +0000123 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000124 if (VT.isInteger()) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000125 setOperationAction(ISD::SHL, VT, Custom);
126 setOperationAction(ISD::SRA, VT, Custom);
127 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson2e076c42009-06-22 23:27:02 +0000128 }
129
130 // Promote all bit-wise operations.
131 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000132 setOperationAction(ISD::AND, VT, Promote);
133 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
134 setOperationAction(ISD::OR, VT, Promote);
135 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
136 setOperationAction(ISD::XOR, VT, Promote);
137 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000138 }
Bob Wilson4ed397c2009-09-16 00:17:28 +0000139
140 // Neon does not support vector divide/remainder operations.
Craig Topper4fa625f2012-08-12 03:16:37 +0000141 setOperationAction(ISD::SDIV, VT, Expand);
142 setOperationAction(ISD::UDIV, VT, Expand);
143 setOperationAction(ISD::FDIV, VT, Expand);
144 setOperationAction(ISD::SREM, VT, Expand);
145 setOperationAction(ISD::UREM, VT, Expand);
146 setOperationAction(ISD::FREM, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000147}
148
Craig Topper4fa625f2012-08-12 03:16:37 +0000149void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000150 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000151 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000152}
153
Craig Topper4fa625f2012-08-12 03:16:37 +0000154void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000155 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000156 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000157}
158
Eric Christopher1889fdc2015-01-29 00:19:39 +0000159ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM,
160 const ARMSubtarget &STI)
161 : TargetLowering(TM), Subtarget(&STI) {
162 RegInfo = Subtarget->getRegisterInfo();
163 Itins = Subtarget->getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000164
Duncan Sandsf2641e12011-09-06 19:07:46 +0000165 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
166
Tim Northoverd6a729b2014-01-06 14:28:05 +0000167 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000168 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000169 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
Tim Northover978d25f2014-04-22 10:10:09 +0000170 Subtarget->hasARMOps() && !TM.Options.UseSoftFloat) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000171 // Single-precision floating-point arithmetic.
172 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
173 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
174 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
175 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000176
Evan Chengc9f22fd12007-04-27 08:15:43 +0000177 // Double-precision floating-point arithmetic.
178 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
179 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
180 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
181 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng143576d2007-01-31 09:30:58 +0000182
Evan Chengc9f22fd12007-04-27 08:15:43 +0000183 // Single-precision comparisons.
184 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
185 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
186 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
187 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
188 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
189 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
190 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
191 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000192
Evan Chengc9f22fd12007-04-27 08:15:43 +0000193 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
194 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
195 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
196 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
197 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
198 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
199 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
200 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng143576d2007-01-31 09:30:58 +0000201
Evan Chengc9f22fd12007-04-27 08:15:43 +0000202 // Double-precision comparisons.
203 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
204 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
205 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
206 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
207 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
208 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
209 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
210 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000211
Evan Chengc9f22fd12007-04-27 08:15:43 +0000212 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
215 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
216 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
217 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
218 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
219 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Cheng10043e22007-01-19 07:51:42 +0000220
Evan Chengc9f22fd12007-04-27 08:15:43 +0000221 // Floating-point to integer conversions.
222 // i64 conversions are done via library routines even when generating VFP
223 // instructions, so use the same ones.
224 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
225 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
226 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
227 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000228
Evan Chengc9f22fd12007-04-27 08:15:43 +0000229 // Conversions between floating types.
230 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
231 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
232
233 // Integer to floating-point conversions.
234 // i64 conversions are done via library routines even when generating VFP
235 // instructions, so use the same ones.
Bob Wilsondc40d5a2009-03-20 23:16:43 +0000236 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
237 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengc9f22fd12007-04-27 08:15:43 +0000238 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
239 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
240 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
241 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
242 }
Evan Cheng10043e22007-01-19 07:51:42 +0000243 }
244
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000245 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000246 setLibcallName(RTLIB::SHL_I128, nullptr);
247 setLibcallName(RTLIB::SRL_I128, nullptr);
248 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000249
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000250 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetMachO() &&
251 !Subtarget->isTargetWindows()) {
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000252 static const struct {
253 const RTLIB::Libcall Op;
254 const char * const Name;
255 const CallingConv::ID CC;
256 const ISD::CondCode Cond;
257 } LibraryCalls[] = {
258 // Double-precision floating-point arithmetic helper functions
259 // RTABI chapter 4.1.2, Table 2
260 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
261 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
262 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
263 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000264
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000265 // Double-precision floating-point comparison helper functions
266 // RTABI chapter 4.1.2, Table 3
267 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
268 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
269 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
270 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
271 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
272 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
273 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
274 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000275
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000276 // Single-precision floating-point arithmetic helper functions
277 // RTABI chapter 4.1.2, Table 4
278 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
279 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
280 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
281 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000282
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000283 // Single-precision floating-point comparison helper functions
284 // RTABI chapter 4.1.2, Table 5
285 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
286 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
287 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
288 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
289 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
290 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
291 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
292 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000293
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000294 // Floating-point to integer conversions.
295 // RTABI chapter 4.1.2, Table 6
296 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
297 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
298 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
299 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
300 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
301 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
302 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
303 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000304
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000305 // Conversions between floating types.
306 // RTABI chapter 4.1.2, Table 7
307 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000308 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Chad Rosierad7c9102014-08-23 18:29:43 +0000309 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000310
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000311 // Integer to floating-point conversions.
312 // RTABI chapter 4.1.2, Table 8
313 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
314 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
315 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
316 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
317 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
318 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
319 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
320 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000321
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000322 // Long long helper functions
323 // RTABI chapter 4.2, Table 9
Chad Rosierad7c9102014-08-23 18:29:43 +0000324 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
325 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
326 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
327 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000328
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000329 // Integer division functions
330 // RTABI chapter 4.3.1
Chad Rosierad7c9102014-08-23 18:29:43 +0000331 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
332 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
333 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
334 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
335 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
336 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
337 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
338 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Renato Golin4cd51872011-05-22 21:41:23 +0000339
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000340 // Memory operations
341 // RTABI chapter 4.3.4
342 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
343 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
344 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
345 };
346
347 for (const auto &LC : LibraryCalls) {
348 setLibcallName(LC.Op, LC.Name);
349 setLibcallCallingConv(LC.Op, LC.CC);
350 if (LC.Cond != ISD::SETCC_INVALID)
351 setCmpLibcallCC(LC.Op, LC.Cond);
352 }
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000353 }
354
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000355 if (Subtarget->isTargetWindows()) {
356 static const struct {
357 const RTLIB::Libcall Op;
358 const char * const Name;
359 const CallingConv::ID CC;
360 } LibraryCalls[] = {
361 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
362 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
363 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
364 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
365 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
366 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
367 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
368 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
369 };
370
371 for (const auto &LC : LibraryCalls) {
372 setLibcallName(LC.Op, LC.Name);
373 setLibcallCallingConv(LC.Op, LC.CC);
374 }
375 }
376
Bob Wilsonbc158992011-10-07 16:59:21 +0000377 // Use divmod compiler-rt calls for iOS 5.0 and later.
Cameron Esfahani943908b2013-08-29 20:23:14 +0000378 if (Subtarget->getTargetTriple().isiOS() &&
Bob Wilsonbc158992011-10-07 16:59:21 +0000379 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
380 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
381 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
382 }
383
Oliver Stannard11790b22014-08-11 09:12:32 +0000384 // The half <-> float conversion functions are always soft-float, but are
385 // needed for some targets which use a hard-float calling convention by
386 // default.
387 if (Subtarget->isAAPCS_ABI()) {
388 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
389 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
390 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
391 } else {
392 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
393 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
394 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
395 }
396
David Goodwin22c2fba2009-07-08 23:10:31 +0000397 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000398 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000399 else
Craig Topperc7242e02012-04-20 07:30:17 +0000400 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000401 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
402 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000403 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000404 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Evan Cheng10043e22007-01-19 07:51:42 +0000405 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000406
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000407 for (MVT VT : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000408 for (MVT InnerVT : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000409 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000410 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
411 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
412 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
413 }
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000414
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000415 setOperationAction(ISD::MULHS, VT, Expand);
416 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
417 setOperationAction(ISD::MULHU, VT, Expand);
418 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000419
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000420 setOperationAction(ISD::BSWAP, VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000421 }
422
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000423 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000424 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000425
Bob Wilson2e076c42009-06-22 23:27:02 +0000426 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000427 addDRTypeForNEON(MVT::v2f32);
428 addDRTypeForNEON(MVT::v8i8);
429 addDRTypeForNEON(MVT::v4i16);
430 addDRTypeForNEON(MVT::v2i32);
431 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000432
Owen Anderson9f944592009-08-11 20:47:22 +0000433 addQRTypeForNEON(MVT::v4f32);
434 addQRTypeForNEON(MVT::v2f64);
435 addQRTypeForNEON(MVT::v16i8);
436 addQRTypeForNEON(MVT::v8i16);
437 addQRTypeForNEON(MVT::v4i32);
438 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000439
Bob Wilson194a2512009-09-15 23:55:57 +0000440 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
441 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000442 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
443 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000444 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
445 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
446 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000447 // FIXME: Code duplication: FDIV and FREM are expanded always, see
448 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000449 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
450 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000451 // FIXME: Create unittest.
452 // In another words, find a way when "copysign" appears in DAG with vector
453 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000454 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000455 // FIXME: Code duplication: SETCC has custom operation action, see
456 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000457 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000458 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000459 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
460 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
461 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
462 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
463 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
464 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
465 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
466 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
467 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
468 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
469 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
470 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000471 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000472 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
473 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
474 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
475 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
476 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000477 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000478
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000479 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
480 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
481 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
482 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
483 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
484 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
485 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
486 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
487 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
488 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000489 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
490 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
491 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
492 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000493 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000494
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000495 // Mark v2f32 intrinsics.
496 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
497 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
498 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
499 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
500 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
501 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
502 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
503 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
504 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
505 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
506 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
507 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
508 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
509 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
510 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
511
Bob Wilson6cc46572009-09-16 00:32:15 +0000512 // Neon does not support some operations on v1i64 and v2i64 types.
513 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000514 // Custom handling for some quad-vector types to detect VMULL.
515 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
516 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
517 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000518 // Custom handling for some vector types to avoid expensive expansions
519 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
520 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
521 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
522 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000523 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
524 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000525 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000526 // a destination type that is wider than the source, and nor does
527 // it have a FP_TO_[SU]INT instruction with a narrower destination than
528 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000529 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
530 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000531 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
532 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000533
Eli Friedmane6385e62012-11-15 22:44:27 +0000534 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000535 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000536
Evan Chengb4eae132012-12-04 22:41:50 +0000537 // NEON does not have single instruction CTPOP for vectors with element
538 // types wider than 8-bits. However, custom lowering can leverage the
539 // v8i8/v16i8 vcnt instruction.
540 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
541 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
542 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
543 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
544
Jim Grosbach5f215872013-02-27 21:31:12 +0000545 // NEON only has FMA instructions as of VFP4.
546 if (!Subtarget->hasVFP4()) {
547 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
548 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
549 }
550
Bob Wilson06fce872011-02-07 17:43:21 +0000551 setTargetDAGCombine(ISD::INTRINSIC_VOID);
552 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000553 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
554 setTargetDAGCombine(ISD::SHL);
555 setTargetDAGCombine(ISD::SRL);
556 setTargetDAGCombine(ISD::SRA);
557 setTargetDAGCombine(ISD::SIGN_EXTEND);
558 setTargetDAGCombine(ISD::ZERO_EXTEND);
559 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsonc6c13a32010-02-18 06:05:53 +0000560 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000561 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000562 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000563 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
564 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000565 setTargetDAGCombine(ISD::FP_TO_SINT);
566 setTargetDAGCombine(ISD::FP_TO_UINT);
567 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem097106b2011-10-15 20:03:12 +0000568
James Molloy547d4c02012-02-20 09:24:05 +0000569 // It is legal to extload from v4i8 to v4i16 or v4i32.
570 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
571 MVT::v4i16, MVT::v2i16,
572 MVT::v2i32};
573 for (unsigned i = 0; i < 6; ++i) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000574 for (MVT VT : MVT::integer_vector_valuetypes()) {
575 setLoadExtAction(ISD::EXTLOAD, VT, Tys[i], Legal);
576 setLoadExtAction(ISD::ZEXTLOAD, VT, Tys[i], Legal);
577 setLoadExtAction(ISD::SEXTLOAD, VT, Tys[i], Legal);
578 }
James Molloy547d4c02012-02-20 09:24:05 +0000579 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000580 }
581
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000582 // ARM and Thumb2 support UMLAL/SMLAL.
583 if (!Subtarget->isThumb1Only())
584 setTargetDAGCombine(ISD::ADDC);
585
Oliver Stannard51b1d462014-08-21 12:50:31 +0000586 if (Subtarget->isFPOnlySP()) {
587 // When targetting a floating-point unit with only single-precision
588 // operations, f64 is legal for the few double-precision instructions which
589 // are present However, no double-precision operations other than moves,
590 // loads and stores are provided by the hardware.
591 setOperationAction(ISD::FADD, MVT::f64, Expand);
592 setOperationAction(ISD::FSUB, MVT::f64, Expand);
593 setOperationAction(ISD::FMUL, MVT::f64, Expand);
594 setOperationAction(ISD::FMA, MVT::f64, Expand);
595 setOperationAction(ISD::FDIV, MVT::f64, Expand);
596 setOperationAction(ISD::FREM, MVT::f64, Expand);
597 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
598 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
599 setOperationAction(ISD::FNEG, MVT::f64, Expand);
600 setOperationAction(ISD::FABS, MVT::f64, Expand);
601 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
602 setOperationAction(ISD::FSIN, MVT::f64, Expand);
603 setOperationAction(ISD::FCOS, MVT::f64, Expand);
604 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
605 setOperationAction(ISD::FPOW, MVT::f64, Expand);
606 setOperationAction(ISD::FLOG, MVT::f64, Expand);
607 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
608 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
609 setOperationAction(ISD::FEXP, MVT::f64, Expand);
610 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
611 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
612 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
613 setOperationAction(ISD::FRINT, MVT::f64, Expand);
614 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
615 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
616 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
617 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
618 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000619
Evan Cheng6addd652007-05-18 00:19:34 +0000620 computeRegisterProperties();
Evan Cheng10043e22007-01-19 07:51:42 +0000621
Tim Northover4e80b582014-07-18 13:01:19 +0000622 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000623 for (MVT VT : MVT::fp_valuetypes()) {
624 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
625 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
626 }
Tim Northover4e80b582014-07-18 13:01:19 +0000627
628 // ... or truncating stores
629 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
630 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
631 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000632
Duncan Sands95d46ef2008-01-23 20:39:46 +0000633 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000634 for (MVT VT : MVT::integer_valuetypes())
635 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000636
Evan Cheng10043e22007-01-19 07:51:42 +0000637 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000638 if (!Subtarget->isThumb1Only()) {
639 for (unsigned im = (unsigned)ISD::PRE_INC;
640 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000641 setIndexedLoadAction(im, MVT::i1, Legal);
642 setIndexedLoadAction(im, MVT::i8, Legal);
643 setIndexedLoadAction(im, MVT::i16, Legal);
644 setIndexedLoadAction(im, MVT::i32, Legal);
645 setIndexedStoreAction(im, MVT::i1, Legal);
646 setIndexedStoreAction(im, MVT::i8, Legal);
647 setIndexedStoreAction(im, MVT::i16, Legal);
648 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000649 }
Evan Cheng10043e22007-01-19 07:51:42 +0000650 }
651
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000652 setOperationAction(ISD::SADDO, MVT::i32, Custom);
653 setOperationAction(ISD::UADDO, MVT::i32, Custom);
654 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
655 setOperationAction(ISD::USUBO, MVT::i32, Custom);
656
Evan Cheng10043e22007-01-19 07:51:42 +0000657 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000658 setOperationAction(ISD::MUL, MVT::i64, Expand);
659 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000660 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000661 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
662 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000663 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000664 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
665 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000666 setOperationAction(ISD::MULHS, MVT::i32, Expand);
667
Jim Grosbach5d994042009-10-31 19:38:01 +0000668 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000669 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000670 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000671 setOperationAction(ISD::SRL, MVT::i64, Custom);
672 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000673
Evan Chenge8916542011-08-30 01:34:54 +0000674 if (!Subtarget->isThumb1Only()) {
675 // FIXME: We should do this for Thumb1 as well.
676 setOperationAction(ISD::ADDC, MVT::i32, Custom);
677 setOperationAction(ISD::ADDE, MVT::i32, Custom);
678 setOperationAction(ISD::SUBC, MVT::i32, Custom);
679 setOperationAction(ISD::SUBE, MVT::i32, Custom);
680 }
681
Evan Cheng10043e22007-01-19 07:51:42 +0000682 // ARM does not have ROTL.
Owen Anderson9f944592009-08-11 20:47:22 +0000683 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach8546ec92010-01-18 19:58:49 +0000684 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000685 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000686 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000687 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000688
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000689 // These just redirect to CTTZ and CTLZ on ARM.
690 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
691 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
692
Tim Northoverbc933082013-05-23 19:11:20 +0000693 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
694
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000695 // Only ARMv6 has BSWAP.
696 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000697 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000698
Bob Wilsone8a549c2012-09-29 21:43:49 +0000699 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
700 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
701 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbach92d999002010-05-05 20:44:35 +0000702 setOperationAction(ISD::SDIV, MVT::i32, Expand);
703 setOperationAction(ISD::UDIV, MVT::i32, Expand);
704 }
Renato Golin87610692013-07-16 09:32:17 +0000705
706 // FIXME: Also set divmod for SREM on EABI
Chad Rosierad7c9102014-08-23 18:29:43 +0000707 setOperationAction(ISD::SREM, MVT::i32, Expand);
708 setOperationAction(ISD::UREM, MVT::i32, Expand);
709 // Register based DivRem for AEABI (RTABI 4.2)
710 if (Subtarget->isTargetAEABI()) {
711 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
712 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
713 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
714 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
715 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
716 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
717 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
718 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
719
720 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
721 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
722 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
723 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
724 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
725 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
726 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
727 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
728
729 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
730 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
731 } else {
Renato Golin87610692013-07-16 09:32:17 +0000732 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
733 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
734 }
Bob Wilson7117a912009-03-20 22:42:55 +0000735
Owen Anderson9f944592009-08-11 20:47:22 +0000736 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
737 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
738 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
739 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000740 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000741
Evan Cheng74d92c12011-04-08 21:37:21 +0000742 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000743
Evan Cheng10043e22007-01-19 07:51:42 +0000744 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000745 setOperationAction(ISD::VASTART, MVT::Other, Custom);
746 setOperationAction(ISD::VAARG, MVT::Other, Expand);
747 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
748 setOperationAction(ISD::VAEND, MVT::Other, Expand);
749 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
750 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000751
Tim Northoverd6a729b2014-01-06 14:28:05 +0000752 if (!Subtarget->isTargetMachO()) {
753 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000754 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000755 setExceptionPointerRegister(ARM::R0);
756 setExceptionSelectorRegister(ARM::R1);
757 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000758
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000759 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
760 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
761 else
762 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
763
Evan Cheng6e809de2010-08-11 06:22:01 +0000764 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000765 // the default expansion. If we are targeting a single threaded system,
766 // then set them all for expand so we can lower them later into their
767 // non-atomic form.
768 if (TM.Options.ThreadModel == ThreadModel::Single)
769 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
770 else if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000771 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
772 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000773 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000774
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000775 // On v8, we have particularly efficient implementations of atomic fences
776 // if they can be combined with nearby atomic loads and stores.
777 if (!Subtarget->hasV8Ops()) {
Robin Morissetd18cda62014-08-15 22:17:28 +0000778 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000779 setInsertFencesForAtomic(true);
780 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000781 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000782 // If there's anything we can use as a barrier, go through custom lowering
783 // for ATOMIC_FENCE.
784 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
785 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
786
Jim Grosbach6860bb72010-06-18 22:35:32 +0000787 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000788 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000789 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000790 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000791 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000792 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000793 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000794 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000795 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000796 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000797 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000798 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000799 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000800 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
801 // Unordered/Monotonic case.
802 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
803 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000804 }
Evan Cheng10043e22007-01-19 07:51:42 +0000805
Evan Cheng21acf9f2010-11-04 05:19:35 +0000806 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000807
Eli Friedman8cfa7712010-06-26 04:36:50 +0000808 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
809 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000810 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
811 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000812 }
Owen Anderson9f944592009-08-11 20:47:22 +0000813 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000814
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000815 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
816 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000817 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000818 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000819 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000820 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
821 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000822
823 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000824 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbach31984832010-07-07 00:07:57 +0000825 if (Subtarget->isTargetDarwin()) {
826 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
827 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall7d84ece2011-05-29 19:50:32 +0000828 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbach31984832010-07-07 00:07:57 +0000829 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000830
Owen Anderson9f944592009-08-11 20:47:22 +0000831 setOperationAction(ISD::SETCC, MVT::i32, Expand);
832 setOperationAction(ISD::SETCC, MVT::f32, Expand);
833 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000834 setOperationAction(ISD::SELECT, MVT::i32, Custom);
835 setOperationAction(ISD::SELECT, MVT::f32, Custom);
836 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000837 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
838 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
839 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000840
Owen Anderson9f944592009-08-11 20:47:22 +0000841 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
842 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
843 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
844 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
845 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000846
Dan Gohman482732a2007-10-11 23:21:31 +0000847 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000848 setOperationAction(ISD::FSIN, MVT::f64, Expand);
849 setOperationAction(ISD::FSIN, MVT::f32, Expand);
850 setOperationAction(ISD::FCOS, MVT::f32, Expand);
851 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000852 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
853 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000854 setOperationAction(ISD::FREM, MVT::f64, Expand);
855 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000856 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
857 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000858 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
859 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000860 }
Owen Anderson9f944592009-08-11 20:47:22 +0000861 setOperationAction(ISD::FPOW, MVT::f64, Expand);
862 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000863
Evan Chengd0007f32012-04-10 21:40:28 +0000864 if (!Subtarget->hasVFP4()) {
865 setOperationAction(ISD::FMA, MVT::f64, Expand);
866 setOperationAction(ISD::FMA, MVT::f32, Expand);
867 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000868
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000869 // Various VFP goodness
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000870 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilsone4191e72010-03-19 22:51:32 +0000871 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
872 if (Subtarget->hasVFP2()) {
873 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
874 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
875 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
876 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
877 }
Tim Northover53f3bcf2014-07-17 11:27:04 +0000878
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000879 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
880 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
Tim Northover53f3bcf2014-07-17 11:27:04 +0000881 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
882 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
883 }
884
885 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000886 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +0000887 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
888 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000889 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000890 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000891
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000892 // Combine sin / cos into one node or libcall if possible.
893 if (Subtarget->hasSinCos()) {
894 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
895 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Bob Wilson9868d712014-10-09 05:43:30 +0000896 if (Subtarget->getTargetTriple().isiOS()) {
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000897 // For iOS, we don't want to the normal expansion of a libcall to
898 // sincos. We want to issue a libcall to __sincos_stret.
899 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
900 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
901 }
902 }
Evan Cheng10043e22007-01-19 07:51:42 +0000903
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000904 // FP-ARMv8 implements a lot of rounding-like FP operations.
905 if (Subtarget->hasFPARMv8()) {
906 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
907 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
908 setOperationAction(ISD::FROUND, MVT::f32, Legal);
909 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
910 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
911 setOperationAction(ISD::FRINT, MVT::f32, Legal);
912 if (!Subtarget->isFPOnlySP()) {
913 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
914 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
915 setOperationAction(ISD::FROUND, MVT::f64, Legal);
916 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
917 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
918 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Chad Rosierb1bbf6f2014-08-15 21:38:16 +0000919 }
920 }
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000921 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000922 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000923 setTargetDAGCombine(ISD::ADD);
924 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000925 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000926 setTargetDAGCombine(ISD::AND);
927 setTargetDAGCombine(ISD::OR);
928 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000929
Evan Chengf258a152012-02-23 02:58:19 +0000930 if (Subtarget->hasV6Ops())
931 setTargetDAGCombine(ISD::SRL);
932
Evan Cheng10043e22007-01-19 07:51:42 +0000933 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +0000934
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000935 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
936 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +0000937 setSchedulingPreference(Sched::RegPressure);
938 else
939 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +0000940
Evan Cheng3ae2b792011-01-06 06:52:41 +0000941 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000942 MaxStoresPerMemset = 8;
943 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
944 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
945 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
946 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
947 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengb71233f2010-06-26 01:52:05 +0000948
Rafael Espindolaa76eccf2010-07-11 04:01:49 +0000949 // On ARM arguments smaller than 4 bytes are extended, so all arguments
950 // are at least 4 bytes aligned.
951 setMinStackArgumentAlignment(4);
952
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000953 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000954 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000955
Eli Friedman2518f832011-05-06 20:34:06 +0000956 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +0000957}
958
Andrew Trick43f25632011-01-19 02:35:27 +0000959// FIXME: It might make sense to define the representative register class as the
960// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
961// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
962// SPR's representative would be DPR_VFP2. This should work well if register
963// pressure tracking were modified such that a register use would increment the
964// pressure of the register class's representative and all of it's super
965// classes' representatives transitively. We have not implemented this because
966// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000967// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +0000968// and extractions.
Evan Chenga77f3d32010-07-21 06:09:07 +0000969std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000970ARMTargetLowering::findRepresentativeClass(MVT VT) const{
Craig Topper062a2ba2014-04-25 05:30:21 +0000971 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +0000972 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000973 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +0000974 default:
Evan Chenga77f3d32010-07-21 06:09:07 +0000975 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng28590382010-07-21 23:53:58 +0000976 // Use DPR as representative register class for all floating point
977 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
978 // the cost is 1 for both f32 and f64.
979 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +0000980 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +0000981 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +0000982 // When NEON is used for SP, only half of the register file is available
983 // because operations that define both SP and DP results will be constrained
984 // to the VFP2 class (D0-D15). We currently model this constraint prior to
985 // coalescing by double-counting the SP regs. See the FIXME above.
986 if (Subtarget->useNEONForSinglePrecisionFP())
987 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000988 break;
989 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
990 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +0000991 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000992 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000993 break;
994 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000995 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000996 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +0000997 break;
998 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000999 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001000 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001001 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001002 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001003 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001004}
1005
Evan Cheng10043e22007-01-19 07:51:42 +00001006const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
1007 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001008 default: return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001009 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001010 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001011 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
1012 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001013 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001014 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
1015 case ARMISD::tCALL: return "ARMISD::tCALL";
1016 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1017 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001018 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001019 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001020 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001021 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1022 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001023 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001024 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001025 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1026 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001027 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001028 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001029
Evan Cheng10043e22007-01-19 07:51:42 +00001030 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001031
Jim Grosbach8546ec92010-01-18 19:58:49 +00001032 case ARMISD::RBIT: return "ARMISD::RBIT";
1033
Bob Wilsone4191e72010-03-19 22:51:32 +00001034 case ARMISD::FTOSI: return "ARMISD::FTOSI";
1035 case ARMISD::FTOUI: return "ARMISD::FTOUI";
1036 case ARMISD::SITOF: return "ARMISD::SITOF";
1037 case ARMISD::UITOF: return "ARMISD::UITOF";
1038
Evan Cheng10043e22007-01-19 07:51:42 +00001039 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1040 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1041 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001042
Evan Chenge8916542011-08-30 01:34:54 +00001043 case ARMISD::ADDC: return "ARMISD::ADDC";
1044 case ARMISD::ADDE: return "ARMISD::ADDE";
1045 case ARMISD::SUBC: return "ARMISD::SUBC";
1046 case ARMISD::SUBE: return "ARMISD::SUBE";
1047
Bob Wilson22806742010-09-22 22:09:21 +00001048 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1049 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001050
Evan Chengec6d7c92009-10-28 06:55:03 +00001051 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
1052 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
1053
Dale Johannesend679ff72010-06-03 21:09:53 +00001054 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001055
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001056 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001057
Evan Chengb972e562009-08-07 00:34:42 +00001058 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1059
Bob Wilson7ed59712010-10-30 00:54:37 +00001060 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001061
Evan Cheng8740ee32010-11-03 06:34:55 +00001062 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1063
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001064 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
1065
Bob Wilson2e076c42009-06-22 23:27:02 +00001066 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001067 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001068 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001069 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1070 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001071 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1072 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001073 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1074 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001075 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1076 case ARMISD::VTST: return "ARMISD::VTST";
1077
1078 case ARMISD::VSHL: return "ARMISD::VSHL";
1079 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1080 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001081 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1082 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1083 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1084 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1085 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1086 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1087 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1088 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1089 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1090 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1091 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1092 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
1093 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1094 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001095 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001096 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001097 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001098 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001099 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001100 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001101 case ARMISD::VREV64: return "ARMISD::VREV64";
1102 case ARMISD::VREV32: return "ARMISD::VREV32";
1103 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001104 case ARMISD::VZIP: return "ARMISD::VZIP";
1105 case ARMISD::VUZP: return "ARMISD::VUZP";
1106 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001107 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1108 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001109 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1110 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001111 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1112 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001113 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilsonc6c13a32010-02-18 06:05:53 +00001114 case ARMISD::FMAX: return "ARMISD::FMAX";
1115 case ARMISD::FMIN: return "ARMISD::FMIN";
Joey Goulye3dd6842013-08-23 12:01:13 +00001116 case ARMISD::VMAXNM: return "ARMISD::VMAX";
1117 case ARMISD::VMINNM: return "ARMISD::VMIN";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001118 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001119 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1120 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001121 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilson2d790df2010-11-28 06:51:26 +00001122 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1123 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1124 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001125 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1126 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1127 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1128 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1129 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1130 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1131 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1132 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1133 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1134 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1135 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1136 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1137 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1138 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1139 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1140 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1141 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001142 }
1143}
1144
Matt Arsenault758659232013-05-18 00:21:46 +00001145EVT ARMTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Duncan Sandsf2641e12011-09-06 19:07:46 +00001146 if (!VT.isVector()) return getPointerTy();
1147 return VT.changeVectorElementTypeToInteger();
1148}
1149
Evan Cheng4cad68e2010-05-15 02:18:07 +00001150/// getRegClassFor - Return the register class that should be used for the
1151/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001152const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001153 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1154 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1155 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001156 if (Subtarget->hasNEON()) {
1157 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001158 return &ARM::QQPRRegClass;
1159 if (VT == MVT::v8i64)
1160 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001161 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001162 return TargetLowering::getRegClassFor(VT);
1163}
1164
Eric Christopher84bdfd82010-07-21 22:26:11 +00001165// Create a fast isel object.
1166FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001167ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1168 const TargetLibraryInfo *libInfo) const {
1169 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001170}
1171
Anton Korobeynikov19edda02010-07-24 21:52:08 +00001172/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1173/// be used for loads / stores from the global.
1174unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1175 return (Subtarget->isThumb1Only() ? 127 : 4095);
1176}
1177
Evan Cheng4401f882010-05-20 23:26:43 +00001178Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001179 unsigned NumVals = N->getNumValues();
1180 if (!NumVals)
1181 return Sched::RegPressure;
1182
1183 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001184 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001185 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001186 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001187 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001188 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001189 }
Evan Chengbf914992010-05-28 23:25:23 +00001190
1191 if (!N->isMachineOpcode())
1192 return Sched::RegPressure;
1193
1194 // Load are scheduled for latency even if there instruction itinerary
1195 // is not available.
Eric Christopher1889fdc2015-01-29 00:19:39 +00001196 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001197 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001198
Evan Cheng6cc775f2011-06-28 19:10:37 +00001199 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001200 return Sched::RegPressure;
1201 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001202 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001203 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001204
Evan Cheng4401f882010-05-20 23:26:43 +00001205 return Sched::RegPressure;
1206}
1207
Evan Cheng10043e22007-01-19 07:51:42 +00001208//===----------------------------------------------------------------------===//
1209// Lowering Code
1210//===----------------------------------------------------------------------===//
1211
Evan Cheng10043e22007-01-19 07:51:42 +00001212/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1213static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1214 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001215 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001216 case ISD::SETNE: return ARMCC::NE;
1217 case ISD::SETEQ: return ARMCC::EQ;
1218 case ISD::SETGT: return ARMCC::GT;
1219 case ISD::SETGE: return ARMCC::GE;
1220 case ISD::SETLT: return ARMCC::LT;
1221 case ISD::SETLE: return ARMCC::LE;
1222 case ISD::SETUGT: return ARMCC::HI;
1223 case ISD::SETUGE: return ARMCC::HS;
1224 case ISD::SETULT: return ARMCC::LO;
1225 case ISD::SETULE: return ARMCC::LS;
1226 }
1227}
1228
Bob Wilsona2e83332009-09-09 23:14:54 +00001229/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1230static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001231 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001232 CondCode2 = ARMCC::AL;
1233 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001234 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001235 case ISD::SETEQ:
1236 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1237 case ISD::SETGT:
1238 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1239 case ISD::SETGE:
1240 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1241 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001242 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001243 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1244 case ISD::SETO: CondCode = ARMCC::VC; break;
1245 case ISD::SETUO: CondCode = ARMCC::VS; break;
1246 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1247 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1248 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1249 case ISD::SETLT:
1250 case ISD::SETULT: CondCode = ARMCC::LT; break;
1251 case ISD::SETLE:
1252 case ISD::SETULE: CondCode = ARMCC::LE; break;
1253 case ISD::SETNE:
1254 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1255 }
Evan Cheng10043e22007-01-19 07:51:42 +00001256}
1257
Bob Wilsona4c22902009-04-17 19:07:39 +00001258//===----------------------------------------------------------------------===//
1259// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001260//===----------------------------------------------------------------------===//
1261
1262#include "ARMGenCallingConv.inc"
1263
Oliver Stannardc24f2172014-05-09 14:01:47 +00001264/// getEffectiveCallingConv - Get the effective calling convention, taking into
1265/// account presence of floating point hardware and calling convention
1266/// limitations, such as support for variadic functions.
1267CallingConv::ID
1268ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1269 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001270 switch (CC) {
1271 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001272 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001273 case CallingConv::ARM_AAPCS:
1274 case CallingConv::ARM_APCS:
1275 case CallingConv::GHC:
1276 return CC;
1277 case CallingConv::ARM_AAPCS_VFP:
1278 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1279 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001280 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001281 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001282 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001283 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1284 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001285 return CallingConv::ARM_AAPCS_VFP;
1286 else
1287 return CallingConv::ARM_AAPCS;
1288 case CallingConv::Fast:
1289 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001290 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001291 return CallingConv::Fast;
1292 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001293 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001294 return CallingConv::ARM_AAPCS_VFP;
1295 else
1296 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001297 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001298}
1299
1300/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1301/// CallingConvention.
1302CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1303 bool Return,
1304 bool isVarArg) const {
1305 switch (getEffectiveCallingConv(CC, isVarArg)) {
1306 default:
1307 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001308 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001309 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001310 case CallingConv::ARM_AAPCS:
1311 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1312 case CallingConv::ARM_AAPCS_VFP:
1313 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1314 case CallingConv::Fast:
1315 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001316 case CallingConv::GHC:
1317 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001318 }
1319}
1320
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001321/// LowerCallResult - Lower the result values of a call into the
1322/// appropriate copies out of appropriate physical registers.
1323SDValue
1324ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001325 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001326 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001327 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001328 SmallVectorImpl<SDValue> &InVals,
1329 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001330
Bob Wilsona4c22902009-04-17 19:07:39 +00001331 // Assign locations to each value returned by this call.
1332 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001333 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1334 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001335 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001336 CCAssignFnForNode(CallConv, /* Return*/ true,
1337 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001338
1339 // Copy all of the result registers out of their specified physreg.
1340 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1341 CCValAssign VA = RVLocs[i];
1342
Stephen Linb8bd2322013-04-20 05:14:40 +00001343 // Pass 'this' value directly from the argument to return value, to avoid
1344 // reg unit interference
1345 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001346 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1347 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001348 InVals.push_back(ThisVal);
1349 continue;
1350 }
1351
Bob Wilson0041bd32009-04-25 00:33:20 +00001352 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001353 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001354 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001355 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001356 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001357 Chain = Lo.getValue(1);
1358 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001359 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001360 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001361 InFlag);
1362 Chain = Hi.getValue(1);
1363 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001364 if (!Subtarget->isLittle())
1365 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001366 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001367
Owen Anderson9f944592009-08-11 20:47:22 +00001368 if (VA.getLocVT() == MVT::v2f64) {
1369 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1370 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1371 DAG.getConstant(0, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001372
1373 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001374 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001375 Chain = Lo.getValue(1);
1376 InFlag = Lo.getValue(2);
1377 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001378 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001379 Chain = Hi.getValue(1);
1380 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001381 if (!Subtarget->isLittle())
1382 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001383 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001384 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1385 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001386 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001387 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001388 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1389 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001390 Chain = Val.getValue(1);
1391 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001392 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001393
1394 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001395 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001396 case CCValAssign::Full: break;
1397 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001398 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001399 break;
1400 }
1401
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001402 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001403 }
1404
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001405 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001406}
1407
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001408/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001409SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001410ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1411 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001412 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001413 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001414 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001415 unsigned LocMemOffset = VA.getLocMemOffset();
1416 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1417 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilsona4c22902009-04-17 19:07:39 +00001418 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner886250c2010-09-21 18:51:21 +00001419 MachinePointerInfo::getStack(LocMemOffset),
David Greene0d0149f2010-02-15 16:55:24 +00001420 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001421}
1422
Andrew Trickef9de2a2013-05-25 02:42:55 +00001423void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001424 SDValue Chain, SDValue &Arg,
1425 RegsToPassVector &RegsToPass,
1426 CCValAssign &VA, CCValAssign &NextVA,
1427 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001428 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001429 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001430
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001431 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001432 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001433 unsigned id = Subtarget->isLittle() ? 0 : 1;
1434 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001435
1436 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001437 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001438 else {
1439 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001440 if (!StackPtr.getNode())
Bob Wilson2e076c42009-06-22 23:27:02 +00001441 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1442
Christian Pirkerb5728192014-05-08 14:06:24 +00001443 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001444 dl, DAG, NextVA,
1445 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001446 }
1447}
1448
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001449/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001450/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1451/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001452SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001453ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001454 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001455 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001456 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001457 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1458 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1459 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001460 SDValue Chain = CLI.Chain;
1461 SDValue Callee = CLI.Callee;
1462 bool &isTailCall = CLI.IsTailCall;
1463 CallingConv::ID CallConv = CLI.CallConv;
1464 bool doesNotRet = CLI.DoesNotReturn;
1465 bool isVarArg = CLI.IsVarArg;
1466
Dale Johannesend679ff72010-06-03 21:09:53 +00001467 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001468 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1469 bool isThisReturn = false;
1470 bool isSibCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001471
Bob Wilson8decdc42011-10-07 17:17:49 +00001472 // Disable tail calls if they're not supported.
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001473 if (!Subtarget->supportsTailCall() || MF.getTarget().Options.DisableTailCalls)
Bob Wilson3c9ed762010-08-13 22:43:33 +00001474 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001475
Dale Johannesend679ff72010-06-03 21:09:53 +00001476 if (isTailCall) {
1477 // Check if it's really possible to do a tail call.
1478 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001479 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001480 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001481 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1482 report_fatal_error("failed to perform tail call elimination on a call "
1483 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001484 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1485 // detected sibcalls.
1486 if (isTailCall) {
1487 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001488 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001489 }
1490 }
Evan Cheng10043e22007-01-19 07:51:42 +00001491
Bob Wilsona4c22902009-04-17 19:07:39 +00001492 // Analyze operands of the call, assigning locations to each operand.
1493 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001494 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1495 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001496 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001497 CCAssignFnForNode(CallConv, /* Return*/ false,
1498 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001499
Bob Wilsona4c22902009-04-17 19:07:39 +00001500 // Get a count of how many bytes are to be pushed on the stack.
1501 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001502
Dale Johannesend679ff72010-06-03 21:09:53 +00001503 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001504 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001505 NumBytes = 0;
1506
Evan Cheng10043e22007-01-19 07:51:42 +00001507 // Adjust the stack pointer for the new arguments...
1508 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001509 if (!isSibCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00001510 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
1511 dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001512
Jim Grosbach6ad4bcb2010-02-24 01:43:03 +00001513 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +00001514
Bob Wilson2e076c42009-06-22 23:27:02 +00001515 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001516 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001517
Bob Wilsona4c22902009-04-17 19:07:39 +00001518 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001519 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001520 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1521 i != e;
1522 ++i, ++realArgIdx) {
1523 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001524 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001525 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001526 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001527
Bob Wilsona4c22902009-04-17 19:07:39 +00001528 // Promote the value if needed.
1529 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001530 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001531 case CCValAssign::Full: break;
1532 case CCValAssign::SExt:
1533 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1534 break;
1535 case CCValAssign::ZExt:
1536 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1537 break;
1538 case CCValAssign::AExt:
1539 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1540 break;
1541 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001542 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001543 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001544 }
1545
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001546 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001547 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001548 if (VA.getLocVT() == MVT::v2f64) {
1549 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1550 DAG.getConstant(0, MVT::i32));
1551 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1552 DAG.getConstant(1, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001553
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001554 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001555 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1556
1557 VA = ArgLocs[++i]; // skip ahead to next loc
1558 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001559 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001560 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1561 } else {
1562 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001563
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001564 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1565 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001566 }
1567 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001568 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001569 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001570 }
1571 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001572 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1573 assert(VA.getLocVT() == MVT::i32 &&
1574 "unexpected calling convention register assignment");
1575 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001576 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001577 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001578 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001579 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001580 } else if (isByVal) {
1581 assert(VA.isMemLoc());
1582 unsigned offset = 0;
1583
1584 // True if this byval aggregate will be split between registers
1585 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001586 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001587 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001588
1589 if (CurByValIdx < ByValArgsCount) {
1590
1591 unsigned RegBegin, RegEnd;
1592 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1593
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001594 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1595 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001596 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001597 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1598 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1599 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1600 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001601 false, false, false,
1602 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001603 MemOpChains.push_back(Load.getValue(1));
1604 RegsToPass.push_back(std::make_pair(j, Load));
1605 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001606
1607 // If parameter size outsides register area, "offset" value
1608 // helps us to calculate stack slot for remained part properly.
1609 offset = RegEnd - RegBegin;
1610
1611 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001612 }
1613
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001614 if (Flags.getByValSize() > 4*offset) {
Manman Ren9f911162012-06-01 02:44:42 +00001615 unsigned LocMemOffset = VA.getLocMemOffset();
1616 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1617 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1618 StkPtrOff);
1619 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1620 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1621 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1622 MVT::i32);
Manman Rene8735522012-06-01 19:33:18 +00001623 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001624
Manman Ren9f911162012-06-01 02:44:42 +00001625 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001626 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001627 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001628 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001629 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001630 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001631 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001632
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001633 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1634 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001635 }
Evan Cheng10043e22007-01-19 07:51:42 +00001636 }
1637
1638 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001639 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001640
1641 // Build a sequence of copy-to-reg nodes chained together with token chain
1642 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001643 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001644 // Tail call byval lowering might overwrite argument registers so in case of
1645 // tail call optimization the copies to registers are lowered later.
1646 if (!isTailCall)
1647 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1648 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1649 RegsToPass[i].second, InFlag);
1650 InFlag = Chain.getValue(1);
1651 }
Evan Cheng10043e22007-01-19 07:51:42 +00001652
Dale Johannesend679ff72010-06-03 21:09:53 +00001653 // For tail calls lower the arguments to the 'real' stack slot.
1654 if (isTailCall) {
1655 // Force all the incoming stack arguments to be loaded from the stack
1656 // before any new outgoing arguments are stored to the stack, because the
1657 // outgoing stack slots may alias the incoming argument stack slots, and
1658 // the alias isn't otherwise explicit. This is slightly more conservative
1659 // than necessary, because it means that each store effectively depends
1660 // on every argument instead of just those arguments it would clobber.
1661
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001662 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001663 InFlag = SDValue();
1664 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1665 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1666 RegsToPass[i].second, InFlag);
1667 InFlag = Chain.getValue(1);
1668 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001669 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001670 }
1671
Bill Wendling24c79f22008-09-16 21:48:12 +00001672 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1673 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1674 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001675 bool isDirect = false;
1676 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001677 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001678 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001679
1680 if (EnableARMLongCalls) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001681 assert((Subtarget->isTargetWindows() ||
1682 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1683 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001684 // Handle a global address or an external symbol. If it's not one of
1685 // those, the target's already in a register, so we don't need to do
1686 // anything extra.
1687 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001688 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001689 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001690 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001691 ARMConstantPoolValue *CPV =
1692 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1693
Jim Grosbach32bb3622010-04-14 22:28:31 +00001694 // Get the address of the callee into a register
1695 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1696 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1697 Callee = DAG.getLoad(getPointerTy(), dl,
1698 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001699 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001700 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001701 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1702 const char *Sym = S->getSymbol();
1703
1704 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001705 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001706 ARMConstantPoolValue *CPV =
1707 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1708 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001709 // Get the address of the callee into a register
1710 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1711 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1712 Callee = DAG.getLoad(getPointerTy(), dl,
1713 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001714 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001715 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001716 }
1717 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001718 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001719 isDirect = true;
Chris Lattner55452c22009-07-15 04:12:33 +00001720 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Tim Northoverd6a729b2014-01-06 14:28:05 +00001721 bool isStub = (isExt && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001722 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001723 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00001724 // ARM call to a local ARM function is predicable.
Evan Chengf128bdc2010-06-16 07:35:02 +00001725 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001726 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001727 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001728 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Tim Northover72360d22013-12-02 10:35:41 +00001729 Callee = DAG.getNode(ARMISD::WrapperPIC, dl, getPointerTy(),
Tim Northoverd4d294d2014-08-06 11:13:06 +00001730 DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
1731 0, ARMII::MO_NONLAZY));
1732 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
1733 MachinePointerInfo::getGOT(), false, false, true, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001734 } else if (Subtarget->isTargetCOFF()) {
1735 assert(Subtarget->isTargetWindows() &&
1736 "Windows is the only supported COFF target");
1737 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1738 ? ARMII::MO_DLLIMPORT
1739 : ARMII::MO_NO_FLAG;
1740 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), /*Offset=*/0,
1741 TargetFlags);
1742 if (GV->hasDLLImportStorageClass())
1743 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
1744 DAG.getNode(ARMISD::Wrapper, dl, getPointerTy(),
1745 Callee), MachinePointerInfo::getGOT(),
1746 false, false, false, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001747 } else {
1748 // On ELF targets for PIC code, direct calls should go through the PLT
1749 unsigned OpFlags = 0;
1750 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001751 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001752 OpFlags = ARMII::MO_PLT;
1753 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1754 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001755 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001756 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001757 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001758 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001759 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Cheng83f35172007-01-30 20:37:08 +00001760 // tBX takes a register source operand.
1761 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001762 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001763 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001764 ARMConstantPoolValue *CPV =
1765 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1766 ARMPCLabelIndex, 4);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001767 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001768 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen021052a2009-02-04 20:06:27 +00001769 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Chengcdbb70c2009-10-31 03:39:36 +00001770 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001771 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001772 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00001773 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson7117a912009-03-20 22:42:55 +00001774 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen021052a2009-02-04 20:06:27 +00001775 getPointerTy(), Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001776 } else {
1777 unsigned OpFlags = 0;
1778 // On ELF targets for PIC code, direct calls should go through the PLT
1779 if (Subtarget->isTargetELF() &&
1780 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1781 OpFlags = ARMII::MO_PLT;
1782 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1783 }
Evan Cheng10043e22007-01-19 07:51:42 +00001784 }
1785
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001786 // FIXME: handle tail calls differently.
1787 unsigned CallOpc;
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00001788 bool HasMinSizeAttr = MF.getFunction()->hasFnAttribute(Attribute::MinSize);
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001789 if (Subtarget->isThumb()) {
1790 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001791 CallOpc = ARMISD::CALL_NOLINK;
1792 else
1793 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1794 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001795 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001796 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001797 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet8e1fe842012-11-02 21:32:17 +00001798 // Emit regular call when code size is the priority
1799 !HasMinSizeAttr)
Evan Cheng65f9d192012-02-28 18:51:51 +00001800 // "mov lr, pc; b _foo" to avoid confusing the RSP
1801 CallOpc = ARMISD::CALL_NOLINK;
1802 else
1803 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001804 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001805
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001806 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001807 Ops.push_back(Chain);
1808 Ops.push_back(Callee);
1809
1810 // Add argument registers to the end of the list so that they are known live
1811 // into the call.
1812 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1813 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1814 RegsToPass[i].second.getValueType()));
1815
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001816 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001817 if (!isTailCall) {
1818 const uint32_t *Mask;
Eric Christopher1889fdc2015-01-29 00:19:39 +00001819 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001820 if (isThisReturn) {
1821 // For 'this' returns, use the R0-preserving mask if applicable
1822 Mask = ARI->getThisReturnPreservedMask(CallConv);
1823 if (!Mask) {
1824 // Set isThisReturn to false if the calling convention is not one that
1825 // allows 'returned' to be modeled in this way, so LowerCallResult does
1826 // not try to pass 'this' straight through
1827 isThisReturn = false;
1828 Mask = ARI->getCallPreservedMask(CallConv);
1829 }
1830 } else
Stephen Linff7fcee2013-06-26 21:42:14 +00001831 Mask = ARI->getCallPreservedMask(CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001832
Matthias Braunc22630e2013-10-04 16:52:54 +00001833 assert(Mask && "Missing call preserved mask for calling convention");
1834 Ops.push_back(DAG.getRegisterMask(Mask));
1835 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001836
Gabor Greiff304a7a2008-08-28 21:40:38 +00001837 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001838 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001839
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001840 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001841 if (isTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001842 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Dale Johannesend679ff72010-06-03 21:09:53 +00001843
Duncan Sands739a0542008-07-02 17:40:58 +00001844 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001845 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001846 InFlag = Chain.getValue(1);
1847
Chris Lattner27539552008-10-11 22:08:30 +00001848 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001849 DAG.getIntPtrConstant(0, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001850 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001851 InFlag = Chain.getValue(1);
1852
Bob Wilsona4c22902009-04-17 19:07:39 +00001853 // Handle result values, copying them out of physregs into vregs that we
1854 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001855 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001856 InVals, isThisReturn,
1857 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001858}
1859
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001860/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001861/// on the stack. Remember the next parameter register to allocate,
1862/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001863/// this.
1864void
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001865ARMTargetLowering::HandleByVal(
1866 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001867 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1868 assert((State->getCallOrPrologue() == Prologue ||
1869 State->getCallOrPrologue() == Call) &&
1870 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001871
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001872 if ((ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001873 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1874 unsigned AlignInRegs = Align / 4;
1875 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1876 for (unsigned i = 0; i < Waste; ++i)
1877 reg = State->AllocateReg(GPRArgRegs, 4);
1878 }
1879 if (reg != 0) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001880 unsigned excess = 4 * (ARM::R4 - reg);
1881
1882 // Special case when NSAA != SP and parameter size greater than size of
1883 // all remained GPR regs. In that case we can't split parameter, we must
1884 // send it to stack. We also must set NCRN to R4, so waste all
1885 // remained registers.
Oliver Stannardd55e1152014-03-05 15:25:27 +00001886 const unsigned NSAAOffset = State->getNextStackOffset();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001887 if (Subtarget->isAAPCS_ABI() && NSAAOffset != 0 && size > excess) {
1888 while (State->AllocateReg(GPRArgRegs, 4))
1889 ;
1890 return;
1891 }
1892
1893 // First register for byval parameter is the first register that wasn't
1894 // allocated before this method call, so it would be "reg".
1895 // If parameter is small enough to be saved in range [reg, r4), then
1896 // the end (first after last) register would be reg + param-size-in-regs,
1897 // else parameter would be splitted between registers and stack,
1898 // end register would be r4 in this case.
1899 unsigned ByValRegBegin = reg;
Stepan Dyatkovskiy2703bca2013-05-08 14:51:27 +00001900 unsigned ByValRegEnd = (size < excess) ? reg + size/4 : (unsigned)ARM::R4;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001901 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1902 // Note, first register is allocated in the beginning of function already,
1903 // allocate remained amount of registers we need.
1904 for (unsigned i = reg+1; i != ByValRegEnd; ++i)
1905 State->AllocateReg(GPRArgRegs, 4);
Oliver Stannardd55e1152014-03-05 15:25:27 +00001906 // A byval parameter that is split between registers and memory needs its
1907 // size truncated here.
1908 // In the case where the entire structure fits in registers, we set the
1909 // size in memory to zero.
1910 if (size < excess)
1911 size = 0;
1912 else
1913 size -= excess;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001914 }
1915 }
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001916}
1917
Dale Johannesend679ff72010-06-03 21:09:53 +00001918/// MatchingStackOffset - Return true if the given stack call argument is
1919/// already available in the same position (relatively) of the caller's
1920/// incoming argument stack.
1921static
1922bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1923 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00001924 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001925 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1926 int FI = INT_MAX;
1927 if (Arg.getOpcode() == ISD::CopyFromReg) {
1928 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001929 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00001930 return false;
1931 MachineInstr *Def = MRI->getVRegDef(VR);
1932 if (!Def)
1933 return false;
1934 if (!Flags.isByVal()) {
1935 if (!TII->isLoadFromStackSlot(Def, FI))
1936 return false;
1937 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00001938 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00001939 }
1940 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1941 if (Flags.isByVal())
1942 // ByVal argument is passed in as a pointer but it's now being
1943 // dereferenced. e.g.
1944 // define @foo(%struct.X* %A) {
1945 // tail call @bar(%struct.X* byval %A)
1946 // }
1947 return false;
1948 SDValue Ptr = Ld->getBasePtr();
1949 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1950 if (!FINode)
1951 return false;
1952 FI = FINode->getIndex();
1953 } else
1954 return false;
1955
1956 assert(FI != INT_MAX);
1957 if (!MFI->isFixedObjectIndex(FI))
1958 return false;
1959 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1960}
1961
1962/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1963/// for tail call optimization. Targets which want to do tail call
1964/// optimization should implement this function.
1965bool
1966ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1967 CallingConv::ID CalleeCC,
1968 bool isVarArg,
1969 bool isCalleeStructRet,
1970 bool isCallerStructRet,
1971 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001972 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00001973 const SmallVectorImpl<ISD::InputArg> &Ins,
1974 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00001975 const Function *CallerF = DAG.getMachineFunction().getFunction();
1976 CallingConv::ID CallerCC = CallerF->getCallingConv();
1977 bool CCMatch = CallerCC == CalleeCC;
1978
1979 // Look for obvious safe cases to perform tail call optimization that do not
1980 // require ABI changes. This is what gcc calls sibcall.
1981
Jim Grosbache3864cc2010-06-16 23:45:49 +00001982 // Do not sibcall optimize vararg calls unless the call site is not passing
1983 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00001984 if (isVarArg && !Outs.empty())
1985 return false;
1986
Tim Northoverd8407452013-10-01 14:33:28 +00001987 // Exception-handling functions need a special set of instructions to indicate
1988 // a return to the hardware. Tail-calling another function would probably
1989 // break this.
1990 if (CallerF->hasFnAttribute("interrupt"))
1991 return false;
1992
Dale Johannesend679ff72010-06-03 21:09:53 +00001993 // Also avoid sibcall optimization if either caller or callee uses struct
1994 // return semantics.
1995 if (isCalleeStructRet || isCallerStructRet)
1996 return false;
1997
Dale Johannesend24c66b2010-06-23 18:52:34 +00001998 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach3840c902011-07-08 20:18:11 +00001999 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
2000 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
2001 // support in the assembler and linker to be used. This would need to be
2002 // fixed to fully support tail calls in Thumb1.
2003 //
Dale Johannesene2289282010-07-08 01:18:23 +00002004 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
2005 // LR. This means if we need to reload LR, it takes an extra instructions,
2006 // which outweighs the value of the tail call; but here we don't know yet
2007 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach535d3b42010-09-08 03:54:02 +00002008 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesene2289282010-07-08 01:18:23 +00002009 // emitEpilogue if LR is used.
Dale Johannesene2289282010-07-08 01:18:23 +00002010
2011 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
2012 // but we need to make sure there are enough registers; the only valid
2013 // registers are the 4 used for parameters. We don't currently do this
2014 // case.
Evan Chengd4b08732010-11-30 23:55:39 +00002015 if (Subtarget->isThumb1Only())
2016 return false;
Dale Johannesen3ac52b32010-06-18 18:13:11 +00002017
Oliver Stannard12993dd2014-08-18 12:42:15 +00002018 // Externally-defined functions with weak linkage should not be
2019 // tail-called on ARM when the OS does not support dynamic
2020 // pre-emption of symbols, as the AAELF spec requires normal calls
2021 // to undefined weak functions to be replaced with a NOP or jump to the
2022 // next instruction. The behaviour of branch instructions in this
2023 // situation (as used for tail calls) is implementation-defined, so we
2024 // cannot rely on the linker replacing the tail call with a return.
2025 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2026 const GlobalValue *GV = G->getGlobal();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002027 const Triple TT(getTargetMachine().getTargetTriple());
2028 if (GV->hasExternalWeakLinkage() &&
2029 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002030 return false;
2031 }
2032
Dale Johannesend679ff72010-06-03 21:09:53 +00002033 // If the calling conventions do not match, then we'd better make sure the
2034 // results are returned in the same way as what the caller expects.
2035 if (!CCMatch) {
2036 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopherb5217502014-08-06 18:45:26 +00002037 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
2038 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002039 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
2040
2041 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopherb5217502014-08-06 18:45:26 +00002042 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
2043 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002044 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
2045
2046 if (RVLocs1.size() != RVLocs2.size())
2047 return false;
2048 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2049 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2050 return false;
2051 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2052 return false;
2053 if (RVLocs1[i].isRegLoc()) {
2054 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2055 return false;
2056 } else {
2057 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2058 return false;
2059 }
2060 }
2061 }
2062
Manman Ren7e48b252012-10-12 23:39:43 +00002063 // If Caller's vararg or byval argument has been split between registers and
2064 // stack, do not perform tail call, since part of the argument is in caller's
2065 // local frame.
2066 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
2067 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002068 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002069 return false;
2070
Dale Johannesend679ff72010-06-03 21:09:53 +00002071 // If the callee takes no arguments then go on to check the results of the
2072 // call.
2073 if (!Outs.empty()) {
2074 // Check if stack adjustment is needed. For now, do not do this if any
2075 // argument is passed on the stack.
2076 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002077 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2078 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002079 CCInfo.AnalyzeCallOperands(Outs,
2080 CCAssignFnForNode(CalleeCC, false, isVarArg));
2081 if (CCInfo.getNextStackOffset()) {
2082 MachineFunction &MF = DAG.getMachineFunction();
2083
2084 // Check if the arguments are already laid out in the right way as
2085 // the caller's fixed stack objects.
2086 MachineFrameInfo *MFI = MF.getFrameInfo();
2087 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopher1889fdc2015-01-29 00:19:39 +00002088 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002089 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2090 i != e;
2091 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002092 CCValAssign &VA = ArgLocs[i];
2093 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002094 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002095 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002096 if (VA.getLocInfo() == CCValAssign::Indirect)
2097 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002098 if (VA.needsCustom()) {
2099 // f64 and vector types are split into multiple registers or
2100 // register/stack-slot combinations. The types will not match
2101 // the registers; give up on memory f64 refs until we figure
2102 // out what to do about this.
2103 if (!VA.isRegLoc())
2104 return false;
2105 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002106 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002107 if (RegVT == MVT::v2f64) {
2108 if (!ArgLocs[++i].isRegLoc())
2109 return false;
2110 if (!ArgLocs[++i].isRegLoc())
2111 return false;
2112 }
2113 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002114 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2115 MFI, MRI, TII))
2116 return false;
2117 }
2118 }
2119 }
2120 }
2121
2122 return true;
2123}
2124
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002125bool
2126ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2127 MachineFunction &MF, bool isVarArg,
2128 const SmallVectorImpl<ISD::OutputArg> &Outs,
2129 LLVMContext &Context) const {
2130 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002131 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002132 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2133 isVarArg));
2134}
2135
Tim Northoverd8407452013-10-01 14:33:28 +00002136static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2137 SDLoc DL, SelectionDAG &DAG) {
2138 const MachineFunction &MF = DAG.getMachineFunction();
2139 const Function *F = MF.getFunction();
2140
2141 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2142
2143 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2144 // version of the "preferred return address". These offsets affect the return
2145 // instruction if this is a return from PL1 without hypervisor extensions.
2146 // IRQ/FIQ: +4 "subs pc, lr, #4"
2147 // SWI: 0 "subs pc, lr, #0"
2148 // ABORT: +4 "subs pc, lr, #4"
2149 // UNDEF: +4/+2 "subs pc, lr, #0"
2150 // UNDEF varies depending on where the exception came from ARM or Thumb
2151 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2152
2153 int64_t LROffset;
2154 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2155 IntKind == "ABORT")
2156 LROffset = 4;
2157 else if (IntKind == "SWI" || IntKind == "UNDEF")
2158 LROffset = 0;
2159 else
2160 report_fatal_error("Unsupported interrupt attribute. If present, value "
2161 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2162
2163 RetOps.insert(RetOps.begin() + 1, DAG.getConstant(LROffset, MVT::i32, false));
2164
Craig Topper48d114b2014-04-26 18:35:24 +00002165 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002166}
2167
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002168SDValue
2169ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002170 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002171 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002172 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002173 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002174
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002175 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002176 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002177
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002178 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002179 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2180 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002181
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002182 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002183 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2184 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002185
Bob Wilsona4c22902009-04-17 19:07:39 +00002186 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002187 SmallVector<SDValue, 4> RetOps;
2188 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002189 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002190
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002191 MachineFunction &MF = DAG.getMachineFunction();
2192 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2193 AFI->setReturnRegsCount(RVLocs.size());
2194
Bob Wilsona4c22902009-04-17 19:07:39 +00002195 // Copy the result values into the output registers.
2196 for (unsigned i = 0, realRVLocIdx = 0;
2197 i != RVLocs.size();
2198 ++i, ++realRVLocIdx) {
2199 CCValAssign &VA = RVLocs[i];
2200 assert(VA.isRegLoc() && "Can only return in registers!");
2201
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002202 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002203
2204 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002205 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002206 case CCValAssign::Full: break;
2207 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002208 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002209 break;
2210 }
2211
Bob Wilsona4c22902009-04-17 19:07:39 +00002212 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002213 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002214 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002215 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2216 DAG.getConstant(0, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002217 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002218 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002219
Christian Pirkerb5728192014-05-08 14:06:24 +00002220 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2221 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2222 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002223 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002224 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002225 VA = RVLocs[++i]; // skip ahead to next loc
2226 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002227 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2228 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002229 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002230 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002231 VA = RVLocs[++i]; // skip ahead to next loc
2232
2233 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002234 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2235 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002236 }
2237 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2238 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002239 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002240 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002241 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2242 fmrrd.getValue(isLittleEndian ? 0 : 1),
2243 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002244 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002245 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002246 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002247 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2248 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002249 Flag);
2250 } else
2251 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2252
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002253 // Guarantee that all emitted copies are
2254 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002255 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002256 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002257 }
2258
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002259 // Update chain and glue.
2260 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002261 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002262 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002263
Tim Northoverd8407452013-10-01 14:33:28 +00002264 // CPUs which aren't M-class use a special sequence to return from
2265 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2266 // though we use "subs pc, lr, #N").
2267 //
2268 // M-class CPUs actually use a normal return sequence with a special
2269 // (hardware-provided) value in LR, so the normal code path works.
2270 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2271 !Subtarget->isMClass()) {
2272 if (Subtarget->isThumb1Only())
2273 report_fatal_error("interrupt attribute is not supported in Thumb1");
2274 return LowerInterruptReturn(RetOps, dl, DAG);
2275 }
2276
Craig Topper48d114b2014-04-26 18:35:24 +00002277 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002278}
2279
Evan Chengf8bad082012-04-10 01:51:00 +00002280bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002281 if (N->getNumValues() != 1)
2282 return false;
2283 if (!N->hasNUsesOfValue(1, 0))
2284 return false;
2285
Evan Chengf8bad082012-04-10 01:51:00 +00002286 SDValue TCChain = Chain;
2287 SDNode *Copy = *N->use_begin();
2288 if (Copy->getOpcode() == ISD::CopyToReg) {
2289 // If the copy has a glue operand, we conservatively assume it isn't safe to
2290 // perform a tail call.
2291 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2292 return false;
2293 TCChain = Copy->getOperand(0);
2294 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2295 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002296 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002297 SmallPtrSet<SDNode*, 2> Copies;
2298 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002299 UI != UE; ++UI) {
2300 if (UI->getOpcode() != ISD::CopyToReg)
2301 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002302 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002303 }
Evan Chengf8bad082012-04-10 01:51:00 +00002304 if (Copies.size() > 2)
2305 return false;
2306
2307 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2308 UI != UE; ++UI) {
2309 SDValue UseChain = UI->getOperand(0);
2310 if (Copies.count(UseChain.getNode()))
2311 // Second CopyToReg
2312 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002313 else {
2314 // We are at the top of this chain.
2315 // If the copy has a glue operand, we conservatively assume it
2316 // isn't safe to perform a tail call.
2317 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2318 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002319 // First CopyToReg
2320 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002321 }
Evan Chengf8bad082012-04-10 01:51:00 +00002322 }
2323 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002324 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002325 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002326 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002327 Copy = *Copy->use_begin();
2328 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002329 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002330 // If the copy has a glue operand, we conservatively assume it isn't safe to
2331 // perform a tail call.
2332 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2333 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002334 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002335 } else {
2336 return false;
2337 }
2338
Evan Cheng419ea282010-12-01 22:59:46 +00002339 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002340 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2341 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002342 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2343 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002344 return false;
2345 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002346 }
2347
Evan Chengf8bad082012-04-10 01:51:00 +00002348 if (!HasRet)
2349 return false;
2350
2351 Chain = TCChain;
2352 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002353}
2354
Evan Cheng0663f232011-03-21 01:19:09 +00002355bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002356 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002357 return false;
2358
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00002359 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng0663f232011-03-21 01:19:09 +00002360 return false;
2361
2362 return !Subtarget->isThumb1Only();
2363}
2364
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002365// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2366// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2367// one of the above mentioned nodes. It has to be wrapped because otherwise
2368// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2369// be used to form addressing mode. These wrapped nodes will be selected
2370// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002371static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002372 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002373 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002374 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002375 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002376 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002377 if (CP->isMachineConstantPoolEntry())
2378 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2379 CP->getAlignment());
2380 else
2381 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2382 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002383 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002384}
2385
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002386unsigned ARMTargetLowering::getJumpTableEncoding() const {
2387 return MachineJumpTableInfo::EK_Inline;
2388}
2389
Dan Gohman21cea8a2010-04-17 15:26:15 +00002390SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2391 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002392 MachineFunction &MF = DAG.getMachineFunction();
2393 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2394 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002395 SDLoc DL(Op);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002396 EVT PtrVT = getPointerTy();
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002397 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002398 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2399 SDValue CPAddr;
2400 if (RelocM == Reloc::Static) {
2401 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2402 } else {
2403 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002404 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002405 ARMConstantPoolValue *CPV =
2406 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2407 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002408 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2409 }
2410 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2411 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002412 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002413 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002414 if (RelocM == Reloc::Static)
2415 return Result;
Evan Cheng408aa562009-11-06 22:24:13 +00002416 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002417 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002418}
2419
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002420// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002421SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002422ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002423 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002424 SDLoc dl(GA);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002425 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002426 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002427 MachineFunction &MF = DAG.getMachineFunction();
2428 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002429 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002430 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002431 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2432 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002433 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002434 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002435 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattner7727d052010-09-21 06:44:06 +00002436 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002437 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002438 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002439
Evan Cheng408aa562009-11-06 22:24:13 +00002440 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002441 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002442
2443 // call __tls_get_addr.
2444 ArgListTy Args;
2445 ArgListEntry Entry;
2446 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002447 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002448 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002449
Dale Johannesen555a3752009-01-30 23:10:59 +00002450 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002451 TargetLowering::CallLoweringInfo CLI(DAG);
2452 CLI.setDebugLoc(dl).setChain(Chain)
2453 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002454 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args),
2455 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002456
Justin Holewinskiaa583972012-05-25 16:35:28 +00002457 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002458 return CallResult.first;
2459}
2460
2461// Lower ISD::GlobalTLSAddress using the "initial exec" or
2462// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002463SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002464ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002465 SelectionDAG &DAG,
2466 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002467 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002468 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002469 SDValue Offset;
2470 SDValue Chain = DAG.getEntryNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002471 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002472 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002473 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002474
Hans Wennborgaea41202012-05-04 09:40:39 +00002475 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002476 MachineFunction &MF = DAG.getMachineFunction();
2477 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002478 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002479 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002480 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2481 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002482 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2483 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2484 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002485 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002486 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002487 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002488 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002489 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002490 Chain = Offset.getValue(1);
2491
Evan Cheng408aa562009-11-06 22:24:13 +00002492 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002493 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002494
Evan Chengcdbb70c2009-10-31 03:39:36 +00002495 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002496 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002497 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002498 } else {
2499 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002500 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002501 ARMConstantPoolValue *CPV =
2502 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002503 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002504 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002505 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002506 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002507 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002508 }
2509
2510 // The address of the thread local variable is the add of the thread
2511 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002512 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002513}
2514
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002515SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002516ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002517 // TODO: implement the "local dynamic" model
2518 assert(Subtarget->isTargetELF() &&
2519 "TLS not implemented for non-ELF targets");
2520 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgaea41202012-05-04 09:40:39 +00002521
2522 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2523
2524 switch (model) {
2525 case TLSModel::GeneralDynamic:
2526 case TLSModel::LocalDynamic:
2527 return LowerToTLSGeneralDynamicModel(GA, DAG);
2528 case TLSModel::InitialExec:
2529 case TLSModel::LocalExec:
2530 return LowerToTLSExecModels(GA, DAG, model);
2531 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002532 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002533}
2534
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002535SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002536 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002537 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002538 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002539 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002540 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002541 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002542 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002543 ARMConstantPoolConstant::Create(GV,
2544 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002545 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002546 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson7117a912009-03-20 22:42:55 +00002547 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002548 CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002549 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002550 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002551 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002552 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002553 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002554 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002555 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002556 MachinePointerInfo::getGOT(),
2557 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002558 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002559 }
2560
2561 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002562 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002563 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002564 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002565 // FIXME: Once remat is capable of dealing with instructions with register
2566 // operands, expand this into two nodes.
2567 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2568 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002569 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002570 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2571 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2572 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2573 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002574 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002575 }
2576}
2577
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002578SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002579 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002580 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002581 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002582 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002583 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002584
Eric Christopherc1058df2014-07-04 01:55:26 +00002585 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002586 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002587
Tim Northover72360d22013-12-02 10:35:41 +00002588 // FIXME: Once remat is capable of dealing with instructions with register
2589 // operands, expand this into multiple nodes
2590 unsigned Wrapper =
2591 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002592
Tim Northover72360d22013-12-02 10:35:41 +00002593 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2594 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002595
Evan Cheng1b389522009-09-03 07:04:02 +00002596 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002597 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2598 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002599 return Result;
2600}
2601
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002602SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2603 SelectionDAG &DAG) const {
2604 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002605 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2606 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002607
2608 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002609 const ARMII::TOF TargetFlags =
2610 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002611 EVT PtrVT = getPointerTy();
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002612 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002613 SDLoc DL(Op);
2614
2615 ++NumMovwMovt;
2616
2617 // FIXME: Once remat is capable of dealing with instructions with register
2618 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002619 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2620 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
2621 TargetFlags));
2622 if (GV->hasDLLImportStorageClass())
2623 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
2624 MachinePointerInfo::getGOT(), false, false, false, 0);
2625 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002626}
2627
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002628SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002629 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002630 assert(Subtarget->isTargetELF() &&
2631 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002632 MachineFunction &MF = DAG.getMachineFunction();
2633 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002634 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002635 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002636 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002637 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002638 ARMConstantPoolValue *CPV =
2639 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2640 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002641 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002642 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002643 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002644 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002645 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00002646 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002647 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002648}
2649
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002650SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002651ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002652 SDLoc dl(Op);
Jim Grosbachfaa3abb2010-05-27 23:49:24 +00002653 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002654 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2655 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002656 Op.getOperand(1), Val);
2657}
2658
2659SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002660ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002661 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002662 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2663 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2664}
2665
2666SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002667ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002668 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002669 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002670 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002671 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002672 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002673 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00002674 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00002675 "RBIT intrinsic must have i32 type!");
Yi Kongc655f0c2014-08-20 10:40:20 +00002676 return DAG.getNode(ARMISD::RBIT, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00002677 }
Bob Wilson17f88782009-08-04 00:25:01 +00002678 case Intrinsic::arm_thread_pointer: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002679 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson17f88782009-08-04 00:25:01 +00002680 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2681 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002682 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002683 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002684 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002685 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002686 EVT PtrVT = getPointerTy();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002687 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2688 SDValue CPAddr;
2689 unsigned PCAdj = (RelocM != Reloc::PIC_)
2690 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002691 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002692 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2693 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002694 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002695 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002696 SDValue Result =
Evan Chengcdbb70c2009-10-31 03:39:36 +00002697 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002698 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002699 false, false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002700
2701 if (RelocM == Reloc::PIC_) {
Evan Cheng408aa562009-11-06 22:24:13 +00002702 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002703 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2704 }
2705 return Result;
2706 }
Evan Cheng18381b42011-03-29 23:06:19 +00002707 case Intrinsic::arm_neon_vmulls:
2708 case Intrinsic::arm_neon_vmullu: {
2709 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2710 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002711 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002712 Op.getOperand(1), Op.getOperand(2));
2713 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002714 }
2715}
2716
Eli Friedman30a49e92011-08-03 21:06:02 +00002717static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2718 const ARMSubtarget *Subtarget) {
2719 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002720 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002721 if (!Subtarget->hasDataBarrier()) {
2722 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2723 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2724 // here.
2725 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002726 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002727 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002728 DAG.getConstant(0, MVT::i32));
2729 }
2730
Tim Northover36b24172013-07-03 09:20:36 +00002731 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2732 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00002733 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002734 if (Subtarget->isMClass()) {
2735 // Only a full system barrier exists in the M-class architectures.
2736 Domain = ARM_MB::SY;
2737 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002738 // Swift happens to implement ISHST barriers in a way that's compatible with
2739 // Release semantics but weaker than ISH so we'd be fools not to use
2740 // it. Beware: other processors probably don't!
2741 Domain = ARM_MB::ISHST;
2742 }
2743
Joey Gouly926d3f52013-09-05 15:35:24 +00002744 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
2745 DAG.getConstant(Intrinsic::arm_dmb, MVT::i32),
Tim Northover36b24172013-07-03 09:20:36 +00002746 DAG.getConstant(Domain, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002747}
2748
Evan Cheng8740ee32010-11-03 06:34:55 +00002749static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2750 const ARMSubtarget *Subtarget) {
2751 // ARM pre v5TE and Thumb1 does not have preload instructions.
2752 if (!(Subtarget->isThumb2() ||
2753 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2754 // Just preserve the chain.
2755 return Op.getOperand(0);
2756
Andrew Trickef9de2a2013-05-25 02:42:55 +00002757 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002758 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2759 if (!isRead &&
2760 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2761 // ARMv7 with MP extension has PLDW.
2762 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002763
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002764 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2765 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002766 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002767 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002768 isData = ~isData & 1;
2769 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002770
2771 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng21acf9f2010-11-04 05:19:35 +00002772 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2773 DAG.getConstant(isData, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002774}
2775
Dan Gohman31ae5862010-04-17 14:41:14 +00002776static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2777 MachineFunction &MF = DAG.getMachineFunction();
2778 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2779
Evan Cheng10043e22007-01-19 07:51:42 +00002780 // vastart just stores the address of the VarArgsFrameIndex slot into the
2781 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002782 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002783 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002784 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002785 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002786 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2787 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002788}
2789
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002790SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002791ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2792 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002793 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002794 MachineFunction &MF = DAG.getMachineFunction();
2795 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2796
Craig Topper760b1342012-02-22 05:59:10 +00002797 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002798 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002799 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002800 else
Craig Topperc7242e02012-04-20 07:30:17 +00002801 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002802
2803 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002804 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002805 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002806
2807 SDValue ArgValue2;
2808 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002809 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002810 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002811
2812 // Create load node to retrieve arguments from the stack.
2813 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Chengcdbb70c2009-10-31 03:39:36 +00002814 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002815 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002816 false, false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002817 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002818 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002819 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002820 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002821 if (!Subtarget->isLittle())
2822 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002823 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002824}
2825
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002826void
2827ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002828 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002829 unsigned ArgSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002830 unsigned &ArgRegsSize,
2831 unsigned &ArgRegsSaveSize)
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002832 const {
2833 unsigned NumGPRs;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002834 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2835 unsigned RBegin, REnd;
2836 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2837 NumGPRs = REnd - RBegin;
2838 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002839 unsigned int firstUnalloced;
2840 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2841 sizeof(GPRArgRegs) /
2842 sizeof(GPRArgRegs[0]));
2843 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2844 }
2845
Eric Christopher1889fdc2015-01-29 00:19:39 +00002846 unsigned Align = Subtarget->getFrameLowering()->getStackAlignment();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002847 ArgRegsSize = NumGPRs * 4;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002848
2849 // If parameter is split between stack and GPRs...
Mark Seabornbe266aa2014-02-16 18:59:48 +00002850 if (NumGPRs && Align > 4 &&
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002851 (ArgRegsSize < ArgSize ||
2852 InRegsParamRecordIdx >= CCInfo.getInRegsParamsCount())) {
Mark Seabornbe266aa2014-02-16 18:59:48 +00002853 // Add padding for part of param recovered from GPRs. For example,
2854 // if Align == 8, its last byte must be at address K*8 - 1.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002855 // We need to do it, since remained (stack) part of parameter has
2856 // stack alignment, and we need to "attach" "GPRs head" without gaps
2857 // to it:
2858 // Stack:
2859 // |---- 8 bytes block ----| |---- 8 bytes block ----| |---- 8 bytes...
2860 // [ [padding] [GPRs head] ] [ Tail passed via stack ....
2861 //
2862 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2863 unsigned Padding =
Mark Seabornbe266aa2014-02-16 18:59:48 +00002864 OffsetToAlignment(ArgRegsSize + AFI->getArgRegsSaveSize(), Align);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002865 ArgRegsSaveSize = ArgRegsSize + Padding;
2866 } else
2867 // We don't need to extend regs save size for byval parameters if they
2868 // are passed via GPRs only.
2869 ArgRegsSaveSize = ArgRegsSize;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002870}
2871
2872// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002873// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002874// byval). Either way, we allocate stack slots adjacent to the data
2875// provided by our caller, and store the unallocated registers there.
2876// If this is a variadic function, the va_list pointer will begin with
2877// these values; otherwise, this reassembles a (byval) structure that
2878// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002879// Return: The frame index registers were stored into.
2880int
2881ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002882 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002883 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002884 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002885 unsigned OffsetFromOrigArg,
2886 unsigned ArgOffset,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002887 unsigned ArgSize,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002888 bool ForceMutable,
2889 unsigned ByValStoreOffset,
2890 unsigned TotalArgRegsSaveSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002891
2892 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002893 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002894 // Setup first unallocated register as first byval register;
2895 // eat all remained registers
2896 // (these two actions are performed by HandleByVal method).
2897 // Then, here, we initialize stack frame with
2898 // "store-reg" instructions.
2899 // Case #2. Var-args function, that doesn't contain byval parameters.
2900 // The same: eat all remained unallocated registers,
2901 // initialize stack frame.
2902
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002903 MachineFunction &MF = DAG.getMachineFunction();
2904 MachineFrameInfo *MFI = MF.getFrameInfo();
2905 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002906 unsigned firstRegToSaveIndex, lastRegToSaveIndex;
2907 unsigned RBegin, REnd;
2908 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2909 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2910 firstRegToSaveIndex = RBegin - ARM::R0;
2911 lastRegToSaveIndex = REnd - ARM::R0;
2912 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002913 firstRegToSaveIndex = CCInfo.getFirstUnallocated
Craig Topper58713212013-07-15 04:27:47 +00002914 (GPRArgRegs, array_lengthof(GPRArgRegs));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002915 lastRegToSaveIndex = 4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002916 }
2917
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002918 unsigned ArgRegsSize, ArgRegsSaveSize;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002919 computeRegArea(CCInfo, MF, InRegsParamRecordIdx, ArgSize,
2920 ArgRegsSize, ArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002921
2922 // Store any by-val regs to their spots on the stack so that they may be
2923 // loaded by deferencing the result of formal parameter pointer or va_next.
2924 // Note: once stack area for byval/varargs registers
2925 // was initialized, it can't be initialized again.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002926 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002927 unsigned Padding = ArgRegsSaveSize - ArgRegsSize;
2928
2929 if (Padding) {
2930 assert(AFI->getStoredByValParamsPadding() == 0 &&
2931 "The only parameter may be padded.");
2932 AFI->setStoredByValParamsPadding(Padding);
2933 }
2934
Oliver Stannardd55e1152014-03-05 15:25:27 +00002935 int FrameIndex = MFI->CreateFixedObject(ArgRegsSaveSize,
2936 Padding +
2937 ByValStoreOffset -
2938 (int64_t)TotalArgRegsSaveSize,
2939 false);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002940 SDValue FIN = DAG.getFrameIndex(FrameIndex, getPointerTy());
Oliver Stannardd55e1152014-03-05 15:25:27 +00002941 if (Padding) {
2942 MFI->CreateFixedObject(Padding,
2943 ArgOffset + ByValStoreOffset -
2944 (int64_t)ArgRegsSaveSize,
2945 false);
2946 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002947
2948 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002949 for (unsigned i = 0; firstRegToSaveIndex < lastRegToSaveIndex;
2950 ++firstRegToSaveIndex, ++i) {
Craig Topper760b1342012-02-22 05:59:10 +00002951 const TargetRegisterClass *RC;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002952 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002953 RC = &ARM::tGPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002954 else
Craig Topperc7242e02012-04-20 07:30:17 +00002955 RC = &ARM::GPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002956
2957 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2958 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2959 SDValue Store =
2960 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002961 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002962 false, false, 0);
2963 MemOps.push_back(Store);
2964 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2965 DAG.getConstant(4, getPointerTy()));
2966 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002967
2968 AFI->setArgRegsSaveSize(ArgRegsSaveSize + AFI->getArgRegsSaveSize());
2969
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002970 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002971 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002972 return FrameIndex;
Oliver Stannardd55e1152014-03-05 15:25:27 +00002973 } else {
2974 if (ArgSize == 0) {
2975 // We cannot allocate a zero-byte object for the first variadic argument,
2976 // so just make up a size.
2977 ArgSize = 4;
2978 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002979 // This will point to the next argument passed via stack.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002980 return MFI->CreateFixedObject(
Oliver Stannardd55e1152014-03-05 15:25:27 +00002981 ArgSize, ArgOffset, !ForceMutable);
2982 }
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002983}
2984
2985// Setup stack frame, the va_list pointer will start from.
2986void
2987ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002988 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002989 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002990 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002991 bool ForceMutable) const {
2992 MachineFunction &MF = DAG.getMachineFunction();
2993 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2994
2995 // Try to store any remaining integer argument regs
2996 // to their spots on the stack so that they may be loaded by deferencing
2997 // the result of va_next.
2998 // If there is no regs to be stored, just point address after last
2999 // argument passed via stack.
3000 int FrameIndex =
Craig Topper062a2ba2014-04-25 05:30:21 +00003001 StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
3002 CCInfo.getInRegsParamsCount(), 0, ArgOffset, 0, ForceMutable,
3003 0, TotalArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003004
3005 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003006}
3007
Bob Wilson2e076c42009-06-22 23:27:02 +00003008SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003009ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003010 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003011 const SmallVectorImpl<ISD::InputArg>
3012 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003013 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003014 SmallVectorImpl<SDValue> &InVals)
3015 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00003016 MachineFunction &MF = DAG.getMachineFunction();
3017 MachineFrameInfo *MFI = MF.getFrameInfo();
3018
Bob Wilsona4c22902009-04-17 19:07:39 +00003019 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3020
3021 // Assign locations to all of the incoming arguments.
3022 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003023 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3024 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003025 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003026 CCAssignFnForNode(CallConv, /* Return*/ false,
3027 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00003028
Bob Wilsona4c22902009-04-17 19:07:39 +00003029 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003030 int lastInsIndex = -1;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003031 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003032 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
3033 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003034
3035 // Initially ArgRegsSaveSize is zero.
3036 // Then we increase this value each time we meet byval parameter.
3037 // We also increase this value in case of varargs function.
3038 AFI->setArgRegsSaveSize(0);
3039
Oliver Stannardd55e1152014-03-05 15:25:27 +00003040 unsigned ByValStoreOffset = 0;
3041 unsigned TotalArgRegsSaveSize = 0;
3042 unsigned ArgRegsSaveSizeMaxAlign = 4;
3043
3044 // Calculate the amount of stack space that we need to allocate to store
3045 // byval and variadic arguments that are passed in registers.
3046 // We need to know this before we allocate the first byval or variadic
3047 // argument, as they will be allocated a stack slot below the CFA (Canonical
3048 // Frame Address, the stack pointer at entry to the function).
3049 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3050 CCValAssign &VA = ArgLocs[i];
3051 if (VA.isMemLoc()) {
3052 int index = VA.getValNo();
3053 if (index != lastInsIndex) {
3054 ISD::ArgFlagsTy Flags = Ins[index].Flags;
3055 if (Flags.isByVal()) {
3056 unsigned ExtraArgRegsSize;
3057 unsigned ExtraArgRegsSaveSize;
Daniel Sanders8104b752014-11-01 19:32:23 +00003058 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsProcessed(),
Oliver Stannardd55e1152014-03-05 15:25:27 +00003059 Flags.getByValSize(),
3060 ExtraArgRegsSize, ExtraArgRegsSaveSize);
3061
3062 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
3063 if (Flags.getByValAlign() > ArgRegsSaveSizeMaxAlign)
3064 ArgRegsSaveSizeMaxAlign = Flags.getByValAlign();
3065 CCInfo.nextInRegsParam();
3066 }
3067 lastInsIndex = index;
3068 }
3069 }
3070 }
3071 CCInfo.rewindByValRegsInfo();
3072 lastInsIndex = -1;
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003073 if (isVarArg && MFI->hasVAStart()) {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003074 unsigned ExtraArgRegsSize;
3075 unsigned ExtraArgRegsSaveSize;
3076 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsCount(), 0,
3077 ExtraArgRegsSize, ExtraArgRegsSaveSize);
3078 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
3079 }
3080 // If the arg regs save area contains N-byte aligned values, the
3081 // bottom of it must be at least N-byte aligned.
3082 TotalArgRegsSaveSize = RoundUpToAlignment(TotalArgRegsSaveSize, ArgRegsSaveSizeMaxAlign);
3083 TotalArgRegsSaveSize = std::min(TotalArgRegsSaveSize, 16U);
3084
Bob Wilsona4c22902009-04-17 19:07:39 +00003085 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3086 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003087 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
3088 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003089 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003090 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003091 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003092
Bob Wilsona4c22902009-04-17 19:07:39 +00003093 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003094 // f64 and vector types are split up into multiple registers or
3095 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003096 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003097 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003098 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003099 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003100 SDValue ArgValue2;
3101 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003102 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson699bdf72010-04-13 22:03:22 +00003103 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3104 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003105 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003106 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003107 } else {
3108 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3109 Chain, DAG, dl);
3110 }
Owen Anderson9f944592009-08-11 20:47:22 +00003111 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3112 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00003113 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson9f944592009-08-11 20:47:22 +00003114 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00003115 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
3116 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003117 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003118
Bob Wilson2e076c42009-06-22 23:27:02 +00003119 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003120 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003121
Owen Anderson9f944592009-08-11 20:47:22 +00003122 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003123 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003124 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003125 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003126 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003127 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003128 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003129 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3130 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003131 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003132 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003133
3134 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003135 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003136 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003137 }
3138
3139 // If this is an 8 or 16-bit value, it is really passed promoted
3140 // to 32 bits. Insert an assert[sz]ext to capture this, then
3141 // truncate to the right size.
3142 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003143 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003144 case CCValAssign::Full: break;
3145 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003146 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003147 break;
3148 case CCValAssign::SExt:
3149 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3150 DAG.getValueType(VA.getValVT()));
3151 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3152 break;
3153 case CCValAssign::ZExt:
3154 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3155 DAG.getValueType(VA.getValVT()));
3156 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3157 break;
3158 }
3159
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003160 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003161
3162 } else { // VA.isRegLoc()
3163
3164 // sanity check
3165 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003166 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003167
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003168 int index = ArgLocs[i].getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003169
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003170 // Some Ins[] entries become multiple ArgLoc[] entries.
3171 // Process them only once.
3172 if (index != lastInsIndex)
3173 {
3174 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003175 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003176 // This can be changed with more analysis.
3177 // In case of tail call optimization mark all arguments mutable.
3178 // Since they could be overwritten by lowering of arguments in case of
3179 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003180 if (Flags.isByVal()) {
Daniel Sanders8104b752014-11-01 19:32:23 +00003181 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003182
3183 ByValStoreOffset = RoundUpToAlignment(ByValStoreOffset, Flags.getByValAlign());
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003184 int FrameIndex = StoreByValRegs(
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003185 CCInfo, DAG, dl, Chain, CurOrigArg,
3186 CurByValIndex,
3187 Ins[VA.getValNo()].PartOffset,
3188 VA.getLocMemOffset(),
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003189 Flags.getByValSize(),
Oliver Stannardd55e1152014-03-05 15:25:27 +00003190 true /*force mutable frames*/,
3191 ByValStoreOffset,
3192 TotalArgRegsSaveSize);
3193 ByValStoreOffset += Flags.getByValSize();
3194 ByValStoreOffset = std::min(ByValStoreOffset, 16U);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003195 InVals.push_back(DAG.getFrameIndex(FrameIndex, getPointerTy()));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003196 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003197 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003198 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003199 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003200 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003201
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003202 // Create load nodes to retrieve arguments from the stack.
3203 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3204 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3205 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003206 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003207 }
3208 lastInsIndex = index;
3209 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003210 }
3211 }
3212
3213 // varargs
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003214 if (isVarArg && MFI->hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003215 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003216 CCInfo.getNextStackOffset(),
3217 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003218
Oliver Stannardb14c6252014-04-02 16:10:33 +00003219 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3220
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003221 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003222}
3223
3224/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003225static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003226 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003227 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003228 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003229 // Maybe this has already been legalized into the constant pool?
3230 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003231 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003232 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003233 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003234 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003235 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003236 } else if (Op->getOpcode() == ISD::BITCAST &&
3237 Op->getValueType(0) == MVT::f64) {
3238 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3239 // created by LowerConstantFP().
3240 SDValue BitcastOp = Op->getOperand(0);
3241 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM) {
3242 SDValue MoveOp = BitcastOp->getOperand(0);
3243 if (MoveOp->getOpcode() == ISD::TargetConstant &&
3244 cast<ConstantSDNode>(MoveOp)->getZExtValue() == 0) {
3245 return true;
3246 }
3247 }
Evan Cheng10043e22007-01-19 07:51:42 +00003248 }
3249 return false;
3250}
3251
Evan Cheng10043e22007-01-19 07:51:42 +00003252/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3253/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003254SDValue
3255ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003256 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003257 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003258 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003259 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003260 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003261 // Constant does not fit, try adjusting it by one?
3262 switch (CC) {
3263 default: break;
3264 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003265 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003266 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003267 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003268 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003269 }
3270 break;
3271 case ISD::SETULT:
3272 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003273 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003274 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003275 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003276 }
3277 break;
3278 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003279 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003280 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003281 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003282 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003283 }
3284 break;
3285 case ISD::SETULE:
3286 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003287 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003288 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003289 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003290 }
3291 break;
3292 }
3293 }
3294 }
3295
3296 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003297 ARMISD::NodeType CompareType;
3298 switch (CondCode) {
3299 default:
3300 CompareType = ARMISD::CMP;
3301 break;
3302 case ARMCC::EQ:
3303 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003304 // Uses only Z Flag
3305 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003306 break;
3307 }
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003308 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003309 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003310}
3311
3312/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003313SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003314ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003315 SDLoc dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003316 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003317 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003318 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003319 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003320 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003321 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3322 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003323}
3324
Bob Wilson45acbd02011-03-08 01:17:20 +00003325/// duplicateCmp - Glue values can have only one use, so this function
3326/// duplicates a comparison node.
3327SDValue
3328ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3329 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003330 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003331 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3332 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3333
3334 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3335 Cmp = Cmp.getOperand(0);
3336 Opc = Cmp.getOpcode();
3337 if (Opc == ARMISD::CMPFP)
3338 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3339 else {
3340 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3341 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3342 }
3343 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3344}
3345
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003346std::pair<SDValue, SDValue>
3347ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3348 SDValue &ARMcc) const {
3349 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3350
3351 SDValue Value, OverflowCmp;
3352 SDValue LHS = Op.getOperand(0);
3353 SDValue RHS = Op.getOperand(1);
3354
3355
3356 // FIXME: We are currently always generating CMPs because we don't support
3357 // generating CMN through the backend. This is not as good as the natural
3358 // CMP case because it causes a register dependency and cannot be folded
3359 // later.
3360
3361 switch (Op.getOpcode()) {
3362 default:
3363 llvm_unreachable("Unknown overflow instruction!");
3364 case ISD::SADDO:
3365 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3366 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3367 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3368 break;
3369 case ISD::UADDO:
3370 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3371 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3372 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3373 break;
3374 case ISD::SSUBO:
3375 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3376 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3377 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3378 break;
3379 case ISD::USUBO:
3380 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3381 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3382 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3383 break;
3384 } // switch (...)
3385
3386 return std::make_pair(Value, OverflowCmp);
3387}
3388
3389
3390SDValue
3391ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3392 // Let legalize expand this if it isn't a legal type yet.
3393 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3394 return SDValue();
3395
3396 SDValue Value, OverflowCmp;
3397 SDValue ARMcc;
3398 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3399 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3400 // We use 0 and 1 as false and true values.
3401 SDValue TVal = DAG.getConstant(1, MVT::i32);
3402 SDValue FVal = DAG.getConstant(0, MVT::i32);
3403 EVT VT = Op.getValueType();
3404
3405 SDValue Overflow = DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, TVal, FVal,
3406 ARMcc, CCR, OverflowCmp);
3407
3408 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
3409 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), VTs, Value, Overflow);
3410}
3411
3412
Bill Wendling6a981312010-08-11 08:43:16 +00003413SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3414 SDValue Cond = Op.getOperand(0);
3415 SDValue SelectTrue = Op.getOperand(1);
3416 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003417 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003418 unsigned Opc = Cond.getOpcode();
3419
3420 if (Cond.getResNo() == 1 &&
3421 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3422 Opc == ISD::USUBO)) {
3423 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3424 return SDValue();
3425
3426 SDValue Value, OverflowCmp;
3427 SDValue ARMcc;
3428 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3429 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3430 EVT VT = Op.getValueType();
3431
Oliver Stannard51b1d462014-08-21 12:50:31 +00003432 return getCMOV(SDLoc(Op), VT, SelectTrue, SelectFalse, ARMcc, CCR,
3433 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003434 }
Bill Wendling6a981312010-08-11 08:43:16 +00003435
3436 // Convert:
3437 //
3438 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3439 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3440 //
3441 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3442 const ConstantSDNode *CMOVTrue =
3443 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3444 const ConstantSDNode *CMOVFalse =
3445 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3446
3447 if (CMOVTrue && CMOVFalse) {
3448 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3449 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3450
3451 SDValue True;
3452 SDValue False;
3453 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3454 True = SelectTrue;
3455 False = SelectFalse;
3456 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3457 True = SelectFalse;
3458 False = SelectTrue;
3459 }
3460
3461 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003462 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003463 SDValue ARMcc = Cond.getOperand(2);
3464 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003465 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003466 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003467 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00003468 }
3469 }
3470 }
3471
Dan Gohmand4a77c42012-02-24 00:09:36 +00003472 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3473 // undefined bits before doing a full-word comparison with zero.
3474 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
3475 DAG.getConstant(1, Cond.getValueType()));
3476
Bill Wendling6a981312010-08-11 08:43:16 +00003477 return DAG.getSelectCC(dl, Cond,
3478 DAG.getConstant(0, Cond.getValueType()),
3479 SelectTrue, SelectFalse, ISD::SETNE);
3480}
3481
Joey Gouly881eab52013-08-22 15:29:11 +00003482static ISD::CondCode getInverseCCForVSEL(ISD::CondCode CC) {
3483 if (CC == ISD::SETNE)
3484 return ISD::SETEQ;
Weiming Zhao63871d22013-12-18 22:25:17 +00003485 return ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003486}
3487
3488static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3489 bool &swpCmpOps, bool &swpVselOps) {
3490 // Start by selecting the GE condition code for opcodes that return true for
3491 // 'equality'
3492 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3493 CC == ISD::SETULE)
3494 CondCode = ARMCC::GE;
3495
3496 // and GT for opcodes that return false for 'equality'.
3497 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3498 CC == ISD::SETULT)
3499 CondCode = ARMCC::GT;
3500
3501 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3502 // to swap the compare operands.
3503 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3504 CC == ISD::SETULT)
3505 swpCmpOps = true;
3506
3507 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3508 // If we have an unordered opcode, we need to swap the operands to the VSEL
3509 // instruction (effectively negating the condition).
3510 //
3511 // This also has the effect of swapping which one of 'less' or 'greater'
3512 // returns true, so we also swap the compare operands. It also switches
3513 // whether we return true for 'equality', so we compensate by picking the
3514 // opposite condition code to our original choice.
3515 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3516 CC == ISD::SETUGT) {
3517 swpCmpOps = !swpCmpOps;
3518 swpVselOps = !swpVselOps;
3519 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3520 }
3521
3522 // 'ordered' is 'anything but unordered', so use the VS condition code and
3523 // swap the VSEL operands.
3524 if (CC == ISD::SETO) {
3525 CondCode = ARMCC::VS;
3526 swpVselOps = true;
3527 }
3528
3529 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3530 // code and swap the VSEL operands.
3531 if (CC == ISD::SETUNE) {
3532 CondCode = ARMCC::EQ;
3533 swpVselOps = true;
3534 }
3535}
3536
Oliver Stannard51b1d462014-08-21 12:50:31 +00003537SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal,
3538 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
3539 SDValue Cmp, SelectionDAG &DAG) const {
3540 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
3541 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3542 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
3543 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3544 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
3545
3546 SDValue TrueLow = TrueVal.getValue(0);
3547 SDValue TrueHigh = TrueVal.getValue(1);
3548 SDValue FalseLow = FalseVal.getValue(0);
3549 SDValue FalseHigh = FalseVal.getValue(1);
3550
3551 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
3552 ARMcc, CCR, Cmp);
3553 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
3554 ARMcc, CCR, duplicateCmp(Cmp, DAG));
3555
3556 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
3557 } else {
3558 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3559 Cmp);
3560 }
3561}
3562
Dan Gohman21cea8a2010-04-17 15:26:15 +00003563SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003564 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003565 SDValue LHS = Op.getOperand(0);
3566 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003567 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003568 SDValue TrueVal = Op.getOperand(2);
3569 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003570 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003571
Oliver Stannard51b1d462014-08-21 12:50:31 +00003572 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3573 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3574 dl);
3575
3576 // If softenSetCCOperands only returned one value, we should compare it to
3577 // zero.
3578 if (!RHS.getNode()) {
3579 RHS = DAG.getConstant(0, LHS.getValueType());
3580 CC = ISD::SETNE;
3581 }
3582 }
3583
Owen Anderson9f944592009-08-11 20:47:22 +00003584 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003585 // Try to generate VSEL on ARMv8.
3586 // The VSEL instruction can't use all the usual ARM condition
3587 // codes: it only has two bits to select the condition code, so it's
3588 // constrained to use only GE, GT, VS and EQ.
3589 //
3590 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3591 // swap the operands of the previous compare instruction (effectively
3592 // inverting the compare condition, swapping 'less' and 'greater') and
3593 // sometimes need to swap the operands to the VSEL (which inverts the
3594 // condition in the sense of firing whenever the previous condition didn't)
Eric Christopher1889fdc2015-01-29 00:19:39 +00003595 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3596 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003597 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3598 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3599 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
3600 CC = getInverseCCForVSEL(CC);
3601 std::swap(TrueVal, FalseVal);
3602 }
3603 }
3604
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003605 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003606 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003607 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003608 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003609 }
3610
3611 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003612 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003613
Joey Gouly881eab52013-08-22 15:29:11 +00003614 // Try to generate VSEL on ARMv8.
Eric Christopher1889fdc2015-01-29 00:19:39 +00003615 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3616 TrueVal.getValueType() == MVT::f64)) {
Joey Goulye3dd6842013-08-23 12:01:13 +00003617 // We can select VMAXNM/VMINNM from a compare followed by a select with the
3618 // same operands, as follows:
3619 // c = fcmp [ogt, olt, ugt, ult] a, b
3620 // select c, a, b
3621 // We only do this in unsafe-fp-math, because signed zeros and NaNs are
3622 // handled differently than the original code sequence.
Oliver Stannard79efe412014-10-27 09:23:02 +00003623 if (getTargetMachine().Options.UnsafeFPMath) {
3624 if (LHS == TrueVal && RHS == FalseVal) {
3625 if (CC == ISD::SETOGT || CC == ISD::SETUGT)
3626 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal);
3627 if (CC == ISD::SETOLT || CC == ISD::SETULT)
3628 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal);
3629 } else if (LHS == FalseVal && RHS == TrueVal) {
3630 if (CC == ISD::SETOLT || CC == ISD::SETULT)
3631 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal);
3632 if (CC == ISD::SETOGT || CC == ISD::SETUGT)
3633 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal);
3634 }
Joey Goulye3dd6842013-08-23 12:01:13 +00003635 }
3636
Joey Gouly881eab52013-08-22 15:29:11 +00003637 bool swpCmpOps = false;
3638 bool swpVselOps = false;
3639 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3640
3641 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3642 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3643 if (swpCmpOps)
3644 std::swap(LHS, RHS);
3645 if (swpVselOps)
3646 std::swap(TrueVal, FalseVal);
3647 }
3648 }
3649
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003650 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3651 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003652 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003653 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003654 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003655 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003656 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003657 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003658 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003659 }
3660 return Result;
3661}
3662
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003663/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3664/// to morph to an integer compare sequence.
3665static bool canChangeToInt(SDValue Op, bool &SeenZero,
3666 const ARMSubtarget *Subtarget) {
3667 SDNode *N = Op.getNode();
3668 if (!N->hasOneUse())
3669 // Otherwise it requires moving the value from fp to integer registers.
3670 return false;
3671 if (!N->getNumValues())
3672 return false;
3673 EVT VT = Op.getValueType();
3674 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3675 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3676 // vmrs are very slow, e.g. cortex-a8.
3677 return false;
3678
3679 if (isFloatingPointZero(Op)) {
3680 SeenZero = true;
3681 return true;
3682 }
3683 return ISD::isNormalLoad(N);
3684}
3685
3686static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3687 if (isFloatingPointZero(Op))
3688 return DAG.getConstant(0, MVT::i32);
3689
3690 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003691 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003692 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003693 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003694 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003695
3696 llvm_unreachable("Unknown VFP cmp argument!");
3697}
3698
3699static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3700 SDValue &RetVal1, SDValue &RetVal2) {
3701 if (isFloatingPointZero(Op)) {
3702 RetVal1 = DAG.getConstant(0, MVT::i32);
3703 RetVal2 = DAG.getConstant(0, MVT::i32);
3704 return;
3705 }
3706
3707 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3708 SDValue Ptr = Ld->getBasePtr();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003709 RetVal1 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003710 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003711 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003712 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003713 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003714
3715 EVT PtrType = Ptr.getValueType();
3716 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003717 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003718 PtrType, Ptr, DAG.getConstant(4, PtrType));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003719 RetVal2 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003720 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003721 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003722 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003723 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003724 return;
3725 }
3726
3727 llvm_unreachable("Unknown VFP cmp argument!");
3728}
3729
3730/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3731/// f32 and even f64 comparisons to integer ones.
3732SDValue
3733ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3734 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003735 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003736 SDValue LHS = Op.getOperand(2);
3737 SDValue RHS = Op.getOperand(3);
3738 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003739 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003740
Evan Chengd12af5d2012-03-01 23:27:13 +00003741 bool LHSSeenZero = false;
3742 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3743 bool RHSSeenZero = false;
3744 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3745 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003746 // If unsafe fp math optimization is enabled and there are no other uses of
3747 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003748 // to an integer comparison.
3749 if (CC == ISD::SETOEQ)
3750 CC = ISD::SETEQ;
3751 else if (CC == ISD::SETUNE)
3752 CC = ISD::SETNE;
3753
Evan Chengd12af5d2012-03-01 23:27:13 +00003754 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003755 SDValue ARMcc;
3756 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003757 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3758 bitcastf32Toi32(LHS, DAG), Mask);
3759 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3760 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003761 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3762 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3763 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3764 Chain, Dest, ARMcc, CCR, Cmp);
3765 }
3766
3767 SDValue LHS1, LHS2;
3768 SDValue RHS1, RHS2;
3769 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3770 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003771 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3772 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003773 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3774 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003775 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003776 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003777 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003778 }
3779
3780 return SDValue();
3781}
3782
3783SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3784 SDValue Chain = Op.getOperand(0);
3785 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3786 SDValue LHS = Op.getOperand(2);
3787 SDValue RHS = Op.getOperand(3);
3788 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003789 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003790
Oliver Stannard51b1d462014-08-21 12:50:31 +00003791 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3792 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3793 dl);
3794
3795 // If softenSetCCOperands only returned one value, we should compare it to
3796 // zero.
3797 if (!RHS.getNode()) {
3798 RHS = DAG.getConstant(0, LHS.getValueType());
3799 CC = ISD::SETNE;
3800 }
3801 }
3802
Owen Anderson9f944592009-08-11 20:47:22 +00003803 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003804 SDValue ARMcc;
3805 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003806 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003807 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003808 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003809 }
3810
Owen Anderson9f944592009-08-11 20:47:22 +00003811 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003812
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003813 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003814 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3815 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3816 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3817 if (Result.getNode())
3818 return Result;
3819 }
3820
Evan Cheng10043e22007-01-19 07:51:42 +00003821 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003822 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003823
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003824 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3825 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003826 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003827 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003828 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003829 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003830 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003831 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3832 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003833 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003834 }
3835 return Res;
3836}
3837
Dan Gohman21cea8a2010-04-17 15:26:15 +00003838SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003839 SDValue Chain = Op.getOperand(0);
3840 SDValue Table = Op.getOperand(1);
3841 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003842 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003843
Owen Anderson53aa7a92009-08-10 22:56:29 +00003844 EVT PTy = getPointerTy();
Evan Cheng10043e22007-01-19 07:51:42 +00003845 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3846 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3f17aee2009-07-14 18:44:34 +00003847 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003848 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson9f944592009-08-11 20:47:22 +00003849 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chengc8bed032009-07-28 20:53:24 +00003850 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3851 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003852 if (Subtarget->isThumb2()) {
3853 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3854 // which does another jump to the destination. This also makes it easier
3855 // to translate it to TBB / TBH later.
3856 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003857 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Chengc6d70ae2009-07-29 02:18:14 +00003858 Addr, Op.getOperand(2), JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003859 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003860 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003861 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattner7727d052010-09-21 06:44:06 +00003862 MachinePointerInfo::getJumpTable(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003863 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003864 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003865 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson9f944592009-08-11 20:47:22 +00003866 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003867 } else {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003868 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003869 MachinePointerInfo::getJumpTable(),
3870 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003871 Chain = Addr.getValue(1);
Owen Anderson9f944592009-08-11 20:47:22 +00003872 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003873 }
Evan Cheng10043e22007-01-19 07:51:42 +00003874}
3875
Eli Friedman2d4055b2011-11-09 23:36:02 +00003876static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003877 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003878 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003879
James Molloy547d4c02012-02-20 09:24:05 +00003880 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3881 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3882 return Op;
3883 return DAG.UnrollVectorOp(Op.getNode());
3884 }
3885
3886 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3887 "Invalid type for custom lowering!");
3888 if (VT != MVT::v4i16)
3889 return DAG.UnrollVectorOp(Op.getNode());
3890
3891 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3892 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003893}
3894
Oliver Stannard51b1d462014-08-21 12:50:31 +00003895SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003896 EVT VT = Op.getValueType();
3897 if (VT.isVector())
3898 return LowerVectorFP_TO_INT(Op, DAG);
3899
Oliver Stannard51b1d462014-08-21 12:50:31 +00003900 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
3901 RTLIB::Libcall LC;
3902 if (Op.getOpcode() == ISD::FP_TO_SINT)
3903 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
3904 Op.getValueType());
3905 else
3906 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
3907 Op.getValueType());
3908 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3909 /*isSigned*/ false, SDLoc(Op)).first;
3910 }
3911
Andrew Trickef9de2a2013-05-25 02:42:55 +00003912 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003913 unsigned Opc;
3914
3915 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003916 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003917 case ISD::FP_TO_SINT:
3918 Opc = ARMISD::FTOSI;
3919 break;
3920 case ISD::FP_TO_UINT:
3921 Opc = ARMISD::FTOUI;
3922 break;
3923 }
3924 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peck527da1b2010-11-23 03:31:01 +00003925 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003926}
3927
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003928static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3929 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003930 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003931
Eli Friedman2d4055b2011-11-09 23:36:02 +00003932 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3933 if (VT.getVectorElementType() == MVT::f32)
3934 return Op;
3935 return DAG.UnrollVectorOp(Op.getNode());
3936 }
3937
Duncan Sandsa41634e2011-08-12 14:54:45 +00003938 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3939 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003940 if (VT != MVT::v4f32)
3941 return DAG.UnrollVectorOp(Op.getNode());
3942
3943 unsigned CastOpc;
3944 unsigned Opc;
3945 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003946 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003947 case ISD::SINT_TO_FP:
3948 CastOpc = ISD::SIGN_EXTEND;
3949 Opc = ISD::SINT_TO_FP;
3950 break;
3951 case ISD::UINT_TO_FP:
3952 CastOpc = ISD::ZERO_EXTEND;
3953 Opc = ISD::UINT_TO_FP;
3954 break;
3955 }
3956
3957 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3958 return DAG.getNode(Opc, dl, VT, Op);
3959}
3960
Oliver Stannard51b1d462014-08-21 12:50:31 +00003961SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00003962 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003963 if (VT.isVector())
3964 return LowerVectorINT_TO_FP(Op, DAG);
3965
Oliver Stannard51b1d462014-08-21 12:50:31 +00003966 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
3967 RTLIB::Libcall LC;
3968 if (Op.getOpcode() == ISD::SINT_TO_FP)
3969 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
3970 Op.getValueType());
3971 else
3972 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
3973 Op.getValueType());
3974 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3975 /*isSigned*/ false, SDLoc(Op)).first;
3976 }
3977
Andrew Trickef9de2a2013-05-25 02:42:55 +00003978 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003979 unsigned Opc;
3980
3981 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003982 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003983 case ISD::SINT_TO_FP:
3984 Opc = ARMISD::SITOF;
3985 break;
3986 case ISD::UINT_TO_FP:
3987 Opc = ARMISD::UITOF;
3988 break;
3989 }
3990
Wesley Peck527da1b2010-11-23 03:31:01 +00003991 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilsone4191e72010-03-19 22:51:32 +00003992 return DAG.getNode(Opc, dl, VT, Op);
3993}
3994
Evan Cheng25f93642010-07-08 02:08:50 +00003995SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00003996 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003997 SDValue Tmp0 = Op.getOperand(0);
3998 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003999 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004000 EVT VT = Op.getValueType();
4001 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00004002 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
4003 Tmp0.getOpcode() == ARMISD::VMOVDRR;
4004 bool UseNEON = !InGPR && Subtarget->hasNEON();
4005
4006 if (UseNEON) {
4007 // Use VBSL to copy the sign bit.
4008 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
4009 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
4010 DAG.getTargetConstant(EncodedVal, MVT::i32));
4011 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
4012 if (VT == MVT::f64)
4013 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4014 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
4015 DAG.getConstant(32, MVT::i32));
4016 else /*if (VT == MVT::f32)*/
4017 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
4018 if (SrcVT == MVT::f32) {
4019 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
4020 if (VT == MVT::f64)
4021 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4022 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
4023 DAG.getConstant(32, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00004024 } else if (VT == MVT::f32)
4025 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
4026 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
4027 DAG.getConstant(32, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004028 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4029 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4030
4031 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
4032 MVT::i32);
4033 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4034 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4035 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00004036
Evan Chengd6b641e2011-02-23 02:24:55 +00004037 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4038 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4039 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004040 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004041 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4042 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
4043 DAG.getConstant(0, MVT::i32));
4044 } else {
4045 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4046 }
4047
4048 return Res;
4049 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004050
4051 // Bitcast operand 1 to i32.
4052 if (SrcVT == MVT::f64)
4053 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004054 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004055 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4056
Evan Chengd6b641e2011-02-23 02:24:55 +00004057 // Or in the signbit with integer operations.
4058 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
4059 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
4060 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4061 if (VT == MVT::f32) {
4062 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4063 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4064 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4065 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004066 }
4067
Evan Chengd6b641e2011-02-23 02:24:55 +00004068 // f64: Or the high part with signbit and then combine two parts.
4069 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004070 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004071 SDValue Lo = Tmp0.getValue(0);
4072 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4073 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4074 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004075}
4076
Evan Cheng168ced92010-05-22 01:47:14 +00004077SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4078 MachineFunction &MF = DAG.getMachineFunction();
4079 MachineFrameInfo *MFI = MF.getFrameInfo();
4080 MFI->setReturnAddressIsTaken(true);
4081
Bill Wendling908bf812014-01-06 00:43:20 +00004082 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004083 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004084
Evan Cheng168ced92010-05-22 01:47:14 +00004085 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004086 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004087 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4088 if (Depth) {
4089 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
4090 SDValue Offset = DAG.getConstant(4, MVT::i32);
4091 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4092 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004093 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00004094 }
4095
4096 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004097 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004098 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4099}
4100
Dan Gohman21cea8a2010-04-17 15:26:15 +00004101SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004102 const ARMBaseRegisterInfo &ARI =
4103 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4104 MachineFunction &MF = DAG.getMachineFunction();
4105 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004106 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004107
Owen Anderson53aa7a92009-08-10 22:56:29 +00004108 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004109 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004110 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004111 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004112 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4113 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004114 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
4115 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004116 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004117 return FrameAddr;
4118}
4119
Renato Golinc7aea402014-05-06 16:51:25 +00004120// FIXME? Maybe this could be a TableGen attribute on some registers and
4121// this table could be generated automatically from RegInfo.
Hal Finkelf0e086a2014-05-11 19:29:07 +00004122unsigned ARMTargetLowering::getRegisterByName(const char* RegName,
4123 EVT VT) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004124 unsigned Reg = StringSwitch<unsigned>(RegName)
4125 .Case("sp", ARM::SP)
4126 .Default(0);
4127 if (Reg)
4128 return Reg;
4129 report_fatal_error("Invalid register name global variable");
4130}
4131
Wesley Peck527da1b2010-11-23 03:31:01 +00004132/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004133/// expand a bit convert where either the source or destination type is i64 to
4134/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4135/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4136/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004137static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004138 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004139 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004140 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004141
Bob Wilson59b70ea2010-04-17 05:30:19 +00004142 // This function is only supposed to be called for i64 types, either as the
4143 // source or destination of the bit convert.
4144 EVT SrcVT = Op.getValueType();
4145 EVT DstVT = N->getValueType(0);
4146 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004147 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004148
Bob Wilson59b70ea2010-04-17 05:30:19 +00004149 // Turn i64->f64 into VMOVDRR.
4150 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00004151 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
4152 DAG.getConstant(0, MVT::i32));
4153 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
4154 DAG.getConstant(1, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004155 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004156 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004157 }
Bob Wilson7117a912009-03-20 22:42:55 +00004158
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004159 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004160 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004161 SDValue Cvt;
Christian Pirker6692e7c2014-05-14 16:59:44 +00004162 if (TLI.isBigEndian() && SrcVT.isVector() &&
4163 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004164 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4165 DAG.getVTList(MVT::i32, MVT::i32),
4166 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4167 else
4168 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4169 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004170 // Merge the pieces into a single i64 value.
4171 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4172 }
Bob Wilson7117a912009-03-20 22:42:55 +00004173
Bob Wilson59b70ea2010-04-17 05:30:19 +00004174 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004175}
4176
Bob Wilson2e076c42009-06-22 23:27:02 +00004177/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004178/// Zero vectors are used to represent vector negation and in those cases
4179/// will be implemented with the NEON VNEG instruction. However, VNEG does
4180/// not support i64 elements, so sometimes the zero vectors will need to be
4181/// explicitly constructed. Regardless, use a canonical VMOV to create the
4182/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004183static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004184 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004185 // The canonical modified immediate encoding of a zero vector is....0!
4186 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
4187 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4188 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004189 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004190}
4191
Jim Grosbach624fcb22009-10-31 21:00:56 +00004192/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4193/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004194SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4195 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004196 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4197 EVT VT = Op.getValueType();
4198 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004199 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004200 SDValue ShOpLo = Op.getOperand(0);
4201 SDValue ShOpHi = Op.getOperand(1);
4202 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004203 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004204 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004205
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004206 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4207
Jim Grosbach624fcb22009-10-31 21:00:56 +00004208 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4209 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4210 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4211 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4212 DAG.getConstant(VTBits, MVT::i32));
4213 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4214 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004215 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004216
4217 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4218 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004219 ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004220 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004221 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004222 CCR, Cmp);
4223
4224 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004225 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004226}
4227
Jim Grosbach5d994042009-10-31 19:38:01 +00004228/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4229/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004230SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4231 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004232 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4233 EVT VT = Op.getValueType();
4234 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004235 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004236 SDValue ShOpLo = Op.getOperand(0);
4237 SDValue ShOpHi = Op.getOperand(1);
4238 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004239 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004240
4241 assert(Op.getOpcode() == ISD::SHL_PARTS);
4242 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4243 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4244 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4245 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4246 DAG.getConstant(VTBits, MVT::i32));
4247 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4248 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4249
4250 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4251 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4252 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004253 ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004254 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004255 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004256 CCR, Cmp);
4257
4258 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004259 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004260}
4261
Jim Grosbach535d3b42010-09-08 03:54:02 +00004262SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004263 SelectionDAG &DAG) const {
4264 // The rounding mode is in bits 23:22 of the FPSCR.
4265 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4266 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4267 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004268 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004269 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
4270 DAG.getConstant(Intrinsic::arm_get_fpscr,
4271 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004272 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemanb69b1822010-08-03 21:31:55 +00004273 DAG.getConstant(1U << 22, MVT::i32));
4274 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
4275 DAG.getConstant(22, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004276 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemanb69b1822010-08-03 21:31:55 +00004277 DAG.getConstant(3, MVT::i32));
4278}
4279
Jim Grosbach8546ec92010-01-18 19:58:49 +00004280static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4281 const ARMSubtarget *ST) {
4282 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004283 SDLoc dl(N);
Jim Grosbach8546ec92010-01-18 19:58:49 +00004284
4285 if (!ST->hasV6T2Ops())
4286 return SDValue();
4287
4288 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
4289 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4290}
4291
Evan Chengb4eae132012-12-04 22:41:50 +00004292/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4293/// for each 16-bit element from operand, repeated. The basic idea is to
4294/// leverage vcnt to get the 8-bit counts, gather and add the results.
4295///
4296/// Trace for v4i16:
4297/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4298/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4299/// 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 +00004300/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004301/// [b0 b1 b2 b3 b4 b5 b6 b7]
4302/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4303/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4304/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4305static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4306 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004307 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004308
4309 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4310 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4311 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4312 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4313 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4314 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4315}
4316
4317/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4318/// bit-count for each 16-bit element from the operand. We need slightly
4319/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4320/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004321///
Evan Chengb4eae132012-12-04 22:41:50 +00004322/// Trace for v4i16:
4323/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4324/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4325/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4326/// v4i16:Extracted = [k0 k1 k2 k3 ]
4327static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4328 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004329 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004330
4331 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4332 if (VT.is64BitVector()) {
4333 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4334 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
4335 DAG.getIntPtrConstant(0));
4336 } else {
4337 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
4338 BitCounts, DAG.getIntPtrConstant(0));
4339 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4340 }
4341}
4342
4343/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4344/// bit-count for each 32-bit element from the operand. The idea here is
4345/// to split the vector into 16-bit elements, leverage the 16-bit count
4346/// routine, and then combine the results.
4347///
4348/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4349/// input = [v0 v1 ] (vi: 32-bit elements)
4350/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4351/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004352/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004353/// [k0 k1 k2 k3 ]
4354/// N1 =+[k1 k0 k3 k2 ]
4355/// [k0 k2 k1 k3 ]
4356/// N2 =+[k1 k3 k0 k2 ]
4357/// [k0 k2 k1 k3 ]
4358/// Extended =+[k1 k3 k0 k2 ]
4359/// [k0 k2 ]
4360/// Extracted=+[k1 k3 ]
4361///
4362static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4363 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004364 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004365
4366 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4367
4368 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4369 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4370 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4371 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4372 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4373
4374 if (VT.is64BitVector()) {
4375 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4376 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
4377 DAG.getIntPtrConstant(0));
4378 } else {
4379 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
4380 DAG.getIntPtrConstant(0));
4381 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4382 }
4383}
4384
4385static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4386 const ARMSubtarget *ST) {
4387 EVT VT = N->getValueType(0);
4388
4389 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004390 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4391 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004392 "Unexpected type for custom ctpop lowering");
4393
4394 if (VT.getVectorElementType() == MVT::i32)
4395 return lowerCTPOP32BitElements(N, DAG);
4396 else
4397 return lowerCTPOP16BitElements(N, DAG);
4398}
4399
Bob Wilson2e076c42009-06-22 23:27:02 +00004400static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4401 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004402 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004403 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004404
Bob Wilson7d471332010-11-18 21:16:28 +00004405 if (!VT.isVector())
4406 return SDValue();
4407
Bob Wilson2e076c42009-06-22 23:27:02 +00004408 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004409 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004410
Bob Wilson7d471332010-11-18 21:16:28 +00004411 // Left shifts translate directly to the vshiftu intrinsic.
4412 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004413 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilson7d471332010-11-18 21:16:28 +00004414 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
4415 N->getOperand(0), N->getOperand(1));
4416
4417 assert((N->getOpcode() == ISD::SRA ||
4418 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4419
4420 // NEON uses the same intrinsics for both left and right shifts. For
4421 // right shifts, the shift amounts are negative, so negate the vector of
4422 // shift amounts.
4423 EVT ShiftVT = N->getOperand(1).getValueType();
4424 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4425 getZeroVector(ShiftVT, DAG, dl),
4426 N->getOperand(1));
4427 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4428 Intrinsic::arm_neon_vshifts :
4429 Intrinsic::arm_neon_vshiftu);
4430 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4431 DAG.getConstant(vshiftInt, MVT::i32),
4432 N->getOperand(0), NegatedCount);
4433}
4434
4435static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4436 const ARMSubtarget *ST) {
4437 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004438 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004439
Eli Friedman682d8c12009-08-22 03:13:10 +00004440 // We can get here for a node like i32 = ISD::SHL i32, i64
4441 if (VT != MVT::i64)
4442 return SDValue();
4443
4444 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004445 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004446
Chris Lattnerf81d5882007-11-24 07:07:01 +00004447 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4448 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004449 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004450 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004451
Chris Lattnerf81d5882007-11-24 07:07:01 +00004452 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004453 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004454
Chris Lattnerf81d5882007-11-24 07:07:01 +00004455 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004456 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004457 DAG.getConstant(0, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004458 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004459 DAG.getConstant(1, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004460
Chris Lattnerf81d5882007-11-24 07:07:01 +00004461 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4462 // captures the result into a carry flag.
4463 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004464 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004465
Chris Lattnerf81d5882007-11-24 07:07:01 +00004466 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004467 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004468
Chris Lattnerf81d5882007-11-24 07:07:01 +00004469 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004470 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004471}
4472
Bob Wilson2e076c42009-06-22 23:27:02 +00004473static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4474 SDValue TmpOp0, TmpOp1;
4475 bool Invert = false;
4476 bool Swap = false;
4477 unsigned Opc = 0;
4478
4479 SDValue Op0 = Op.getOperand(0);
4480 SDValue Op1 = Op.getOperand(1);
4481 SDValue CC = Op.getOperand(2);
Tim Northover45aa89c2015-02-08 00:50:47 +00004482 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004483 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004484 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004485 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004486
Oliver Stannard51b1d462014-08-21 12:50:31 +00004487 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004488 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004489 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004490 case ISD::SETUNE:
4491 case ISD::SETNE: Invert = true; // Fallthrough
4492 case ISD::SETOEQ:
4493 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4494 case ISD::SETOLT:
4495 case ISD::SETLT: Swap = true; // Fallthrough
4496 case ISD::SETOGT:
4497 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4498 case ISD::SETOLE:
4499 case ISD::SETLE: Swap = true; // Fallthrough
4500 case ISD::SETOGE:
4501 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4502 case ISD::SETUGE: Swap = true; // Fallthrough
4503 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4504 case ISD::SETUGT: Swap = true; // Fallthrough
4505 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4506 case ISD::SETUEQ: Invert = true; // Fallthrough
4507 case ISD::SETONE:
4508 // Expand this to (OLT | OGT).
4509 TmpOp0 = Op0;
4510 TmpOp1 = Op1;
4511 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004512 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4513 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004514 break;
4515 case ISD::SETUO: Invert = true; // Fallthrough
4516 case ISD::SETO:
4517 // Expand this to (OLT | OGE).
4518 TmpOp0 = Op0;
4519 TmpOp1 = Op1;
4520 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004521 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4522 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004523 break;
4524 }
4525 } else {
4526 // Integer comparisons.
4527 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004528 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004529 case ISD::SETNE: Invert = true;
4530 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4531 case ISD::SETLT: Swap = true;
4532 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4533 case ISD::SETLE: Swap = true;
4534 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4535 case ISD::SETULT: Swap = true;
4536 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4537 case ISD::SETULE: Swap = true;
4538 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4539 }
4540
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004541 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004542 if (Opc == ARMISD::VCEQ) {
4543
4544 SDValue AndOp;
4545 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4546 AndOp = Op0;
4547 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4548 AndOp = Op1;
4549
4550 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004551 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004552 AndOp = AndOp.getOperand(0);
4553
4554 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4555 Opc = ARMISD::VTST;
Tim Northover45aa89c2015-02-08 00:50:47 +00004556 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
4557 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004558 Invert = !Invert;
4559 }
4560 }
4561 }
4562
4563 if (Swap)
4564 std::swap(Op0, Op1);
4565
Owen Andersonc7baee32010-11-08 23:21:22 +00004566 // If one of the operands is a constant vector zero, attempt to fold the
4567 // comparison to a specialized compare-against-zero form.
4568 SDValue SingleOp;
4569 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4570 SingleOp = Op0;
4571 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4572 if (Opc == ARMISD::VCGE)
4573 Opc = ARMISD::VCLEZ;
4574 else if (Opc == ARMISD::VCGT)
4575 Opc = ARMISD::VCLTZ;
4576 SingleOp = Op1;
4577 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004578
Owen Andersonc7baee32010-11-08 23:21:22 +00004579 SDValue Result;
4580 if (SingleOp.getNode()) {
4581 switch (Opc) {
4582 case ARMISD::VCEQ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004583 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004584 case ARMISD::VCGE:
Tim Northover45aa89c2015-02-08 00:50:47 +00004585 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004586 case ARMISD::VCLEZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004587 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004588 case ARMISD::VCGT:
Tim Northover45aa89c2015-02-08 00:50:47 +00004589 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004590 case ARMISD::VCLTZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004591 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004592 default:
Tim Northover45aa89c2015-02-08 00:50:47 +00004593 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004594 }
4595 } else {
Tim Northover45aa89c2015-02-08 00:50:47 +00004596 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004597 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004598
Tim Northover45aa89c2015-02-08 00:50:47 +00004599 Result = DAG.getSExtOrTrunc(Result, dl, VT);
4600
Bob Wilson2e076c42009-06-22 23:27:02 +00004601 if (Invert)
4602 Result = DAG.getNOT(dl, Result, VT);
4603
4604 return Result;
4605}
4606
Bob Wilson5b2b5042010-06-14 22:19:57 +00004607/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4608/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004609/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004610static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4611 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Andersona4076922010-11-05 21:57:54 +00004612 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004613 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004614
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004615 // SplatBitSize is set to the smallest size that splats the vector, so a
4616 // zero vector will always have SplatBitSize == 8. However, NEON modified
4617 // immediate instructions others than VMOV do not support the 8-bit encoding
4618 // of a zero vector, and the default encoding of zero is supposed to be the
4619 // 32-bit version.
4620 if (SplatBits == 0)
4621 SplatBitSize = 32;
4622
Bob Wilson2e076c42009-06-22 23:27:02 +00004623 switch (SplatBitSize) {
4624 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004625 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004626 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004627 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004628 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004629 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004630 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004631 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004632 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004633
4634 case 16:
4635 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004636 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004637 if ((SplatBits & ~0xff) == 0) {
4638 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004639 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004640 Imm = SplatBits;
4641 break;
4642 }
4643 if ((SplatBits & ~0xff00) == 0) {
4644 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004645 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004646 Imm = SplatBits >> 8;
4647 break;
4648 }
4649 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004650
4651 case 32:
4652 // NEON's 32-bit VMOV supports splat values where:
4653 // * only one byte is nonzero, or
4654 // * the least significant byte is 0xff and the second byte is nonzero, or
4655 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004656 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004657 if ((SplatBits & ~0xff) == 0) {
4658 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004659 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004660 Imm = SplatBits;
4661 break;
4662 }
4663 if ((SplatBits & ~0xff00) == 0) {
4664 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004665 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004666 Imm = SplatBits >> 8;
4667 break;
4668 }
4669 if ((SplatBits & ~0xff0000) == 0) {
4670 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004671 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004672 Imm = SplatBits >> 16;
4673 break;
4674 }
4675 if ((SplatBits & ~0xff000000) == 0) {
4676 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004677 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004678 Imm = SplatBits >> 24;
4679 break;
4680 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004681
Owen Andersona4076922010-11-05 21:57:54 +00004682 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4683 if (type == OtherModImm) return SDValue();
4684
Bob Wilson2e076c42009-06-22 23:27:02 +00004685 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004686 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4687 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004688 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004689 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004690 break;
4691 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004692
4693 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004694 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4695 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004696 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004697 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004698 break;
4699 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004700
4701 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4702 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4703 // VMOV.I32. A (very) minor optimization would be to replicate the value
4704 // and fall through here to test for a valid 64-bit splat. But, then the
4705 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004706 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004707
4708 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004709 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004710 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004711 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004712 uint64_t BitMask = 0xff;
4713 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004714 unsigned ImmMask = 1;
4715 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004716 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004717 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004718 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004719 Imm |= ImmMask;
4720 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004721 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004722 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004723 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004724 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004725 }
Christian Pirker6f81e752014-06-23 18:05:53 +00004726
4727 if (DAG.getTargetLoweringInfo().isBigEndian())
4728 // swap higher and lower 32 bit word
4729 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
4730
Bob Wilson6eae5202010-06-11 21:34:50 +00004731 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004732 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004733 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004734 break;
4735 }
4736
Bob Wilson6eae5202010-06-11 21:34:50 +00004737 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004738 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004739 }
4740
Bob Wilsona3f19012010-07-13 21:16:48 +00004741 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4742 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004743}
4744
Lang Hames591cdaf2012-03-29 21:56:11 +00004745SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4746 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004747 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004748 return SDValue();
4749
Tim Northoverf79c3a52013-08-20 08:57:11 +00004750 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004751 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004752
Oliver Stannard51b1d462014-08-21 12:50:31 +00004753 // Use the default (constant pool) lowering for double constants when we have
4754 // an SP-only FPU
4755 if (IsDouble && Subtarget->isFPOnlySP())
4756 return SDValue();
4757
Lang Hames591cdaf2012-03-29 21:56:11 +00004758 // Try splatting with a VMOV.f32...
4759 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004760 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4761
Lang Hames591cdaf2012-03-29 21:56:11 +00004762 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004763 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4764 // We have code in place to select a valid ConstantFP already, no need to
4765 // do any mangling.
4766 return Op;
4767 }
4768
4769 // It's a float and we are trying to use NEON operations where
4770 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004771 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004772 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4773 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4774 NewVal);
4775 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4776 DAG.getConstant(0, MVT::i32));
4777 }
4778
Tim Northoverf79c3a52013-08-20 08:57:11 +00004779 // The rest of our options are NEON only, make sure that's allowed before
4780 // proceeding..
4781 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4782 return SDValue();
4783
Lang Hames591cdaf2012-03-29 21:56:11 +00004784 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004785 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4786
4787 // It wouldn't really be worth bothering for doubles except for one very
4788 // important value, which does happen to match: 0.0. So make sure we don't do
4789 // anything stupid.
4790 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4791 return SDValue();
4792
4793 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
4794 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4795 false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004796 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004797 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004798 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4799 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004800 if (IsDouble)
4801 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4802
4803 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004804 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4805 VecConstant);
4806 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4807 DAG.getConstant(0, MVT::i32));
4808 }
4809
4810 // Finally, try a VMVN.i32
Tim Northoverf79c3a52013-08-20 08:57:11 +00004811 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4812 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004813 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004814 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004815 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004816
4817 if (IsDouble)
4818 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4819
4820 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004821 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4822 VecConstant);
4823 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4824 DAG.getConstant(0, MVT::i32));
4825 }
4826
4827 return SDValue();
4828}
4829
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004830// check if an VEXT instruction can handle the shuffle mask when the
4831// vector sources of the shuffle are the same.
4832static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4833 unsigned NumElts = VT.getVectorNumElements();
4834
4835 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4836 if (M[0] < 0)
4837 return false;
4838
4839 Imm = M[0];
4840
4841 // If this is a VEXT shuffle, the immediate value is the index of the first
4842 // element. The other shuffle indices must be the successive elements after
4843 // the first one.
4844 unsigned ExpectedElt = Imm;
4845 for (unsigned i = 1; i < NumElts; ++i) {
4846 // Increment the expected index. If it wraps around, just follow it
4847 // back to index zero and keep going.
4848 ++ExpectedElt;
4849 if (ExpectedElt == NumElts)
4850 ExpectedElt = 0;
4851
4852 if (M[i] < 0) continue; // ignore UNDEF indices
4853 if (ExpectedElt != static_cast<unsigned>(M[i]))
4854 return false;
4855 }
4856
4857 return true;
4858}
4859
Lang Hames591cdaf2012-03-29 21:56:11 +00004860
Benjamin Kramer339ced42012-01-15 13:16:05 +00004861static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004862 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004863 unsigned NumElts = VT.getVectorNumElements();
4864 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004865
4866 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4867 if (M[0] < 0)
4868 return false;
4869
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004870 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004871
4872 // If this is a VEXT shuffle, the immediate value is the index of the first
4873 // element. The other shuffle indices must be the successive elements after
4874 // the first one.
4875 unsigned ExpectedElt = Imm;
4876 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004877 // Increment the expected index. If it wraps around, it may still be
4878 // a VEXT but the source vectors must be swapped.
4879 ExpectedElt += 1;
4880 if (ExpectedElt == NumElts * 2) {
4881 ExpectedElt = 0;
4882 ReverseVEXT = true;
4883 }
4884
Bob Wilson411dfad2010-08-17 05:54:34 +00004885 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004886 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004887 return false;
4888 }
4889
4890 // Adjust the index value if the source operands will be swapped.
4891 if (ReverseVEXT)
4892 Imm -= NumElts;
4893
Bob Wilson32cd8552009-08-19 17:03:43 +00004894 return true;
4895}
4896
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004897/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4898/// instruction with the specified blocksize. (The order of the elements
4899/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00004900static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004901 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4902 "Only possible block sizes for VREV are: 16, 32, 64");
4903
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004904 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00004905 if (EltSz == 64)
4906 return false;
4907
4908 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004909 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00004910 // If the first shuffle index is UNDEF, be optimistic.
4911 if (M[0] < 0)
4912 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004913
4914 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4915 return false;
4916
4917 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004918 if (M[i] < 0) continue; // ignore UNDEF indices
4919 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004920 return false;
4921 }
4922
4923 return true;
4924}
4925
Benjamin Kramer339ced42012-01-15 13:16:05 +00004926static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00004927 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4928 // range, then 0 is placed into the resulting vector. So pretty much any mask
4929 // of 8 elements can work here.
4930 return VT == MVT::v8i8 && M.size() == 8;
4931}
4932
Benjamin Kramer339ced42012-01-15 13:16:05 +00004933static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004934 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4935 if (EltSz == 64)
4936 return false;
4937
Bob Wilsona7062312009-08-21 20:54:19 +00004938 unsigned NumElts = VT.getVectorNumElements();
4939 WhichResult = (M[0] == 0 ? 0 : 1);
4940 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004941 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4942 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsona7062312009-08-21 20:54:19 +00004943 return false;
4944 }
4945 return true;
4946}
4947
Bob Wilson0bbd3072009-12-03 06:40:55 +00004948/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4949/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4950/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004951static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004952 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4953 if (EltSz == 64)
4954 return false;
4955
4956 unsigned NumElts = VT.getVectorNumElements();
4957 WhichResult = (M[0] == 0 ? 0 : 1);
4958 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004959 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4960 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004961 return false;
4962 }
4963 return true;
4964}
4965
Benjamin Kramer339ced42012-01-15 13:16:05 +00004966static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004967 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4968 if (EltSz == 64)
4969 return false;
4970
Bob Wilsona7062312009-08-21 20:54:19 +00004971 unsigned NumElts = VT.getVectorNumElements();
4972 WhichResult = (M[0] == 0 ? 0 : 1);
4973 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004974 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsona7062312009-08-21 20:54:19 +00004975 if ((unsigned) M[i] != 2 * i + WhichResult)
4976 return false;
4977 }
4978
4979 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004980 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004981 return false;
4982
4983 return true;
4984}
4985
Bob Wilson0bbd3072009-12-03 06:40:55 +00004986/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4987/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4988/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00004989static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004990 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4991 if (EltSz == 64)
4992 return false;
4993
4994 unsigned Half = VT.getVectorNumElements() / 2;
4995 WhichResult = (M[0] == 0 ? 0 : 1);
4996 for (unsigned j = 0; j != 2; ++j) {
4997 unsigned Idx = WhichResult;
4998 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004999 int MIdx = M[i + j * Half];
5000 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson0bbd3072009-12-03 06:40:55 +00005001 return false;
5002 Idx += 2;
5003 }
5004 }
5005
5006 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5007 if (VT.is64BitVector() && EltSz == 32)
5008 return false;
5009
5010 return true;
5011}
5012
Benjamin Kramer339ced42012-01-15 13:16:05 +00005013static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005014 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5015 if (EltSz == 64)
5016 return false;
5017
Bob Wilsona7062312009-08-21 20:54:19 +00005018 unsigned NumElts = VT.getVectorNumElements();
5019 WhichResult = (M[0] == 0 ? 0 : 1);
5020 unsigned Idx = WhichResult * NumElts / 2;
5021 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005022 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
5023 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsona7062312009-08-21 20:54:19 +00005024 return false;
5025 Idx += 1;
5026 }
5027
5028 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005029 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005030 return false;
5031
5032 return true;
5033}
5034
Bob Wilson0bbd3072009-12-03 06:40:55 +00005035/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
5036/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5037/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005038static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005039 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5040 if (EltSz == 64)
5041 return false;
5042
5043 unsigned NumElts = VT.getVectorNumElements();
5044 WhichResult = (M[0] == 0 ? 0 : 1);
5045 unsigned Idx = WhichResult * NumElts / 2;
5046 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005047 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
5048 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson0bbd3072009-12-03 06:40:55 +00005049 return false;
5050 Idx += 1;
5051 }
5052
5053 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5054 if (VT.is64BitVector() && EltSz == 32)
5055 return false;
5056
5057 return true;
5058}
5059
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005060/// \return true if this is a reverse operation on an vector.
5061static bool isReverseMask(ArrayRef<int> M, EVT VT) {
5062 unsigned NumElts = VT.getVectorNumElements();
5063 // Make sure the mask has the right size.
5064 if (NumElts != M.size())
5065 return false;
5066
5067 // Look for <15, ..., 3, -1, 1, 0>.
5068 for (unsigned i = 0; i != NumElts; ++i)
5069 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
5070 return false;
5071
5072 return true;
5073}
5074
Dale Johannesen2bff5052010-07-29 20:10:08 +00005075// If N is an integer constant that can be moved into a register in one
5076// instruction, return an SDValue of such a constant (will become a MOV
5077// instruction). Otherwise return null.
5078static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005079 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00005080 uint64_t Val;
5081 if (!isa<ConstantSDNode>(N))
5082 return SDValue();
5083 Val = cast<ConstantSDNode>(N)->getZExtValue();
5084
5085 if (ST->isThumb1Only()) {
5086 if (Val <= 255 || ~Val <= 255)
5087 return DAG.getConstant(Val, MVT::i32);
5088 } else {
5089 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
5090 return DAG.getConstant(Val, MVT::i32);
5091 }
5092 return SDValue();
5093}
5094
Bob Wilson2e076c42009-06-22 23:27:02 +00005095// If this is a case we can't handle, return null and let the default
5096// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005097SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
5098 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00005099 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005100 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005101 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005102
5103 APInt SplatBits, SplatUndef;
5104 unsigned SplatBitSize;
5105 bool HasAnyUndefs;
5106 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005107 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00005108 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00005109 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00005110 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00005111 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00005112 DAG, VmovVT, VT.is128BitVector(),
5113 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00005114 if (Val.getNode()) {
5115 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005116 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00005117 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00005118
5119 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00005120 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005121 Val = isNEONModifiedImm(NegatedImm,
5122 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00005123 DAG, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005124 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005125 if (Val.getNode()) {
5126 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005127 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005128 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005129
5130 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00005131 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00005132 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005133 if (ImmVal != -1) {
5134 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
5135 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
5136 }
5137 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005138 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00005139 }
5140
Bob Wilson91fdf682010-05-22 00:23:12 +00005141 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00005142 //
5143 // As an optimisation, even if more than one value is used it may be more
5144 // profitable to splat with one value then change some lanes.
5145 //
5146 // Heuristically we decide to do this if the vector has a "dominant" value,
5147 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00005148 unsigned NumElts = VT.getVectorNumElements();
5149 bool isOnlyLowElement = true;
5150 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005151 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00005152 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005153
5154 // Map of the number of times a particular SDValue appears in the
5155 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00005156 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00005157 SDValue Value;
5158 for (unsigned i = 0; i < NumElts; ++i) {
5159 SDValue V = Op.getOperand(i);
5160 if (V.getOpcode() == ISD::UNDEF)
5161 continue;
5162 if (i > 0)
5163 isOnlyLowElement = false;
5164 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5165 isConstant = false;
5166
James Molloy49bdbce2012-09-06 09:55:02 +00005167 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005168 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005169
James Molloy49bdbce2012-09-06 09:55:02 +00005170 // Is this value dominant? (takes up more than half of the lanes)
5171 if (++Count > (NumElts / 2)) {
5172 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005173 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005174 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005175 }
James Molloy49bdbce2012-09-06 09:55:02 +00005176 if (ValueCounts.size() != 1)
5177 usesOnlyOneValue = false;
5178 if (!Value.getNode() && ValueCounts.size() > 0)
5179 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005180
James Molloy49bdbce2012-09-06 09:55:02 +00005181 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005182 return DAG.getUNDEF(VT);
5183
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005184 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5185 // Keep going if we are hitting this case.
5186 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005187 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5188
Dale Johannesen2bff5052010-07-29 20:10:08 +00005189 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5190
Dale Johannesen710a2d92010-10-19 20:00:17 +00005191 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5192 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005193 if (hasDominantValue && EltSize <= 32) {
5194 if (!isConstant) {
5195 SDValue N;
5196
5197 // If we are VDUPing a value that comes directly from a vector, that will
5198 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005199 // just use VDUPLANE. We can only do this if the lane being extracted
5200 // is at a constant index, as the VDUP from lane instructions only have
5201 // constant-index forms.
5202 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5203 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005204 // We need to create a new undef vector to use for the VDUPLANE if the
5205 // size of the vector from which we get the value is different than the
5206 // size of the vector that we need to create. We will insert the element
5207 // such that the register coalescer will remove unnecessary copies.
5208 if (VT != Value->getOperand(0).getValueType()) {
5209 ConstantSDNode *constIndex;
5210 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
5211 assert(constIndex && "The index is not a constant!");
5212 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5213 VT.getVectorNumElements();
5214 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5215 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
5216 Value, DAG.getConstant(index, MVT::i32)),
5217 DAG.getConstant(index, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005218 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005219 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005220 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005221 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005222 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5223
5224 if (!usesOnlyOneValue) {
5225 // The dominant value was splatted as 'N', but we now have to insert
5226 // all differing elements.
5227 for (unsigned I = 0; I < NumElts; ++I) {
5228 if (Op.getOperand(I) == Value)
5229 continue;
5230 SmallVector<SDValue, 3> Ops;
5231 Ops.push_back(N);
5232 Ops.push_back(Op.getOperand(I));
5233 Ops.push_back(DAG.getConstant(I, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005234 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005235 }
5236 }
5237 return N;
5238 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005239 if (VT.getVectorElementType().isFloatingPoint()) {
5240 SmallVector<SDValue, 8> Ops;
5241 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005242 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005243 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005244 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005245 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005246 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5247 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005248 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005249 }
James Molloy49bdbce2012-09-06 09:55:02 +00005250 if (usesOnlyOneValue) {
5251 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5252 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005253 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005254 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005255 }
5256
5257 // If all elements are constants and the case above didn't get hit, fall back
5258 // to the default expansion, which will generate a load from the constant
5259 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005260 if (isConstant)
5261 return SDValue();
5262
Bob Wilson6f2b8962011-01-07 21:37:30 +00005263 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5264 if (NumElts >= 4) {
5265 SDValue shuffle = ReconstructShuffle(Op, DAG);
5266 if (shuffle != SDValue())
5267 return shuffle;
5268 }
5269
Bob Wilson91fdf682010-05-22 00:23:12 +00005270 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005271 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5272 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005273 if (EltSize >= 32) {
5274 // Do the expansion with floating-point types, since that is what the VFP
5275 // registers are defined to use, and since i64 is not legal.
5276 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5277 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005278 SmallVector<SDValue, 8> Ops;
5279 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005280 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005281 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005282 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005283 }
5284
Jim Grosbach24e102a2013-07-08 18:18:52 +00005285 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5286 // know the default expansion would otherwise fall back on something even
5287 // worse. For a vector with one or two non-undef values, that's
5288 // scalar_to_vector for the elements followed by a shuffle (provided the
5289 // shuffle is valid for the target) and materialization element by element
5290 // on the stack followed by a load for everything else.
5291 if (!isConstant && !usesOnlyOneValue) {
5292 SDValue Vec = DAG.getUNDEF(VT);
5293 for (unsigned i = 0 ; i < NumElts; ++i) {
5294 SDValue V = Op.getOperand(i);
5295 if (V.getOpcode() == ISD::UNDEF)
5296 continue;
5297 SDValue LaneIdx = DAG.getConstant(i, MVT::i32);
5298 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5299 }
5300 return Vec;
5301 }
5302
Bob Wilson2e076c42009-06-22 23:27:02 +00005303 return SDValue();
5304}
5305
Bob Wilson6f2b8962011-01-07 21:37:30 +00005306// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005307// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005308SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5309 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005310 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005311 EVT VT = Op.getValueType();
5312 unsigned NumElts = VT.getVectorNumElements();
5313
5314 SmallVector<SDValue, 2> SourceVecs;
5315 SmallVector<unsigned, 2> MinElts;
5316 SmallVector<unsigned, 2> MaxElts;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005317
Bob Wilson6f2b8962011-01-07 21:37:30 +00005318 for (unsigned i = 0; i < NumElts; ++i) {
5319 SDValue V = Op.getOperand(i);
5320 if (V.getOpcode() == ISD::UNDEF)
5321 continue;
5322 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5323 // A shuffle can only come from building a vector from various
5324 // elements of other vectors.
5325 return SDValue();
Eli Friedman74d1da52011-10-14 23:58:49 +00005326 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
5327 VT.getVectorElementType()) {
5328 // This code doesn't know how to handle shuffles where the vector
5329 // element types do not match (this happens because type legalization
5330 // promotes the return type of EXTRACT_VECTOR_ELT).
5331 // FIXME: It might be appropriate to extend this code to handle
5332 // mismatched types.
5333 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005334 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005335
Bob Wilson6f2b8962011-01-07 21:37:30 +00005336 // Record this extraction against the appropriate vector if possible...
5337 SDValue SourceVec = V.getOperand(0);
Jim Grosbach6df755c2012-07-25 17:02:47 +00005338 // If the element number isn't a constant, we can't effectively
5339 // analyze what's going on.
5340 if (!isa<ConstantSDNode>(V.getOperand(1)))
5341 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005342 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5343 bool FoundSource = false;
5344 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
5345 if (SourceVecs[j] == SourceVec) {
5346 if (MinElts[j] > EltNo)
5347 MinElts[j] = EltNo;
5348 if (MaxElts[j] < EltNo)
5349 MaxElts[j] = EltNo;
5350 FoundSource = true;
5351 break;
5352 }
5353 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005354
Bob Wilson6f2b8962011-01-07 21:37:30 +00005355 // Or record a new source if not...
5356 if (!FoundSource) {
5357 SourceVecs.push_back(SourceVec);
5358 MinElts.push_back(EltNo);
5359 MaxElts.push_back(EltNo);
5360 }
5361 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005362
Bob Wilson6f2b8962011-01-07 21:37:30 +00005363 // Currently only do something sane when at most two source vectors
5364 // involved.
5365 if (SourceVecs.size() > 2)
5366 return SDValue();
5367
5368 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
5369 int VEXTOffsets[2] = {0, 0};
Andrew Trick5eb0a302011-01-19 02:26:13 +00005370
Bob Wilson6f2b8962011-01-07 21:37:30 +00005371 // This loop extracts the usage patterns of the source vectors
5372 // and prepares appropriate SDValues for a shuffle if possible.
5373 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
5374 if (SourceVecs[i].getValueType() == VT) {
5375 // No VEXT necessary
5376 ShuffleSrcs[i] = SourceVecs[i];
5377 VEXTOffsets[i] = 0;
5378 continue;
5379 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
5380 // It probably isn't worth padding out a smaller vector just to
5381 // break it down again in a shuffle.
5382 return SDValue();
5383 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005384
Bob Wilson6f2b8962011-01-07 21:37:30 +00005385 // Since only 64-bit and 128-bit vectors are legal on ARM and
5386 // we've eliminated the other cases...
Bob Wilson3fa9c062011-01-07 23:40:46 +00005387 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
5388 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick5eb0a302011-01-19 02:26:13 +00005389
Bob Wilson6f2b8962011-01-07 21:37:30 +00005390 if (MaxElts[i] - MinElts[i] >= NumElts) {
5391 // Span too large for a VEXT to cope
5392 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005393 }
5394
Bob Wilson6f2b8962011-01-07 21:37:30 +00005395 if (MinElts[i] >= NumElts) {
5396 // The extraction can just take the second half
5397 VEXTOffsets[i] = NumElts;
Eric Christopher2af95512011-01-14 23:50:53 +00005398 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5399 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005400 DAG.getIntPtrConstant(NumElts));
5401 } else if (MaxElts[i] < NumElts) {
5402 // The extraction can just take the first half
5403 VEXTOffsets[i] = 0;
Eric Christopher2af95512011-01-14 23:50:53 +00005404 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5405 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005406 DAG.getIntPtrConstant(0));
5407 } else {
5408 // An actual VEXT is needed
5409 VEXTOffsets[i] = MinElts[i];
Eric Christopher2af95512011-01-14 23:50:53 +00005410 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5411 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005412 DAG.getIntPtrConstant(0));
Eric Christopher2af95512011-01-14 23:50:53 +00005413 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5414 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005415 DAG.getIntPtrConstant(NumElts));
5416 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
5417 DAG.getConstant(VEXTOffsets[i], MVT::i32));
5418 }
5419 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005420
Bob Wilson6f2b8962011-01-07 21:37:30 +00005421 SmallVector<int, 8> Mask;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005422
Bob Wilson6f2b8962011-01-07 21:37:30 +00005423 for (unsigned i = 0; i < NumElts; ++i) {
5424 SDValue Entry = Op.getOperand(i);
5425 if (Entry.getOpcode() == ISD::UNDEF) {
5426 Mask.push_back(-1);
5427 continue;
5428 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005429
Bob Wilson6f2b8962011-01-07 21:37:30 +00005430 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher2af95512011-01-14 23:50:53 +00005431 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
5432 .getOperand(1))->getSExtValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005433 if (ExtractVec == SourceVecs[0]) {
5434 Mask.push_back(ExtractElt - VEXTOffsets[0]);
5435 } else {
5436 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
5437 }
5438 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005439
Bob Wilson6f2b8962011-01-07 21:37:30 +00005440 // Final check before we try to produce nonsense...
5441 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher2af95512011-01-14 23:50:53 +00005442 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
5443 &Mask[0]);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005444
Bob Wilson6f2b8962011-01-07 21:37:30 +00005445 return SDValue();
5446}
5447
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005448/// isShuffleMaskLegal - Targets can use this to indicate that they only
5449/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5450/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5451/// are assumed to be legal.
5452bool
5453ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5454 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005455 if (VT.getVectorNumElements() == 4 &&
5456 (VT.is128BitVector() || VT.is64BitVector())) {
5457 unsigned PFIndexes[4];
5458 for (unsigned i = 0; i != 4; ++i) {
5459 if (M[i] < 0)
5460 PFIndexes[i] = 8;
5461 else
5462 PFIndexes[i] = M[i];
5463 }
5464
5465 // Compute the index in the perfect shuffle table.
5466 unsigned PFTableIndex =
5467 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5468 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5469 unsigned Cost = (PFEntry >> 30);
5470
5471 if (Cost <= 4)
5472 return true;
5473 }
5474
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005475 bool ReverseVEXT;
Bob Wilsona7062312009-08-21 20:54:19 +00005476 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005477
Bob Wilson846bd792010-06-07 23:53:38 +00005478 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5479 return (EltSize >= 32 ||
5480 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005481 isVREVMask(M, VT, 64) ||
5482 isVREVMask(M, VT, 32) ||
5483 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005484 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005485 isVTBLMask(M, VT) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005486 isVTRNMask(M, VT, WhichResult) ||
5487 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson0bbd3072009-12-03 06:40:55 +00005488 isVZIPMask(M, VT, WhichResult) ||
5489 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
5490 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005491 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
5492 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005493}
5494
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005495/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5496/// the specified operations to build the shuffle.
5497static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5498 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005499 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005500 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5501 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5502 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5503
5504 enum {
5505 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5506 OP_VREV,
5507 OP_VDUP0,
5508 OP_VDUP1,
5509 OP_VDUP2,
5510 OP_VDUP3,
5511 OP_VEXT1,
5512 OP_VEXT2,
5513 OP_VEXT3,
5514 OP_VUZPL, // VUZP, left result
5515 OP_VUZPR, // VUZP, right result
5516 OP_VZIPL, // VZIP, left result
5517 OP_VZIPR, // VZIP, right result
5518 OP_VTRNL, // VTRN, left result
5519 OP_VTRNR // VTRN, right result
5520 };
5521
5522 if (OpNum == OP_COPY) {
5523 if (LHSID == (1*9+2)*9+3) return LHS;
5524 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5525 return RHS;
5526 }
5527
5528 SDValue OpLHS, OpRHS;
5529 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5530 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5531 EVT VT = OpLHS.getValueType();
5532
5533 switch (OpNum) {
5534 default: llvm_unreachable("Unknown shuffle opcode!");
5535 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005536 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005537 if (VT.getVectorElementType() == MVT::i32 ||
5538 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005539 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5540 // vrev <4 x i16> -> VREV32
5541 if (VT.getVectorElementType() == MVT::i16)
5542 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5543 // vrev <4 x i8> -> VREV16
5544 assert(VT.getVectorElementType() == MVT::i8);
5545 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005546 case OP_VDUP0:
5547 case OP_VDUP1:
5548 case OP_VDUP2:
5549 case OP_VDUP3:
5550 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005551 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005552 case OP_VEXT1:
5553 case OP_VEXT2:
5554 case OP_VEXT3:
5555 return DAG.getNode(ARMISD::VEXT, dl, VT,
5556 OpLHS, OpRHS,
5557 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
5558 case OP_VUZPL:
5559 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005560 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005561 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5562 case OP_VZIPL:
5563 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005564 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005565 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5566 case OP_VTRNL:
5567 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005568 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5569 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005570 }
5571}
5572
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005573static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005574 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005575 SelectionDAG &DAG) {
5576 // Check to see if we can use the VTBL instruction.
5577 SDValue V1 = Op.getOperand(0);
5578 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005579 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005580
5581 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005582 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005583 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
5584 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
5585
5586 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5587 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005588 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005589
Owen Anderson77aa2662011-04-05 21:48:57 +00005590 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005591 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005592}
5593
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005594static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5595 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005596 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005597 SDValue OpLHS = Op.getOperand(0);
5598 EVT VT = OpLHS.getValueType();
5599
5600 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5601 "Expect an v8i16/v16i8 type");
5602 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5603 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5604 // extract the first 8 bytes into the top double word and the last 8 bytes
5605 // into the bottom double word. The v8i16 case is similar.
5606 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5607 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
5608 DAG.getConstant(ExtractNum, MVT::i32));
5609}
5610
Bob Wilson2e076c42009-06-22 23:27:02 +00005611static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005612 SDValue V1 = Op.getOperand(0);
5613 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005614 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005615 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005616 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005617
Bob Wilsonc6800b52009-08-13 02:13:04 +00005618 // Convert shuffles that are directly supported on NEON to target-specific
5619 // DAG nodes, instead of keeping them as shuffles and matching them again
5620 // during code selection. This is more efficient and avoids the possibility
5621 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005622 // FIXME: floating-point vectors should be canonicalized to integer vectors
5623 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005624 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005625
Bob Wilson846bd792010-06-07 23:53:38 +00005626 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5627 if (EltSize <= 32) {
5628 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5629 int Lane = SVN->getSplatIndex();
5630 // If this is undef splat, generate it via "just" vdup, if possible.
5631 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005632
Dan Gohman198b7ff2011-11-03 21:49:52 +00005633 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005634 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5635 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5636 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005637 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5638 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5639 // reaches it).
5640 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5641 !isa<ConstantSDNode>(V1.getOperand(0))) {
5642 bool IsScalarToVector = true;
5643 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5644 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5645 IsScalarToVector = false;
5646 break;
5647 }
5648 if (IsScalarToVector)
5649 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5650 }
Bob Wilson846bd792010-06-07 23:53:38 +00005651 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
5652 DAG.getConstant(Lane, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005653 }
Bob Wilson846bd792010-06-07 23:53:38 +00005654
5655 bool ReverseVEXT;
5656 unsigned Imm;
5657 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5658 if (ReverseVEXT)
5659 std::swap(V1, V2);
5660 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
5661 DAG.getConstant(Imm, MVT::i32));
5662 }
5663
5664 if (isVREVMask(ShuffleMask, VT, 64))
5665 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5666 if (isVREVMask(ShuffleMask, VT, 32))
5667 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5668 if (isVREVMask(ShuffleMask, VT, 16))
5669 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5670
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005671 if (V2->getOpcode() == ISD::UNDEF &&
5672 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5673 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
5674 DAG.getConstant(Imm, MVT::i32));
5675 }
5676
Bob Wilson846bd792010-06-07 23:53:38 +00005677 // Check for Neon shuffles that modify both input vectors in place.
5678 // If both results are used, i.e., if there are two shuffles with the same
5679 // source operands and with masks corresponding to both results of one of
5680 // these operations, DAG memoization will ensure that a single node is
5681 // used for both shuffles.
5682 unsigned WhichResult;
5683 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5684 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5685 V1, V2).getValue(WhichResult);
5686 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5687 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5688 V1, V2).getValue(WhichResult);
5689 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5690 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5691 V1, V2).getValue(WhichResult);
5692
5693 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5694 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5695 V1, V1).getValue(WhichResult);
5696 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5697 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5698 V1, V1).getValue(WhichResult);
5699 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5700 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5701 V1, V1).getValue(WhichResult);
Bob Wilsoncce31f62009-08-14 05:08:32 +00005702 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005703
Bob Wilsona7062312009-08-21 20:54:19 +00005704 // If the shuffle is not directly supported and it has 4 elements, use
5705 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005706 unsigned NumElts = VT.getVectorNumElements();
5707 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005708 unsigned PFIndexes[4];
5709 for (unsigned i = 0; i != 4; ++i) {
5710 if (ShuffleMask[i] < 0)
5711 PFIndexes[i] = 8;
5712 else
5713 PFIndexes[i] = ShuffleMask[i];
5714 }
5715
5716 // Compute the index in the perfect shuffle table.
5717 unsigned PFTableIndex =
5718 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005719 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5720 unsigned Cost = (PFEntry >> 30);
5721
5722 if (Cost <= 4)
5723 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5724 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00005725
Bob Wilsond8a9a042010-06-04 00:04:02 +00005726 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00005727 if (EltSize >= 32) {
5728 // Do the expansion with floating-point types, since that is what the VFP
5729 // registers are defined to use, and since i64 is not legal.
5730 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5731 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005732 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5733 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005734 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00005735 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00005736 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00005737 Ops.push_back(DAG.getUNDEF(EltVT));
5738 else
5739 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5740 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5741 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
5742 MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00005743 }
Craig Topper48d114b2014-04-26 18:35:24 +00005744 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005745 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00005746 }
5747
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005748 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5749 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
5750
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005751 if (VT == MVT::v8i8) {
5752 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
5753 if (NewOp.getNode())
5754 return NewOp;
5755 }
5756
Bob Wilson6f34e272009-08-14 05:16:33 +00005757 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005758}
5759
Eli Friedmana5e244c2011-10-24 23:08:52 +00005760static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5761 // INSERT_VECTOR_ELT is legal only for immediate indexes.
5762 SDValue Lane = Op.getOperand(2);
5763 if (!isa<ConstantSDNode>(Lane))
5764 return SDValue();
5765
5766 return Op;
5767}
5768
Bob Wilson2e076c42009-06-22 23:27:02 +00005769static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00005770 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00005771 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00005772 if (!isa<ConstantSDNode>(Lane))
5773 return SDValue();
5774
5775 SDValue Vec = Op.getOperand(0);
5776 if (Op.getValueType() == MVT::i32 &&
5777 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005778 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00005779 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5780 }
5781
5782 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00005783}
5784
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005785static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5786 // The only time a CONCAT_VECTORS operation can have legal types is when
5787 // two 64-bit vectors are concatenated to a 128-bit vector.
5788 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5789 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005790 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005791 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005792 SDValue Op0 = Op.getOperand(0);
5793 SDValue Op1 = Op.getOperand(1);
5794 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005795 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005796 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005797 DAG.getIntPtrConstant(0));
5798 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005799 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005800 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005801 DAG.getIntPtrConstant(1));
Wesley Peck527da1b2010-11-23 03:31:01 +00005802 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005803}
5804
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005805/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5806/// element has been zero/sign-extended, depending on the isSigned parameter,
5807/// from an integer type half its size.
5808static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5809 bool isSigned) {
5810 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5811 EVT VT = N->getValueType(0);
5812 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5813 SDNode *BVN = N->getOperand(0).getNode();
5814 if (BVN->getValueType(0) != MVT::v4i32 ||
5815 BVN->getOpcode() != ISD::BUILD_VECTOR)
5816 return false;
5817 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5818 unsigned HiElt = 1 - LoElt;
5819 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5820 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5821 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5822 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5823 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5824 return false;
5825 if (isSigned) {
5826 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5827 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5828 return true;
5829 } else {
5830 if (Hi0->isNullValue() && Hi1->isNullValue())
5831 return true;
5832 }
5833 return false;
5834 }
5835
5836 if (N->getOpcode() != ISD::BUILD_VECTOR)
5837 return false;
5838
5839 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5840 SDNode *Elt = N->getOperand(i).getNode();
5841 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5842 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5843 unsigned HalfSize = EltSize / 2;
5844 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005845 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005846 return false;
5847 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005848 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005849 return false;
5850 }
5851 continue;
5852 }
5853 return false;
5854 }
5855
5856 return true;
5857}
5858
5859/// isSignExtended - Check if a node is a vector value that is sign-extended
5860/// or a constant BUILD_VECTOR with sign-extended elements.
5861static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5862 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5863 return true;
5864 if (isExtendedBUILD_VECTOR(N, DAG, true))
5865 return true;
5866 return false;
5867}
5868
5869/// isZeroExtended - Check if a node is a vector value that is zero-extended
5870/// or a constant BUILD_VECTOR with zero-extended elements.
5871static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5872 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5873 return true;
5874 if (isExtendedBUILD_VECTOR(N, DAG, false))
5875 return true;
5876 return false;
5877}
5878
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005879static EVT getExtensionTo64Bits(const EVT &OrigVT) {
5880 if (OrigVT.getSizeInBits() >= 64)
5881 return OrigVT;
5882
5883 assert(OrigVT.isSimple() && "Expecting a simple value type");
5884
5885 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
5886 switch (OrigSimpleTy) {
5887 default: llvm_unreachable("Unexpected Vector Type");
5888 case MVT::v2i8:
5889 case MVT::v2i16:
5890 return MVT::v2i32;
5891 case MVT::v4i8:
5892 return MVT::v4i16;
5893 }
5894}
5895
Sebastian Popa204f722012-11-30 19:08:04 +00005896/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5897/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5898/// We insert the required extension here to get the vector to fill a D register.
5899static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5900 const EVT &OrigTy,
5901 const EVT &ExtTy,
5902 unsigned ExtOpcode) {
5903 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5904 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5905 // 64-bits we need to insert a new extension so that it will be 64-bits.
5906 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5907 if (OrigTy.getSizeInBits() >= 64)
5908 return N;
5909
5910 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005911 EVT NewVT = getExtensionTo64Bits(OrigTy);
5912
Andrew Trickef9de2a2013-05-25 02:42:55 +00005913 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00005914}
5915
5916/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5917/// does not do any sign/zero extension. If the original vector is less
5918/// than 64 bits, an appropriate extension will be added after the load to
5919/// reach a total size of 64 bits. We have to add the extension separately
5920/// because ARM does not have a sign/zero extending load for vectors.
5921static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005922 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
5923
5924 // The load already has the right type.
5925 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005926 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00005927 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5928 LD->isNonTemporal(), LD->isInvariant(),
5929 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005930
5931 // We need to create a zextload/sextload. We cannot just create a load
5932 // followed by a zext/zext node because LowerMUL is also run during normal
5933 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005934 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005935 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00005936 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(),
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005937 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00005938}
5939
5940/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5941/// extending load, or BUILD_VECTOR with extended elements, return the
5942/// unextended value. The unextended vector should be 64 bits so that it can
5943/// be used as an operand to a VMULL instruction. If the original vector size
5944/// before extension is less than 64 bits we add a an extension to resize
5945/// the vector to 64 bits.
5946static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00005947 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00005948 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5949 N->getOperand(0)->getValueType(0),
5950 N->getValueType(0),
5951 N->getOpcode());
5952
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005953 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00005954 return SkipLoadExtensionForVMULL(LD, DAG);
5955
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005956 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5957 // have been legalized as a BITCAST from v4i32.
5958 if (N->getOpcode() == ISD::BITCAST) {
5959 SDNode *BVN = N->getOperand(0).getNode();
5960 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5961 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5962 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00005963 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005964 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5965 }
5966 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5967 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5968 EVT VT = N->getValueType(0);
5969 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5970 unsigned NumElts = VT.getVectorNumElements();
5971 MVT TruncVT = MVT::getIntegerVT(EltSize);
5972 SmallVector<SDValue, 8> Ops;
5973 for (unsigned i = 0; i != NumElts; ++i) {
5974 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5975 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00005976 // Element types smaller than 32 bits are not legal, so use i32 elements.
5977 // The values are implicitly truncated so sext vs. zext doesn't matter.
5978 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005979 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005980 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topper48d114b2014-04-26 18:35:24 +00005981 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00005982}
5983
Evan Chenge2086e72011-03-29 01:56:09 +00005984static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5985 unsigned Opcode = N->getOpcode();
5986 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5987 SDNode *N0 = N->getOperand(0).getNode();
5988 SDNode *N1 = N->getOperand(1).getNode();
5989 return N0->hasOneUse() && N1->hasOneUse() &&
5990 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5991 }
5992 return false;
5993}
5994
5995static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5996 unsigned Opcode = N->getOpcode();
5997 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5998 SDNode *N0 = N->getOperand(0).getNode();
5999 SDNode *N1 = N->getOperand(1).getNode();
6000 return N0->hasOneUse() && N1->hasOneUse() &&
6001 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
6002 }
6003 return false;
6004}
6005
Bob Wilson38ab35a2010-09-01 23:50:19 +00006006static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
6007 // Multiplications are only custom-lowered for 128-bit vectors so that
6008 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
6009 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00006010 assert(VT.is128BitVector() && VT.isInteger() &&
6011 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00006012 SDNode *N0 = Op.getOperand(0).getNode();
6013 SDNode *N1 = Op.getOperand(1).getNode();
6014 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00006015 bool isMLA = false;
6016 bool isN0SExt = isSignExtended(N0, DAG);
6017 bool isN1SExt = isSignExtended(N1, DAG);
6018 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00006019 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00006020 else {
6021 bool isN0ZExt = isZeroExtended(N0, DAG);
6022 bool isN1ZExt = isZeroExtended(N1, DAG);
6023 if (isN0ZExt && isN1ZExt)
6024 NewOpc = ARMISD::VMULLu;
6025 else if (isN1SExt || isN1ZExt) {
6026 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
6027 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
6028 if (isN1SExt && isAddSubSExt(N0, DAG)) {
6029 NewOpc = ARMISD::VMULLs;
6030 isMLA = true;
6031 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
6032 NewOpc = ARMISD::VMULLu;
6033 isMLA = true;
6034 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
6035 std::swap(N0, N1);
6036 NewOpc = ARMISD::VMULLu;
6037 isMLA = true;
6038 }
6039 }
6040
6041 if (!NewOpc) {
6042 if (VT == MVT::v2i64)
6043 // Fall through to expand this. It is not legal.
6044 return SDValue();
6045 else
6046 // Other vector multiplications are legal.
6047 return Op;
6048 }
6049 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006050
6051 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006052 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00006053 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00006054 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006055 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00006056 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006057 assert(Op0.getValueType().is64BitVector() &&
6058 Op1.getValueType().is64BitVector() &&
6059 "unexpected types for extended operands to VMULL");
6060 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
6061 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006062
Evan Chenge2086e72011-03-29 01:56:09 +00006063 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
6064 // isel lowering to take advantage of no-stall back to back vmul + vmla.
6065 // vmull q0, d4, d6
6066 // vmlal q0, d5, d6
6067 // is faster than
6068 // vaddl q0, d4, d5
6069 // vmovl q1, d6
6070 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00006071 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
6072 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006073 EVT Op1VT = Op1.getValueType();
6074 return DAG.getNode(N0->getOpcode(), DL, VT,
6075 DAG.getNode(NewOpc, DL, VT,
6076 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
6077 DAG.getNode(NewOpc, DL, VT,
6078 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00006079}
6080
Owen Anderson77aa2662011-04-05 21:48:57 +00006081static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006082LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00006083 // Convert to float
6084 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
6085 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
6086 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
6087 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
6088 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
6089 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
6090 // Get reciprocal estimate.
6091 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00006092 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006093 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
6094 // Because char has a smaller range than uchar, we can actually get away
6095 // without any newton steps. This requires that we use a weird bias
6096 // of 0xb000, however (again, this has been exhaustively tested).
6097 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
6098 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
6099 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
6100 Y = DAG.getConstant(0xb000, MVT::i32);
6101 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
6102 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
6103 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
6104 // Convert back to short.
6105 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
6106 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
6107 return X;
6108}
6109
Owen Anderson77aa2662011-04-05 21:48:57 +00006110static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006111LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00006112 SDValue N2;
6113 // Convert to float.
6114 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
6115 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
6116 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
6117 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
6118 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
6119 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006120
Nate Begemanfa62d502011-02-11 20:53:29 +00006121 // Use reciprocal estimate and one refinement step.
6122 // float4 recip = vrecpeq_f32(yf);
6123 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006124 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006125 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006126 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006127 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
6128 N1, N2);
6129 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6130 // Because short has a smaller range than ushort, we can actually get away
6131 // with only a single newton step. This requires that we use a weird bias
6132 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006133 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00006134 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6135 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006136 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006137 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6138 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6139 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6140 // Convert back to integer and return.
6141 // return vmovn_s32(vcvt_s32_f32(result));
6142 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6143 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6144 return N0;
6145}
6146
6147static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
6148 EVT VT = Op.getValueType();
6149 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6150 "unexpected type for custom-lowering ISD::SDIV");
6151
Andrew Trickef9de2a2013-05-25 02:42:55 +00006152 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006153 SDValue N0 = Op.getOperand(0);
6154 SDValue N1 = Op.getOperand(1);
6155 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006156
Nate Begemanfa62d502011-02-11 20:53:29 +00006157 if (VT == MVT::v8i8) {
6158 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6159 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006160
Nate Begemanfa62d502011-02-11 20:53:29 +00006161 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6162 DAG.getIntPtrConstant(4));
6163 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00006164 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00006165 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6166 DAG.getIntPtrConstant(0));
6167 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
6168 DAG.getIntPtrConstant(0));
6169
6170 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6171 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6172
6173 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6174 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006175
Nate Begemanfa62d502011-02-11 20:53:29 +00006176 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6177 return N0;
6178 }
6179 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6180}
6181
6182static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
6183 EVT VT = Op.getValueType();
6184 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6185 "unexpected type for custom-lowering ISD::UDIV");
6186
Andrew Trickef9de2a2013-05-25 02:42:55 +00006187 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006188 SDValue N0 = Op.getOperand(0);
6189 SDValue N1 = Op.getOperand(1);
6190 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006191
Nate Begemanfa62d502011-02-11 20:53:29 +00006192 if (VT == MVT::v8i8) {
6193 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6194 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006195
Nate Begemanfa62d502011-02-11 20:53:29 +00006196 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6197 DAG.getIntPtrConstant(4));
6198 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00006199 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00006200 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6201 DAG.getIntPtrConstant(0));
6202 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
6203 DAG.getIntPtrConstant(0));
Owen Anderson77aa2662011-04-05 21:48:57 +00006204
Nate Begemanfa62d502011-02-11 20:53:29 +00006205 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6206 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006207
Nate Begemanfa62d502011-02-11 20:53:29 +00006208 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6209 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006210
6211 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begemanfa62d502011-02-11 20:53:29 +00006212 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
6213 N0);
6214 return N0;
6215 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006216
Nate Begemanfa62d502011-02-11 20:53:29 +00006217 // v4i16 sdiv ... Convert to float.
6218 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6219 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6220 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6221 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6222 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006223 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006224
6225 // Use reciprocal estimate and two refinement steps.
6226 // float4 recip = vrecpeq_f32(yf);
6227 // recip *= vrecpsq_f32(yf, recip);
6228 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006229 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006230 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006231 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006232 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006233 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006234 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006235 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006236 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006237 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006238 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6239 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6240 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6241 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006242 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006243 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6244 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
6245 N1 = DAG.getConstant(2, MVT::i32);
6246 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6247 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6248 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6249 // Convert back to integer and return.
6250 // return vmovn_u32(vcvt_s32_f32(result));
6251 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6252 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6253 return N0;
6254}
6255
Evan Chenge8916542011-08-30 01:34:54 +00006256static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6257 EVT VT = Op.getNode()->getValueType(0);
6258 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6259
6260 unsigned Opc;
6261 bool ExtraOp = false;
6262 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006263 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006264 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6265 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6266 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6267 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6268 }
6269
6270 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006271 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006272 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006273 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006274 Op.getOperand(1), Op.getOperand(2));
6275}
6276
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006277SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6278 assert(Subtarget->isTargetDarwin());
6279
6280 // For iOS, we want to call an alternative entry point: __sincos_stret,
6281 // return values are passed via sret.
6282 SDLoc dl(Op);
6283 SDValue Arg = Op.getOperand(0);
6284 EVT ArgVT = Arg.getValueType();
6285 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
6286
6287 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
6288 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6289
6290 // Pair of floats / doubles used to pass the result.
Reid Kleckner343c3952014-11-20 23:51:47 +00006291 StructType *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006292
6293 // Create stack object for sret.
6294 const uint64_t ByteSize = TLI.getDataLayout()->getTypeAllocSize(RetTy);
6295 const unsigned StackAlign = TLI.getDataLayout()->getPrefTypeAlignment(RetTy);
6296 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6297 SDValue SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
6298
6299 ArgListTy Args;
6300 ArgListEntry Entry;
6301
6302 Entry.Node = SRet;
6303 Entry.Ty = RetTy->getPointerTo();
6304 Entry.isSExt = false;
6305 Entry.isZExt = false;
6306 Entry.isSRet = true;
6307 Args.push_back(Entry);
6308
6309 Entry.Node = Arg;
6310 Entry.Ty = ArgTy;
6311 Entry.isSExt = false;
6312 Entry.isZExt = false;
6313 Args.push_back(Entry);
6314
6315 const char *LibcallName = (ArgVT == MVT::f64)
6316 ? "__sincos_stret" : "__sincosf_stret";
6317 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
6318
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006319 TargetLowering::CallLoweringInfo CLI(DAG);
6320 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
6321 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), Callee,
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00006322 std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006323 .setDiscardResult();
6324
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006325 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6326
6327 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6328 MachinePointerInfo(), false, false, false, 0);
6329
6330 // Address of cos field.
6331 SDValue Add = DAG.getNode(ISD::ADD, dl, getPointerTy(), SRet,
6332 DAG.getIntPtrConstant(ArgVT.getStoreSize()));
6333 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6334 MachinePointerInfo(), false, false, false, 0);
6335
6336 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6337 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6338 LoadSin.getValue(0), LoadCos.getValue(0));
6339}
6340
Eli Friedman10f9ce22011-09-15 22:26:18 +00006341static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006342 // Monotonic load/store is legal for all targets
6343 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6344 return Op;
6345
Alp Tokercb402912014-01-24 17:20:08 +00006346 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006347 // dmb or equivalent available.
6348 return SDValue();
6349}
6350
Tim Northoverbc933082013-05-23 19:11:20 +00006351static void ReplaceREADCYCLECOUNTER(SDNode *N,
6352 SmallVectorImpl<SDValue> &Results,
6353 SelectionDAG &DAG,
6354 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006355 SDLoc DL(N);
Tim Northoverbc933082013-05-23 19:11:20 +00006356 SDValue Cycles32, OutChain;
6357
6358 if (Subtarget->hasPerfMon()) {
6359 // Under Power Management extensions, the cycle-count is:
6360 // mrc p15, #0, <Rt>, c9, c13, #0
6361 SDValue Ops[] = { N->getOperand(0), // Chain
6362 DAG.getConstant(Intrinsic::arm_mrc, MVT::i32),
6363 DAG.getConstant(15, MVT::i32),
6364 DAG.getConstant(0, MVT::i32),
6365 DAG.getConstant(9, MVT::i32),
6366 DAG.getConstant(13, MVT::i32),
6367 DAG.getConstant(0, MVT::i32)
6368 };
6369
6370 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00006371 DAG.getVTList(MVT::i32, MVT::Other), Ops);
Tim Northoverbc933082013-05-23 19:11:20 +00006372 OutChain = Cycles32.getValue(1);
6373 } else {
6374 // Intrinsic is defined to return 0 on unsupported platforms. Technically
6375 // there are older ARM CPUs that have implementation-specific ways of
6376 // obtaining this information (FIXME!).
6377 Cycles32 = DAG.getConstant(0, MVT::i32);
6378 OutChain = DAG.getEntryNode();
6379 }
6380
6381
6382 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64,
6383 Cycles32, DAG.getConstant(0, MVT::i32));
6384 Results.push_back(Cycles64);
6385 Results.push_back(OutChain);
6386}
6387
Dan Gohman21cea8a2010-04-17 15:26:15 +00006388SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006389 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006390 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Cheng10043e22007-01-19 07:51:42 +00006391 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006392 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006393 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006394 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6395 default: llvm_unreachable("unknown object format");
6396 case Triple::COFF:
6397 return LowerGlobalAddressWindows(Op, DAG);
6398 case Triple::ELF:
6399 return LowerGlobalAddressELF(Op, DAG);
6400 case Triple::MachO:
6401 return LowerGlobalAddressDarwin(Op, DAG);
6402 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006403 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006404 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006405 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6406 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006407 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006408 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006409 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006410 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006411 case ISD::SINT_TO_FP:
6412 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6413 case ISD::FP_TO_SINT:
6414 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006415 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006416 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006417 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006418 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006419 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006420 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006421 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6422 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006423 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006424 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006425 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006426 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng15b80e42009-11-12 07:13:11 +00006427 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006428 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006429 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach8546ec92010-01-18 19:58:49 +00006430 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006431 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006432 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006433 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006434 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006435 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006436 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006437 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006438 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006439 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006440 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanfa62d502011-02-11 20:53:29 +00006441 case ISD::SDIV: return LowerSDIV(Op, DAG);
6442 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006443 case ISD::ADDC:
6444 case ISD::ADDE:
6445 case ISD::SUBC:
6446 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006447 case ISD::SADDO:
6448 case ISD::UADDO:
6449 case ISD::SSUBO:
6450 case ISD::USUBO:
6451 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006452 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006453 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006454 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006455 case ISD::SDIVREM:
6456 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00006457 case ISD::DYNAMIC_STACKALLOC:
6458 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
6459 return LowerDYNAMIC_STACKALLOC(Op, DAG);
6460 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00006461 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
6462 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006463 }
Evan Cheng10043e22007-01-19 07:51:42 +00006464}
6465
Duncan Sands6ed40142008-12-01 11:39:25 +00006466/// ReplaceNodeResults - Replace the results of node with an illegal result
6467/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006468void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
6469 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006470 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006471 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006472 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006473 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006474 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peck527da1b2010-11-23 03:31:01 +00006475 case ISD::BITCAST:
6476 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006477 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006478 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006479 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006480 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006481 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006482 case ISD::READCYCLECOUNTER:
6483 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6484 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00006485 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006486 if (Res.getNode())
6487 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006488}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006489
Evan Cheng10043e22007-01-19 07:51:42 +00006490//===----------------------------------------------------------------------===//
6491// ARM Scheduler Hooks
6492//===----------------------------------------------------------------------===//
6493
Bill Wendling030b58e2011-10-06 22:18:16 +00006494/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6495/// registers the function context.
6496void ARMTargetLowering::
6497SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6498 MachineBasicBlock *DispatchBB, int FI) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006499 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling374ee192011-10-03 21:25:38 +00006500 DebugLoc dl = MI->getDebugLoc();
6501 MachineFunction *MF = MBB->getParent();
6502 MachineRegisterInfo *MRI = &MF->getRegInfo();
6503 MachineConstantPool *MCP = MF->getConstantPool();
6504 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6505 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006506
Bill Wendling374ee192011-10-03 21:25:38 +00006507 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006508 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006509
Bill Wendling374ee192011-10-03 21:25:38 +00006510 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006511 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006512 ARMConstantPoolValue *CPV =
6513 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6514 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6515
Craig Topper61e88f42014-11-21 05:58:21 +00006516 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
6517 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006518
Bill Wendling030b58e2011-10-06 22:18:16 +00006519 // Grab constant pool and fixed stack memory operands.
6520 MachineMemOperand *CPMMO =
6521 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6522 MachineMemOperand::MOLoad, 4, 4);
6523
6524 MachineMemOperand *FIMMOSt =
6525 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6526 MachineMemOperand::MOStore, 4, 4);
6527
6528 // Load the address of the dispatch MBB into the jump buffer.
6529 if (isThumb2) {
6530 // Incoming value: jbuf
6531 // ldr.n r5, LCPI1_1
6532 // orr r5, r5, #1
6533 // add r5, pc
6534 // str r5, [$jbuf, #+4] ; &jbuf[1]
6535 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6536 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6537 .addConstantPoolIndex(CPI)
6538 .addMemOperand(CPMMO));
6539 // Set the low bit because of thumb mode.
6540 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6541 AddDefaultCC(
6542 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6543 .addReg(NewVReg1, RegState::Kill)
6544 .addImm(0x01)));
6545 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6546 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6547 .addReg(NewVReg2, RegState::Kill)
6548 .addImm(PCLabelId);
6549 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6550 .addReg(NewVReg3, RegState::Kill)
6551 .addFrameIndex(FI)
6552 .addImm(36) // &jbuf[1] :: pc
6553 .addMemOperand(FIMMOSt));
6554 } else if (isThumb) {
6555 // Incoming value: jbuf
6556 // ldr.n r1, LCPI1_4
6557 // add r1, pc
6558 // mov r2, #1
6559 // orrs r1, r2
6560 // add r2, $jbuf, #+4 ; &jbuf[1]
6561 // str r1, [r2]
6562 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6563 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6564 .addConstantPoolIndex(CPI)
6565 .addMemOperand(CPMMO));
6566 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6567 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6568 .addReg(NewVReg1, RegState::Kill)
6569 .addImm(PCLabelId);
6570 // Set the low bit because of thumb mode.
6571 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6572 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6573 .addReg(ARM::CPSR, RegState::Define)
6574 .addImm(1));
6575 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6576 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6577 .addReg(ARM::CPSR, RegState::Define)
6578 .addReg(NewVReg2, RegState::Kill)
6579 .addReg(NewVReg3, RegState::Kill));
6580 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00006581 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
6582 .addFrameIndex(FI)
6583 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00006584 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6585 .addReg(NewVReg4, RegState::Kill)
6586 .addReg(NewVReg5, RegState::Kill)
6587 .addImm(0)
6588 .addMemOperand(FIMMOSt));
6589 } else {
6590 // Incoming value: jbuf
6591 // ldr r1, LCPI1_1
6592 // add r1, pc, r1
6593 // str r1, [$jbuf, #+4] ; &jbuf[1]
6594 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6595 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6596 .addConstantPoolIndex(CPI)
6597 .addImm(0)
6598 .addMemOperand(CPMMO));
6599 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6600 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6601 .addReg(NewVReg1, RegState::Kill)
6602 .addImm(PCLabelId));
6603 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6604 .addReg(NewVReg2, RegState::Kill)
6605 .addFrameIndex(FI)
6606 .addImm(36) // &jbuf[1] :: pc
6607 .addMemOperand(FIMMOSt));
6608 }
6609}
6610
6611MachineBasicBlock *ARMTargetLowering::
6612EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006613 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00006614 DebugLoc dl = MI->getDebugLoc();
6615 MachineFunction *MF = MBB->getParent();
6616 MachineRegisterInfo *MRI = &MF->getRegInfo();
6617 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6618 MachineFrameInfo *MFI = MF->getFrameInfo();
6619 int FI = MFI->getFunctionContextIndex();
6620
Craig Topper61e88f42014-11-21 05:58:21 +00006621 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
6622 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00006623
Bill Wendling362c1b02011-10-06 21:29:56 +00006624 // Get a mapping of the call site numbers to all of the landing pads they're
6625 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00006626 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6627 unsigned MaxCSNum = 0;
6628 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00006629 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6630 ++BB) {
Bill Wendling202803e2011-10-05 00:02:33 +00006631 if (!BB->isLandingPad()) continue;
6632
6633 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6634 // pad.
6635 for (MachineBasicBlock::iterator
6636 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6637 if (!II->isEHLabel()) continue;
6638
6639 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006640 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006641
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006642 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6643 for (SmallVectorImpl<unsigned>::iterator
6644 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6645 CSI != CSE; ++CSI) {
6646 CallSiteNumToLPad[*CSI].push_back(BB);
6647 MaxCSNum = std::max(MaxCSNum, *CSI);
6648 }
Bill Wendling202803e2011-10-05 00:02:33 +00006649 break;
6650 }
6651 }
6652
6653 // Get an ordered list of the machine basic blocks for the jump table.
6654 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00006655 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00006656 LPadList.reserve(CallSiteNumToLPad.size());
6657 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6658 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6659 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006660 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00006661 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00006662 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6663 }
Bill Wendling202803e2011-10-05 00:02:33 +00006664 }
6665
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006666 assert(!LPadList.empty() &&
6667 "No landing pad destinations for the dispatch jump table!");
6668
Bill Wendling362c1b02011-10-06 21:29:56 +00006669 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00006670 MachineJumpTableInfo *JTI =
6671 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6672 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6673 unsigned UId = AFI->createJumpTableUId();
Chad Rosier96603432013-03-01 18:30:38 +00006674 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00006675
Bill Wendling362c1b02011-10-06 21:29:56 +00006676 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00006677
6678 // Shove the dispatch's address into the return slot in the function context.
6679 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6680 DispatchBB->setIsLandingPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00006681
Bill Wendling324be982011-10-05 00:39:32 +00006682 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006683 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00006684 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006685 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00006686 else
6687 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6688
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006689 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00006690 DispatchBB->addSuccessor(TrapBB);
6691
6692 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6693 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00006694
Bill Wendling510fbcd2011-10-17 21:32:56 +00006695 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00006696 MF->insert(MF->end(), DispatchBB);
6697 MF->insert(MF->end(), DispContBB);
6698 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00006699
Bill Wendling030b58e2011-10-06 22:18:16 +00006700 // Insert code into the entry block that creates and registers the function
6701 // context.
6702 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6703
Bill Wendling030b58e2011-10-06 22:18:16 +00006704 MachineMemOperand *FIMMOLd =
Bill Wendling362c1b02011-10-06 21:29:56 +00006705 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendlingb3d46782011-10-06 23:37:36 +00006706 MachineMemOperand::MOLoad |
6707 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00006708
Chad Rosier1ec8e402012-11-06 23:05:24 +00006709 MachineInstrBuilder MIB;
6710 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6711
6712 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6713 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6714
6715 // Add a register mask with no preserved registers. This results in all
6716 // registers being marked as clobbered.
6717 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00006718
Bill Wendling85833f72011-10-18 22:49:07 +00006719 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00006720 if (Subtarget->isThumb2()) {
6721 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6722 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6723 .addFrameIndex(FI)
6724 .addImm(4)
6725 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006726
Bill Wendling85833f72011-10-18 22:49:07 +00006727 if (NumLPads < 256) {
6728 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6729 .addReg(NewVReg1)
6730 .addImm(LPadList.size()));
6731 } else {
6732 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6733 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006734 .addImm(NumLPads & 0xFFFF));
6735
6736 unsigned VReg2 = VReg1;
6737 if ((NumLPads & 0xFFFF0000) != 0) {
6738 VReg2 = MRI->createVirtualRegister(TRC);
6739 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6740 .addReg(VReg1)
6741 .addImm(NumLPads >> 16));
6742 }
6743
Bill Wendling85833f72011-10-18 22:49:07 +00006744 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6745 .addReg(NewVReg1)
6746 .addReg(VReg2));
6747 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006748
Bill Wendling5626c662011-10-06 22:53:00 +00006749 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6750 .addMBB(TrapBB)
6751 .addImm(ARMCC::HI)
6752 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00006753
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006754 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6755 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006756 .addJumpTableIndex(MJTI)
6757 .addImm(UId));
Bill Wendling202803e2011-10-05 00:02:33 +00006758
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006759 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006760 AddDefaultCC(
6761 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006762 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6763 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00006764 .addReg(NewVReg1)
6765 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6766
6767 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006768 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00006769 .addReg(NewVReg1)
Bill Wendling5626c662011-10-06 22:53:00 +00006770 .addJumpTableIndex(MJTI)
6771 .addImm(UId);
6772 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00006773 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6774 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6775 .addFrameIndex(FI)
6776 .addImm(1)
6777 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00006778
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006779 if (NumLPads < 256) {
6780 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6781 .addReg(NewVReg1)
6782 .addImm(NumLPads));
6783 } else {
6784 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00006785 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6786 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6787
6788 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006789 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006790 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006791 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006792 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006793
6794 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6795 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6796 .addReg(VReg1, RegState::Define)
6797 .addConstantPoolIndex(Idx));
6798 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6799 .addReg(NewVReg1)
6800 .addReg(VReg1));
6801 }
6802
Bill Wendlingb3d46782011-10-06 23:37:36 +00006803 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6804 .addMBB(TrapBB)
6805 .addImm(ARMCC::HI)
6806 .addReg(ARM::CPSR);
6807
6808 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6809 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6810 .addReg(ARM::CPSR, RegState::Define)
6811 .addReg(NewVReg1)
6812 .addImm(2));
6813
6814 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00006815 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendlingb3d46782011-10-06 23:37:36 +00006816 .addJumpTableIndex(MJTI)
6817 .addImm(UId));
6818
6819 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6820 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6821 .addReg(ARM::CPSR, RegState::Define)
6822 .addReg(NewVReg2, RegState::Kill)
6823 .addReg(NewVReg3));
6824
6825 MachineMemOperand *JTMMOLd =
6826 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6827 MachineMemOperand::MOLoad, 4, 4);
6828
6829 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6830 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6831 .addReg(NewVReg4, RegState::Kill)
6832 .addImm(0)
6833 .addMemOperand(JTMMOLd));
6834
Chad Rosier96603432013-03-01 18:30:38 +00006835 unsigned NewVReg6 = NewVReg5;
6836 if (RelocM == Reloc::PIC_) {
6837 NewVReg6 = MRI->createVirtualRegister(TRC);
6838 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6839 .addReg(ARM::CPSR, RegState::Define)
6840 .addReg(NewVReg5, RegState::Kill)
6841 .addReg(NewVReg3));
6842 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00006843
6844 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6845 .addReg(NewVReg6, RegState::Kill)
6846 .addJumpTableIndex(MJTI)
6847 .addImm(UId);
Bill Wendling5626c662011-10-06 22:53:00 +00006848 } else {
6849 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6850 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6851 .addFrameIndex(FI)
6852 .addImm(4)
6853 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00006854
Bill Wendling4969dcd2011-10-18 22:52:20 +00006855 if (NumLPads < 256) {
6856 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6857 .addReg(NewVReg1)
6858 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00006859 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00006860 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6861 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006862 .addImm(NumLPads & 0xFFFF));
6863
6864 unsigned VReg2 = VReg1;
6865 if ((NumLPads & 0xFFFF0000) != 0) {
6866 VReg2 = MRI->createVirtualRegister(TRC);
6867 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6868 .addReg(VReg1)
6869 .addImm(NumLPads >> 16));
6870 }
6871
Bill Wendling4969dcd2011-10-18 22:52:20 +00006872 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6873 .addReg(NewVReg1)
6874 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00006875 } else {
6876 MachineConstantPool *ConstantPool = MF->getConstantPool();
6877 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6878 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6879
6880 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006881 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006882 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006883 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006884 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6885
6886 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6887 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6888 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00006889 .addConstantPoolIndex(Idx)
6890 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00006891 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6892 .addReg(NewVReg1)
6893 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00006894 }
6895
Bill Wendling5626c662011-10-06 22:53:00 +00006896 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6897 .addMBB(TrapBB)
6898 .addImm(ARMCC::HI)
6899 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00006900
Bill Wendling973c8172011-10-18 22:11:18 +00006901 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006902 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00006903 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006904 .addReg(NewVReg1)
6905 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00006906 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6907 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006908 .addJumpTableIndex(MJTI)
6909 .addImm(UId));
6910
6911 MachineMemOperand *JTMMOLd =
6912 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6913 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00006914 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006915 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00006916 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6917 .addReg(NewVReg3, RegState::Kill)
6918 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006919 .addImm(0)
6920 .addMemOperand(JTMMOLd));
6921
Chad Rosier96603432013-03-01 18:30:38 +00006922 if (RelocM == Reloc::PIC_) {
6923 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
6924 .addReg(NewVReg5, RegState::Kill)
6925 .addReg(NewVReg4)
6926 .addJumpTableIndex(MJTI)
6927 .addImm(UId);
6928 } else {
6929 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
6930 .addReg(NewVReg5, RegState::Kill)
6931 .addJumpTableIndex(MJTI)
6932 .addImm(UId);
6933 }
Bill Wendling5626c662011-10-06 22:53:00 +00006934 }
Bill Wendling202803e2011-10-05 00:02:33 +00006935
Bill Wendling324be982011-10-05 00:39:32 +00006936 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006937 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00006938 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006939 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6940 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00006941 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00006942 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006943 }
6944
Bill Wendling26d27802011-10-17 05:25:09 +00006945 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00006946 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00006947 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00006948 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006949
6950 // Remove the landing pad successor from the invoke block and replace it
6951 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00006952 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6953 BB->succ_end());
6954 while (!Successors.empty()) {
6955 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling883ec972011-10-07 23:18:02 +00006956 if (SMBB->isLandingPad()) {
6957 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00006958 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006959 }
6960 }
6961
6962 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006963
6964 // Find the invoke call and mark all of the callee-saved registers as
6965 // 'implicit defined' so that they're spilled. This prevents code from
6966 // moving instructions to before the EH block, where they will never be
6967 // executed.
6968 for (MachineBasicBlock::reverse_iterator
6969 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00006970 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006971
6972 DenseMap<unsigned, bool> DefRegs;
6973 for (MachineInstr::mop_iterator
6974 OI = II->operands_begin(), OE = II->operands_end();
6975 OI != OE; ++OI) {
6976 if (!OI->isReg()) continue;
6977 DefRegs[OI->getReg()] = true;
6978 }
6979
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00006980 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006981
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006982 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00006983 unsigned Reg = SavedRegs[i];
6984 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00006985 !ARM::tGPRRegClass.contains(Reg) &&
6986 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006987 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006988 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006989 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006990 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006991 continue;
6992 if (!DefRegs[Reg])
6993 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006994 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006995
6996 break;
6997 }
Bill Wendling883ec972011-10-07 23:18:02 +00006998 }
Bill Wendling324be982011-10-05 00:39:32 +00006999
Bill Wendling617075f2011-10-18 18:30:49 +00007000 // Mark all former landing pads as non-landing pads. The dispatch is the only
7001 // landing pad now.
7002 for (SmallVectorImpl<MachineBasicBlock*>::iterator
7003 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
7004 (*I)->setIsLandingPad(false);
7005
Bill Wendling324be982011-10-05 00:39:32 +00007006 // The instruction is gone now.
7007 MI->eraseFromParent();
7008
Bill Wendling374ee192011-10-03 21:25:38 +00007009 return MBB;
7010}
7011
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007012static
7013MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
7014 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
7015 E = MBB->succ_end(); I != E; ++I)
7016 if (*I != Succ)
7017 return *I;
7018 llvm_unreachable("Expecting a BB with two successors!");
7019}
7020
Manman Renb504f492013-10-29 22:27:32 +00007021/// Return the load opcode for a given load size. If load size >= 8,
7022/// neon opcode will be returned.
7023static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
7024 if (LdSize >= 8)
7025 return LdSize == 16 ? ARM::VLD1q32wb_fixed
7026 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
7027 if (IsThumb1)
7028 return LdSize == 4 ? ARM::tLDRi
7029 : LdSize == 2 ? ARM::tLDRHi
7030 : LdSize == 1 ? ARM::tLDRBi : 0;
7031 if (IsThumb2)
7032 return LdSize == 4 ? ARM::t2LDR_POST
7033 : LdSize == 2 ? ARM::t2LDRH_POST
7034 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
7035 return LdSize == 4 ? ARM::LDR_POST_IMM
7036 : LdSize == 2 ? ARM::LDRH_POST
7037 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
7038}
7039
7040/// Return the store opcode for a given store size. If store size >= 8,
7041/// neon opcode will be returned.
7042static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
7043 if (StSize >= 8)
7044 return StSize == 16 ? ARM::VST1q32wb_fixed
7045 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
7046 if (IsThumb1)
7047 return StSize == 4 ? ARM::tSTRi
7048 : StSize == 2 ? ARM::tSTRHi
7049 : StSize == 1 ? ARM::tSTRBi : 0;
7050 if (IsThumb2)
7051 return StSize == 4 ? ARM::t2STR_POST
7052 : StSize == 2 ? ARM::t2STRH_POST
7053 : StSize == 1 ? ARM::t2STRB_POST : 0;
7054 return StSize == 4 ? ARM::STR_POST_IMM
7055 : StSize == 2 ? ARM::STRH_POST
7056 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
7057}
7058
7059/// Emit a post-increment load operation with given size. The instructions
7060/// will be added to BB at Pos.
7061static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
7062 const TargetInstrInfo *TII, DebugLoc dl,
7063 unsigned LdSize, unsigned Data, unsigned AddrIn,
7064 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7065 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
7066 assert(LdOpc != 0 && "Should have a load opcode");
7067 if (LdSize >= 8) {
7068 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7069 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7070 .addImm(0));
7071 } else if (IsThumb1) {
7072 // load + update AddrIn
7073 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7074 .addReg(AddrIn).addImm(0));
7075 MachineInstrBuilder MIB =
7076 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7077 MIB = AddDefaultT1CC(MIB);
7078 MIB.addReg(AddrIn).addImm(LdSize);
7079 AddDefaultPred(MIB);
7080 } else if (IsThumb2) {
7081 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7082 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7083 .addImm(LdSize));
7084 } else { // arm
7085 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7086 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7087 .addReg(0).addImm(LdSize));
7088 }
7089}
7090
7091/// Emit a post-increment store operation with given size. The instructions
7092/// will be added to BB at Pos.
7093static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
7094 const TargetInstrInfo *TII, DebugLoc dl,
7095 unsigned StSize, unsigned Data, unsigned AddrIn,
7096 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7097 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
7098 assert(StOpc != 0 && "Should have a store opcode");
7099 if (StSize >= 8) {
7100 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7101 .addReg(AddrIn).addImm(0).addReg(Data));
7102 } else if (IsThumb1) {
7103 // store + update AddrIn
7104 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
7105 .addReg(AddrIn).addImm(0));
7106 MachineInstrBuilder MIB =
7107 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7108 MIB = AddDefaultT1CC(MIB);
7109 MIB.addReg(AddrIn).addImm(StSize);
7110 AddDefaultPred(MIB);
7111 } else if (IsThumb2) {
7112 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7113 .addReg(Data).addReg(AddrIn).addImm(StSize));
7114 } else { // arm
7115 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7116 .addReg(Data).addReg(AddrIn).addReg(0)
7117 .addImm(StSize));
7118 }
7119}
7120
David Peixottoc32e24a2013-10-17 19:49:22 +00007121MachineBasicBlock *
7122ARMTargetLowering::EmitStructByval(MachineInstr *MI,
7123 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00007124 // This pseudo instruction has 3 operands: dst, src, size
7125 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7126 // Otherwise, we will generate unrolled scalar copies.
Eric Christopher1889fdc2015-01-29 00:19:39 +00007127 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00007128 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7129 MachineFunction::iterator It = BB;
7130 ++It;
7131
7132 unsigned dest = MI->getOperand(0).getReg();
7133 unsigned src = MI->getOperand(1).getReg();
7134 unsigned SizeVal = MI->getOperand(2).getImm();
7135 unsigned Align = MI->getOperand(3).getImm();
7136 DebugLoc dl = MI->getDebugLoc();
7137
Manman Rene8735522012-06-01 19:33:18 +00007138 MachineFunction *MF = BB->getParent();
7139 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00007140 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00007141 const TargetRegisterClass *TRC = nullptr;
7142 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007143
7144 bool IsThumb1 = Subtarget->isThumb1Only();
7145 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00007146
7147 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00007148 UnitSize = 1;
7149 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00007150 UnitSize = 2;
7151 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00007152 // Check whether we can use NEON instructions.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00007153 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007154 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007155 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007156 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007157 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007158 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007159 }
7160 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007161 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007162 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007163 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007164
David Peixottob0653e532013-10-24 16:39:36 +00007165 // Select the correct opcode and register class for unit size load/store
7166 bool IsNeon = UnitSize >= 8;
Craig Topper61e88f42014-11-21 05:58:21 +00007167 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007168 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00007169 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
7170 : UnitSize == 8 ? &ARM::DPRRegClass
7171 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007172
Manman Rene8735522012-06-01 19:33:18 +00007173 unsigned BytesLeft = SizeVal % UnitSize;
7174 unsigned LoopSize = SizeVal - BytesLeft;
7175
7176 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7177 // Use LDR and STR to copy.
7178 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7179 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7180 unsigned srcIn = src;
7181 unsigned destIn = dest;
7182 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007183 unsigned srcOut = MRI.createVirtualRegister(TRC);
7184 unsigned destOut = MRI.createVirtualRegister(TRC);
7185 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007186 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7187 IsThumb1, IsThumb2);
7188 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7189 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007190 srcIn = srcOut;
7191 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007192 }
7193
7194 // Handle the leftover bytes with LDRB and STRB.
7195 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7196 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007197 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007198 unsigned srcOut = MRI.createVirtualRegister(TRC);
7199 unsigned destOut = MRI.createVirtualRegister(TRC);
7200 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007201 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7202 IsThumb1, IsThumb2);
7203 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7204 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007205 srcIn = srcOut;
7206 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007207 }
7208 MI->eraseFromParent(); // The instruction is gone now.
7209 return BB;
7210 }
7211
7212 // Expand the pseudo op to a loop.
7213 // thisMBB:
7214 // ...
7215 // movw varEnd, # --> with thumb2
7216 // movt varEnd, #
7217 // ldrcp varEnd, idx --> without thumb2
7218 // fallthrough --> loopMBB
7219 // loopMBB:
7220 // PHI varPhi, varEnd, varLoop
7221 // PHI srcPhi, src, srcLoop
7222 // PHI destPhi, dst, destLoop
7223 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7224 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7225 // subs varLoop, varPhi, #UnitSize
7226 // bne loopMBB
7227 // fallthrough --> exitMBB
7228 // exitMBB:
7229 // epilogue to handle left-over bytes
7230 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7231 // [destOut] = STRB_POST(scratch, destLoop, 1)
7232 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7233 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7234 MF->insert(It, loopMBB);
7235 MF->insert(It, exitMBB);
7236
7237 // Transfer the remainder of BB and its successor edges to exitMBB.
7238 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007239 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007240 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7241
7242 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007243 unsigned varEnd = MRI.createVirtualRegister(TRC);
7244 if (IsThumb2) {
7245 unsigned Vtmp = varEnd;
7246 if ((LoopSize & 0xFFFF0000) != 0)
7247 Vtmp = MRI.createVirtualRegister(TRC);
7248 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), Vtmp)
7249 .addImm(LoopSize & 0xFFFF));
7250
7251 if ((LoopSize & 0xFFFF0000) != 0)
7252 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
7253 .addReg(Vtmp).addImm(LoopSize >> 16));
7254 } else {
7255 MachineConstantPool *ConstantPool = MF->getConstantPool();
7256 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7257 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7258
7259 // MachineConstantPool wants an explicit alignment.
7260 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
7261 if (Align == 0)
7262 Align = getDataLayout()->getTypeAllocSize(C->getType());
7263 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7264
7265 if (IsThumb1)
7266 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7267 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7268 else
7269 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7270 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7271 }
Manman Rene8735522012-06-01 19:33:18 +00007272 BB->addSuccessor(loopMBB);
7273
7274 // Generate the loop body:
7275 // varPhi = PHI(varLoop, varEnd)
7276 // srcPhi = PHI(srcLoop, src)
7277 // destPhi = PHI(destLoop, dst)
7278 MachineBasicBlock *entryBB = BB;
7279 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007280 unsigned varLoop = MRI.createVirtualRegister(TRC);
7281 unsigned varPhi = MRI.createVirtualRegister(TRC);
7282 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7283 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7284 unsigned destLoop = MRI.createVirtualRegister(TRC);
7285 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007286
7287 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7288 .addReg(varLoop).addMBB(loopMBB)
7289 .addReg(varEnd).addMBB(entryBB);
7290 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7291 .addReg(srcLoop).addMBB(loopMBB)
7292 .addReg(src).addMBB(entryBB);
7293 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7294 .addReg(destLoop).addMBB(loopMBB)
7295 .addReg(dest).addMBB(entryBB);
7296
7297 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7298 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007299 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007300 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7301 IsThumb1, IsThumb2);
7302 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7303 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007304
7305 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007306 if (IsThumb1) {
7307 MachineInstrBuilder MIB =
7308 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7309 MIB = AddDefaultT1CC(MIB);
7310 MIB.addReg(varPhi).addImm(UnitSize);
7311 AddDefaultPred(MIB);
7312 } else {
7313 MachineInstrBuilder MIB =
7314 BuildMI(*BB, BB->end(), dl,
7315 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7316 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7317 MIB->getOperand(5).setReg(ARM::CPSR);
7318 MIB->getOperand(5).setIsDef(true);
7319 }
7320 BuildMI(*BB, BB->end(), dl,
7321 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7322 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007323
7324 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7325 BB->addSuccessor(loopMBB);
7326 BB->addSuccessor(exitMBB);
7327
7328 // Add epilogue to handle BytesLeft.
7329 BB = exitMBB;
7330 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007331
7332 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7333 // [destOut] = STRB_POST(scratch, destLoop, 1)
7334 unsigned srcIn = srcLoop;
7335 unsigned destIn = destLoop;
7336 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007337 unsigned srcOut = MRI.createVirtualRegister(TRC);
7338 unsigned destOut = MRI.createVirtualRegister(TRC);
7339 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007340 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7341 IsThumb1, IsThumb2);
7342 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7343 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007344 srcIn = srcOut;
7345 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007346 }
7347
7348 MI->eraseFromParent(); // The instruction is gone now.
7349 return BB;
7350}
7351
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007352MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007353ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
7354 MachineBasicBlock *MBB) const {
7355 const TargetMachine &TM = getTargetMachine();
Eric Christopher1889fdc2015-01-29 00:19:39 +00007356 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007357 DebugLoc DL = MI->getDebugLoc();
7358
7359 assert(Subtarget->isTargetWindows() &&
7360 "__chkstk is only supported on Windows");
7361 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
7362
7363 // __chkstk takes the number of words to allocate on the stack in R4, and
7364 // returns the stack adjustment in number of bytes in R4. This will not
7365 // clober any other registers (other than the obvious lr).
7366 //
7367 // Although, technically, IP should be considered a register which may be
7368 // clobbered, the call itself will not touch it. Windows on ARM is a pure
7369 // thumb-2 environment, so there is no interworking required. As a result, we
7370 // do not expect a veneer to be emitted by the linker, clobbering IP.
7371 //
Alp Toker1d099d92014-06-19 19:41:26 +00007372 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007373 // required, again, ensuring that IP is not clobbered.
7374 //
7375 // Finally, although some linkers may theoretically provide a trampoline for
7376 // out of range calls (which is quite common due to a 32M range limitation of
7377 // branches for Thumb), we can generate the long-call version via
7378 // -mcmodel=large, alleviating the need for the trampoline which may clobber
7379 // IP.
7380
7381 switch (TM.getCodeModel()) {
7382 case CodeModel::Small:
7383 case CodeModel::Medium:
7384 case CodeModel::Default:
7385 case CodeModel::Kernel:
7386 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
7387 .addImm((unsigned)ARMCC::AL).addReg(0)
7388 .addExternalSymbol("__chkstk")
7389 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7390 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7391 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7392 break;
7393 case CodeModel::Large:
7394 case CodeModel::JITDefault: {
7395 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7396 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
7397
7398 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
7399 .addExternalSymbol("__chkstk");
7400 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
7401 .addImm((unsigned)ARMCC::AL).addReg(0)
7402 .addReg(Reg, RegState::Kill)
7403 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7404 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7405 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7406 break;
7407 }
7408 }
7409
7410 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
7411 ARM::SP)
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +00007412 .addReg(ARM::SP).addReg(ARM::R4)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007413
7414 MI->eraseFromParent();
7415 return MBB;
7416}
7417
7418MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007419ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007420 MachineBasicBlock *BB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007421 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007422 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007423 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007424 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007425 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007426 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007427 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007428 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007429 // The Thumb2 pre-indexed stores have the same MI operands, they just
7430 // define them differently in the .td files from the isel patterns, so
7431 // they need pseudos.
7432 case ARM::t2STR_preidx:
7433 MI->setDesc(TII->get(ARM::t2STR_PRE));
7434 return BB;
7435 case ARM::t2STRB_preidx:
7436 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7437 return BB;
7438 case ARM::t2STRH_preidx:
7439 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7440 return BB;
7441
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007442 case ARM::STRi_preidx:
7443 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007444 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007445 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7446 // Decode the offset.
7447 unsigned Offset = MI->getOperand(4).getImm();
7448 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7449 Offset = ARM_AM::getAM2Offset(Offset);
7450 if (isSub)
7451 Offset = -Offset;
7452
Jim Grosbachf402f692011-08-12 21:02:34 +00007453 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007454 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007455 .addOperand(MI->getOperand(0)) // Rn_wb
7456 .addOperand(MI->getOperand(1)) // Rt
7457 .addOperand(MI->getOperand(2)) // Rn
7458 .addImm(Offset) // offset (skip GPR==zero_reg)
7459 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007460 .addOperand(MI->getOperand(6))
7461 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007462 MI->eraseFromParent();
7463 return BB;
7464 }
7465 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007466 case ARM::STRBr_preidx:
7467 case ARM::STRH_preidx: {
7468 unsigned NewOpc;
7469 switch (MI->getOpcode()) {
7470 default: llvm_unreachable("unexpected opcode!");
7471 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7472 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7473 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7474 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007475 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7476 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7477 MIB.addOperand(MI->getOperand(i));
7478 MI->eraseFromParent();
7479 return BB;
7480 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007481
Evan Chengbb2af352009-08-12 05:17:19 +00007482 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007483 // To "insert" a SELECT_CC instruction, we actually have to insert the
7484 // diamond control-flow pattern. The incoming instruction knows the
7485 // destination vreg to set, the condition code register to branch on, the
7486 // true/false values to select between, and a branch opcode to use.
7487 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007488 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007489 ++It;
7490
7491 // thisMBB:
7492 // ...
7493 // TrueVal = ...
7494 // cmpTY ccX, r1, r2
7495 // bCC copy1MBB
7496 // fallthrough --> copy0MBB
7497 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007498 MachineFunction *F = BB->getParent();
7499 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7500 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007501 F->insert(It, copy0MBB);
7502 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007503
7504 // Transfer the remainder of BB and its successor edges to sinkMBB.
7505 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007506 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007507 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7508
Dan Gohmanf4f04102010-07-06 15:49:48 +00007509 BB->addSuccessor(copy0MBB);
7510 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007511
Dan Gohman34396292010-07-06 20:24:04 +00007512 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7513 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7514
Evan Cheng10043e22007-01-19 07:51:42 +00007515 // copy0MBB:
7516 // %FalseValue = ...
7517 // # fallthrough to sinkMBB
7518 BB = copy0MBB;
7519
7520 // Update machine-CFG edges
7521 BB->addSuccessor(sinkMBB);
7522
7523 // sinkMBB:
7524 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7525 // ...
7526 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007527 BuildMI(*BB, BB->begin(), dl,
7528 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007529 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7530 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7531
Dan Gohman34396292010-07-06 20:24:04 +00007532 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007533 return BB;
7534 }
Evan Chengb972e562009-08-07 00:34:42 +00007535
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007536 case ARM::BCCi64:
7537 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007538 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007539 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007540
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007541 // Compare both parts that make up the double comparison separately for
7542 // equality.
7543 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7544
7545 unsigned LHS1 = MI->getOperand(1).getReg();
7546 unsigned LHS2 = MI->getOperand(2).getReg();
7547 if (RHSisZero) {
7548 AddDefaultPred(BuildMI(BB, dl,
7549 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7550 .addReg(LHS1).addImm(0));
7551 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7552 .addReg(LHS2).addImm(0)
7553 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7554 } else {
7555 unsigned RHS1 = MI->getOperand(3).getReg();
7556 unsigned RHS2 = MI->getOperand(4).getReg();
7557 AddDefaultPred(BuildMI(BB, dl,
7558 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7559 .addReg(LHS1).addReg(RHS1));
7560 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7561 .addReg(LHS2).addReg(RHS2)
7562 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7563 }
7564
7565 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7566 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7567 if (MI->getOperand(0).getImm() == ARMCC::NE)
7568 std::swap(destMBB, exitMBB);
7569
7570 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7571 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007572 if (isThumb2)
7573 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7574 else
7575 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007576
7577 MI->eraseFromParent(); // The pseudo instruction is gone now.
7578 return BB;
7579 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007580
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007581 case ARM::Int_eh_sjlj_setjmp:
7582 case ARM::Int_eh_sjlj_setjmp_nofp:
7583 case ARM::tInt_eh_sjlj_setjmp:
7584 case ARM::t2Int_eh_sjlj_setjmp:
7585 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7586 EmitSjLjDispatchBlock(MI, BB);
7587 return BB;
7588
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007589 case ARM::ABS:
7590 case ARM::t2ABS: {
7591 // To insert an ABS instruction, we have to insert the
7592 // diamond control-flow pattern. The incoming instruction knows the
7593 // source vreg to test against 0, the destination vreg to set,
7594 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007595 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007596 // It transforms
7597 // V1 = ABS V0
7598 // into
7599 // V2 = MOVS V0
7600 // BCC (branch to SinkBB if V0 >= 0)
7601 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007602 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007603 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7604 MachineFunction::iterator BBI = BB;
7605 ++BBI;
7606 MachineFunction *Fn = BB->getParent();
7607 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7608 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7609 Fn->insert(BBI, RSBBB);
7610 Fn->insert(BBI, SinkBB);
7611
7612 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7613 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7614 bool isThumb2 = Subtarget->isThumb2();
7615 MachineRegisterInfo &MRI = Fn->getRegInfo();
7616 // In Thumb mode S must not be specified if source register is the SP or
7617 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00007618 unsigned NewRsbDstReg =
7619 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007620
7621 // Transfer the remainder of BB and its successor edges to sinkMBB.
7622 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007623 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007624 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7625
7626 BB->addSuccessor(RSBBB);
7627 BB->addSuccessor(SinkBB);
7628
7629 // fall through to SinkMBB
7630 RSBBB->addSuccessor(SinkBB);
7631
Manman Rene0763c72012-06-15 21:32:12 +00007632 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00007633 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00007634 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7635 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007636
7637 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00007638 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007639 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7640 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7641
7642 // insert rsbri in RSBBB
7643 // Note: BCC and rsbri will be converted into predicated rsbmi
7644 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00007645 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007646 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Rene0763c72012-06-15 21:32:12 +00007647 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007648 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7649
Andrew Trick3f07c422011-10-18 18:40:53 +00007650 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007651 // reuse ABSDstReg to not change uses of ABS instruction
7652 BuildMI(*SinkBB, SinkBB->begin(), dl,
7653 TII->get(ARM::PHI), ABSDstReg)
7654 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00007655 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007656
7657 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00007658 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007659
7660 // return last added BB
7661 return SinkBB;
7662 }
Manman Rene8735522012-06-01 19:33:18 +00007663 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00007664 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00007665 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007666 case ARM::WIN__CHKSTK:
7667 return EmitLowered__chkstk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00007668 }
7669}
7670
Evan Chenge6fba772011-08-30 19:09:48 +00007671void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7672 SDNode *Node) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007673 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00007674 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7675 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7676 // operand is still set to noreg. If needed, set the optional operand's
7677 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00007678 //
Andrew Trick88b24502011-10-18 19:18:52 +00007679 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00007680
Andrew Trick924123a2011-09-21 02:20:46 +00007681 // Rename pseudo opcodes.
7682 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7683 if (NewOpc) {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007684 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
Andrew Trick88b24502011-10-18 19:18:52 +00007685 MCID = &TII->get(NewOpc);
7686
7687 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7688 "converted opcode should be the same except for cc_out");
7689
7690 MI->setDesc(*MCID);
7691
7692 // Add the optional cc_out operand
7693 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00007694 }
Andrew Trick88b24502011-10-18 19:18:52 +00007695 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00007696
7697 // Any ARM instruction that sets the 's' bit should specify an optional
7698 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00007699 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007700 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007701 return;
7702 }
Andrew Trick924123a2011-09-21 02:20:46 +00007703 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7704 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007705 bool definesCPSR = false;
7706 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00007707 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00007708 i != e; ++i) {
7709 const MachineOperand &MO = MI->getOperand(i);
7710 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7711 definesCPSR = true;
7712 if (MO.isDead())
7713 deadCPSR = true;
7714 MI->RemoveOperand(i);
7715 break;
Evan Chenge6fba772011-08-30 19:09:48 +00007716 }
7717 }
Andrew Trick8586e622011-09-20 03:17:40 +00007718 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00007719 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007720 return;
7721 }
7722 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00007723 if (deadCPSR) {
7724 assert(!MI->getOperand(ccOutIdx).getReg() &&
7725 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00007726 return;
Andrew Trick924123a2011-09-21 02:20:46 +00007727 }
Andrew Trick8586e622011-09-20 03:17:40 +00007728
Andrew Trick924123a2011-09-21 02:20:46 +00007729 // If this instruction was defined with an optional CPSR def and its dag node
7730 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007731 MachineOperand &MO = MI->getOperand(ccOutIdx);
7732 MO.setReg(ARM::CPSR);
7733 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00007734}
7735
Evan Cheng10043e22007-01-19 07:51:42 +00007736//===----------------------------------------------------------------------===//
7737// ARM Optimization Hooks
7738//===----------------------------------------------------------------------===//
7739
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007740// Helper function that checks if N is a null or all ones constant.
7741static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7742 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7743 if (!C)
7744 return false;
7745 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7746}
7747
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007748// Return true if N is conditionally 0 or all ones.
7749// Detects these expressions where cc is an i1 value:
7750//
7751// (select cc 0, y) [AllOnes=0]
7752// (select cc y, 0) [AllOnes=0]
7753// (zext cc) [AllOnes=0]
7754// (sext cc) [AllOnes=0/1]
7755// (select cc -1, y) [AllOnes=1]
7756// (select cc y, -1) [AllOnes=1]
7757//
7758// Invert is set when N is the null/all ones constant when CC is false.
7759// OtherOp is set to the alternative value of N.
7760static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7761 SDValue &CC, bool &Invert,
7762 SDValue &OtherOp,
7763 SelectionDAG &DAG) {
7764 switch (N->getOpcode()) {
7765 default: return false;
7766 case ISD::SELECT: {
7767 CC = N->getOperand(0);
7768 SDValue N1 = N->getOperand(1);
7769 SDValue N2 = N->getOperand(2);
7770 if (isZeroOrAllOnes(N1, AllOnes)) {
7771 Invert = false;
7772 OtherOp = N2;
7773 return true;
7774 }
7775 if (isZeroOrAllOnes(N2, AllOnes)) {
7776 Invert = true;
7777 OtherOp = N1;
7778 return true;
7779 }
7780 return false;
7781 }
7782 case ISD::ZERO_EXTEND:
7783 // (zext cc) can never be the all ones value.
7784 if (AllOnes)
7785 return false;
7786 // Fall through.
7787 case ISD::SIGN_EXTEND: {
7788 EVT VT = N->getValueType(0);
7789 CC = N->getOperand(0);
7790 if (CC.getValueType() != MVT::i1)
7791 return false;
7792 Invert = !AllOnes;
7793 if (AllOnes)
7794 // When looking for an AllOnes constant, N is an sext, and the 'other'
7795 // value is 0.
7796 OtherOp = DAG.getConstant(0, VT);
7797 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7798 // When looking for a 0 constant, N can be zext or sext.
7799 OtherOp = DAG.getConstant(1, VT);
7800 else
7801 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7802 return true;
7803 }
7804 }
7805}
7806
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007807// Combine a constant select operand into its use:
7808//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007809// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7810// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7811// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7812// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7813// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007814//
7815// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007816// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007817//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007818// Also recognize sext/zext from i1:
7819//
7820// (add (zext cc), x) -> (select cc (add x, 1), x)
7821// (add (sext cc), x) -> (select cc (add x, -1), x)
7822//
7823// These transformations eventually create predicated instructions.
7824//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007825// @param N The node to transform.
7826// @param Slct The N operand that is a select.
7827// @param OtherOp The other N operand (x above).
7828// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007829// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007830// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00007831static
7832SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007833 TargetLowering::DAGCombinerInfo &DCI,
7834 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00007835 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007836 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007837 SDValue NonConstantVal;
7838 SDValue CCOp;
7839 bool SwapSelectOps;
7840 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7841 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007842 return SDValue();
7843
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007844 // Slct is now know to be the desired identity constant when CC is true.
7845 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007846 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007847 OtherOp, NonConstantVal);
7848 // Unless SwapSelectOps says CC should be false.
7849 if (SwapSelectOps)
7850 std::swap(TrueVal, FalseVal);
7851
Andrew Trickef9de2a2013-05-25 02:42:55 +00007852 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007853 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00007854}
7855
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007856// Attempt combineSelectAndUse on each operand of a commutative operator N.
7857static
7858SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7859 TargetLowering::DAGCombinerInfo &DCI) {
7860 SDValue N0 = N->getOperand(0);
7861 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007862 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007863 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7864 if (Result.getNode())
7865 return Result;
7866 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007867 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007868 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7869 if (Result.getNode())
7870 return Result;
7871 }
7872 return SDValue();
7873}
7874
Eric Christopher1b8b94192011-06-29 21:10:36 +00007875// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00007876// (only after legalization).
7877static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7878 TargetLowering::DAGCombinerInfo &DCI,
7879 const ARMSubtarget *Subtarget) {
7880
7881 // Only perform optimization if after legalize, and if NEON is available. We
7882 // also expected both operands to be BUILD_VECTORs.
7883 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7884 || N0.getOpcode() != ISD::BUILD_VECTOR
7885 || N1.getOpcode() != ISD::BUILD_VECTOR)
7886 return SDValue();
7887
7888 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7889 EVT VT = N->getValueType(0);
7890 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7891 return SDValue();
7892
7893 // Check that the vector operands are of the right form.
7894 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7895 // operands, where N is the size of the formed vector.
7896 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7897 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007898
7899 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00007900 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00007901 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00007902 SDValue Vec = N0->getOperand(0)->getOperand(0);
7903 SDNode *V = Vec.getNode();
7904 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00007905
Eric Christopher1b8b94192011-06-29 21:10:36 +00007906 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007907 // check to see if each of their operands are an EXTRACT_VECTOR with
7908 // the same vector and appropriate index.
7909 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7910 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7911 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00007912
Tanya Lattnere9e67052011-06-14 23:48:48 +00007913 SDValue ExtVec0 = N0->getOperand(i);
7914 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007915
Tanya Lattnere9e67052011-06-14 23:48:48 +00007916 // First operand is the vector, verify its the same.
7917 if (V != ExtVec0->getOperand(0).getNode() ||
7918 V != ExtVec1->getOperand(0).getNode())
7919 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00007920
Tanya Lattnere9e67052011-06-14 23:48:48 +00007921 // Second is the constant, verify its correct.
7922 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7923 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00007924
Tanya Lattnere9e67052011-06-14 23:48:48 +00007925 // For the constant, we want to see all the even or all the odd.
7926 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7927 || C1->getZExtValue() != nextIndex+1)
7928 return SDValue();
7929
7930 // Increment index.
7931 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007932 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00007933 return SDValue();
7934 }
7935
7936 // Create VPADDL node.
7937 SelectionDAG &DAG = DCI.DAG;
7938 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00007939
7940 // Build operand list.
7941 SmallVector<SDValue, 8> Ops;
7942 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7943 TLI.getPointerTy()));
7944
7945 // Input is the vector.
7946 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007947
Tanya Lattnere9e67052011-06-14 23:48:48 +00007948 // Get widened type and narrowed type.
7949 MVT widenType;
7950 unsigned numElem = VT.getVectorNumElements();
Silviu Barangaa3106e62014-04-03 10:44:27 +00007951
7952 EVT inputLaneType = Vec.getValueType().getVectorElementType();
7953 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00007954 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7955 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7956 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7957 default:
Craig Toppere55c5562012-02-07 02:50:20 +00007958 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00007959 }
7960
Craig Topper48d114b2014-04-26 18:35:24 +00007961 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00007962 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
7963 return DAG.getNode(ExtOp, SDLoc(N), VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00007964}
7965
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007966static SDValue findMUL_LOHI(SDValue V) {
7967 if (V->getOpcode() == ISD::UMUL_LOHI ||
7968 V->getOpcode() == ISD::SMUL_LOHI)
7969 return V;
7970 return SDValue();
7971}
7972
7973static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7974 TargetLowering::DAGCombinerInfo &DCI,
7975 const ARMSubtarget *Subtarget) {
7976
7977 if (Subtarget->isThumb1Only()) return SDValue();
7978
7979 // Only perform the checks after legalize when the pattern is available.
7980 if (DCI.isBeforeLegalize()) return SDValue();
7981
7982 // Look for multiply add opportunities.
7983 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7984 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7985 // a glue link from the first add to the second add.
7986 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7987 // a S/UMLAL instruction.
7988 // loAdd UMUL_LOHI
7989 // \ / :lo \ :hi
7990 // \ / \ [no multiline comment]
7991 // ADDC | hiAdd
7992 // \ :glue / /
7993 // \ / /
7994 // ADDE
7995 //
7996 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7997 SDValue AddcOp0 = AddcNode->getOperand(0);
7998 SDValue AddcOp1 = AddcNode->getOperand(1);
7999
8000 // Check if the two operands are from the same mul_lohi node.
8001 if (AddcOp0.getNode() == AddcOp1.getNode())
8002 return SDValue();
8003
8004 assert(AddcNode->getNumValues() == 2 &&
8005 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00008006 "Expect ADDC with two result values. First: i32");
8007
8008 // Check that we have a glued ADDC node.
8009 if (AddcNode->getValueType(1) != MVT::Glue)
8010 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008011
8012 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
8013 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
8014 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
8015 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
8016 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
8017 return SDValue();
8018
8019 // Look for the glued ADDE.
8020 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00008021 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008022 return SDValue();
8023
8024 // Make sure it is really an ADDE.
8025 if (AddeNode->getOpcode() != ISD::ADDE)
8026 return SDValue();
8027
8028 assert(AddeNode->getNumOperands() == 3 &&
8029 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
8030 "ADDE node has the wrong inputs");
8031
8032 // Check for the triangle shape.
8033 SDValue AddeOp0 = AddeNode->getOperand(0);
8034 SDValue AddeOp1 = AddeNode->getOperand(1);
8035
8036 // Make sure that the ADDE operands are not coming from the same node.
8037 if (AddeOp0.getNode() == AddeOp1.getNode())
8038 return SDValue();
8039
8040 // Find the MUL_LOHI node walking up ADDE's operands.
8041 bool IsLeftOperandMUL = false;
8042 SDValue MULOp = findMUL_LOHI(AddeOp0);
8043 if (MULOp == SDValue())
8044 MULOp = findMUL_LOHI(AddeOp1);
8045 else
8046 IsLeftOperandMUL = true;
8047 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00008048 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008049
8050 // Figure out the right opcode.
8051 unsigned Opc = MULOp->getOpcode();
8052 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
8053
8054 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00008055 SDValue* HiAdd = nullptr;
8056 SDValue* LoMul = nullptr;
8057 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008058
Jyoti Allurf1d70502015-01-23 09:10:03 +00008059 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
8060 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
8061 return SDValue();
8062
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008063 if (IsLeftOperandMUL)
8064 HiAdd = &AddeOp1;
8065 else
8066 HiAdd = &AddeOp0;
8067
8068
Jyoti Allurf1d70502015-01-23 09:10:03 +00008069 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
8070 // whose low result is fed to the ADDC we are checking.
8071
8072 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008073 LoMul = &AddcOp0;
8074 LowAdd = &AddcOp1;
8075 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00008076 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008077 LoMul = &AddcOp1;
8078 LowAdd = &AddcOp0;
8079 }
8080
Craig Topper062a2ba2014-04-25 05:30:21 +00008081 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008082 return SDValue();
8083
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008084 // Create the merged node.
8085 SelectionDAG &DAG = DCI.DAG;
8086
8087 // Build operand list.
8088 SmallVector<SDValue, 8> Ops;
8089 Ops.push_back(LoMul->getOperand(0));
8090 Ops.push_back(LoMul->getOperand(1));
8091 Ops.push_back(*LowAdd);
8092 Ops.push_back(*HiAdd);
8093
Andrew Trickef9de2a2013-05-25 02:42:55 +00008094 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00008095 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008096
8097 // Replace the ADDs' nodes uses by the MLA node's values.
8098 SDValue HiMLALResult(MLALNode.getNode(), 1);
8099 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
8100
8101 SDValue LoMLALResult(MLALNode.getNode(), 0);
8102 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
8103
8104 // Return original node to notify the driver to stop replacing.
8105 SDValue resNode(AddcNode, 0);
8106 return resNode;
8107}
8108
8109/// PerformADDCCombine - Target-specific dag combine transform from
8110/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
8111static SDValue PerformADDCCombine(SDNode *N,
8112 TargetLowering::DAGCombinerInfo &DCI,
8113 const ARMSubtarget *Subtarget) {
8114
8115 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
8116
8117}
8118
Bob Wilson728eb292010-07-29 20:34:14 +00008119/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
8120/// operands N0 and N1. This is a helper for PerformADDCombine that is
8121/// called with the default operands, and if that fails, with commuted
8122/// operands.
8123static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008124 TargetLowering::DAGCombinerInfo &DCI,
8125 const ARMSubtarget *Subtarget){
8126
8127 // Attempt to create vpaddl for this add.
8128 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
8129 if (Result.getNode())
8130 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008131
Chris Lattner4147f082009-03-12 06:52:53 +00008132 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008133 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008134 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
8135 if (Result.getNode()) return Result;
8136 }
Chris Lattner4147f082009-03-12 06:52:53 +00008137 return SDValue();
8138}
8139
Bob Wilson728eb292010-07-29 20:34:14 +00008140/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
8141///
8142static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008143 TargetLowering::DAGCombinerInfo &DCI,
8144 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00008145 SDValue N0 = N->getOperand(0);
8146 SDValue N1 = N->getOperand(1);
8147
8148 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008149 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008150 if (Result.getNode())
8151 return Result;
8152
8153 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008154 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008155}
8156
Chris Lattner4147f082009-03-12 06:52:53 +00008157/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00008158///
Chris Lattner4147f082009-03-12 06:52:53 +00008159static SDValue PerformSUBCombine(SDNode *N,
8160 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00008161 SDValue N0 = N->getOperand(0);
8162 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00008163
Chris Lattner4147f082009-03-12 06:52:53 +00008164 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008165 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008166 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
8167 if (Result.getNode()) return Result;
8168 }
Bob Wilson7117a912009-03-20 22:42:55 +00008169
Chris Lattner4147f082009-03-12 06:52:53 +00008170 return SDValue();
8171}
8172
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008173/// PerformVMULCombine
8174/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8175/// special multiplier accumulator forwarding.
8176/// vmul d3, d0, d2
8177/// vmla d3, d1, d2
8178/// is faster than
8179/// vadd d3, d0, d1
8180/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00008181// However, for (A + B) * (A + B),
8182// vadd d2, d0, d1
8183// vmul d3, d0, d2
8184// vmla d3, d1, d2
8185// is slower than
8186// vadd d2, d0, d1
8187// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008188static SDValue PerformVMULCombine(SDNode *N,
8189 TargetLowering::DAGCombinerInfo &DCI,
8190 const ARMSubtarget *Subtarget) {
8191 if (!Subtarget->hasVMLxForwarding())
8192 return SDValue();
8193
8194 SelectionDAG &DAG = DCI.DAG;
8195 SDValue N0 = N->getOperand(0);
8196 SDValue N1 = N->getOperand(1);
8197 unsigned Opcode = N0.getOpcode();
8198 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8199 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00008200 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008201 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8202 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8203 return SDValue();
8204 std::swap(N0, N1);
8205 }
8206
Weiming Zhao2052f482013-09-25 23:12:06 +00008207 if (N0 == N1)
8208 return SDValue();
8209
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008210 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008211 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008212 SDValue N00 = N0->getOperand(0);
8213 SDValue N01 = N0->getOperand(1);
8214 return DAG.getNode(Opcode, DL, VT,
8215 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8216 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8217}
8218
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008219static SDValue PerformMULCombine(SDNode *N,
8220 TargetLowering::DAGCombinerInfo &DCI,
8221 const ARMSubtarget *Subtarget) {
8222 SelectionDAG &DAG = DCI.DAG;
8223
8224 if (Subtarget->isThumb1Only())
8225 return SDValue();
8226
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008227 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8228 return SDValue();
8229
8230 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008231 if (VT.is64BitVector() || VT.is128BitVector())
8232 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008233 if (VT != MVT::i32)
8234 return SDValue();
8235
8236 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8237 if (!C)
8238 return SDValue();
8239
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008240 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008241 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008242
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008243 ShiftAmt = ShiftAmt & (32 - 1);
8244 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008245 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008246
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008247 SDValue Res;
8248 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008249
8250 if (MulAmt >= 0) {
8251 if (isPowerOf2_32(MulAmt - 1)) {
8252 // (mul x, 2^N + 1) => (add (shl x, N), x)
8253 Res = DAG.getNode(ISD::ADD, DL, VT,
8254 V,
8255 DAG.getNode(ISD::SHL, DL, VT,
8256 V,
8257 DAG.getConstant(Log2_32(MulAmt - 1),
8258 MVT::i32)));
8259 } else if (isPowerOf2_32(MulAmt + 1)) {
8260 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8261 Res = DAG.getNode(ISD::SUB, DL, VT,
8262 DAG.getNode(ISD::SHL, DL, VT,
8263 V,
8264 DAG.getConstant(Log2_32(MulAmt + 1),
8265 MVT::i32)),
8266 V);
8267 } else
8268 return SDValue();
8269 } else {
8270 uint64_t MulAmtAbs = -MulAmt;
8271 if (isPowerOf2_32(MulAmtAbs + 1)) {
8272 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8273 Res = DAG.getNode(ISD::SUB, DL, VT,
8274 V,
8275 DAG.getNode(ISD::SHL, DL, VT,
8276 V,
8277 DAG.getConstant(Log2_32(MulAmtAbs + 1),
8278 MVT::i32)));
8279 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8280 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8281 Res = DAG.getNode(ISD::ADD, DL, VT,
8282 V,
8283 DAG.getNode(ISD::SHL, DL, VT,
8284 V,
8285 DAG.getConstant(Log2_32(MulAmtAbs-1),
8286 MVT::i32)));
8287 Res = DAG.getNode(ISD::SUB, DL, VT,
8288 DAG.getConstant(0, MVT::i32),Res);
8289
8290 } else
8291 return SDValue();
8292 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008293
8294 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008295 Res = DAG.getNode(ISD::SHL, DL, VT,
8296 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008297
8298 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008299 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008300 return SDValue();
8301}
8302
Owen Anderson30c48922010-11-05 19:27:46 +00008303static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008304 TargetLowering::DAGCombinerInfo &DCI,
8305 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008306
Owen Anderson30c48922010-11-05 19:27:46 +00008307 // Attempt to use immediate-form VBIC
8308 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008309 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008310 EVT VT = N->getValueType(0);
8311 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008312
Tanya Lattner266792a2011-04-07 15:24:20 +00008313 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8314 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008315
Owen Anderson30c48922010-11-05 19:27:46 +00008316 APInt SplatBits, SplatUndef;
8317 unsigned SplatBitSize;
8318 bool HasAnyUndefs;
8319 if (BVN &&
8320 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8321 if (SplatBitSize <= 64) {
8322 EVT VbicVT;
8323 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8324 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00008325 DAG, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008326 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008327 if (Val.getNode()) {
8328 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008329 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008330 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008331 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008332 }
8333 }
8334 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008335
Evan Chenge87681c2012-02-23 01:19:06 +00008336 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008337 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8338 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8339 if (Result.getNode())
8340 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008341 }
8342
Owen Anderson30c48922010-11-05 19:27:46 +00008343 return SDValue();
8344}
8345
Jim Grosbach11013ed2010-07-16 23:05:05 +00008346/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8347static SDValue PerformORCombine(SDNode *N,
8348 TargetLowering::DAGCombinerInfo &DCI,
8349 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008350 // Attempt to use immediate-form VORR
8351 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008352 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008353 EVT VT = N->getValueType(0);
8354 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008355
Tanya Lattner266792a2011-04-07 15:24:20 +00008356 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8357 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008358
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008359 APInt SplatBits, SplatUndef;
8360 unsigned SplatBitSize;
8361 bool HasAnyUndefs;
8362 if (BVN && Subtarget->hasNEON() &&
8363 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8364 if (SplatBitSize <= 64) {
8365 EVT VorrVT;
8366 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8367 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00008368 DAG, VorrVT, VT.is128BitVector(),
8369 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008370 if (Val.getNode()) {
8371 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008372 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008373 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008374 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008375 }
8376 }
8377 }
8378
Evan Chenge87681c2012-02-23 01:19:06 +00008379 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008380 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8381 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8382 if (Result.getNode())
8383 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008384 }
8385
Nadav Rotem3a94c542012-08-13 18:52:44 +00008386 // The code below optimizes (or (and X, Y), Z).
8387 // The AND operand needs to have a single user to make these optimizations
8388 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008389 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008390 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008391 return SDValue();
8392 SDValue N1 = N->getOperand(1);
8393
8394 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8395 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8396 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8397 APInt SplatUndef;
8398 unsigned SplatBitSize;
8399 bool HasAnyUndefs;
8400
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008401 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008402 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008403 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8404 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008405 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008406 HasAnyUndefs) && !HasAnyUndefs) {
8407 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8408 HasAnyUndefs) && !HasAnyUndefs) {
8409 // Ensure that the bit width of the constants are the same and that
8410 // the splat arguments are logical inverses as per the pattern we
8411 // are trying to simplify.
8412 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8413 SplatBits0 == ~SplatBits1) {
8414 // Canonicalize the vector type to make instruction selection
8415 // simpler.
8416 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8417 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8418 N0->getOperand(1),
8419 N0->getOperand(0),
8420 N1->getOperand(0));
8421 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8422 }
8423 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008424 }
8425 }
8426
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008427 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8428 // reasonable.
8429
Jim Grosbach11013ed2010-07-16 23:05:05 +00008430 // BFI is only available on V6T2+
8431 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8432 return SDValue();
8433
Andrew Trickef9de2a2013-05-25 02:42:55 +00008434 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008435 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008436 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008437 //
8438 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008439 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008440 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008441 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008442 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008443 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008444
Jim Grosbach11013ed2010-07-16 23:05:05 +00008445 if (VT != MVT::i32)
8446 return SDValue();
8447
Evan Cheng2e51bb42010-12-13 20:32:54 +00008448 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008449
Jim Grosbach11013ed2010-07-16 23:05:05 +00008450 // The value and the mask need to be constants so we can verify this is
8451 // actually a bitfield set. If the mask is 0xffff, we can do better
8452 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008453 SDValue MaskOp = N0.getOperand(1);
8454 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8455 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008456 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008457 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008458 if (Mask == 0xffff)
8459 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008460 SDValue Res;
8461 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008462 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8463 if (N1C) {
8464 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008465 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008466 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008467
Evan Cheng34345752010-12-11 04:11:38 +00008468 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008469 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008470
Evan Cheng2e51bb42010-12-13 20:32:54 +00008471 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Cheng34345752010-12-11 04:11:38 +00008472 DAG.getConstant(Val, MVT::i32),
8473 DAG.getConstant(Mask, MVT::i32));
8474
8475 // Do not add new nodes to DAG combiner worklist.
8476 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008477 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008478 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008479 } else if (N1.getOpcode() == ISD::AND) {
8480 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008481 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8482 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008483 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008484 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008485
Eric Christopherd5530962011-03-26 01:21:03 +00008486 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8487 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008488 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008489 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008490 // The pack halfword instruction works better for masks that fit it,
8491 // so use that when it's available.
8492 if (Subtarget->hasT2ExtractPack() &&
8493 (Mask == 0xffff || Mask == 0xffff0000))
8494 return SDValue();
8495 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008496 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008497 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopherd5530962011-03-26 01:21:03 +00008498 DAG.getConstant(amt, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008499 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008500 DAG.getConstant(Mask, MVT::i32));
8501 // Do not add new nodes to DAG combiner worklist.
8502 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008503 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008504 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008505 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008506 // The pack halfword instruction works better for masks that fit it,
8507 // so use that when it's available.
8508 if (Subtarget->hasT2ExtractPack() &&
8509 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8510 return SDValue();
8511 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008512 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008513 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008514 DAG.getConstant(lsb, MVT::i32));
8515 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopherd5530962011-03-26 01:21:03 +00008516 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008517 // Do not add new nodes to DAG combiner worklist.
8518 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008519 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008520 }
8521 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008522
Evan Cheng2e51bb42010-12-13 20:32:54 +00008523 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8524 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8525 ARM::isBitFieldInvertedMask(~Mask)) {
8526 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8527 // where lsb(mask) == #shamt and masked bits of B are known zero.
8528 SDValue ShAmt = N00.getOperand(1);
8529 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008530 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008531 if (ShAmtC != LSB)
8532 return SDValue();
8533
8534 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8535 DAG.getConstant(~Mask, MVT::i32));
8536
8537 // Do not add new nodes to DAG combiner worklist.
8538 DCI.CombineTo(N, Res, false);
8539 }
8540
Jim Grosbach11013ed2010-07-16 23:05:05 +00008541 return SDValue();
8542}
8543
Evan Chenge87681c2012-02-23 01:19:06 +00008544static SDValue PerformXORCombine(SDNode *N,
8545 TargetLowering::DAGCombinerInfo &DCI,
8546 const ARMSubtarget *Subtarget) {
8547 EVT VT = N->getValueType(0);
8548 SelectionDAG &DAG = DCI.DAG;
8549
8550 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8551 return SDValue();
8552
8553 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008554 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8555 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8556 if (Result.getNode())
8557 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008558 }
8559
8560 return SDValue();
8561}
8562
Evan Cheng6d02d902011-06-15 01:12:31 +00008563/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8564/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00008565static SDValue PerformBFICombine(SDNode *N,
8566 TargetLowering::DAGCombinerInfo &DCI) {
8567 SDValue N1 = N->getOperand(1);
8568 if (N1.getOpcode() == ISD::AND) {
8569 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8570 if (!N11C)
8571 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008572 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008573 unsigned LSB = countTrailingZeros(~InvMask);
8574 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +00008575 assert(Width <
8576 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00008577 "undefined behavior");
8578 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +00008579 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008580 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008581 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00008582 N->getOperand(0), N1.getOperand(0),
8583 N->getOperand(2));
8584 }
8585 return SDValue();
8586}
8587
Bob Wilson22806742010-09-22 22:09:21 +00008588/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8589/// ARMISD::VMOVRRD.
8590static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008591 TargetLowering::DAGCombinerInfo &DCI,
8592 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +00008593 // vmovrrd(vmovdrr x, y) -> x,y
8594 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00008595 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +00008596 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008597
8598 // vmovrrd(load f64) -> (load i32), (load i32)
8599 SDNode *InNode = InDouble.getNode();
8600 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8601 InNode->getValueType(0) == MVT::f64 &&
8602 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8603 !cast<LoadSDNode>(InNode)->isVolatile()) {
8604 // TODO: Should this be done for non-FrameIndex operands?
8605 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8606
8607 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008608 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008609 SDValue BasePtr = LD->getBasePtr();
8610 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8611 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008612 LD->isNonTemporal(), LD->isInvariant(),
8613 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008614
8615 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8616 DAG.getConstant(4, MVT::i32));
8617 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8618 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008619 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008620 std::min(4U, LD->getAlignment() / 2));
8621
8622 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Christian Pirker762b2c62014-06-01 09:30:52 +00008623 if (DCI.DAG.getTargetLoweringInfo().isBigEndian())
8624 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008625 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008626 return Result;
8627 }
8628
Bob Wilson22806742010-09-22 22:09:21 +00008629 return SDValue();
8630}
8631
8632/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8633/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8634static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8635 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8636 SDValue Op0 = N->getOperand(0);
8637 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00008638 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008639 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008640 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008641 Op1 = Op1.getOperand(0);
8642 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8643 Op0.getNode() == Op1.getNode() &&
8644 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008645 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00008646 N->getValueType(0), Op0.getOperand(0));
8647 return SDValue();
8648}
8649
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008650/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8651/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8652/// i64 vector to have f64 elements, since the value can then be loaded
8653/// directly into a VFP register.
8654static bool hasNormalLoadOperand(SDNode *N) {
8655 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8656 for (unsigned i = 0; i < NumElts; ++i) {
8657 SDNode *Elt = N->getOperand(i).getNode();
8658 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8659 return true;
8660 }
8661 return false;
8662}
8663
Bob Wilsoncb6db982010-09-17 22:59:05 +00008664/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8665/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008666static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008667 TargetLowering::DAGCombinerInfo &DCI,
8668 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +00008669 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8670 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8671 // into a pair of GPRs, which is fine when the value is used as a scalar,
8672 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008673 SelectionDAG &DAG = DCI.DAG;
8674 if (N->getNumOperands() == 2) {
8675 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8676 if (RV.getNode())
8677 return RV;
8678 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00008679
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008680 // Load i64 elements as f64 values so that type legalization does not split
8681 // them up into i32 values.
8682 EVT VT = N->getValueType(0);
8683 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8684 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008685 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008686 SmallVector<SDValue, 8> Ops;
8687 unsigned NumElts = VT.getVectorNumElements();
8688 for (unsigned i = 0; i < NumElts; ++i) {
8689 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8690 Ops.push_back(V);
8691 // Make the DAGCombiner fold the bitcast.
8692 DCI.AddToWorklist(V.getNode());
8693 }
8694 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00008695 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008696 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8697}
8698
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008699/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
8700static SDValue
8701PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8702 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
8703 // At that time, we may have inserted bitcasts from integer to float.
8704 // If these bitcasts have survived DAGCombine, change the lowering of this
8705 // BUILD_VECTOR in something more vector friendly, i.e., that does not
8706 // force to use floating point types.
8707
8708 // Make sure we can change the type of the vector.
8709 // This is possible iff:
8710 // 1. The vector is only used in a bitcast to a integer type. I.e.,
8711 // 1.1. Vector is used only once.
8712 // 1.2. Use is a bit convert to an integer type.
8713 // 2. The size of its operands are 32-bits (64-bits are not legal).
8714 EVT VT = N->getValueType(0);
8715 EVT EltVT = VT.getVectorElementType();
8716
8717 // Check 1.1. and 2.
8718 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
8719 return SDValue();
8720
8721 // By construction, the input type must be float.
8722 assert(EltVT == MVT::f32 && "Unexpected type!");
8723
8724 // Check 1.2.
8725 SDNode *Use = *N->use_begin();
8726 if (Use->getOpcode() != ISD::BITCAST ||
8727 Use->getValueType(0).isFloatingPoint())
8728 return SDValue();
8729
8730 // Check profitability.
8731 // Model is, if more than half of the relevant operands are bitcast from
8732 // i32, turn the build_vector into a sequence of insert_vector_elt.
8733 // Relevant operands are everything that is not statically
8734 // (i.e., at compile time) bitcasted.
8735 unsigned NumOfBitCastedElts = 0;
8736 unsigned NumElts = VT.getVectorNumElements();
8737 unsigned NumOfRelevantElts = NumElts;
8738 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
8739 SDValue Elt = N->getOperand(Idx);
8740 if (Elt->getOpcode() == ISD::BITCAST) {
8741 // Assume only bit cast to i32 will go away.
8742 if (Elt->getOperand(0).getValueType() == MVT::i32)
8743 ++NumOfBitCastedElts;
8744 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
8745 // Constants are statically casted, thus do not count them as
8746 // relevant operands.
8747 --NumOfRelevantElts;
8748 }
8749
8750 // Check if more than half of the elements require a non-free bitcast.
8751 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
8752 return SDValue();
8753
8754 SelectionDAG &DAG = DCI.DAG;
8755 // Create the new vector type.
8756 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
8757 // Check if the type is legal.
8758 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8759 if (!TLI.isTypeLegal(VecVT))
8760 return SDValue();
8761
8762 // Combine:
8763 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
8764 // => BITCAST INSERT_VECTOR_ELT
8765 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
8766 // (BITCAST EN), N.
8767 SDValue Vec = DAG.getUNDEF(VecVT);
8768 SDLoc dl(N);
8769 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
8770 SDValue V = N->getOperand(Idx);
8771 if (V.getOpcode() == ISD::UNDEF)
8772 continue;
8773 if (V.getOpcode() == ISD::BITCAST &&
8774 V->getOperand(0).getValueType() == MVT::i32)
8775 // Fold obvious case.
8776 V = V.getOperand(0);
8777 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00008778 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008779 // Make the DAGCombiner fold the bitcasts.
8780 DCI.AddToWorklist(V.getNode());
8781 }
8782 SDValue LaneIdx = DAG.getConstant(Idx, MVT::i32);
8783 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
8784 }
8785 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
8786 // Make the DAGCombiner fold the bitcasts.
8787 DCI.AddToWorklist(Vec.getNode());
8788 return Vec;
8789}
8790
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008791/// PerformInsertEltCombine - Target-specific dag combine xforms for
8792/// ISD::INSERT_VECTOR_ELT.
8793static SDValue PerformInsertEltCombine(SDNode *N,
8794 TargetLowering::DAGCombinerInfo &DCI) {
8795 // Bitcast an i64 load inserted into a vector to f64.
8796 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8797 EVT VT = N->getValueType(0);
8798 SDNode *Elt = N->getOperand(1).getNode();
8799 if (VT.getVectorElementType() != MVT::i64 ||
8800 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8801 return SDValue();
8802
8803 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008804 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008805 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8806 VT.getVectorNumElements());
8807 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8808 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8809 // Make the DAGCombiner fold the bitcasts.
8810 DCI.AddToWorklist(Vec.getNode());
8811 DCI.AddToWorklist(V.getNode());
8812 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8813 Vec, V, N->getOperand(2));
8814 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00008815}
8816
Bob Wilsonc7334a12010-10-27 20:38:28 +00008817/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8818/// ISD::VECTOR_SHUFFLE.
8819static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8820 // The LLVM shufflevector instruction does not require the shuffle mask
8821 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8822 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8823 // operands do not match the mask length, they are extended by concatenating
8824 // them with undef vectors. That is probably the right thing for other
8825 // targets, but for NEON it is better to concatenate two double-register
8826 // size vector operands into a single quad-register size vector. Do that
8827 // transformation here:
8828 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8829 // shuffle(concat(v1, v2), undef)
8830 SDValue Op0 = N->getOperand(0);
8831 SDValue Op1 = N->getOperand(1);
8832 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8833 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8834 Op0.getNumOperands() != 2 ||
8835 Op1.getNumOperands() != 2)
8836 return SDValue();
8837 SDValue Concat0Op1 = Op0.getOperand(1);
8838 SDValue Concat1Op1 = Op1.getOperand(1);
8839 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8840 Concat1Op1.getOpcode() != ISD::UNDEF)
8841 return SDValue();
8842 // Skip the transformation if any of the types are illegal.
8843 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8844 EVT VT = N->getValueType(0);
8845 if (!TLI.isTypeLegal(VT) ||
8846 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8847 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8848 return SDValue();
8849
Andrew Trickef9de2a2013-05-25 02:42:55 +00008850 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008851 Op0.getOperand(0), Op1.getOperand(0));
8852 // Translate the shuffle mask.
8853 SmallVector<int, 16> NewMask;
8854 unsigned NumElts = VT.getVectorNumElements();
8855 unsigned HalfElts = NumElts/2;
8856 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8857 for (unsigned n = 0; n < NumElts; ++n) {
8858 int MaskElt = SVN->getMaskElt(n);
8859 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00008860 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00008861 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00008862 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00008863 NewElt = HalfElts + MaskElt - NumElts;
8864 NewMask.push_back(NewElt);
8865 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00008866 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008867 DAG.getUNDEF(VT), NewMask.data());
8868}
8869
Renato Golin2a5c0a52015-02-04 10:11:59 +00008870/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8871/// NEON load/store intrinsics to merge base address updates.
Bob Wilson06fce872011-02-07 17:43:21 +00008872static SDValue CombineBaseUpdate(SDNode *N,
8873 TargetLowering::DAGCombinerInfo &DCI) {
Renato Golin2a5c0a52015-02-04 10:11:59 +00008874 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8875 return SDValue();
8876
Bob Wilson06fce872011-02-07 17:43:21 +00008877 SelectionDAG &DAG = DCI.DAG;
8878 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8879 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Renato Golin2a5c0a52015-02-04 10:11:59 +00008880 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +00008881 SDValue Addr = N->getOperand(AddrOpIdx);
8882
8883 // Search for a use of the address operand that is an increment.
8884 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8885 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8886 SDNode *User = *UI;
8887 if (User->getOpcode() != ISD::ADD ||
8888 UI.getUse().getResNo() != Addr.getResNo())
8889 continue;
8890
8891 // Check that the add is independent of the load/store. Otherwise, folding
8892 // it would create a cycle.
8893 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8894 continue;
8895
8896 // Find the new opcode for the updating load/store.
8897 bool isLoad = true;
8898 bool isLaneOp = false;
8899 unsigned NewOpc = 0;
8900 unsigned NumVecs = 0;
8901 if (isIntrinsic) {
8902 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8903 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00008904 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008905 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8906 NumVecs = 1; break;
8907 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8908 NumVecs = 2; break;
8909 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8910 NumVecs = 3; break;
8911 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8912 NumVecs = 4; break;
8913 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8914 NumVecs = 2; isLaneOp = true; break;
8915 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8916 NumVecs = 3; isLaneOp = true; break;
8917 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8918 NumVecs = 4; isLaneOp = true; break;
8919 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8920 NumVecs = 1; isLoad = false; break;
8921 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8922 NumVecs = 2; isLoad = false; break;
8923 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8924 NumVecs = 3; isLoad = false; break;
8925 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8926 NumVecs = 4; isLoad = false; break;
8927 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8928 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8929 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8930 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8931 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8932 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8933 }
8934 } else {
8935 isLaneOp = true;
8936 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00008937 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008938 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8939 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8940 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8941 }
8942 }
8943
8944 // Find the size of memory referenced by the load/store.
8945 EVT VecTy;
8946 if (isLoad)
8947 VecTy = N->getValueType(0);
Ahmed Bougacha0cb86162014-12-13 23:22:12 +00008948 else
Renato Golin2a5c0a52015-02-04 10:11:59 +00008949 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Bob Wilson06fce872011-02-07 17:43:21 +00008950 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8951 if (isLaneOp)
8952 NumBytes /= VecTy.getVectorNumElements();
8953
8954 // If the increment is a constant, it must match the memory ref size.
8955 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8956 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8957 uint64_t IncVal = CInc->getZExtValue();
8958 if (IncVal != NumBytes)
8959 continue;
8960 } else if (NumBytes >= 3 * 16) {
8961 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8962 // separate instructions that make it harder to use a non-constant update.
8963 continue;
8964 }
8965
8966 // Create the new updating load/store node.
8967 EVT Tys[6];
8968 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8969 unsigned n;
8970 for (n = 0; n < NumResultVecs; ++n)
Renato Golin2a5c0a52015-02-04 10:11:59 +00008971 Tys[n] = VecTy;
Bob Wilson06fce872011-02-07 17:43:21 +00008972 Tys[n++] = MVT::i32;
8973 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00008974 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Bob Wilson06fce872011-02-07 17:43:21 +00008975 SmallVector<SDValue, 8> Ops;
8976 Ops.push_back(N->getOperand(0)); // incoming chain
8977 Ops.push_back(N->getOperand(AddrOpIdx));
8978 Ops.push_back(Inc);
Renato Golin2a5c0a52015-02-04 10:11:59 +00008979 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8980 Ops.push_back(N->getOperand(i));
Bob Wilson06fce872011-02-07 17:43:21 +00008981 }
Renato Golin2a5c0a52015-02-04 10:11:59 +00008982 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008983 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys,
Renato Golin2a5c0a52015-02-04 10:11:59 +00008984 Ops, MemInt->getMemoryVT(),
Bob Wilson06fce872011-02-07 17:43:21 +00008985 MemInt->getMemOperand());
8986
8987 // Update the uses.
8988 std::vector<SDValue> NewResults;
8989 for (unsigned i = 0; i < NumResultVecs; ++i) {
8990 NewResults.push_back(SDValue(UpdN.getNode(), i));
8991 }
8992 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8993 DCI.CombineTo(N, NewResults);
8994 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8995
8996 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00008997 }
Bob Wilson06fce872011-02-07 17:43:21 +00008998 return SDValue();
8999}
9000
Bob Wilson2d790df2010-11-28 06:51:26 +00009001/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
9002/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
9003/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
9004/// return true.
9005static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9006 SelectionDAG &DAG = DCI.DAG;
9007 EVT VT = N->getValueType(0);
9008 // vldN-dup instructions only support 64-bit vectors for N > 1.
9009 if (!VT.is64BitVector())
9010 return false;
9011
9012 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
9013 SDNode *VLD = N->getOperand(0).getNode();
9014 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
9015 return false;
9016 unsigned NumVecs = 0;
9017 unsigned NewOpc = 0;
9018 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
9019 if (IntNo == Intrinsic::arm_neon_vld2lane) {
9020 NumVecs = 2;
9021 NewOpc = ARMISD::VLD2DUP;
9022 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
9023 NumVecs = 3;
9024 NewOpc = ARMISD::VLD3DUP;
9025 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
9026 NumVecs = 4;
9027 NewOpc = ARMISD::VLD4DUP;
9028 } else {
9029 return false;
9030 }
9031
9032 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9033 // numbers match the load.
9034 unsigned VLDLaneNo =
9035 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9036 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9037 UI != UE; ++UI) {
9038 // Ignore uses of the chain result.
9039 if (UI.getUse().getResNo() == NumVecs)
9040 continue;
9041 SDNode *User = *UI;
9042 if (User->getOpcode() != ARMISD::VDUPLANE ||
9043 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9044 return false;
9045 }
9046
9047 // Create the vldN-dup node.
9048 EVT Tys[5];
9049 unsigned n;
9050 for (n = 0; n < NumVecs; ++n)
9051 Tys[n] = VT;
9052 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009053 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00009054 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9055 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009056 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009057 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00009058 VLDMemInt->getMemOperand());
9059
9060 // Update the uses.
9061 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9062 UI != UE; ++UI) {
9063 unsigned ResNo = UI.getUse().getResNo();
9064 // Ignore uses of the chain result.
9065 if (ResNo == NumVecs)
9066 continue;
9067 SDNode *User = *UI;
9068 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9069 }
9070
9071 // Now the vldN-lane intrinsic is dead except for its chain result.
9072 // Update uses of the chain.
9073 std::vector<SDValue> VLDDupResults;
9074 for (unsigned n = 0; n < NumVecs; ++n)
9075 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9076 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9077 DCI.CombineTo(VLD, VLDDupResults);
9078
9079 return true;
9080}
9081
Bob Wilson103a0dc2010-07-14 01:22:12 +00009082/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9083/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009084static SDValue PerformVDUPLANECombine(SDNode *N,
9085 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009086 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009087
Bob Wilson2d790df2010-11-28 06:51:26 +00009088 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9089 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9090 if (CombineVLDDUP(N, DCI))
9091 return SDValue(N, 0);
9092
9093 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9094 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009095 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009096 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009097 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009098 return SDValue();
9099
9100 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9101 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9102 // The canonical VMOV for a zero vector uses a 32-bit element size.
9103 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9104 unsigned EltBits;
9105 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9106 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00009107 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009108 if (EltSize > VT.getVectorElementType().getSizeInBits())
9109 return SDValue();
9110
Andrew Trickef9de2a2013-05-25 02:42:55 +00009111 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009112}
9113
Ahmed Bougacha23167462014-12-09 21:26:53 +00009114/// PerformSTORECombine - Target-specific dag combine xforms for
9115/// ISD::STORE.
9116static SDValue PerformSTORECombine(SDNode *N,
9117 TargetLowering::DAGCombinerInfo &DCI) {
9118 StoreSDNode *St = cast<StoreSDNode>(N);
9119 if (St->isVolatile())
9120 return SDValue();
9121
9122 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
9123 // pack all of the elements in one place. Next, store to memory in fewer
9124 // chunks.
9125 SDValue StVal = St->getValue();
9126 EVT VT = StVal.getValueType();
9127 if (St->isTruncatingStore() && VT.isVector()) {
9128 SelectionDAG &DAG = DCI.DAG;
9129 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9130 EVT StVT = St->getMemoryVT();
9131 unsigned NumElems = VT.getVectorNumElements();
9132 assert(StVT != VT && "Cannot truncate to the same type");
9133 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
9134 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
9135
9136 // From, To sizes and ElemCount must be pow of two
9137 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
9138
9139 // We are going to use the original vector elt for storing.
9140 // Accumulated smaller vector elements must be a multiple of the store size.
9141 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
9142
9143 unsigned SizeRatio = FromEltSz / ToEltSz;
9144 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
9145
9146 // Create a type on which we perform the shuffle.
9147 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
9148 NumElems*SizeRatio);
9149 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
9150
9151 SDLoc DL(St);
9152 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
9153 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
9154 for (unsigned i = 0; i < NumElems; ++i)
9155 ShuffleVec[i] = TLI.isBigEndian() ? (i+1) * SizeRatio - 1 : i * SizeRatio;
9156
9157 // Can't shuffle using an illegal type.
9158 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
9159
9160 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
9161 DAG.getUNDEF(WideVec.getValueType()),
9162 ShuffleVec.data());
9163 // At this point all of the data is stored at the bottom of the
9164 // register. We now need to save it to mem.
9165
9166 // Find the largest store unit
9167 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +00009168 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +00009169 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
9170 StoreType = Tp;
9171 }
9172 // Didn't find a legal store type.
9173 if (!TLI.isTypeLegal(StoreType))
9174 return SDValue();
9175
9176 // Bitcast the original vector into a vector of store-size units
9177 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
9178 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
9179 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
9180 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
9181 SmallVector<SDValue, 8> Chains;
9182 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
9183 TLI.getPointerTy());
9184 SDValue BasePtr = St->getBasePtr();
9185
9186 // Perform one or more big stores into memory.
9187 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
9188 for (unsigned I = 0; I < E; I++) {
9189 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
9190 StoreType, ShuffWide,
9191 DAG.getIntPtrConstant(I));
9192 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
9193 St->getPointerInfo(), St->isVolatile(),
9194 St->isNonTemporal(), St->getAlignment());
9195 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
9196 Increment);
9197 Chains.push_back(Ch);
9198 }
9199 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
9200 }
9201
9202 if (!ISD::isNormalStore(St))
9203 return SDValue();
9204
9205 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
9206 // ARM stores of arguments in the same cache line.
9207 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
9208 StVal.getNode()->hasOneUse()) {
9209 SelectionDAG &DAG = DCI.DAG;
9210 bool isBigEndian = DAG.getTargetLoweringInfo().isBigEndian();
9211 SDLoc DL(St);
9212 SDValue BasePtr = St->getBasePtr();
9213 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
9214 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
9215 BasePtr, St->getPointerInfo(), St->isVolatile(),
9216 St->isNonTemporal(), St->getAlignment());
9217
9218 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
9219 DAG.getConstant(4, MVT::i32));
9220 return DAG.getStore(NewST1.getValue(0), DL,
9221 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
9222 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
9223 St->isNonTemporal(),
9224 std::min(4U, St->getAlignment() / 2));
9225 }
9226
9227 if (StVal.getValueType() == MVT::i64 &&
9228 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9229
9230 // Bitcast an i64 store extracted from a vector to f64.
9231 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9232 SelectionDAG &DAG = DCI.DAG;
9233 SDLoc dl(StVal);
9234 SDValue IntVec = StVal.getOperand(0);
9235 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9236 IntVec.getValueType().getVectorNumElements());
9237 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
9238 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
9239 Vec, StVal.getOperand(1));
9240 dl = SDLoc(N);
9241 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
9242 // Make the DAGCombiner fold the bitcasts.
9243 DCI.AddToWorklist(Vec.getNode());
9244 DCI.AddToWorklist(ExtElt.getNode());
9245 DCI.AddToWorklist(V.getNode());
9246 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
9247 St->getPointerInfo(), St->isVolatile(),
9248 St->isNonTemporal(), St->getAlignment(),
9249 St->getAAInfo());
9250 }
9251
9252 return SDValue();
9253}
9254
Eric Christopher1b8b94192011-06-29 21:10:36 +00009255// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00009256// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
9257static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
9258{
Chad Rosier6b610b32011-06-28 17:26:57 +00009259 integerPart cN;
9260 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009261 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
9262 I != E; I++) {
9263 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
9264 if (!C)
9265 return false;
9266
Eric Christopher1b8b94192011-06-29 21:10:36 +00009267 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009268 APFloat APF = C->getValueAPF();
9269 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
9270 != APFloat::opOK || !isExact)
9271 return false;
9272
9273 c0 = (I == 0) ? cN : c0;
9274 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
9275 return false;
9276 }
9277 C = c0;
9278 return true;
9279}
9280
9281/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
9282/// can replace combinations of VMUL and VCVT (floating-point to integer)
9283/// when the VMUL has a constant operand that is a power of 2.
9284///
9285/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9286/// vmul.f32 d16, d17, d16
9287/// vcvt.s32.f32 d16, d16
9288/// becomes:
9289/// vcvt.s32.f32 d16, d16, #3
9290static SDValue PerformVCVTCombine(SDNode *N,
9291 TargetLowering::DAGCombinerInfo &DCI,
9292 const ARMSubtarget *Subtarget) {
9293 SelectionDAG &DAG = DCI.DAG;
9294 SDValue Op = N->getOperand(0);
9295
9296 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
9297 Op.getOpcode() != ISD::FMUL)
9298 return SDValue();
9299
9300 uint64_t C;
9301 SDValue N0 = Op->getOperand(0);
9302 SDValue ConstVec = Op->getOperand(1);
9303 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
9304
Eric Christopher1b8b94192011-06-29 21:10:36 +00009305 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00009306 !isConstVecPow2(ConstVec, isSigned, C))
9307 return SDValue();
9308
Tim Northover7cbc2152013-06-28 15:29:25 +00009309 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
9310 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Bradley Smithececb7f2014-12-16 10:59:27 +00009311 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9312 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32 ||
9313 NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +00009314 // These instructions only exist converting from f32 to i32. We can handle
9315 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +00009316 // be lossy. We also can't handle more then 4 lanes, since these intructions
9317 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +00009318 return SDValue();
9319 }
9320
Chad Rosierfa8d8932011-06-24 19:23:04 +00009321 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9322 Intrinsic::arm_neon_vcvtfp2fxu;
Tim Northover7cbc2152013-06-28 15:29:25 +00009323 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
9324 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9325 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
9326 DAG.getConstant(Log2_64(C), MVT::i32));
9327
9328 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9329 FixConv = DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), FixConv);
9330
9331 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009332}
9333
9334/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9335/// can replace combinations of VCVT (integer to floating-point) and VDIV
9336/// when the VDIV has a constant operand that is a power of 2.
9337///
9338/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9339/// vcvt.f32.s32 d16, d16
9340/// vdiv.f32 d16, d17, d16
9341/// becomes:
9342/// vcvt.f32.s32 d16, d16, #3
9343static SDValue PerformVDIVCombine(SDNode *N,
9344 TargetLowering::DAGCombinerInfo &DCI,
9345 const ARMSubtarget *Subtarget) {
9346 SelectionDAG &DAG = DCI.DAG;
9347 SDValue Op = N->getOperand(0);
9348 unsigned OpOpcode = Op.getNode()->getOpcode();
9349
9350 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
9351 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9352 return SDValue();
9353
9354 uint64_t C;
9355 SDValue ConstVec = N->getOperand(1);
9356 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
9357
9358 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9359 !isConstVecPow2(ConstVec, isSigned, C))
9360 return SDValue();
9361
Tim Northover7cbc2152013-06-28 15:29:25 +00009362 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
9363 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
9364 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9365 // These instructions only exist converting from i32 to f32. We can handle
9366 // smaller integers by generating an extra extend, but larger ones would
9367 // be lossy.
9368 return SDValue();
9369 }
9370
9371 SDValue ConvInput = Op.getOperand(0);
9372 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9373 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9374 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
9375 SDLoc(N), NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9376 ConvInput);
9377
Eric Christopher1b8b94192011-06-29 21:10:36 +00009378 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00009379 Intrinsic::arm_neon_vcvtfxu2fp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009380 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
Chad Rosierfa8d8932011-06-24 19:23:04 +00009381 Op.getValueType(),
Eric Christopher1b8b94192011-06-29 21:10:36 +00009382 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Tim Northover7cbc2152013-06-28 15:29:25 +00009383 ConvInput, DAG.getConstant(Log2_64(C), MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00009384}
9385
9386/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00009387/// operand of a vector shift operation, where all the elements of the
9388/// build_vector must have the same constant integer value.
9389static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9390 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00009391 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00009392 Op = Op.getOperand(0);
9393 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9394 APInt SplatBits, SplatUndef;
9395 unsigned SplatBitSize;
9396 bool HasAnyUndefs;
9397 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9398 HasAnyUndefs, ElementBits) ||
9399 SplatBitSize > ElementBits)
9400 return false;
9401 Cnt = SplatBits.getSExtValue();
9402 return true;
9403}
9404
9405/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9406/// operand of a vector shift left operation. That value must be in the range:
9407/// 0 <= Value < ElementBits for a left shift; or
9408/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009409static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009410 assert(VT.isVector() && "vector shift count is not a vector type");
9411 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9412 if (! getVShiftImm(Op, ElementBits, Cnt))
9413 return false;
9414 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9415}
9416
9417/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9418/// operand of a vector shift right operation. For a shift opcode, the value
9419/// is positive, but for an intrinsic the value count must be negative. The
9420/// absolute value must be in the range:
9421/// 1 <= |Value| <= ElementBits for a right shift; or
9422/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009423static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00009424 int64_t &Cnt) {
9425 assert(VT.isVector() && "vector shift count is not a vector type");
9426 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9427 if (! getVShiftImm(Op, ElementBits, Cnt))
9428 return false;
9429 if (isIntrinsic)
9430 Cnt = -Cnt;
9431 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9432}
9433
9434/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9435static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9436 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9437 switch (IntNo) {
9438 default:
9439 // Don't do anything for most intrinsics.
9440 break;
9441
9442 // Vector shifts: check for immediate versions and lower them.
9443 // Note: This is done during DAG combining instead of DAG legalizing because
9444 // the build_vectors for 64-bit vector element shift counts are generally
9445 // not legal, and it is hard to see their values after they get legalized to
9446 // loads from a constant pool.
9447 case Intrinsic::arm_neon_vshifts:
9448 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009449 case Intrinsic::arm_neon_vrshifts:
9450 case Intrinsic::arm_neon_vrshiftu:
9451 case Intrinsic::arm_neon_vrshiftn:
9452 case Intrinsic::arm_neon_vqshifts:
9453 case Intrinsic::arm_neon_vqshiftu:
9454 case Intrinsic::arm_neon_vqshiftsu:
9455 case Intrinsic::arm_neon_vqshiftns:
9456 case Intrinsic::arm_neon_vqshiftnu:
9457 case Intrinsic::arm_neon_vqshiftnsu:
9458 case Intrinsic::arm_neon_vqrshiftns:
9459 case Intrinsic::arm_neon_vqrshiftnu:
9460 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009461 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009462 int64_t Cnt;
9463 unsigned VShiftOpc = 0;
9464
9465 switch (IntNo) {
9466 case Intrinsic::arm_neon_vshifts:
9467 case Intrinsic::arm_neon_vshiftu:
9468 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9469 VShiftOpc = ARMISD::VSHL;
9470 break;
9471 }
9472 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9473 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9474 ARMISD::VSHRs : ARMISD::VSHRu);
9475 break;
9476 }
9477 return SDValue();
9478
Bob Wilson2e076c42009-06-22 23:27:02 +00009479 case Intrinsic::arm_neon_vrshifts:
9480 case Intrinsic::arm_neon_vrshiftu:
9481 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9482 break;
9483 return SDValue();
9484
9485 case Intrinsic::arm_neon_vqshifts:
9486 case Intrinsic::arm_neon_vqshiftu:
9487 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9488 break;
9489 return SDValue();
9490
9491 case Intrinsic::arm_neon_vqshiftsu:
9492 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9493 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +00009494 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009495
Bob Wilson2e076c42009-06-22 23:27:02 +00009496 case Intrinsic::arm_neon_vrshiftn:
9497 case Intrinsic::arm_neon_vqshiftns:
9498 case Intrinsic::arm_neon_vqshiftnu:
9499 case Intrinsic::arm_neon_vqshiftnsu:
9500 case Intrinsic::arm_neon_vqrshiftns:
9501 case Intrinsic::arm_neon_vqrshiftnu:
9502 case Intrinsic::arm_neon_vqrshiftnsu:
9503 // Narrowing shifts require an immediate right shift.
9504 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9505 break;
Jim Grosbach84511e12010-06-02 21:53:11 +00009506 llvm_unreachable("invalid shift count for narrowing vector shift "
9507 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009508
9509 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00009510 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00009511 }
9512
9513 switch (IntNo) {
9514 case Intrinsic::arm_neon_vshifts:
9515 case Intrinsic::arm_neon_vshiftu:
9516 // Opcode already set above.
9517 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00009518 case Intrinsic::arm_neon_vrshifts:
9519 VShiftOpc = ARMISD::VRSHRs; break;
9520 case Intrinsic::arm_neon_vrshiftu:
9521 VShiftOpc = ARMISD::VRSHRu; break;
9522 case Intrinsic::arm_neon_vrshiftn:
9523 VShiftOpc = ARMISD::VRSHRN; break;
9524 case Intrinsic::arm_neon_vqshifts:
9525 VShiftOpc = ARMISD::VQSHLs; break;
9526 case Intrinsic::arm_neon_vqshiftu:
9527 VShiftOpc = ARMISD::VQSHLu; break;
9528 case Intrinsic::arm_neon_vqshiftsu:
9529 VShiftOpc = ARMISD::VQSHLsu; break;
9530 case Intrinsic::arm_neon_vqshiftns:
9531 VShiftOpc = ARMISD::VQSHRNs; break;
9532 case Intrinsic::arm_neon_vqshiftnu:
9533 VShiftOpc = ARMISD::VQSHRNu; break;
9534 case Intrinsic::arm_neon_vqshiftnsu:
9535 VShiftOpc = ARMISD::VQSHRNsu; break;
9536 case Intrinsic::arm_neon_vqrshiftns:
9537 VShiftOpc = ARMISD::VQRSHRNs; break;
9538 case Intrinsic::arm_neon_vqrshiftnu:
9539 VShiftOpc = ARMISD::VQRSHRNu; break;
9540 case Intrinsic::arm_neon_vqrshiftnsu:
9541 VShiftOpc = ARMISD::VQRSHRNsu; break;
9542 }
9543
Andrew Trickef9de2a2013-05-25 02:42:55 +00009544 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009545 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009546 }
9547
9548 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009549 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009550 int64_t Cnt;
9551 unsigned VShiftOpc = 0;
9552
9553 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9554 VShiftOpc = ARMISD::VSLI;
9555 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9556 VShiftOpc = ARMISD::VSRI;
9557 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009558 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009559 }
9560
Andrew Trickef9de2a2013-05-25 02:42:55 +00009561 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +00009562 N->getOperand(1), N->getOperand(2),
Owen Anderson9f944592009-08-11 20:47:22 +00009563 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009564 }
9565
9566 case Intrinsic::arm_neon_vqrshifts:
9567 case Intrinsic::arm_neon_vqrshiftu:
9568 // No immediate versions of these to check for.
9569 break;
9570 }
9571
9572 return SDValue();
9573}
9574
9575/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9576/// lowers them. As with the vector shift intrinsics, this is done during DAG
9577/// combining instead of DAG legalizing because the build_vectors for 64-bit
9578/// vector element shift counts are generally not legal, and it is hard to see
9579/// their values after they get legalized to loads from a constant pool.
9580static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9581 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009582 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +00009583 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9584 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9585 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9586 SDValue N1 = N->getOperand(1);
9587 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9588 SDValue N0 = N->getOperand(0);
9589 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9590 DAG.MaskedValueIsZero(N0.getOperand(0),
9591 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009592 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +00009593 }
9594 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009595
9596 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +00009597 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9598 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +00009599 return SDValue();
9600
9601 assert(ST->hasNEON() && "unexpected vector shift");
9602 int64_t Cnt;
9603
9604 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009605 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009606
9607 case ISD::SHL:
9608 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009609 return DAG.getNode(ARMISD::VSHL, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009610 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009611 break;
9612
9613 case ISD::SRA:
9614 case ISD::SRL:
9615 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9616 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9617 ARMISD::VSHRs : ARMISD::VSHRu);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009618 return DAG.getNode(VShiftOpc, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009619 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009620 }
9621 }
9622 return SDValue();
9623}
9624
9625/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9626/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9627static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9628 const ARMSubtarget *ST) {
9629 SDValue N0 = N->getOperand(0);
9630
9631 // Check for sign- and zero-extensions of vector extract operations of 8-
9632 // and 16-bit vector elements. NEON supports these directly. They are
9633 // handled during DAG combining because type legalization will promote them
9634 // to 32-bit types and it is messy to recognize the operations after that.
9635 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9636 SDValue Vec = N0.getOperand(0);
9637 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009638 EVT VT = N->getValueType(0);
9639 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009640 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9641
Owen Anderson9f944592009-08-11 20:47:22 +00009642 if (VT == MVT::i32 &&
9643 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +00009644 TLI.isTypeLegal(Vec.getValueType()) &&
9645 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009646
9647 unsigned Opc = 0;
9648 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009649 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009650 case ISD::SIGN_EXTEND:
9651 Opc = ARMISD::VGETLANEs;
9652 break;
9653 case ISD::ZERO_EXTEND:
9654 case ISD::ANY_EXTEND:
9655 Opc = ARMISD::VGETLANEu;
9656 break;
9657 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009658 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +00009659 }
9660 }
9661
9662 return SDValue();
9663}
9664
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009665/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9666/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9667static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9668 const ARMSubtarget *ST) {
9669 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng55f0c6b2010-07-15 22:07:12 +00009670 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009671 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9672 // a NaN; only do the transformation when it matches that behavior.
9673
9674 // For now only do this when using NEON for FP operations; if using VFP, it
9675 // is not obvious that the benefit outweighs the cost of switching to the
9676 // NEON pipeline.
9677 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9678 N->getValueType(0) != MVT::f32)
9679 return SDValue();
9680
9681 SDValue CondLHS = N->getOperand(0);
9682 SDValue CondRHS = N->getOperand(1);
9683 SDValue LHS = N->getOperand(2);
9684 SDValue RHS = N->getOperand(3);
9685 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9686
9687 unsigned Opcode = 0;
9688 bool IsReversed;
Bob Wilsonba8ac742010-02-24 22:15:53 +00009689 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009690 IsReversed = false; // x CC y ? x : y
Bob Wilsonba8ac742010-02-24 22:15:53 +00009691 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009692 IsReversed = true ; // x CC y ? y : x
9693 } else {
9694 return SDValue();
9695 }
9696
Bob Wilsonba8ac742010-02-24 22:15:53 +00009697 bool IsUnordered;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009698 switch (CC) {
9699 default: break;
9700 case ISD::SETOLT:
9701 case ISD::SETOLE:
9702 case ISD::SETLT:
9703 case ISD::SETLE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009704 case ISD::SETULT:
9705 case ISD::SETULE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009706 // If LHS is NaN, an ordered comparison will be false and the result will
9707 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9708 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9709 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9710 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9711 break;
9712 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9713 // will return -0, so vmin can only be used for unsafe math or if one of
9714 // the operands is known to be nonzero.
9715 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009716 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009717 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9718 break;
9719 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009720 break;
9721
9722 case ISD::SETOGT:
9723 case ISD::SETOGE:
9724 case ISD::SETGT:
9725 case ISD::SETGE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009726 case ISD::SETUGT:
9727 case ISD::SETUGE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009728 // If LHS is NaN, an ordered comparison will be false and the result will
9729 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9730 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9731 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9732 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9733 break;
9734 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9735 // will return +0, so vmax can only be used for unsafe math or if one of
9736 // the operands is known to be nonzero.
9737 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009738 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009739 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9740 break;
9741 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009742 break;
9743 }
9744
9745 if (!Opcode)
9746 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009747 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009748}
9749
Evan Chengf863e3f2011-07-13 00:42:17 +00009750/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9751SDValue
9752ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9753 SDValue Cmp = N->getOperand(4);
9754 if (Cmp.getOpcode() != ARMISD::CMPZ)
9755 // Only looking at EQ and NE cases.
9756 return SDValue();
9757
9758 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009759 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +00009760 SDValue LHS = Cmp.getOperand(0);
9761 SDValue RHS = Cmp.getOperand(1);
9762 SDValue FalseVal = N->getOperand(0);
9763 SDValue TrueVal = N->getOperand(1);
9764 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +00009765 ARMCC::CondCodes CC =
9766 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +00009767
9768 // Simplify
9769 // mov r1, r0
9770 // cmp r1, x
9771 // mov r0, y
9772 // moveq r0, x
9773 // to
9774 // cmp r0, x
9775 // movne r0, y
9776 //
9777 // mov r1, r0
9778 // cmp r1, x
9779 // mov r0, x
9780 // movne r0, y
9781 // to
9782 // cmp r0, x
9783 // movne r0, y
9784 /// FIXME: Turn this into a target neutral optimization?
9785 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +00009786 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +00009787 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9788 N->getOperand(3), Cmp);
9789 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9790 SDValue ARMcc;
9791 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9792 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9793 N->getOperand(3), NewCmp);
9794 }
9795
9796 if (Res.getNode()) {
9797 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00009798 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +00009799 // Capture demanded bits information that would be otherwise lost.
9800 if (KnownZero == 0xfffffffe)
9801 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9802 DAG.getValueType(MVT::i1));
9803 else if (KnownZero == 0xffffff00)
9804 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9805 DAG.getValueType(MVT::i8));
9806 else if (KnownZero == 0xffff0000)
9807 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9808 DAG.getValueType(MVT::i16));
9809 }
9810
9811 return Res;
9812}
9813
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009814SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +00009815 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009816 switch (N->getOpcode()) {
9817 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009818 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +00009819 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009820 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009821 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009822 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +00009823 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9824 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +00009825 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009826 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +00009827 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009828 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009829 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009830 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +00009831 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +00009832 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009833 case ISD::FP_TO_SINT:
9834 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9835 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009836 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00009837 case ISD::SHL:
9838 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009839 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00009840 case ISD::SIGN_EXTEND:
9841 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009842 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9843 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +00009844 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson06fce872011-02-07 17:43:21 +00009845 case ARMISD::VLD2DUP:
9846 case ARMISD::VLD3DUP:
9847 case ARMISD::VLD4DUP:
Renato Golin2a5c0a52015-02-04 10:11:59 +00009848 return CombineBaseUpdate(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009849 case ARMISD::BUILD_VECTOR:
9850 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009851 case ISD::INTRINSIC_VOID:
9852 case ISD::INTRINSIC_W_CHAIN:
9853 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9854 case Intrinsic::arm_neon_vld1:
9855 case Intrinsic::arm_neon_vld2:
9856 case Intrinsic::arm_neon_vld3:
9857 case Intrinsic::arm_neon_vld4:
9858 case Intrinsic::arm_neon_vld2lane:
9859 case Intrinsic::arm_neon_vld3lane:
9860 case Intrinsic::arm_neon_vld4lane:
9861 case Intrinsic::arm_neon_vst1:
9862 case Intrinsic::arm_neon_vst2:
9863 case Intrinsic::arm_neon_vst3:
9864 case Intrinsic::arm_neon_vst4:
9865 case Intrinsic::arm_neon_vst2lane:
9866 case Intrinsic::arm_neon_vst3lane:
9867 case Intrinsic::arm_neon_vst4lane:
Renato Golin2a5c0a52015-02-04 10:11:59 +00009868 return CombineBaseUpdate(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009869 default: break;
9870 }
9871 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009872 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009873 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009874}
9875
Evan Chengd42641c2011-02-02 01:06:55 +00009876bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9877 EVT VT) const {
9878 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9879}
9880
Matt Arsenault6f2a5262014-07-27 17:46:40 +00009881bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
9882 unsigned,
9883 unsigned,
9884 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009885 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +00009886 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009887
9888 switch (VT.getSimpleVT().SimpleTy) {
9889 default:
9890 return false;
9891 case MVT::i8:
9892 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009893 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009894 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +00009895 if (AllowsUnaligned) {
9896 if (Fast)
9897 *Fast = Subtarget->hasV7Ops();
9898 return true;
9899 }
9900 return false;
9901 }
Evan Chengeec6bc62012-08-15 17:44:53 +00009902 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009903 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009904 // For any little-endian targets with neon, we can support unaligned ld/st
9905 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +00009906 // A big-endian target may also explicitly support unaligned accesses
Evan Cheng79e2ca92012-12-10 23:21:26 +00009907 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
9908 if (Fast)
9909 *Fast = true;
9910 return true;
9911 }
9912 return false;
9913 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009914 }
9915}
9916
Lang Hames9929c422011-11-02 22:52:45 +00009917static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9918 unsigned AlignCheck) {
9919 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9920 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9921}
9922
9923EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9924 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00009925 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +00009926 bool MemcpyStrSrc,
9927 MachineFunction &MF) const {
9928 const Function *F = MF.getFunction();
9929
9930 // See if we can use NEON instructions for this...
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00009931 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
9932 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009933 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +00009934 if (Size >= 16 &&
9935 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +00009936 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009937 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +00009938 } else if (Size >= 8 &&
9939 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +00009940 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
9941 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009942 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +00009943 }
9944 }
9945
Lang Hamesb85fcd02011-11-08 18:56:23 +00009946 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +00009947 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009948 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +00009949 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009950 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +00009951
Lang Hames9929c422011-11-02 22:52:45 +00009952 // Let the target-independent logic figure it out.
9953 return MVT::Other;
9954}
9955
Evan Cheng9ec512d2012-12-06 19:13:27 +00009956bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
9957 if (Val.getOpcode() != ISD::LOAD)
9958 return false;
9959
9960 EVT VT1 = Val.getValueType();
9961 if (!VT1.isSimple() || !VT1.isInteger() ||
9962 !VT2.isSimple() || !VT2.isInteger())
9963 return false;
9964
9965 switch (VT1.getSimpleVT().SimpleTy) {
9966 default: break;
9967 case MVT::i1:
9968 case MVT::i8:
9969 case MVT::i16:
9970 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
9971 return true;
9972 }
9973
9974 return false;
9975}
9976
Tim Northovercc2e9032013-08-06 13:58:03 +00009977bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
9978 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
9979 return false;
9980
9981 if (!isTypeLegal(EVT::getEVT(Ty1)))
9982 return false;
9983
9984 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
9985
9986 // Assuming the caller doesn't have a zeroext or signext return parameter,
9987 // truncation all the way down to i1 is valid.
9988 return true;
9989}
9990
9991
Evan Chengdc49a8d2009-08-14 20:09:37 +00009992static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9993 if (V < 0)
9994 return false;
9995
9996 unsigned Scale = 1;
9997 switch (VT.getSimpleVT().SimpleTy) {
9998 default: return false;
9999 case MVT::i1:
10000 case MVT::i8:
10001 // Scale == 1;
10002 break;
10003 case MVT::i16:
10004 // Scale == 2;
10005 Scale = 2;
10006 break;
10007 case MVT::i32:
10008 // Scale == 4;
10009 Scale = 4;
10010 break;
10011 }
10012
10013 if ((V & (Scale - 1)) != 0)
10014 return false;
10015 V /= Scale;
10016 return V == (V & ((1LL << 5) - 1));
10017}
10018
10019static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
10020 const ARMSubtarget *Subtarget) {
10021 bool isNeg = false;
10022 if (V < 0) {
10023 isNeg = true;
10024 V = - V;
10025 }
10026
10027 switch (VT.getSimpleVT().SimpleTy) {
10028 default: return false;
10029 case MVT::i1:
10030 case MVT::i8:
10031 case MVT::i16:
10032 case MVT::i32:
10033 // + imm12 or - imm8
10034 if (isNeg)
10035 return V == (V & ((1LL << 8) - 1));
10036 return V == (V & ((1LL << 12) - 1));
10037 case MVT::f32:
10038 case MVT::f64:
10039 // Same as ARM mode. FIXME: NEON?
10040 if (!Subtarget->hasVFP2())
10041 return false;
10042 if ((V & 3) != 0)
10043 return false;
10044 V >>= 2;
10045 return V == (V & ((1LL << 8) - 1));
10046 }
10047}
10048
Evan Cheng2150b922007-03-12 23:30:29 +000010049/// isLegalAddressImmediate - Return true if the integer value can be used
10050/// as the offset of the target addressing mode for load / store of the
10051/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010052static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010053 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000010054 if (V == 0)
10055 return true;
10056
Evan Chengce5dfb62009-03-09 19:15:00 +000010057 if (!VT.isSimple())
10058 return false;
10059
Evan Chengdc49a8d2009-08-14 20:09:37 +000010060 if (Subtarget->isThumb1Only())
10061 return isLegalT1AddressImmediate(V, VT);
10062 else if (Subtarget->isThumb2())
10063 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000010064
Evan Chengdc49a8d2009-08-14 20:09:37 +000010065 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000010066 if (V < 0)
10067 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000010068 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000010069 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010070 case MVT::i1:
10071 case MVT::i8:
10072 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000010073 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010074 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010075 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000010076 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010077 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010078 case MVT::f32:
10079 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010080 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000010081 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000010082 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000010083 return false;
10084 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010085 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000010086 }
Evan Cheng10043e22007-01-19 07:51:42 +000010087}
10088
Evan Chengdc49a8d2009-08-14 20:09:37 +000010089bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
10090 EVT VT) const {
10091 int Scale = AM.Scale;
10092 if (Scale < 0)
10093 return false;
10094
10095 switch (VT.getSimpleVT().SimpleTy) {
10096 default: return false;
10097 case MVT::i1:
10098 case MVT::i8:
10099 case MVT::i16:
10100 case MVT::i32:
10101 if (Scale == 1)
10102 return true;
10103 // r + r << imm
10104 Scale = Scale & ~1;
10105 return Scale == 2 || Scale == 4 || Scale == 8;
10106 case MVT::i64:
10107 // r + r
10108 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
10109 return true;
10110 return false;
10111 case MVT::isVoid:
10112 // Note, we allow "void" uses (basically, uses that aren't loads or
10113 // stores), because arm allows folding a scale into many arithmetic
10114 // operations. This should be made more precise and revisited later.
10115
10116 // Allow r << imm, but the imm has to be a multiple of two.
10117 if (Scale & 1) return false;
10118 return isPowerOf2_32(Scale);
10119 }
10120}
10121
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010122/// isLegalAddressingMode - Return true if the addressing mode represented
10123/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson7117a912009-03-20 22:42:55 +000010124bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +000010125 Type *Ty) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010126 EVT VT = getValueType(Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000010127 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000010128 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010129
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010130 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000010131 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010132 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010133
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010134 switch (AM.Scale) {
10135 case 0: // no scale reg, must be "r+i" or "r", or "i".
10136 break;
10137 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010138 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010139 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +000010140 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010141 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000010142 // ARM doesn't support any R+R*scale+imm addr modes.
10143 if (AM.BaseOffs)
10144 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010145
Bob Wilson866c1742009-04-08 17:55:28 +000010146 if (!VT.isSimple())
10147 return false;
10148
Evan Chengdc49a8d2009-08-14 20:09:37 +000010149 if (Subtarget->isThumb2())
10150 return isLegalT2ScaledAddressingMode(AM, VT);
10151
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010152 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000010153 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010154 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010155 case MVT::i1:
10156 case MVT::i8:
10157 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010158 if (Scale < 0) Scale = -Scale;
10159 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010160 return true;
10161 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000010162 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000010163 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010164 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010165 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010166 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010167 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000010168 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010169
Owen Anderson9f944592009-08-11 20:47:22 +000010170 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010171 // Note, we allow "void" uses (basically, uses that aren't loads or
10172 // stores), because arm allows folding a scale into many arithmetic
10173 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000010174
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010175 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000010176 if (Scale & 1) return false;
10177 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010178 }
Evan Cheng2150b922007-03-12 23:30:29 +000010179 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010180 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000010181}
10182
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010183/// isLegalICmpImmediate - Return true if the specified immediate is legal
10184/// icmp immediate, that is the target has icmp instructions which can compare
10185/// a register against the immediate without having to materialize the
10186/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000010187bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010188 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010189 if (!Subtarget->isThumb())
Chandler Carruth8a102c22012-04-06 20:10:52 +000010190 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010191 if (Subtarget->isThumb2())
Chandler Carruth8a102c22012-04-06 20:10:52 +000010192 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010193 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000010194 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010195}
10196
Andrew Tricka22cdb72012-07-18 18:34:27 +000010197/// isLegalAddImmediate - Return true if the specified immediate is a legal add
10198/// *or sub* immediate, that is the target has add or sub instructions which can
10199/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000010200/// immediate into a register.
10201bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000010202 // Same encoding for add/sub, just flip the sign.
10203 int64_t AbsImm = llvm::abs64(Imm);
10204 if (!Subtarget->isThumb())
10205 return ARM_AM::getSOImmVal(AbsImm) != -1;
10206 if (Subtarget->isThumb2())
10207 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
10208 // Thumb1 only has 8-bit unsigned immediate.
10209 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000010210}
10211
Owen Anderson53aa7a92009-08-10 22:56:29 +000010212static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010213 bool isSEXTLoad, SDValue &Base,
10214 SDValue &Offset, bool &isInc,
10215 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000010216 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10217 return false;
10218
Owen Anderson9f944592009-08-11 20:47:22 +000010219 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000010220 // AddressingMode 3
10221 Base = Ptr->getOperand(0);
10222 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010223 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010224 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010225 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010226 isInc = false;
10227 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10228 return true;
10229 }
10230 }
10231 isInc = (Ptr->getOpcode() == ISD::ADD);
10232 Offset = Ptr->getOperand(1);
10233 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000010234 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000010235 // AddressingMode 2
10236 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010237 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010238 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010239 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010240 isInc = false;
10241 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10242 Base = Ptr->getOperand(0);
10243 return true;
10244 }
10245 }
10246
10247 if (Ptr->getOpcode() == ISD::ADD) {
10248 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000010249 ARM_AM::ShiftOpc ShOpcVal=
10250 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000010251 if (ShOpcVal != ARM_AM::no_shift) {
10252 Base = Ptr->getOperand(1);
10253 Offset = Ptr->getOperand(0);
10254 } else {
10255 Base = Ptr->getOperand(0);
10256 Offset = Ptr->getOperand(1);
10257 }
10258 return true;
10259 }
10260
10261 isInc = (Ptr->getOpcode() == ISD::ADD);
10262 Base = Ptr->getOperand(0);
10263 Offset = Ptr->getOperand(1);
10264 return true;
10265 }
10266
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000010267 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000010268 return false;
10269}
10270
Owen Anderson53aa7a92009-08-10 22:56:29 +000010271static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010272 bool isSEXTLoad, SDValue &Base,
10273 SDValue &Offset, bool &isInc,
10274 SelectionDAG &DAG) {
10275 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10276 return false;
10277
10278 Base = Ptr->getOperand(0);
10279 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
10280 int RHSC = (int)RHS->getZExtValue();
10281 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
10282 assert(Ptr->getOpcode() == ISD::ADD);
10283 isInc = false;
10284 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10285 return true;
10286 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
10287 isInc = Ptr->getOpcode() == ISD::ADD;
10288 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
10289 return true;
10290 }
10291 }
10292
10293 return false;
10294}
10295
Evan Cheng10043e22007-01-19 07:51:42 +000010296/// getPreIndexedAddressParts - returns true by value, base pointer and
10297/// offset pointer and addressing mode by reference if the node's address
10298/// can be legally represented as pre-indexed load / store address.
10299bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010300ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
10301 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010302 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010303 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010304 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010305 return false;
10306
Owen Anderson53aa7a92009-08-10 22:56:29 +000010307 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010308 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010309 bool isSEXTLoad = false;
10310 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10311 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010312 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010313 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10314 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10315 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010316 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010317 } else
10318 return false;
10319
10320 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010321 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010322 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010323 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10324 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010325 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010326 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000010327 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000010328 if (!isLegal)
10329 return false;
10330
10331 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10332 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010333}
10334
10335/// getPostIndexedAddressParts - returns true by value, base pointer and
10336/// offset pointer and addressing mode by reference if this node can be
10337/// combined with a load / store to form a post-indexed load / store.
10338bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010339 SDValue &Base,
10340 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010341 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010342 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010343 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010344 return false;
10345
Owen Anderson53aa7a92009-08-10 22:56:29 +000010346 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010347 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010348 bool isSEXTLoad = false;
10349 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010350 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010351 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010352 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10353 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010354 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010355 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010356 } else
10357 return false;
10358
10359 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010360 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010361 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010362 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000010363 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010364 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010365 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10366 isInc, DAG);
10367 if (!isLegal)
10368 return false;
10369
Evan Chengf19384d2010-05-18 21:31:17 +000010370 if (Ptr != Base) {
10371 // Swap base ptr and offset to catch more post-index load / store when
10372 // it's legal. In Thumb2 mode, offset must be an immediate.
10373 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10374 !Subtarget->isThumb2())
10375 std::swap(Base, Offset);
10376
10377 // Post-indexed load / store update the base pointer.
10378 if (Ptr != Base)
10379 return false;
10380 }
10381
Evan Cheng84c6cda2009-07-02 07:28:31 +000010382 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10383 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010384}
10385
Jay Foada0653a32014-05-14 21:14:37 +000010386void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10387 APInt &KnownZero,
10388 APInt &KnownOne,
10389 const SelectionDAG &DAG,
10390 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000010391 unsigned BitWidth = KnownOne.getBitWidth();
10392 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000010393 switch (Op.getOpcode()) {
10394 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000010395 case ARMISD::ADDC:
10396 case ARMISD::ADDE:
10397 case ARMISD::SUBC:
10398 case ARMISD::SUBE:
10399 // These nodes' second result is a boolean
10400 if (Op.getResNo() == 0)
10401 break;
10402 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10403 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010404 case ARMISD::CMOV: {
10405 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000010406 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010407 if (KnownZero == 0 && KnownOne == 0) return;
10408
Dan Gohmanf990faf2008-02-13 00:35:47 +000010409 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000010410 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010411 KnownZero &= KnownZeroRHS;
10412 KnownOne &= KnownOneRHS;
10413 return;
10414 }
Tim Northover01b4aa92014-04-03 15:10:35 +000010415 case ISD::INTRINSIC_W_CHAIN: {
10416 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
10417 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
10418 switch (IntID) {
10419 default: return;
10420 case Intrinsic::arm_ldaex:
10421 case Intrinsic::arm_ldrex: {
10422 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
10423 unsigned MemBits = VT.getScalarType().getSizeInBits();
10424 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
10425 return;
10426 }
10427 }
10428 }
Evan Cheng10043e22007-01-19 07:51:42 +000010429 }
10430}
10431
10432//===----------------------------------------------------------------------===//
10433// ARM Inline Assembly Support
10434//===----------------------------------------------------------------------===//
10435
Evan Cheng078b0b02011-01-08 01:24:27 +000010436bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10437 // Looking for "rev" which is V6+.
10438 if (!Subtarget->hasV6Ops())
10439 return false;
10440
10441 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10442 std::string AsmStr = IA->getAsmString();
10443 SmallVector<StringRef, 4> AsmPieces;
10444 SplitString(AsmStr, AsmPieces, ";\n");
10445
10446 switch (AsmPieces.size()) {
10447 default: return false;
10448 case 1:
10449 AsmStr = AsmPieces[0];
10450 AsmPieces.clear();
10451 SplitString(AsmStr, AsmPieces, " \t,");
10452
10453 // rev $0, $1
10454 if (AsmPieces.size() == 3 &&
10455 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10456 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010457 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010458 if (Ty && Ty->getBitWidth() == 32)
10459 return IntrinsicLowering::LowerToByteSwap(CI);
10460 }
10461 break;
10462 }
10463
10464 return false;
10465}
10466
Evan Cheng10043e22007-01-19 07:51:42 +000010467/// getConstraintType - Given a constraint letter, return the type of
10468/// constraint it is for this target.
10469ARMTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +000010470ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
10471 if (Constraint.size() == 1) {
10472 switch (Constraint[0]) {
10473 default: break;
10474 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010475 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010476 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010477 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010478 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010479 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010480 // An address with a single base register. Due to the way we
10481 // currently handle addresses it is the same as an 'r' memory constraint.
10482 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010483 }
Eric Christophere256cd02011-06-21 22:10:57 +000010484 } else if (Constraint.size() == 2) {
10485 switch (Constraint[0]) {
10486 default: break;
10487 // All 'U+' constraints are addresses.
10488 case 'U': return C_Memory;
10489 }
Evan Cheng10043e22007-01-19 07:51:42 +000010490 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010491 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010492}
10493
John Thompsone8360b72010-10-29 17:29:13 +000010494/// Examine constraint type and operand type and determine a weight value.
10495/// This object must already have been set up with the operand type
10496/// and the current alternative constraint selected.
10497TargetLowering::ConstraintWeight
10498ARMTargetLowering::getSingleConstraintMatchWeight(
10499 AsmOperandInfo &info, const char *constraint) const {
10500 ConstraintWeight weight = CW_Invalid;
10501 Value *CallOperandVal = info.CallOperandVal;
10502 // If we don't have a value, we can't do a match,
10503 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010504 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010505 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010506 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010507 // Look at the constraint type.
10508 switch (*constraint) {
10509 default:
10510 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10511 break;
10512 case 'l':
10513 if (type->isIntegerTy()) {
10514 if (Subtarget->isThumb())
10515 weight = CW_SpecificReg;
10516 else
10517 weight = CW_Register;
10518 }
10519 break;
10520 case 'w':
10521 if (type->isFloatingPointTy())
10522 weight = CW_Register;
10523 break;
10524 }
10525 return weight;
10526}
10527
Eric Christophercf2007c2011-06-30 23:50:52 +000010528typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10529RCPair
Evan Cheng10043e22007-01-19 07:51:42 +000010530ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000010531 MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000010532 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010533 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000010534 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000010535 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010536 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010537 return RCPair(0U, &ARM::tGPRRegClass);
10538 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000010539 case 'h': // High regs or no regs.
10540 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010541 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000010542 break;
Chris Lattner6223e832007-04-02 17:24:08 +000010543 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000010544 if (Subtarget->isThumb1Only())
10545 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000010546 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010547 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000010548 if (VT == MVT::Other)
10549 break;
Owen Anderson9f944592009-08-11 20:47:22 +000010550 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010551 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000010552 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010553 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000010554 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010555 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010556 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010557 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000010558 if (VT == MVT::Other)
10559 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010560 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010561 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010562 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010563 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010564 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010565 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010566 break;
Eric Christopherc011d312011-07-01 00:30:46 +000010567 case 't':
10568 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010569 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000010570 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010571 }
10572 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000010573 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000010574 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000010575
Evan Cheng10043e22007-01-19 07:51:42 +000010576 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10577}
10578
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010579/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10580/// vector. If it is invalid, don't add anything to Ops.
10581void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010582 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010583 std::vector<SDValue>&Ops,
10584 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000010585 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010586
Eric Christopherde9399b2011-06-02 23:16:42 +000010587 // Currently only support length 1 constraints.
10588 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010589
Eric Christopherde9399b2011-06-02 23:16:42 +000010590 char ConstraintLetter = Constraint[0];
10591 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010592 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000010593 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010594 case 'I': case 'J': case 'K': case 'L':
10595 case 'M': case 'N': case 'O':
10596 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10597 if (!C)
10598 return;
10599
10600 int64_t CVal64 = C->getSExtValue();
10601 int CVal = (int) CVal64;
10602 // None of these constraints allow values larger than 32 bits. Check
10603 // that the value fits in an int.
10604 if (CVal != CVal64)
10605 return;
10606
Eric Christopherde9399b2011-06-02 23:16:42 +000010607 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000010608 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000010609 // Constant suitable for movw, must be between 0 and
10610 // 65535.
10611 if (Subtarget->hasV6T2Ops())
10612 if (CVal >= 0 && CVal <= 65535)
10613 break;
10614 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010615 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000010616 if (Subtarget->isThumb1Only()) {
10617 // This must be a constant between 0 and 255, for ADD
10618 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010619 if (CVal >= 0 && CVal <= 255)
10620 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010621 } else if (Subtarget->isThumb2()) {
10622 // A constant that can be used as an immediate value in a
10623 // data-processing instruction.
10624 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10625 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010626 } else {
10627 // A constant that can be used as an immediate value in a
10628 // data-processing instruction.
10629 if (ARM_AM::getSOImmVal(CVal) != -1)
10630 break;
10631 }
10632 return;
10633
10634 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000010635 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010636 // This must be a constant between -255 and -1, for negated ADD
10637 // immediates. This can be used in GCC with an "n" modifier that
10638 // prints the negated value, for use with SUB instructions. It is
10639 // not useful otherwise but is implemented for compatibility.
10640 if (CVal >= -255 && CVal <= -1)
10641 break;
10642 } else {
10643 // This must be a constant between -4095 and 4095. It is not clear
10644 // what this constraint is intended for. Implemented for
10645 // compatibility with GCC.
10646 if (CVal >= -4095 && CVal <= 4095)
10647 break;
10648 }
10649 return;
10650
10651 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000010652 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010653 // A 32-bit value where only one byte has a nonzero value. Exclude
10654 // zero to match GCC. This constraint is used by GCC internally for
10655 // constants that can be loaded with a move/shift combination.
10656 // It is not useful otherwise but is implemented for compatibility.
10657 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10658 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010659 } else if (Subtarget->isThumb2()) {
10660 // A constant whose bitwise inverse can be used as an immediate
10661 // value in a data-processing instruction. This can be used in GCC
10662 // with a "B" modifier that prints the inverted value, for use with
10663 // BIC and MVN instructions. It is not useful otherwise but is
10664 // implemented for compatibility.
10665 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10666 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010667 } else {
10668 // A constant whose bitwise inverse can be used as an immediate
10669 // value in a data-processing instruction. This can be used in GCC
10670 // with a "B" modifier that prints the inverted value, for use with
10671 // BIC and MVN instructions. It is not useful otherwise but is
10672 // implemented for compatibility.
10673 if (ARM_AM::getSOImmVal(~CVal) != -1)
10674 break;
10675 }
10676 return;
10677
10678 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000010679 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010680 // This must be a constant between -7 and 7,
10681 // for 3-operand ADD/SUB immediate instructions.
10682 if (CVal >= -7 && CVal < 7)
10683 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010684 } else if (Subtarget->isThumb2()) {
10685 // A constant whose negation can be used as an immediate value in a
10686 // data-processing instruction. This can be used in GCC with an "n"
10687 // modifier that prints the negated value, for use with SUB
10688 // instructions. It is not useful otherwise but is implemented for
10689 // compatibility.
10690 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10691 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010692 } else {
10693 // A constant whose negation can be used as an immediate value in a
10694 // data-processing instruction. This can be used in GCC with an "n"
10695 // modifier that prints the negated value, for use with SUB
10696 // instructions. It is not useful otherwise but is implemented for
10697 // compatibility.
10698 if (ARM_AM::getSOImmVal(-CVal) != -1)
10699 break;
10700 }
10701 return;
10702
10703 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000010704 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010705 // This must be a multiple of 4 between 0 and 1020, for
10706 // ADD sp + immediate.
10707 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10708 break;
10709 } else {
10710 // A power of two or a constant between 0 and 32. This is used in
10711 // GCC for the shift amount on shifted register operands, but it is
10712 // useful in general for any shift amounts.
10713 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10714 break;
10715 }
10716 return;
10717
10718 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000010719 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010720 // This must be a constant between 0 and 31, for shift amounts.
10721 if (CVal >= 0 && CVal <= 31)
10722 break;
10723 }
10724 return;
10725
10726 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000010727 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010728 // This must be a multiple of 4 between -508 and 508, for
10729 // ADD/SUB sp = sp + immediate.
10730 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10731 break;
10732 }
10733 return;
10734 }
10735 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10736 break;
10737 }
10738
10739 if (Result.getNode()) {
10740 Ops.push_back(Result);
10741 return;
10742 }
Dale Johannesence97d552010-06-25 21:55:36 +000010743 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010744}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010745
Renato Golin87610692013-07-16 09:32:17 +000010746SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
10747 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only");
10748 unsigned Opcode = Op->getOpcode();
10749 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000010750 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000010751 bool isSigned = (Opcode == ISD::SDIVREM);
10752 EVT VT = Op->getValueType(0);
10753 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
10754
10755 RTLIB::Libcall LC;
10756 switch (VT.getSimpleVT().SimpleTy) {
10757 default: llvm_unreachable("Unexpected request for libcall!");
Saleem Abdulrasool740be892014-08-17 22:50:59 +000010758 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
10759 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
10760 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
10761 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
Renato Golin87610692013-07-16 09:32:17 +000010762 }
10763
10764 SDValue InChain = DAG.getEntryNode();
10765
10766 TargetLowering::ArgListTy Args;
10767 TargetLowering::ArgListEntry Entry;
10768 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
10769 EVT ArgVT = Op->getOperand(i).getValueType();
10770 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
10771 Entry.Node = Op->getOperand(i);
10772 Entry.Ty = ArgTy;
10773 Entry.isSExt = isSigned;
10774 Entry.isZExt = !isSigned;
10775 Args.push_back(Entry);
10776 }
10777
10778 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
10779 getPointerTy());
10780
Reid Kleckner343c3952014-11-20 23:51:47 +000010781 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000010782
10783 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010784 TargetLowering::CallLoweringInfo CLI(DAG);
10785 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000010786 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010787 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000010788
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010789 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000010790 return CallInfo.first;
10791}
10792
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000010793SDValue
10794ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
10795 assert(Subtarget->isTargetWindows() && "unsupported target platform");
10796 SDLoc DL(Op);
10797
10798 // Get the inputs.
10799 SDValue Chain = Op.getOperand(0);
10800 SDValue Size = Op.getOperand(1);
10801
10802 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
10803 DAG.getConstant(2, MVT::i32));
10804
10805 SDValue Flag;
10806 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
10807 Flag = Chain.getValue(1);
10808
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000010809 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000010810 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
10811
10812 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
10813 Chain = NewSP.getValue(1);
10814
10815 SDValue Ops[2] = { NewSP, Chain };
10816 return DAG.getMergeValues(Ops, DL);
10817}
10818
Oliver Stannard51b1d462014-08-21 12:50:31 +000010819SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
10820 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
10821 "Unexpected type for custom-lowering FP_EXTEND");
10822
10823 RTLIB::Libcall LC;
10824 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
10825
10826 SDValue SrcVal = Op.getOperand(0);
10827 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
10828 /*isSigned*/ false, SDLoc(Op)).first;
10829}
10830
10831SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
10832 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
10833 Subtarget->isFPOnlySP() &&
10834 "Unexpected type for custom-lowering FP_ROUND");
10835
10836 RTLIB::Libcall LC;
10837 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
10838
10839 SDValue SrcVal = Op.getOperand(0);
10840 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
10841 /*isSigned*/ false, SDLoc(Op)).first;
10842}
10843
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010844bool
10845ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10846 // The ARM target isn't yet aware of offsets.
10847 return false;
10848}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010849
Jim Grosbach11013ed2010-07-16 23:05:05 +000010850bool ARM::isBitFieldInvertedMask(unsigned v) {
10851 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010852 return false;
10853
Jim Grosbach11013ed2010-07-16 23:05:05 +000010854 // there can be 1's on either or both "outsides", all the "inside"
10855 // bits must be 0's
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000010856 return isShiftedMask_32(~v);
Jim Grosbach11013ed2010-07-16 23:05:05 +000010857}
10858
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010859/// isFPImmLegal - Returns true if the target can instruction select the
10860/// specified FP immediate natively. If false, the legalizer will
10861/// materialize the FP immediate as a load from a constant pool.
10862bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10863 if (!Subtarget->hasVFP3())
10864 return false;
10865 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010866 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000010867 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000010868 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010869 return false;
10870}
Bob Wilson5549d492010-09-21 17:56:22 +000010871
Wesley Peck527da1b2010-11-23 03:31:01 +000010872/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000010873/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10874/// specified in the intrinsic calls.
10875bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10876 const CallInst &I,
10877 unsigned Intrinsic) const {
10878 switch (Intrinsic) {
10879 case Intrinsic::arm_neon_vld1:
10880 case Intrinsic::arm_neon_vld2:
10881 case Intrinsic::arm_neon_vld3:
10882 case Intrinsic::arm_neon_vld4:
10883 case Intrinsic::arm_neon_vld2lane:
10884 case Intrinsic::arm_neon_vld3lane:
10885 case Intrinsic::arm_neon_vld4lane: {
10886 Info.opc = ISD::INTRINSIC_W_CHAIN;
10887 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010888 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010889 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10890 Info.ptrVal = I.getArgOperand(0);
10891 Info.offset = 0;
10892 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10893 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10894 Info.vol = false; // volatile loads with NEON intrinsics not supported
10895 Info.readMem = true;
10896 Info.writeMem = false;
10897 return true;
10898 }
10899 case Intrinsic::arm_neon_vst1:
10900 case Intrinsic::arm_neon_vst2:
10901 case Intrinsic::arm_neon_vst3:
10902 case Intrinsic::arm_neon_vst4:
10903 case Intrinsic::arm_neon_vst2lane:
10904 case Intrinsic::arm_neon_vst3lane:
10905 case Intrinsic::arm_neon_vst4lane: {
10906 Info.opc = ISD::INTRINSIC_VOID;
10907 // Conservatively set memVT to the entire set of vectors stored.
10908 unsigned NumElts = 0;
10909 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000010910 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000010911 if (!ArgTy->isVectorTy())
10912 break;
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010913 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010914 }
10915 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10916 Info.ptrVal = I.getArgOperand(0);
10917 Info.offset = 0;
10918 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10919 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10920 Info.vol = false; // volatile stores with NEON intrinsics not supported
10921 Info.readMem = false;
10922 Info.writeMem = true;
10923 return true;
10924 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010925 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010926 case Intrinsic::arm_ldrex: {
10927 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
10928 Info.opc = ISD::INTRINSIC_W_CHAIN;
10929 Info.memVT = MVT::getVT(PtrTy->getElementType());
10930 Info.ptrVal = I.getArgOperand(0);
10931 Info.offset = 0;
10932 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10933 Info.vol = true;
10934 Info.readMem = true;
10935 Info.writeMem = false;
10936 return true;
10937 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010938 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010939 case Intrinsic::arm_strex: {
10940 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
10941 Info.opc = ISD::INTRINSIC_W_CHAIN;
10942 Info.memVT = MVT::getVT(PtrTy->getElementType());
10943 Info.ptrVal = I.getArgOperand(1);
10944 Info.offset = 0;
10945 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10946 Info.vol = true;
10947 Info.readMem = false;
10948 Info.writeMem = true;
10949 return true;
10950 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010951 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010952 case Intrinsic::arm_strexd: {
10953 Info.opc = ISD::INTRINSIC_W_CHAIN;
10954 Info.memVT = MVT::i64;
10955 Info.ptrVal = I.getArgOperand(2);
10956 Info.offset = 0;
10957 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010958 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010959 Info.readMem = false;
10960 Info.writeMem = true;
10961 return true;
10962 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010963 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010964 case Intrinsic::arm_ldrexd: {
10965 Info.opc = ISD::INTRINSIC_W_CHAIN;
10966 Info.memVT = MVT::i64;
10967 Info.ptrVal = I.getArgOperand(0);
10968 Info.offset = 0;
10969 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010970 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010971 Info.readMem = true;
10972 Info.writeMem = false;
10973 return true;
10974 }
Bob Wilson5549d492010-09-21 17:56:22 +000010975 default:
10976 break;
10977 }
10978
10979 return false;
10980}
Juergen Ributzka659ce002014-01-28 01:20:14 +000010981
10982/// \brief Returns true if it is beneficial to convert a load of a constant
10983/// to just the constant itself.
10984bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
10985 Type *Ty) const {
10986 assert(Ty->isIntegerTy());
10987
10988 unsigned Bits = Ty->getPrimitiveSizeInBits();
10989 if (Bits == 0 || Bits > 32)
10990 return false;
10991 return true;
10992}
Tim Northover037f26f22014-04-17 18:22:47 +000010993
Robin Morisset25c8e312014-09-17 00:06:58 +000010994bool ARMTargetLowering::hasLoadLinkedStoreConditional() const { return true; }
10995
Robin Morisset5349e8e2014-09-18 18:56:04 +000010996Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
10997 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000010998 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000010999
11000 // First, if the target has no DMB, see what fallback we can use.
11001 if (!Subtarget->hasDataBarrier()) {
11002 // Some ARMv6 cpus can support data barriers with an mcr instruction.
11003 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
11004 // here.
11005 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
11006 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
11007 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
11008 Builder.getInt32(0), Builder.getInt32(7),
11009 Builder.getInt32(10), Builder.getInt32(5)};
11010 return Builder.CreateCall(MCR, args);
11011 } else {
11012 // Instead of using barriers, atomic accesses on these subtargets use
11013 // libcalls.
11014 llvm_unreachable("makeDMB on a target so old that it has no barriers");
11015 }
11016 } else {
11017 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
11018 // Only a full system barrier exists in the M-class architectures.
11019 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
11020 Constant *CDomain = Builder.getInt32(Domain);
11021 return Builder.CreateCall(DMB, CDomain);
11022 }
Robin Morisseta47cb412014-09-03 21:01:03 +000011023}
11024
11025// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000011026Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011027 AtomicOrdering Ord, bool IsStore,
11028 bool IsLoad) const {
11029 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011030 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011031
11032 switch (Ord) {
11033 case NotAtomic:
11034 case Unordered:
11035 llvm_unreachable("Invalid fence: unordered/non-atomic");
11036 case Monotonic:
11037 case Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000011038 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011039 case SequentiallyConsistent:
11040 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000011041 return nullptr; // Nothing to do
11042 /*FALLTHROUGH*/
Robin Morisseta47cb412014-09-03 21:01:03 +000011043 case Release:
11044 case AcquireRelease:
11045 if (Subtarget->isSwift())
Robin Morissetdedef332014-09-23 20:31:14 +000011046 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000011047 // FIXME: add a comment with a link to documentation justifying this.
11048 else
Robin Morissetdedef332014-09-23 20:31:14 +000011049 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011050 }
Robin Morissetdedef332014-09-23 20:31:14 +000011051 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011052}
11053
Robin Morissetdedef332014-09-23 20:31:14 +000011054Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011055 AtomicOrdering Ord, bool IsStore,
11056 bool IsLoad) const {
11057 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011058 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011059
11060 switch (Ord) {
11061 case NotAtomic:
11062 case Unordered:
11063 llvm_unreachable("Invalid fence: unordered/not-atomic");
11064 case Monotonic:
11065 case Release:
Robin Morissetdedef332014-09-23 20:31:14 +000011066 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011067 case Acquire:
11068 case AcquireRelease:
Robin Morissetdedef332014-09-23 20:31:14 +000011069 case SequentiallyConsistent:
11070 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011071 }
Robin Morissetdedef332014-09-23 20:31:14 +000011072 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011073}
11074
Robin Morisseted3d48f2014-09-03 21:29:59 +000011075// Loads and stores less than 64-bits are already atomic; ones above that
11076// are doomed anyway, so defer to the default libcall and blame the OS when
11077// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11078// anything for those.
11079bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
11080 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
11081 return (Size == 64) && !Subtarget->isMClass();
11082}
Tim Northover037f26f22014-04-17 18:22:47 +000011083
Robin Morisseted3d48f2014-09-03 21:29:59 +000011084// Loads and stores less than 64-bits are already atomic; ones above that
11085// are doomed anyway, so defer to the default libcall and blame the OS when
11086// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11087// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000011088// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
11089// guarantee, see DDI0406C ARM architecture reference manual,
11090// sections A8.8.72-74 LDRD)
Robin Morisseted3d48f2014-09-03 21:29:59 +000011091bool ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
11092 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
11093 return (Size == 64) && !Subtarget->isMClass();
11094}
11095
11096// For the real atomic operations, we have ldrex/strex up to 32 bits,
11097// and up to 64 bits on the non-M profiles
11098bool ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
11099 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
Aaron Ballman169eeb912014-09-04 11:52:24 +000011100 return Size <= (Subtarget->isMClass() ? 32U : 64U);
Tim Northover037f26f22014-04-17 18:22:47 +000011101}
11102
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011103// This has so far only been implemented for MachO.
11104bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000011105 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011106}
11107
Quentin Colombetc32615d2014-10-31 17:52:53 +000011108bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
11109 unsigned &Cost) const {
11110 // If we do not have NEON, vector types are not natively supported.
11111 if (!Subtarget->hasNEON())
11112 return false;
11113
11114 // Floating point values and vector values map to the same register file.
11115 // Therefore, althought we could do a store extract of a vector type, this is
11116 // better to leave at float as we have more freedom in the addressing mode for
11117 // those.
11118 if (VectorTy->isFPOrFPVectorTy())
11119 return false;
11120
11121 // If the index is unknown at compile time, this is very expensive to lower
11122 // and it is not possible to combine the store with the extract.
11123 if (!isa<ConstantInt>(Idx))
11124 return false;
11125
11126 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
11127 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
11128 // We can do a store + vector extract on any vector that fits perfectly in a D
11129 // or Q register.
11130 if (BitWidth == 64 || BitWidth == 128) {
11131 Cost = 0;
11132 return true;
11133 }
11134 return false;
11135}
11136
Tim Northover037f26f22014-04-17 18:22:47 +000011137Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
11138 AtomicOrdering Ord) const {
11139 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
11140 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
Robin Morissetb155f522014-08-18 16:48:58 +000011141 bool IsAcquire = isAtLeastAcquire(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011142
11143 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
11144 // intrinsic must return {i32, i32} and we have to recombine them into a
11145 // single i64 here.
11146 if (ValTy->getPrimitiveSizeInBits() == 64) {
11147 Intrinsic::ID Int =
11148 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
11149 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
11150
11151 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11152 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
11153
11154 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
11155 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011156 if (!Subtarget->isLittle())
11157 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011158 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
11159 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
11160 return Builder.CreateOr(
11161 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
11162 }
11163
11164 Type *Tys[] = { Addr->getType() };
11165 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
11166 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
11167
11168 return Builder.CreateTruncOrBitCast(
11169 Builder.CreateCall(Ldrex, Addr),
11170 cast<PointerType>(Addr->getType())->getElementType());
11171}
11172
11173Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
11174 Value *Addr,
11175 AtomicOrdering Ord) const {
11176 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morissetb155f522014-08-18 16:48:58 +000011177 bool IsRelease = isAtLeastRelease(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011178
11179 // Since the intrinsics must have legal type, the i64 intrinsics take two
11180 // parameters: "i32, i32". We must marshal Val into the appropriate form
11181 // before the call.
11182 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
11183 Intrinsic::ID Int =
11184 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
11185 Function *Strex = Intrinsic::getDeclaration(M, Int);
11186 Type *Int32Ty = Type::getInt32Ty(M->getContext());
11187
11188 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
11189 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011190 if (!Subtarget->isLittle())
11191 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011192 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11193 return Builder.CreateCall3(Strex, Lo, Hi, Addr);
11194 }
11195
11196 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
11197 Type *Tys[] = { Addr->getType() };
11198 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
11199
11200 return Builder.CreateCall2(
11201 Strex, Builder.CreateZExtOrBitCast(
11202 Val, Strex->getFunctionType()->getParamType(0)),
11203 Addr);
11204}
Oliver Stannardc24f2172014-05-09 14:01:47 +000011205
11206enum HABaseType {
11207 HA_UNKNOWN = 0,
11208 HA_FLOAT,
11209 HA_DOUBLE,
11210 HA_VECT64,
11211 HA_VECT128
11212};
11213
11214static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
11215 uint64_t &Members) {
11216 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
11217 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
11218 uint64_t SubMembers = 0;
11219 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
11220 return false;
11221 Members += SubMembers;
11222 }
11223 } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
11224 uint64_t SubMembers = 0;
11225 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
11226 return false;
11227 Members += SubMembers * AT->getNumElements();
11228 } else if (Ty->isFloatTy()) {
11229 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
11230 return false;
11231 Members = 1;
11232 Base = HA_FLOAT;
11233 } else if (Ty->isDoubleTy()) {
11234 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
11235 return false;
11236 Members = 1;
11237 Base = HA_DOUBLE;
11238 } else if (const VectorType *VT = dyn_cast<VectorType>(Ty)) {
11239 Members = 1;
11240 switch (Base) {
11241 case HA_FLOAT:
11242 case HA_DOUBLE:
11243 return false;
11244 case HA_VECT64:
11245 return VT->getBitWidth() == 64;
11246 case HA_VECT128:
11247 return VT->getBitWidth() == 128;
11248 case HA_UNKNOWN:
11249 switch (VT->getBitWidth()) {
11250 case 64:
11251 Base = HA_VECT64;
11252 return true;
11253 case 128:
11254 Base = HA_VECT128;
11255 return true;
11256 default:
11257 return false;
11258 }
11259 }
11260 }
11261
11262 return (Members > 0 && Members <= 4);
11263}
11264
11265/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate.
11266bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
11267 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000011268 if (getEffectiveCallingConv(CallConv, isVarArg) !=
11269 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000011270 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000011271
11272 HABaseType Base = HA_UNKNOWN;
11273 uint64_t Members = 0;
11274 bool result = isHomogeneousAggregate(Ty, Base, Members);
Justin Bognerc0087f32014-08-12 03:24:59 +000011275 DEBUG(dbgs() << "isHA: " << result << " "; Ty->dump());
Tim Northover4f1909f2014-05-27 10:43:38 +000011276 return result;
Oliver Stannardc24f2172014-05-09 14:01:47 +000011277}