blob: 3cf8d8809bbc5a1285f07ec4de57a8bf59d1a8c0 [file] [log] [blame]
Evan Cheng10043e22007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Cheng10043e22007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Craig Topper188ed9d2012-03-17 07:33:42 +000015#include "ARMISelLowering.h"
Eric Christopher1c069172010-09-10 22:42:06 +000016#include "ARMCallingConv.h"
Evan Cheng10043e22007-01-19 07:51:42 +000017#include "ARMConstantPoolValue.h"
Evan Cheng10043e22007-01-19 07:51:42 +000018#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov9a232f42009-08-21 12:41:24 +000019#include "ARMPerfectShuffle.h"
Evan Cheng10043e22007-01-19 07:51:42 +000020#include "ARMSubtarget.h"
21#include "ARMTargetMachine.h"
Chris Lattner4e7dfaf2009-08-02 00:34:36 +000022#include "ARMTargetObjectFile.h"
Evan Chenga20cde32011-07-20 23:34:39 +000023#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/ADT/Statistic.h"
25#include "llvm/ADT/StringExtras.h"
Pete Cooperef21bd42015-03-04 01:24:11 +000026#include "llvm/ADT/StringSwitch.h"
Bob Wilsona4c22902009-04-17 19:07:39 +000027#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng078b0b02011-01-08 01:24:27 +000028#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Cheng10043e22007-01-19 07:51:42 +000029#include "llvm/CodeGen/MachineBasicBlock.h"
30#include "llvm/CodeGen/MachineFrameInfo.h"
31#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Bill Wendling202803e2011-10-05 00:02:33 +000034#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng10043e22007-01-19 07:51:42 +000036#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000037#include "llvm/IR/CallingConv.h"
38#include "llvm/IR/Constants.h"
39#include "llvm/IR/Function.h"
40#include "llvm/IR/GlobalValue.h"
Tim Northover037f26f22014-04-17 18:22:47 +000041#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000042#include "llvm/IR/Instruction.h"
43#include "llvm/IR/Instructions.h"
44#include "llvm/IR/Intrinsics.h"
45#include "llvm/IR/Type.h"
Bill Wendling46ffefc2010-03-09 02:46:12 +000046#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach32bb3622010-04-14 22:28:31 +000047#include "llvm/Support/CommandLine.h"
Oliver Stannardc24f2172014-05-09 14:01:47 +000048#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000049#include "llvm/Support/ErrorHandling.h"
Evan Cheng2150b922007-03-12 23:30:29 +000050#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000051#include "llvm/Target/TargetOptions.h"
David Peixottoc32e24a2013-10-17 19:49:22 +000052#include <utility>
Evan Cheng10043e22007-01-19 07:51:42 +000053using namespace llvm;
54
Chandler Carruth84e68b22014-04-22 02:41:26 +000055#define DEBUG_TYPE "arm-isel"
56
Dale Johannesend679ff72010-06-03 21:09:53 +000057STATISTIC(NumTailCalls, "Number of tail calls");
Evan Cheng68aec142011-01-19 02:16:49 +000058STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren9f911162012-06-01 02:44:42 +000059STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesend679ff72010-06-03 21:09:53 +000060
Eric Christopher347f4c32010-12-15 23:47:29 +000061cl::opt<bool>
Jim Grosbach32bb3622010-04-14 22:28:31 +000062EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng25f93642010-07-08 02:08:50 +000063 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbach32bb3622010-04-14 22:28:31 +000064 cl::init(false));
65
Evan Chengf128bdc2010-06-16 07:35:02 +000066static cl::opt<bool>
67ARMInterworking("arm-interworking", cl::Hidden,
68 cl::desc("Enable / disable ARM interworking (for debugging only)"),
69 cl::init(true));
70
Benjamin Kramer7ba71be2011-11-26 23:01:57 +000071namespace {
Cameron Zwarich89019782011-06-10 20:59:24 +000072 class ARMCCState : public CCState {
73 public:
74 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Eric Christopherb5217502014-08-06 18:45:26 +000075 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
76 ParmContext PC)
77 : CCState(CC, isVarArg, MF, locs, C) {
Cameron Zwarich89019782011-06-10 20:59:24 +000078 assert(((PC == Call) || (PC == Prologue)) &&
79 "ARMCCState users must specify whether their context is call"
80 "or prologue generation.");
81 CallOrPrologue = PC;
82 }
83 };
84}
85
Stuart Hastings45fe3c32011-04-20 16:47:52 +000086// The APCS parameter registers.
Craig Topper840beec2014-04-04 05:16:06 +000087static const MCPhysReg GPRArgRegs[] = {
Stuart Hastings45fe3c32011-04-20 16:47:52 +000088 ARM::R0, ARM::R1, ARM::R2, ARM::R3
89};
90
Craig Topper4fa625f2012-08-12 03:16:37 +000091void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
92 MVT PromotedBitwiseVT) {
Bob Wilson2e076c42009-06-22 23:27:02 +000093 if (VT != PromotedLdStVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +000094 setOperationAction(ISD::LOAD, VT, Promote);
95 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000096
Craig Topper4fa625f2012-08-12 03:16:37 +000097 setOperationAction(ISD::STORE, VT, Promote);
98 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000099 }
100
Craig Topper4fa625f2012-08-12 03:16:37 +0000101 MVT ElemTy = VT.getVectorElementType();
Owen Anderson9f944592009-08-11 20:47:22 +0000102 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Craig Topper4fa625f2012-08-12 03:16:37 +0000103 setOperationAction(ISD::SETCC, VT, Custom);
104 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
105 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000106 if (ElemTy == MVT::i32) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000107 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
108 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
109 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
110 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000111 } else {
Craig Topper4fa625f2012-08-12 03:16:37 +0000112 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
113 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
114 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
115 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson5d8cfb22009-09-16 20:20:44 +0000116 }
Craig Topper4fa625f2012-08-12 03:16:37 +0000117 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
118 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
119 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
120 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
121 setOperationAction(ISD::SELECT, VT, Expand);
122 setOperationAction(ISD::SELECT_CC, VT, Expand);
Jim Grosbach30af4422012-10-12 22:59:21 +0000123 setOperationAction(ISD::VSELECT, VT, Expand);
Craig Topper4fa625f2012-08-12 03:16:37 +0000124 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000125 if (VT.isInteger()) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000126 setOperationAction(ISD::SHL, VT, Custom);
127 setOperationAction(ISD::SRA, VT, Custom);
128 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson2e076c42009-06-22 23:27:02 +0000129 }
130
131 // Promote all bit-wise operations.
132 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000133 setOperationAction(ISD::AND, VT, Promote);
134 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
135 setOperationAction(ISD::OR, VT, Promote);
136 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
137 setOperationAction(ISD::XOR, VT, Promote);
138 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000139 }
Bob Wilson4ed397c2009-09-16 00:17:28 +0000140
141 // Neon does not support vector divide/remainder operations.
Craig Topper4fa625f2012-08-12 03:16:37 +0000142 setOperationAction(ISD::SDIV, VT, Expand);
143 setOperationAction(ISD::UDIV, VT, Expand);
144 setOperationAction(ISD::FDIV, VT, Expand);
145 setOperationAction(ISD::SREM, VT, Expand);
146 setOperationAction(ISD::UREM, VT, Expand);
147 setOperationAction(ISD::FREM, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000148}
149
Craig Topper4fa625f2012-08-12 03:16:37 +0000150void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000151 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000152 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000153}
154
Craig Topper4fa625f2012-08-12 03:16:37 +0000155void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000156 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000157 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000158}
159
Eric Christopher1889fdc2015-01-29 00:19:39 +0000160ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM,
161 const ARMSubtarget &STI)
162 : TargetLowering(TM), Subtarget(&STI) {
163 RegInfo = Subtarget->getRegisterInfo();
164 Itins = Subtarget->getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000165
Duncan Sandsf2641e12011-09-06 19:07:46 +0000166 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
167
Tim Northoverd6a729b2014-01-06 14:28:05 +0000168 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000169 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000170 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
Tim Northover978d25f2014-04-22 10:10:09 +0000171 Subtarget->hasARMOps() && !TM.Options.UseSoftFloat) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000172 // Single-precision floating-point arithmetic.
173 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
174 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
175 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
176 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000177
Evan Chengc9f22fd12007-04-27 08:15:43 +0000178 // Double-precision floating-point arithmetic.
179 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
180 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
181 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
182 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng143576d2007-01-31 09:30:58 +0000183
Evan Chengc9f22fd12007-04-27 08:15:43 +0000184 // Single-precision comparisons.
185 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
186 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
187 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
188 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
189 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
190 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
191 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
192 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000193
Evan Chengc9f22fd12007-04-27 08:15:43 +0000194 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
195 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
196 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
197 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
198 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
199 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
200 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
201 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng143576d2007-01-31 09:30:58 +0000202
Evan Chengc9f22fd12007-04-27 08:15:43 +0000203 // Double-precision comparisons.
204 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
205 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
206 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
207 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
208 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
209 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
210 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
211 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000212
Evan Chengc9f22fd12007-04-27 08:15:43 +0000213 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
215 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
216 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
217 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
218 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
219 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
220 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Cheng10043e22007-01-19 07:51:42 +0000221
Evan Chengc9f22fd12007-04-27 08:15:43 +0000222 // Floating-point to integer conversions.
223 // i64 conversions are done via library routines even when generating VFP
224 // instructions, so use the same ones.
225 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
226 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
227 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
228 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000229
Evan Chengc9f22fd12007-04-27 08:15:43 +0000230 // Conversions between floating types.
231 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
232 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
233
234 // Integer to floating-point conversions.
235 // i64 conversions are done via library routines even when generating VFP
236 // instructions, so use the same ones.
Bob Wilsondc40d5a2009-03-20 23:16:43 +0000237 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
238 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengc9f22fd12007-04-27 08:15:43 +0000239 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
240 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
241 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
242 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
243 }
Evan Cheng10043e22007-01-19 07:51:42 +0000244 }
245
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000246 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000247 setLibcallName(RTLIB::SHL_I128, nullptr);
248 setLibcallName(RTLIB::SRL_I128, nullptr);
249 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000250
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000251 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetMachO() &&
252 !Subtarget->isTargetWindows()) {
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000253 static const struct {
254 const RTLIB::Libcall Op;
255 const char * const Name;
256 const CallingConv::ID CC;
257 const ISD::CondCode Cond;
258 } LibraryCalls[] = {
259 // Double-precision floating-point arithmetic helper functions
260 // RTABI chapter 4.1.2, Table 2
261 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
262 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
263 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
264 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000265
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000266 // Double-precision floating-point comparison helper functions
267 // RTABI chapter 4.1.2, Table 3
268 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
269 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
270 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
271 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
272 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
273 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
274 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
275 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000276
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000277 // Single-precision floating-point arithmetic helper functions
278 // RTABI chapter 4.1.2, Table 4
279 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
280 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
281 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
282 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000283
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000284 // Single-precision floating-point comparison helper functions
285 // RTABI chapter 4.1.2, Table 5
286 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
287 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
288 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
289 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
290 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
291 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
292 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
293 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000294
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000295 // Floating-point to integer conversions.
296 // RTABI chapter 4.1.2, Table 6
297 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
298 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
299 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
300 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
301 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
302 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
303 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
304 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000305
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000306 // Conversions between floating types.
307 // RTABI chapter 4.1.2, Table 7
308 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000309 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Chad Rosierad7c9102014-08-23 18:29:43 +0000310 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000311
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000312 // Integer to floating-point conversions.
313 // RTABI chapter 4.1.2, Table 8
314 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
315 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
316 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
317 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
318 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
319 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
320 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
321 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000322
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000323 // Long long helper functions
324 // RTABI chapter 4.2, Table 9
Chad Rosierad7c9102014-08-23 18:29:43 +0000325 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
326 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
327 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
328 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000329
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000330 // Integer division functions
331 // RTABI chapter 4.3.1
Chad Rosierad7c9102014-08-23 18:29:43 +0000332 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
333 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
334 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
335 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
336 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
337 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
338 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
339 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Renato Golin4cd51872011-05-22 21:41:23 +0000340
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000341 // Memory operations
342 // RTABI chapter 4.3.4
343 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
344 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
345 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
346 };
347
348 for (const auto &LC : LibraryCalls) {
349 setLibcallName(LC.Op, LC.Name);
350 setLibcallCallingConv(LC.Op, LC.CC);
351 if (LC.Cond != ISD::SETCC_INVALID)
352 setCmpLibcallCC(LC.Op, LC.Cond);
353 }
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000354 }
355
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000356 if (Subtarget->isTargetWindows()) {
357 static const struct {
358 const RTLIB::Libcall Op;
359 const char * const Name;
360 const CallingConv::ID CC;
361 } LibraryCalls[] = {
362 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
363 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
364 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
365 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
366 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
367 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
368 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
369 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
370 };
371
372 for (const auto &LC : LibraryCalls) {
373 setLibcallName(LC.Op, LC.Name);
374 setLibcallCallingConv(LC.Op, LC.CC);
375 }
376 }
377
Bob Wilsonbc158992011-10-07 16:59:21 +0000378 // Use divmod compiler-rt calls for iOS 5.0 and later.
Cameron Esfahani943908b2013-08-29 20:23:14 +0000379 if (Subtarget->getTargetTriple().isiOS() &&
Bob Wilsonbc158992011-10-07 16:59:21 +0000380 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
381 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
382 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
383 }
384
Oliver Stannard11790b22014-08-11 09:12:32 +0000385 // The half <-> float conversion functions are always soft-float, but are
386 // needed for some targets which use a hard-float calling convention by
387 // default.
388 if (Subtarget->isAAPCS_ABI()) {
389 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
390 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
391 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
392 } else {
393 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
394 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
395 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
396 }
397
David Goodwin22c2fba2009-07-08 23:10:31 +0000398 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000399 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000400 else
Craig Topperc7242e02012-04-20 07:30:17 +0000401 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000402 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
403 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000404 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000405 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Evan Cheng10043e22007-01-19 07:51:42 +0000406 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000407
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000408 for (MVT VT : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000409 for (MVT InnerVT : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000410 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000411 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
412 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
413 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
414 }
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000415
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000416 setOperationAction(ISD::MULHS, VT, Expand);
417 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
418 setOperationAction(ISD::MULHU, VT, Expand);
419 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000420
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000421 setOperationAction(ISD::BSWAP, VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000422 }
423
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000424 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000425 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000426
Bob Wilson2e076c42009-06-22 23:27:02 +0000427 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000428 addDRTypeForNEON(MVT::v2f32);
429 addDRTypeForNEON(MVT::v8i8);
430 addDRTypeForNEON(MVT::v4i16);
431 addDRTypeForNEON(MVT::v2i32);
432 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000433
Owen Anderson9f944592009-08-11 20:47:22 +0000434 addQRTypeForNEON(MVT::v4f32);
435 addQRTypeForNEON(MVT::v2f64);
436 addQRTypeForNEON(MVT::v16i8);
437 addQRTypeForNEON(MVT::v8i16);
438 addQRTypeForNEON(MVT::v4i32);
439 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000440
Bob Wilson194a2512009-09-15 23:55:57 +0000441 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
442 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000443 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
444 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000445 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
446 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
447 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000448 // FIXME: Code duplication: FDIV and FREM are expanded always, see
449 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000450 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
451 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000452 // FIXME: Create unittest.
453 // In another words, find a way when "copysign" appears in DAG with vector
454 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000455 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000456 // FIXME: Code duplication: SETCC has custom operation action, see
457 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000458 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000459 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000460 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
461 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
462 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
463 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
464 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
465 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
466 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
467 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
468 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
469 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
470 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
471 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000472 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000473 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
474 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
475 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
476 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
477 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000478 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000479
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000480 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
481 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
482 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
483 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
484 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
485 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
486 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
487 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
488 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
489 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000490 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
491 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
492 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
493 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000494 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000495
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000496 // Mark v2f32 intrinsics.
497 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
498 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
499 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
500 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
501 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
502 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
503 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
504 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
505 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
506 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
507 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
508 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
509 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
510 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
511 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
512
Bob Wilson6cc46572009-09-16 00:32:15 +0000513 // Neon does not support some operations on v1i64 and v2i64 types.
514 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000515 // Custom handling for some quad-vector types to detect VMULL.
516 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
517 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
518 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000519 // Custom handling for some vector types to avoid expensive expansions
520 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
521 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
522 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
523 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000524 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
525 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000526 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000527 // a destination type that is wider than the source, and nor does
528 // it have a FP_TO_[SU]INT instruction with a narrower destination than
529 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000530 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
531 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000532 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
533 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000534
Eli Friedmane6385e62012-11-15 22:44:27 +0000535 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000536 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000537
Evan Chengb4eae132012-12-04 22:41:50 +0000538 // NEON does not have single instruction CTPOP for vectors with element
539 // types wider than 8-bits. However, custom lowering can leverage the
540 // v8i8/v16i8 vcnt instruction.
541 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
542 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
543 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
544 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
545
Jim Grosbach5f215872013-02-27 21:31:12 +0000546 // NEON only has FMA instructions as of VFP4.
547 if (!Subtarget->hasVFP4()) {
548 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
549 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
550 }
551
Bob Wilson06fce872011-02-07 17:43:21 +0000552 setTargetDAGCombine(ISD::INTRINSIC_VOID);
553 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000554 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
555 setTargetDAGCombine(ISD::SHL);
556 setTargetDAGCombine(ISD::SRL);
557 setTargetDAGCombine(ISD::SRA);
558 setTargetDAGCombine(ISD::SIGN_EXTEND);
559 setTargetDAGCombine(ISD::ZERO_EXTEND);
560 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsonc6c13a32010-02-18 06:05:53 +0000561 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000562 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000563 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000564 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
565 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000566 setTargetDAGCombine(ISD::FP_TO_SINT);
567 setTargetDAGCombine(ISD::FP_TO_UINT);
568 setTargetDAGCombine(ISD::FDIV);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000569 setTargetDAGCombine(ISD::LOAD);
Nadav Rotem097106b2011-10-15 20:03:12 +0000570
James Molloy547d4c02012-02-20 09:24:05 +0000571 // It is legal to extload from v4i8 to v4i16 or v4i32.
572 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
573 MVT::v4i16, MVT::v2i16,
574 MVT::v2i32};
575 for (unsigned i = 0; i < 6; ++i) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000576 for (MVT VT : MVT::integer_vector_valuetypes()) {
577 setLoadExtAction(ISD::EXTLOAD, VT, Tys[i], Legal);
578 setLoadExtAction(ISD::ZEXTLOAD, VT, Tys[i], Legal);
579 setLoadExtAction(ISD::SEXTLOAD, VT, Tys[i], Legal);
580 }
James Molloy547d4c02012-02-20 09:24:05 +0000581 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000582 }
583
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000584 // ARM and Thumb2 support UMLAL/SMLAL.
585 if (!Subtarget->isThumb1Only())
586 setTargetDAGCombine(ISD::ADDC);
587
Oliver Stannard51b1d462014-08-21 12:50:31 +0000588 if (Subtarget->isFPOnlySP()) {
589 // When targetting a floating-point unit with only single-precision
590 // operations, f64 is legal for the few double-precision instructions which
591 // are present However, no double-precision operations other than moves,
592 // loads and stores are provided by the hardware.
593 setOperationAction(ISD::FADD, MVT::f64, Expand);
594 setOperationAction(ISD::FSUB, MVT::f64, Expand);
595 setOperationAction(ISD::FMUL, MVT::f64, Expand);
596 setOperationAction(ISD::FMA, MVT::f64, Expand);
597 setOperationAction(ISD::FDIV, MVT::f64, Expand);
598 setOperationAction(ISD::FREM, MVT::f64, Expand);
599 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
600 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
601 setOperationAction(ISD::FNEG, MVT::f64, Expand);
602 setOperationAction(ISD::FABS, MVT::f64, Expand);
603 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
604 setOperationAction(ISD::FSIN, MVT::f64, Expand);
605 setOperationAction(ISD::FCOS, MVT::f64, Expand);
606 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
607 setOperationAction(ISD::FPOW, MVT::f64, Expand);
608 setOperationAction(ISD::FLOG, MVT::f64, Expand);
609 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
610 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
611 setOperationAction(ISD::FEXP, MVT::f64, Expand);
612 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
613 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
614 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
615 setOperationAction(ISD::FRINT, MVT::f64, Expand);
616 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
617 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
618 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
619 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
620 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000621
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000622 computeRegisterProperties(Subtarget->getRegisterInfo());
Evan Cheng10043e22007-01-19 07:51:42 +0000623
Tim Northover4e80b582014-07-18 13:01:19 +0000624 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000625 for (MVT VT : MVT::fp_valuetypes()) {
626 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
627 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
628 }
Tim Northover4e80b582014-07-18 13:01:19 +0000629
630 // ... or truncating stores
631 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
632 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
633 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000634
Duncan Sands95d46ef2008-01-23 20:39:46 +0000635 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000636 for (MVT VT : MVT::integer_valuetypes())
637 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000638
Evan Cheng10043e22007-01-19 07:51:42 +0000639 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000640 if (!Subtarget->isThumb1Only()) {
641 for (unsigned im = (unsigned)ISD::PRE_INC;
642 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000643 setIndexedLoadAction(im, MVT::i1, Legal);
644 setIndexedLoadAction(im, MVT::i8, Legal);
645 setIndexedLoadAction(im, MVT::i16, Legal);
646 setIndexedLoadAction(im, MVT::i32, Legal);
647 setIndexedStoreAction(im, MVT::i1, Legal);
648 setIndexedStoreAction(im, MVT::i8, Legal);
649 setIndexedStoreAction(im, MVT::i16, Legal);
650 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000651 }
Evan Cheng10043e22007-01-19 07:51:42 +0000652 }
653
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000654 setOperationAction(ISD::SADDO, MVT::i32, Custom);
655 setOperationAction(ISD::UADDO, MVT::i32, Custom);
656 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
657 setOperationAction(ISD::USUBO, MVT::i32, Custom);
658
Evan Cheng10043e22007-01-19 07:51:42 +0000659 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000660 setOperationAction(ISD::MUL, MVT::i64, Expand);
661 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000662 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000663 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
664 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000665 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000666 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
667 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000668 setOperationAction(ISD::MULHS, MVT::i32, Expand);
669
Jim Grosbach5d994042009-10-31 19:38:01 +0000670 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000671 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000672 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000673 setOperationAction(ISD::SRL, MVT::i64, Custom);
674 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000675
Evan Chenge8916542011-08-30 01:34:54 +0000676 if (!Subtarget->isThumb1Only()) {
677 // FIXME: We should do this for Thumb1 as well.
678 setOperationAction(ISD::ADDC, MVT::i32, Custom);
679 setOperationAction(ISD::ADDE, MVT::i32, Custom);
680 setOperationAction(ISD::SUBC, MVT::i32, Custom);
681 setOperationAction(ISD::SUBE, MVT::i32, Custom);
682 }
683
Evan Cheng10043e22007-01-19 07:51:42 +0000684 // ARM does not have ROTL.
Owen Anderson9f944592009-08-11 20:47:22 +0000685 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach8546ec92010-01-18 19:58:49 +0000686 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000687 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000688 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000689 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000690
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000691 // These just redirect to CTTZ and CTLZ on ARM.
692 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
693 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
694
Tim Northoverbc933082013-05-23 19:11:20 +0000695 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
696
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000697 // Only ARMv6 has BSWAP.
698 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000699 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000700
Bob Wilsone8a549c2012-09-29 21:43:49 +0000701 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
702 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
703 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbach92d999002010-05-05 20:44:35 +0000704 setOperationAction(ISD::SDIV, MVT::i32, Expand);
705 setOperationAction(ISD::UDIV, MVT::i32, Expand);
706 }
Renato Golin87610692013-07-16 09:32:17 +0000707
708 // FIXME: Also set divmod for SREM on EABI
Chad Rosierad7c9102014-08-23 18:29:43 +0000709 setOperationAction(ISD::SREM, MVT::i32, Expand);
710 setOperationAction(ISD::UREM, MVT::i32, Expand);
711 // Register based DivRem for AEABI (RTABI 4.2)
712 if (Subtarget->isTargetAEABI()) {
713 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
714 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
715 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
716 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
717 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
718 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
719 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
720 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
721
722 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
723 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
724 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
725 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
726 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
727 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
728 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
729 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
730
731 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
732 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
733 } else {
Renato Golin87610692013-07-16 09:32:17 +0000734 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
735 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
736 }
Bob Wilson7117a912009-03-20 22:42:55 +0000737
Owen Anderson9f944592009-08-11 20:47:22 +0000738 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
739 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
740 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
741 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000742 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000743
Evan Cheng74d92c12011-04-08 21:37:21 +0000744 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000745
Evan Cheng10043e22007-01-19 07:51:42 +0000746 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000747 setOperationAction(ISD::VASTART, MVT::Other, Custom);
748 setOperationAction(ISD::VAARG, MVT::Other, Expand);
749 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
750 setOperationAction(ISD::VAEND, MVT::Other, Expand);
751 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
752 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000753
Tim Northoverd6a729b2014-01-06 14:28:05 +0000754 if (!Subtarget->isTargetMachO()) {
755 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000756 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000757 setExceptionPointerRegister(ARM::R0);
758 setExceptionSelectorRegister(ARM::R1);
759 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000760
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000761 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
762 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
763 else
764 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
765
Evan Cheng6e809de2010-08-11 06:22:01 +0000766 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000767 // the default expansion. If we are targeting a single threaded system,
768 // then set them all for expand so we can lower them later into their
769 // non-atomic form.
770 if (TM.Options.ThreadModel == ThreadModel::Single)
771 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
772 else if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000773 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
774 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000775 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000776
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000777 // On v8, we have particularly efficient implementations of atomic fences
778 // if they can be combined with nearby atomic loads and stores.
779 if (!Subtarget->hasV8Ops()) {
Robin Morissetd18cda62014-08-15 22:17:28 +0000780 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000781 setInsertFencesForAtomic(true);
782 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000783 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000784 // If there's anything we can use as a barrier, go through custom lowering
785 // for ATOMIC_FENCE.
786 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
787 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
788
Jim Grosbach6860bb72010-06-18 22:35:32 +0000789 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000790 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000791 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000792 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000793 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000794 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000795 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000796 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000797 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000798 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000799 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000800 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000801 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000802 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
803 // Unordered/Monotonic case.
804 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
805 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000806 }
Evan Cheng10043e22007-01-19 07:51:42 +0000807
Evan Cheng21acf9f2010-11-04 05:19:35 +0000808 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000809
Eli Friedman8cfa7712010-06-26 04:36:50 +0000810 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
811 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000812 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
813 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000814 }
Owen Anderson9f944592009-08-11 20:47:22 +0000815 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000816
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000817 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
818 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000819 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000820 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000821 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000822 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
823 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000824
825 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000826 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbach31984832010-07-07 00:07:57 +0000827 if (Subtarget->isTargetDarwin()) {
828 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
829 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall7d84ece2011-05-29 19:50:32 +0000830 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbach31984832010-07-07 00:07:57 +0000831 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000832
Owen Anderson9f944592009-08-11 20:47:22 +0000833 setOperationAction(ISD::SETCC, MVT::i32, Expand);
834 setOperationAction(ISD::SETCC, MVT::f32, Expand);
835 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000836 setOperationAction(ISD::SELECT, MVT::i32, Custom);
837 setOperationAction(ISD::SELECT, MVT::f32, Custom);
838 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000839 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
840 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
841 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000842
Owen Anderson9f944592009-08-11 20:47:22 +0000843 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
844 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
845 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
846 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
847 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000848
Dan Gohman482732a2007-10-11 23:21:31 +0000849 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000850 setOperationAction(ISD::FSIN, MVT::f64, Expand);
851 setOperationAction(ISD::FSIN, MVT::f32, Expand);
852 setOperationAction(ISD::FCOS, MVT::f32, Expand);
853 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000854 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
855 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000856 setOperationAction(ISD::FREM, MVT::f64, Expand);
857 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000858 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
859 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000860 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
861 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000862 }
Owen Anderson9f944592009-08-11 20:47:22 +0000863 setOperationAction(ISD::FPOW, MVT::f64, Expand);
864 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000865
Evan Chengd0007f32012-04-10 21:40:28 +0000866 if (!Subtarget->hasVFP4()) {
867 setOperationAction(ISD::FMA, MVT::f64, Expand);
868 setOperationAction(ISD::FMA, MVT::f32, Expand);
869 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000870
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000871 // Various VFP goodness
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000872 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilsone4191e72010-03-19 22:51:32 +0000873 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
874 if (Subtarget->hasVFP2()) {
875 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
876 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
877 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
878 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
879 }
Tim Northover53f3bcf2014-07-17 11:27:04 +0000880
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000881 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
882 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
Tim Northover53f3bcf2014-07-17 11:27:04 +0000883 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
884 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
885 }
886
887 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000888 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +0000889 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
890 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000891 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000892 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000893
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000894 // Combine sin / cos into one node or libcall if possible.
895 if (Subtarget->hasSinCos()) {
896 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
897 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Bob Wilson9868d712014-10-09 05:43:30 +0000898 if (Subtarget->getTargetTriple().isiOS()) {
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000899 // For iOS, we don't want to the normal expansion of a libcall to
900 // sincos. We want to issue a libcall to __sincos_stret.
901 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
902 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
903 }
904 }
Evan Cheng10043e22007-01-19 07:51:42 +0000905
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000906 // FP-ARMv8 implements a lot of rounding-like FP operations.
907 if (Subtarget->hasFPARMv8()) {
908 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
909 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
910 setOperationAction(ISD::FROUND, MVT::f32, Legal);
911 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
912 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
913 setOperationAction(ISD::FRINT, MVT::f32, Legal);
914 if (!Subtarget->isFPOnlySP()) {
915 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
916 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
917 setOperationAction(ISD::FROUND, MVT::f64, Legal);
918 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
919 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
920 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Chad Rosierb1bbf6f2014-08-15 21:38:16 +0000921 }
922 }
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000923 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000924 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000925 setTargetDAGCombine(ISD::ADD);
926 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000927 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000928 setTargetDAGCombine(ISD::AND);
929 setTargetDAGCombine(ISD::OR);
930 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000931
Evan Chengf258a152012-02-23 02:58:19 +0000932 if (Subtarget->hasV6Ops())
933 setTargetDAGCombine(ISD::SRL);
934
Evan Cheng10043e22007-01-19 07:51:42 +0000935 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +0000936
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000937 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
938 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +0000939 setSchedulingPreference(Sched::RegPressure);
940 else
941 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +0000942
Evan Cheng3ae2b792011-01-06 06:52:41 +0000943 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000944 MaxStoresPerMemset = 8;
945 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
946 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
947 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
948 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
949 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengb71233f2010-06-26 01:52:05 +0000950
Rafael Espindolaa76eccf2010-07-11 04:01:49 +0000951 // On ARM arguments smaller than 4 bytes are extended, so all arguments
952 // are at least 4 bytes aligned.
953 setMinStackArgumentAlignment(4);
954
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000955 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000956 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000957
Eli Friedman2518f832011-05-06 20:34:06 +0000958 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +0000959}
960
Andrew Trick43f25632011-01-19 02:35:27 +0000961// FIXME: It might make sense to define the representative register class as the
962// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
963// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
964// SPR's representative would be DPR_VFP2. This should work well if register
965// pressure tracking were modified such that a register use would increment the
966// pressure of the register class's representative and all of it's super
967// classes' representatives transitively. We have not implemented this because
968// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000969// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +0000970// and extractions.
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000971std::pair<const TargetRegisterClass *, uint8_t>
972ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
973 MVT VT) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000974 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +0000975 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000976 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +0000977 default:
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000978 return TargetLowering::findRepresentativeClass(TRI, VT);
Evan Cheng28590382010-07-21 23:53:58 +0000979 // Use DPR as representative register class for all floating point
980 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
981 // the cost is 1 for both f32 and f64.
982 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +0000983 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +0000984 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +0000985 // When NEON is used for SP, only half of the register file is available
986 // because operations that define both SP and DP results will be constrained
987 // to the VFP2 class (D0-D15). We currently model this constraint prior to
988 // coalescing by double-counting the SP regs. See the FIXME above.
989 if (Subtarget->useNEONForSinglePrecisionFP())
990 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000991 break;
992 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
993 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +0000994 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000995 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000996 break;
997 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000998 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000999 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +00001000 break;
1001 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001002 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001003 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001004 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001005 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001006 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001007}
1008
Evan Cheng10043e22007-01-19 07:51:42 +00001009const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
1010 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001011 default: return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001012 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001013 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001014 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
1015 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001016 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001017 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
1018 case ARMISD::tCALL: return "ARMISD::tCALL";
1019 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1020 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001021 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001022 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001023 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001024 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1025 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001026 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001027 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001028 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1029 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001030 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001031 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001032
Evan Cheng10043e22007-01-19 07:51:42 +00001033 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001034
Jim Grosbach8546ec92010-01-18 19:58:49 +00001035 case ARMISD::RBIT: return "ARMISD::RBIT";
1036
Bob Wilsone4191e72010-03-19 22:51:32 +00001037 case ARMISD::FTOSI: return "ARMISD::FTOSI";
1038 case ARMISD::FTOUI: return "ARMISD::FTOUI";
1039 case ARMISD::SITOF: return "ARMISD::SITOF";
1040 case ARMISD::UITOF: return "ARMISD::UITOF";
1041
Evan Cheng10043e22007-01-19 07:51:42 +00001042 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1043 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1044 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001045
Evan Chenge8916542011-08-30 01:34:54 +00001046 case ARMISD::ADDC: return "ARMISD::ADDC";
1047 case ARMISD::ADDE: return "ARMISD::ADDE";
1048 case ARMISD::SUBC: return "ARMISD::SUBC";
1049 case ARMISD::SUBE: return "ARMISD::SUBE";
1050
Bob Wilson22806742010-09-22 22:09:21 +00001051 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1052 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001053
Evan Chengec6d7c92009-10-28 06:55:03 +00001054 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
1055 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
1056
Dale Johannesend679ff72010-06-03 21:09:53 +00001057 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001058
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001059 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001060
Evan Chengb972e562009-08-07 00:34:42 +00001061 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1062
Bob Wilson7ed59712010-10-30 00:54:37 +00001063 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001064
Evan Cheng8740ee32010-11-03 06:34:55 +00001065 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1066
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001067 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
1068
Bob Wilson2e076c42009-06-22 23:27:02 +00001069 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001070 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001071 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001072 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1073 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001074 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1075 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001076 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1077 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001078 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1079 case ARMISD::VTST: return "ARMISD::VTST";
1080
1081 case ARMISD::VSHL: return "ARMISD::VSHL";
1082 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1083 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001084 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1085 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1086 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1087 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1088 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1089 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1090 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1091 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1092 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1093 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1094 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1095 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
1096 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1097 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001098 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001099 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001100 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001101 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001102 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001103 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001104 case ARMISD::VREV64: return "ARMISD::VREV64";
1105 case ARMISD::VREV32: return "ARMISD::VREV32";
1106 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001107 case ARMISD::VZIP: return "ARMISD::VZIP";
1108 case ARMISD::VUZP: return "ARMISD::VUZP";
1109 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001110 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1111 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001112 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1113 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001114 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1115 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001116 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilsonc6c13a32010-02-18 06:05:53 +00001117 case ARMISD::FMAX: return "ARMISD::FMAX";
1118 case ARMISD::FMIN: return "ARMISD::FMIN";
Joey Goulye3dd6842013-08-23 12:01:13 +00001119 case ARMISD::VMAXNM: return "ARMISD::VMAX";
1120 case ARMISD::VMINNM: return "ARMISD::VMIN";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001121 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001122 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1123 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001124 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilson2d790df2010-11-28 06:51:26 +00001125 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1126 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1127 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001128 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1129 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1130 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1131 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1132 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1133 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1134 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1135 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1136 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1137 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1138 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1139 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1140 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1141 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1142 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1143 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1144 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001145 }
1146}
1147
Matt Arsenault758659232013-05-18 00:21:46 +00001148EVT ARMTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Duncan Sandsf2641e12011-09-06 19:07:46 +00001149 if (!VT.isVector()) return getPointerTy();
1150 return VT.changeVectorElementTypeToInteger();
1151}
1152
Evan Cheng4cad68e2010-05-15 02:18:07 +00001153/// getRegClassFor - Return the register class that should be used for the
1154/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001155const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001156 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1157 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1158 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001159 if (Subtarget->hasNEON()) {
1160 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001161 return &ARM::QQPRRegClass;
1162 if (VT == MVT::v8i64)
1163 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001164 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001165 return TargetLowering::getRegClassFor(VT);
1166}
1167
Eric Christopher84bdfd82010-07-21 22:26:11 +00001168// Create a fast isel object.
1169FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001170ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1171 const TargetLibraryInfo *libInfo) const {
1172 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001173}
1174
Evan Cheng4401f882010-05-20 23:26:43 +00001175Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001176 unsigned NumVals = N->getNumValues();
1177 if (!NumVals)
1178 return Sched::RegPressure;
1179
1180 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001181 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001182 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001183 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001184 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001185 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001186 }
Evan Chengbf914992010-05-28 23:25:23 +00001187
1188 if (!N->isMachineOpcode())
1189 return Sched::RegPressure;
1190
1191 // Load are scheduled for latency even if there instruction itinerary
1192 // is not available.
Eric Christopher1889fdc2015-01-29 00:19:39 +00001193 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001194 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001195
Evan Cheng6cc775f2011-06-28 19:10:37 +00001196 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001197 return Sched::RegPressure;
1198 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001199 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001200 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001201
Evan Cheng4401f882010-05-20 23:26:43 +00001202 return Sched::RegPressure;
1203}
1204
Evan Cheng10043e22007-01-19 07:51:42 +00001205//===----------------------------------------------------------------------===//
1206// Lowering Code
1207//===----------------------------------------------------------------------===//
1208
Evan Cheng10043e22007-01-19 07:51:42 +00001209/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1210static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1211 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001212 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001213 case ISD::SETNE: return ARMCC::NE;
1214 case ISD::SETEQ: return ARMCC::EQ;
1215 case ISD::SETGT: return ARMCC::GT;
1216 case ISD::SETGE: return ARMCC::GE;
1217 case ISD::SETLT: return ARMCC::LT;
1218 case ISD::SETLE: return ARMCC::LE;
1219 case ISD::SETUGT: return ARMCC::HI;
1220 case ISD::SETUGE: return ARMCC::HS;
1221 case ISD::SETULT: return ARMCC::LO;
1222 case ISD::SETULE: return ARMCC::LS;
1223 }
1224}
1225
Bob Wilsona2e83332009-09-09 23:14:54 +00001226/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1227static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001228 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001229 CondCode2 = ARMCC::AL;
1230 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001231 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001232 case ISD::SETEQ:
1233 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1234 case ISD::SETGT:
1235 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1236 case ISD::SETGE:
1237 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1238 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001239 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001240 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1241 case ISD::SETO: CondCode = ARMCC::VC; break;
1242 case ISD::SETUO: CondCode = ARMCC::VS; break;
1243 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1244 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1245 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1246 case ISD::SETLT:
1247 case ISD::SETULT: CondCode = ARMCC::LT; break;
1248 case ISD::SETLE:
1249 case ISD::SETULE: CondCode = ARMCC::LE; break;
1250 case ISD::SETNE:
1251 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1252 }
Evan Cheng10043e22007-01-19 07:51:42 +00001253}
1254
Bob Wilsona4c22902009-04-17 19:07:39 +00001255//===----------------------------------------------------------------------===//
1256// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001257//===----------------------------------------------------------------------===//
1258
1259#include "ARMGenCallingConv.inc"
1260
Oliver Stannardc24f2172014-05-09 14:01:47 +00001261/// getEffectiveCallingConv - Get the effective calling convention, taking into
1262/// account presence of floating point hardware and calling convention
1263/// limitations, such as support for variadic functions.
1264CallingConv::ID
1265ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1266 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001267 switch (CC) {
1268 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001269 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001270 case CallingConv::ARM_AAPCS:
1271 case CallingConv::ARM_APCS:
1272 case CallingConv::GHC:
1273 return CC;
1274 case CallingConv::ARM_AAPCS_VFP:
1275 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1276 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001277 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001278 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001279 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001280 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1281 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001282 return CallingConv::ARM_AAPCS_VFP;
1283 else
1284 return CallingConv::ARM_AAPCS;
1285 case CallingConv::Fast:
1286 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001287 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001288 return CallingConv::Fast;
1289 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001290 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001291 return CallingConv::ARM_AAPCS_VFP;
1292 else
1293 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001294 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001295}
1296
1297/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1298/// CallingConvention.
1299CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1300 bool Return,
1301 bool isVarArg) const {
1302 switch (getEffectiveCallingConv(CC, isVarArg)) {
1303 default:
1304 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001305 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001306 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001307 case CallingConv::ARM_AAPCS:
1308 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1309 case CallingConv::ARM_AAPCS_VFP:
1310 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1311 case CallingConv::Fast:
1312 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001313 case CallingConv::GHC:
1314 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001315 }
1316}
1317
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001318/// LowerCallResult - Lower the result values of a call into the
1319/// appropriate copies out of appropriate physical registers.
1320SDValue
1321ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001322 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001323 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001324 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001325 SmallVectorImpl<SDValue> &InVals,
1326 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001327
Bob Wilsona4c22902009-04-17 19:07:39 +00001328 // Assign locations to each value returned by this call.
1329 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001330 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1331 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001332 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001333 CCAssignFnForNode(CallConv, /* Return*/ true,
1334 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001335
1336 // Copy all of the result registers out of their specified physreg.
1337 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1338 CCValAssign VA = RVLocs[i];
1339
Stephen Linb8bd2322013-04-20 05:14:40 +00001340 // Pass 'this' value directly from the argument to return value, to avoid
1341 // reg unit interference
1342 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001343 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1344 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001345 InVals.push_back(ThisVal);
1346 continue;
1347 }
1348
Bob Wilson0041bd32009-04-25 00:33:20 +00001349 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001350 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001351 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001352 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001353 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001354 Chain = Lo.getValue(1);
1355 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001356 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001357 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001358 InFlag);
1359 Chain = Hi.getValue(1);
1360 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001361 if (!Subtarget->isLittle())
1362 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001363 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001364
Owen Anderson9f944592009-08-11 20:47:22 +00001365 if (VA.getLocVT() == MVT::v2f64) {
1366 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1367 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1368 DAG.getConstant(0, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001369
1370 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001371 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001372 Chain = Lo.getValue(1);
1373 InFlag = Lo.getValue(2);
1374 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001375 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001376 Chain = Hi.getValue(1);
1377 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001378 if (!Subtarget->isLittle())
1379 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001380 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001381 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1382 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001383 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001384 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001385 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1386 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001387 Chain = Val.getValue(1);
1388 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001389 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001390
1391 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001392 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001393 case CCValAssign::Full: break;
1394 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001395 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001396 break;
1397 }
1398
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001399 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001400 }
1401
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001402 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001403}
1404
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001405/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001406SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001407ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1408 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001409 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001410 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001411 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001412 unsigned LocMemOffset = VA.getLocMemOffset();
1413 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1414 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilsona4c22902009-04-17 19:07:39 +00001415 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner886250c2010-09-21 18:51:21 +00001416 MachinePointerInfo::getStack(LocMemOffset),
David Greene0d0149f2010-02-15 16:55:24 +00001417 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001418}
1419
Andrew Trickef9de2a2013-05-25 02:42:55 +00001420void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001421 SDValue Chain, SDValue &Arg,
1422 RegsToPassVector &RegsToPass,
1423 CCValAssign &VA, CCValAssign &NextVA,
1424 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001425 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001426 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001427
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001428 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001429 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001430 unsigned id = Subtarget->isLittle() ? 0 : 1;
1431 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001432
1433 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001434 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001435 else {
1436 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001437 if (!StackPtr.getNode())
Bob Wilson2e076c42009-06-22 23:27:02 +00001438 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1439
Christian Pirkerb5728192014-05-08 14:06:24 +00001440 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001441 dl, DAG, NextVA,
1442 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001443 }
1444}
1445
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001446/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001447/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1448/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001449SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001450ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001451 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001452 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001453 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001454 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1455 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1456 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001457 SDValue Chain = CLI.Chain;
1458 SDValue Callee = CLI.Callee;
1459 bool &isTailCall = CLI.IsTailCall;
1460 CallingConv::ID CallConv = CLI.CallConv;
1461 bool doesNotRet = CLI.DoesNotReturn;
1462 bool isVarArg = CLI.IsVarArg;
1463
Dale Johannesend679ff72010-06-03 21:09:53 +00001464 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001465 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1466 bool isThisReturn = false;
1467 bool isSibCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001468
Bob Wilson8decdc42011-10-07 17:17:49 +00001469 // Disable tail calls if they're not supported.
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001470 if (!Subtarget->supportsTailCall() || MF.getTarget().Options.DisableTailCalls)
Bob Wilson3c9ed762010-08-13 22:43:33 +00001471 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001472
Dale Johannesend679ff72010-06-03 21:09:53 +00001473 if (isTailCall) {
1474 // Check if it's really possible to do a tail call.
1475 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001476 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001477 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001478 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1479 report_fatal_error("failed to perform tail call elimination on a call "
1480 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001481 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1482 // detected sibcalls.
1483 if (isTailCall) {
1484 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001485 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001486 }
1487 }
Evan Cheng10043e22007-01-19 07:51:42 +00001488
Bob Wilsona4c22902009-04-17 19:07:39 +00001489 // Analyze operands of the call, assigning locations to each operand.
1490 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001491 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1492 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001493 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001494 CCAssignFnForNode(CallConv, /* Return*/ false,
1495 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001496
Bob Wilsona4c22902009-04-17 19:07:39 +00001497 // Get a count of how many bytes are to be pushed on the stack.
1498 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001499
Dale Johannesend679ff72010-06-03 21:09:53 +00001500 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001501 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001502 NumBytes = 0;
1503
Evan Cheng10043e22007-01-19 07:51:42 +00001504 // Adjust the stack pointer for the new arguments...
1505 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001506 if (!isSibCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00001507 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
1508 dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001509
Jim Grosbach6ad4bcb2010-02-24 01:43:03 +00001510 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +00001511
Bob Wilson2e076c42009-06-22 23:27:02 +00001512 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001513 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001514
Bob Wilsona4c22902009-04-17 19:07:39 +00001515 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001516 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001517 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1518 i != e;
1519 ++i, ++realArgIdx) {
1520 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001521 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001522 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001523 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001524
Bob Wilsona4c22902009-04-17 19:07:39 +00001525 // Promote the value if needed.
1526 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001527 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001528 case CCValAssign::Full: break;
1529 case CCValAssign::SExt:
1530 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1531 break;
1532 case CCValAssign::ZExt:
1533 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1534 break;
1535 case CCValAssign::AExt:
1536 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1537 break;
1538 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001539 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001540 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001541 }
1542
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001543 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001544 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001545 if (VA.getLocVT() == MVT::v2f64) {
1546 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1547 DAG.getConstant(0, MVT::i32));
1548 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1549 DAG.getConstant(1, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001550
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001551 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001552 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1553
1554 VA = ArgLocs[++i]; // skip ahead to next loc
1555 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001556 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001557 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1558 } else {
1559 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001560
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001561 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1562 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001563 }
1564 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001565 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001566 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001567 }
1568 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001569 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1570 assert(VA.getLocVT() == MVT::i32 &&
1571 "unexpected calling convention register assignment");
1572 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001573 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001574 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001575 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001576 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001577 } else if (isByVal) {
1578 assert(VA.isMemLoc());
1579 unsigned offset = 0;
1580
1581 // True if this byval aggregate will be split between registers
1582 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001583 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001584 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001585
1586 if (CurByValIdx < ByValArgsCount) {
1587
1588 unsigned RegBegin, RegEnd;
1589 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1590
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001591 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1592 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001593 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001594 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1595 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1596 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1597 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001598 false, false, false,
1599 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001600 MemOpChains.push_back(Load.getValue(1));
1601 RegsToPass.push_back(std::make_pair(j, Load));
1602 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001603
1604 // If parameter size outsides register area, "offset" value
1605 // helps us to calculate stack slot for remained part properly.
1606 offset = RegEnd - RegBegin;
1607
1608 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001609 }
1610
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001611 if (Flags.getByValSize() > 4*offset) {
Manman Ren9f911162012-06-01 02:44:42 +00001612 unsigned LocMemOffset = VA.getLocMemOffset();
1613 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1614 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1615 StkPtrOff);
1616 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1617 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1618 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1619 MVT::i32);
Manman Rene8735522012-06-01 19:33:18 +00001620 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001621
Manman Ren9f911162012-06-01 02:44:42 +00001622 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001623 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001624 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001625 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001626 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001627 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001628 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001629
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001630 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1631 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001632 }
Evan Cheng10043e22007-01-19 07:51:42 +00001633 }
1634
1635 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001636 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001637
1638 // Build a sequence of copy-to-reg nodes chained together with token chain
1639 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001640 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001641 // Tail call byval lowering might overwrite argument registers so in case of
1642 // tail call optimization the copies to registers are lowered later.
1643 if (!isTailCall)
1644 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1645 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1646 RegsToPass[i].second, InFlag);
1647 InFlag = Chain.getValue(1);
1648 }
Evan Cheng10043e22007-01-19 07:51:42 +00001649
Dale Johannesend679ff72010-06-03 21:09:53 +00001650 // For tail calls lower the arguments to the 'real' stack slot.
1651 if (isTailCall) {
1652 // Force all the incoming stack arguments to be loaded from the stack
1653 // before any new outgoing arguments are stored to the stack, because the
1654 // outgoing stack slots may alias the incoming argument stack slots, and
1655 // the alias isn't otherwise explicit. This is slightly more conservative
1656 // than necessary, because it means that each store effectively depends
1657 // on every argument instead of just those arguments it would clobber.
1658
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001659 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001660 InFlag = SDValue();
1661 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1662 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1663 RegsToPass[i].second, InFlag);
1664 InFlag = Chain.getValue(1);
1665 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001666 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001667 }
1668
Bill Wendling24c79f22008-09-16 21:48:12 +00001669 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1670 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1671 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001672 bool isDirect = false;
1673 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001674 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001675 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001676
1677 if (EnableARMLongCalls) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001678 assert((Subtarget->isTargetWindows() ||
1679 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1680 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001681 // Handle a global address or an external symbol. If it's not one of
1682 // those, the target's already in a register, so we don't need to do
1683 // anything extra.
1684 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001685 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001686 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001687 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001688 ARMConstantPoolValue *CPV =
1689 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1690
Jim Grosbach32bb3622010-04-14 22:28:31 +00001691 // Get the address of the callee into a register
1692 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1693 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1694 Callee = DAG.getLoad(getPointerTy(), dl,
1695 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001696 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001697 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001698 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1699 const char *Sym = S->getSymbol();
1700
1701 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001702 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001703 ARMConstantPoolValue *CPV =
1704 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1705 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001706 // Get the address of the callee into a register
1707 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1708 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1709 Callee = DAG.getLoad(getPointerTy(), dl,
1710 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001711 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001712 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001713 }
1714 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001715 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001716 isDirect = true;
Chris Lattner55452c22009-07-15 04:12:33 +00001717 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Tim Northoverd6a729b2014-01-06 14:28:05 +00001718 bool isStub = (isExt && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001719 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001720 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00001721 // ARM call to a local ARM function is predicable.
Evan Chengf128bdc2010-06-16 07:35:02 +00001722 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001723 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001724 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001725 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Tim Northover72360d22013-12-02 10:35:41 +00001726 Callee = DAG.getNode(ARMISD::WrapperPIC, dl, getPointerTy(),
Tim Northoverd4d294d2014-08-06 11:13:06 +00001727 DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
1728 0, ARMII::MO_NONLAZY));
1729 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
1730 MachinePointerInfo::getGOT(), false, false, true, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001731 } else if (Subtarget->isTargetCOFF()) {
1732 assert(Subtarget->isTargetWindows() &&
1733 "Windows is the only supported COFF target");
1734 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1735 ? ARMII::MO_DLLIMPORT
1736 : ARMII::MO_NO_FLAG;
1737 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), /*Offset=*/0,
1738 TargetFlags);
1739 if (GV->hasDLLImportStorageClass())
1740 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
1741 DAG.getNode(ARMISD::Wrapper, dl, getPointerTy(),
1742 Callee), MachinePointerInfo::getGOT(),
1743 false, false, false, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001744 } else {
1745 // On ELF targets for PIC code, direct calls should go through the PLT
1746 unsigned OpFlags = 0;
1747 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001748 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001749 OpFlags = ARMII::MO_PLT;
1750 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1751 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001752 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001753 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001754 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001755 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001756 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Cheng83f35172007-01-30 20:37:08 +00001757 // tBX takes a register source operand.
1758 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001759 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001760 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001761 ARMConstantPoolValue *CPV =
1762 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1763 ARMPCLabelIndex, 4);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001764 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001765 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen021052a2009-02-04 20:06:27 +00001766 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Chengcdbb70c2009-10-31 03:39:36 +00001767 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001768 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001769 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00001770 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson7117a912009-03-20 22:42:55 +00001771 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen021052a2009-02-04 20:06:27 +00001772 getPointerTy(), Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001773 } else {
1774 unsigned OpFlags = 0;
1775 // On ELF targets for PIC code, direct calls should go through the PLT
1776 if (Subtarget->isTargetELF() &&
1777 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1778 OpFlags = ARMII::MO_PLT;
1779 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1780 }
Evan Cheng10043e22007-01-19 07:51:42 +00001781 }
1782
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001783 // FIXME: handle tail calls differently.
1784 unsigned CallOpc;
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00001785 bool HasMinSizeAttr = MF.getFunction()->hasFnAttribute(Attribute::MinSize);
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001786 if (Subtarget->isThumb()) {
1787 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001788 CallOpc = ARMISD::CALL_NOLINK;
1789 else
1790 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1791 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001792 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001793 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001794 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet8e1fe842012-11-02 21:32:17 +00001795 // Emit regular call when code size is the priority
1796 !HasMinSizeAttr)
Evan Cheng65f9d192012-02-28 18:51:51 +00001797 // "mov lr, pc; b _foo" to avoid confusing the RSP
1798 CallOpc = ARMISD::CALL_NOLINK;
1799 else
1800 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001801 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001802
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001803 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001804 Ops.push_back(Chain);
1805 Ops.push_back(Callee);
1806
1807 // Add argument registers to the end of the list so that they are known live
1808 // into the call.
1809 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1810 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1811 RegsToPass[i].second.getValueType()));
1812
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001813 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001814 if (!isTailCall) {
1815 const uint32_t *Mask;
Eric Christopher1889fdc2015-01-29 00:19:39 +00001816 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001817 if (isThisReturn) {
1818 // For 'this' returns, use the R0-preserving mask if applicable
1819 Mask = ARI->getThisReturnPreservedMask(CallConv);
1820 if (!Mask) {
1821 // Set isThisReturn to false if the calling convention is not one that
1822 // allows 'returned' to be modeled in this way, so LowerCallResult does
1823 // not try to pass 'this' straight through
1824 isThisReturn = false;
1825 Mask = ARI->getCallPreservedMask(CallConv);
1826 }
1827 } else
Stephen Linff7fcee2013-06-26 21:42:14 +00001828 Mask = ARI->getCallPreservedMask(CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001829
Matthias Braunc22630e2013-10-04 16:52:54 +00001830 assert(Mask && "Missing call preserved mask for calling convention");
1831 Ops.push_back(DAG.getRegisterMask(Mask));
1832 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001833
Gabor Greiff304a7a2008-08-28 21:40:38 +00001834 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001835 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001836
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001837 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001838 if (isTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001839 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Dale Johannesend679ff72010-06-03 21:09:53 +00001840
Duncan Sands739a0542008-07-02 17:40:58 +00001841 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001842 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001843 InFlag = Chain.getValue(1);
1844
Chris Lattner27539552008-10-11 22:08:30 +00001845 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001846 DAG.getIntPtrConstant(0, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001847 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001848 InFlag = Chain.getValue(1);
1849
Bob Wilsona4c22902009-04-17 19:07:39 +00001850 // Handle result values, copying them out of physregs into vregs that we
1851 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001852 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001853 InVals, isThisReturn,
1854 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001855}
1856
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001857/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001858/// on the stack. Remember the next parameter register to allocate,
1859/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001860/// this.
1861void
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001862ARMTargetLowering::HandleByVal(
1863 CCState *State, unsigned &size, unsigned Align) const {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00001864 unsigned reg = State->AllocateReg(GPRArgRegs);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001865 assert((State->getCallOrPrologue() == Prologue ||
1866 State->getCallOrPrologue() == Call) &&
1867 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001868
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001869 if ((ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001870 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1871 unsigned AlignInRegs = Align / 4;
1872 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1873 for (unsigned i = 0; i < Waste; ++i)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00001874 reg = State->AllocateReg(GPRArgRegs);
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001875 }
1876 if (reg != 0) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001877 unsigned excess = 4 * (ARM::R4 - reg);
1878
1879 // Special case when NSAA != SP and parameter size greater than size of
1880 // all remained GPR regs. In that case we can't split parameter, we must
1881 // send it to stack. We also must set NCRN to R4, so waste all
1882 // remained registers.
Oliver Stannardd55e1152014-03-05 15:25:27 +00001883 const unsigned NSAAOffset = State->getNextStackOffset();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001884 if (Subtarget->isAAPCS_ABI() && NSAAOffset != 0 && size > excess) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00001885 while (State->AllocateReg(GPRArgRegs))
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001886 ;
1887 return;
1888 }
1889
1890 // First register for byval parameter is the first register that wasn't
1891 // allocated before this method call, so it would be "reg".
1892 // If parameter is small enough to be saved in range [reg, r4), then
1893 // the end (first after last) register would be reg + param-size-in-regs,
1894 // else parameter would be splitted between registers and stack,
1895 // end register would be r4 in this case.
1896 unsigned ByValRegBegin = reg;
Stepan Dyatkovskiy2703bca2013-05-08 14:51:27 +00001897 unsigned ByValRegEnd = (size < excess) ? reg + size/4 : (unsigned)ARM::R4;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001898 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1899 // Note, first register is allocated in the beginning of function already,
1900 // allocate remained amount of registers we need.
1901 for (unsigned i = reg+1; i != ByValRegEnd; ++i)
Tim Northover3b6b7ca2015-02-21 02:11:17 +00001902 State->AllocateReg(GPRArgRegs);
Oliver Stannardd55e1152014-03-05 15:25:27 +00001903 // A byval parameter that is split between registers and memory needs its
1904 // size truncated here.
1905 // In the case where the entire structure fits in registers, we set the
1906 // size in memory to zero.
1907 if (size < excess)
1908 size = 0;
1909 else
1910 size -= excess;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001911 }
1912 }
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001913}
1914
Dale Johannesend679ff72010-06-03 21:09:53 +00001915/// MatchingStackOffset - Return true if the given stack call argument is
1916/// already available in the same position (relatively) of the caller's
1917/// incoming argument stack.
1918static
1919bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1920 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00001921 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001922 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1923 int FI = INT_MAX;
1924 if (Arg.getOpcode() == ISD::CopyFromReg) {
1925 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001926 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00001927 return false;
1928 MachineInstr *Def = MRI->getVRegDef(VR);
1929 if (!Def)
1930 return false;
1931 if (!Flags.isByVal()) {
1932 if (!TII->isLoadFromStackSlot(Def, FI))
1933 return false;
1934 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00001935 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00001936 }
1937 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1938 if (Flags.isByVal())
1939 // ByVal argument is passed in as a pointer but it's now being
1940 // dereferenced. e.g.
1941 // define @foo(%struct.X* %A) {
1942 // tail call @bar(%struct.X* byval %A)
1943 // }
1944 return false;
1945 SDValue Ptr = Ld->getBasePtr();
1946 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1947 if (!FINode)
1948 return false;
1949 FI = FINode->getIndex();
1950 } else
1951 return false;
1952
1953 assert(FI != INT_MAX);
1954 if (!MFI->isFixedObjectIndex(FI))
1955 return false;
1956 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1957}
1958
1959/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1960/// for tail call optimization. Targets which want to do tail call
1961/// optimization should implement this function.
1962bool
1963ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1964 CallingConv::ID CalleeCC,
1965 bool isVarArg,
1966 bool isCalleeStructRet,
1967 bool isCallerStructRet,
1968 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001969 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00001970 const SmallVectorImpl<ISD::InputArg> &Ins,
1971 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00001972 const Function *CallerF = DAG.getMachineFunction().getFunction();
1973 CallingConv::ID CallerCC = CallerF->getCallingConv();
1974 bool CCMatch = CallerCC == CalleeCC;
1975
1976 // Look for obvious safe cases to perform tail call optimization that do not
1977 // require ABI changes. This is what gcc calls sibcall.
1978
Jim Grosbache3864cc2010-06-16 23:45:49 +00001979 // Do not sibcall optimize vararg calls unless the call site is not passing
1980 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00001981 if (isVarArg && !Outs.empty())
1982 return false;
1983
Tim Northoverd8407452013-10-01 14:33:28 +00001984 // Exception-handling functions need a special set of instructions to indicate
1985 // a return to the hardware. Tail-calling another function would probably
1986 // break this.
1987 if (CallerF->hasFnAttribute("interrupt"))
1988 return false;
1989
Dale Johannesend679ff72010-06-03 21:09:53 +00001990 // Also avoid sibcall optimization if either caller or callee uses struct
1991 // return semantics.
1992 if (isCalleeStructRet || isCallerStructRet)
1993 return false;
1994
Dale Johannesend24c66b2010-06-23 18:52:34 +00001995 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach3840c902011-07-08 20:18:11 +00001996 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1997 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1998 // support in the assembler and linker to be used. This would need to be
1999 // fixed to fully support tail calls in Thumb1.
2000 //
Dale Johannesene2289282010-07-08 01:18:23 +00002001 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
2002 // LR. This means if we need to reload LR, it takes an extra instructions,
2003 // which outweighs the value of the tail call; but here we don't know yet
2004 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach535d3b42010-09-08 03:54:02 +00002005 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesene2289282010-07-08 01:18:23 +00002006 // emitEpilogue if LR is used.
Dale Johannesene2289282010-07-08 01:18:23 +00002007
2008 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
2009 // but we need to make sure there are enough registers; the only valid
2010 // registers are the 4 used for parameters. We don't currently do this
2011 // case.
Evan Chengd4b08732010-11-30 23:55:39 +00002012 if (Subtarget->isThumb1Only())
2013 return false;
Dale Johannesen3ac52b32010-06-18 18:13:11 +00002014
Oliver Stannard12993dd2014-08-18 12:42:15 +00002015 // Externally-defined functions with weak linkage should not be
2016 // tail-called on ARM when the OS does not support dynamic
2017 // pre-emption of symbols, as the AAELF spec requires normal calls
2018 // to undefined weak functions to be replaced with a NOP or jump to the
2019 // next instruction. The behaviour of branch instructions in this
2020 // situation (as used for tail calls) is implementation-defined, so we
2021 // cannot rely on the linker replacing the tail call with a return.
2022 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2023 const GlobalValue *GV = G->getGlobal();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002024 const Triple TT(getTargetMachine().getTargetTriple());
2025 if (GV->hasExternalWeakLinkage() &&
2026 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002027 return false;
2028 }
2029
Dale Johannesend679ff72010-06-03 21:09:53 +00002030 // If the calling conventions do not match, then we'd better make sure the
2031 // results are returned in the same way as what the caller expects.
2032 if (!CCMatch) {
2033 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopherb5217502014-08-06 18:45:26 +00002034 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
2035 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002036 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
2037
2038 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopherb5217502014-08-06 18:45:26 +00002039 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
2040 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002041 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
2042
2043 if (RVLocs1.size() != RVLocs2.size())
2044 return false;
2045 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2046 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2047 return false;
2048 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2049 return false;
2050 if (RVLocs1[i].isRegLoc()) {
2051 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2052 return false;
2053 } else {
2054 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2055 return false;
2056 }
2057 }
2058 }
2059
Manman Ren7e48b252012-10-12 23:39:43 +00002060 // If Caller's vararg or byval argument has been split between registers and
2061 // stack, do not perform tail call, since part of the argument is in caller's
2062 // local frame.
2063 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
2064 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002065 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002066 return false;
2067
Dale Johannesend679ff72010-06-03 21:09:53 +00002068 // If the callee takes no arguments then go on to check the results of the
2069 // call.
2070 if (!Outs.empty()) {
2071 // Check if stack adjustment is needed. For now, do not do this if any
2072 // argument is passed on the stack.
2073 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002074 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2075 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002076 CCInfo.AnalyzeCallOperands(Outs,
2077 CCAssignFnForNode(CalleeCC, false, isVarArg));
2078 if (CCInfo.getNextStackOffset()) {
2079 MachineFunction &MF = DAG.getMachineFunction();
2080
2081 // Check if the arguments are already laid out in the right way as
2082 // the caller's fixed stack objects.
2083 MachineFrameInfo *MFI = MF.getFrameInfo();
2084 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopher1889fdc2015-01-29 00:19:39 +00002085 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002086 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2087 i != e;
2088 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002089 CCValAssign &VA = ArgLocs[i];
2090 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002091 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002092 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002093 if (VA.getLocInfo() == CCValAssign::Indirect)
2094 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002095 if (VA.needsCustom()) {
2096 // f64 and vector types are split into multiple registers or
2097 // register/stack-slot combinations. The types will not match
2098 // the registers; give up on memory f64 refs until we figure
2099 // out what to do about this.
2100 if (!VA.isRegLoc())
2101 return false;
2102 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002103 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002104 if (RegVT == MVT::v2f64) {
2105 if (!ArgLocs[++i].isRegLoc())
2106 return false;
2107 if (!ArgLocs[++i].isRegLoc())
2108 return false;
2109 }
2110 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002111 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2112 MFI, MRI, TII))
2113 return false;
2114 }
2115 }
2116 }
2117 }
2118
2119 return true;
2120}
2121
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002122bool
2123ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2124 MachineFunction &MF, bool isVarArg,
2125 const SmallVectorImpl<ISD::OutputArg> &Outs,
2126 LLVMContext &Context) const {
2127 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002128 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002129 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2130 isVarArg));
2131}
2132
Tim Northoverd8407452013-10-01 14:33:28 +00002133static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2134 SDLoc DL, SelectionDAG &DAG) {
2135 const MachineFunction &MF = DAG.getMachineFunction();
2136 const Function *F = MF.getFunction();
2137
2138 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2139
2140 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2141 // version of the "preferred return address". These offsets affect the return
2142 // instruction if this is a return from PL1 without hypervisor extensions.
2143 // IRQ/FIQ: +4 "subs pc, lr, #4"
2144 // SWI: 0 "subs pc, lr, #0"
2145 // ABORT: +4 "subs pc, lr, #4"
2146 // UNDEF: +4/+2 "subs pc, lr, #0"
2147 // UNDEF varies depending on where the exception came from ARM or Thumb
2148 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2149
2150 int64_t LROffset;
2151 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2152 IntKind == "ABORT")
2153 LROffset = 4;
2154 else if (IntKind == "SWI" || IntKind == "UNDEF")
2155 LROffset = 0;
2156 else
2157 report_fatal_error("Unsupported interrupt attribute. If present, value "
2158 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2159
2160 RetOps.insert(RetOps.begin() + 1, DAG.getConstant(LROffset, MVT::i32, false));
2161
Craig Topper48d114b2014-04-26 18:35:24 +00002162 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002163}
2164
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002165SDValue
2166ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002167 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002168 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002169 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002170 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002171
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002172 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002173 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002174
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002175 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002176 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2177 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002178
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002179 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002180 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2181 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002182
Bob Wilsona4c22902009-04-17 19:07:39 +00002183 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002184 SmallVector<SDValue, 4> RetOps;
2185 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002186 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002187
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002188 MachineFunction &MF = DAG.getMachineFunction();
2189 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2190 AFI->setReturnRegsCount(RVLocs.size());
2191
Bob Wilsona4c22902009-04-17 19:07:39 +00002192 // Copy the result values into the output registers.
2193 for (unsigned i = 0, realRVLocIdx = 0;
2194 i != RVLocs.size();
2195 ++i, ++realRVLocIdx) {
2196 CCValAssign &VA = RVLocs[i];
2197 assert(VA.isRegLoc() && "Can only return in registers!");
2198
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002199 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002200
2201 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002202 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002203 case CCValAssign::Full: break;
2204 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002205 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002206 break;
2207 }
2208
Bob Wilsona4c22902009-04-17 19:07:39 +00002209 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002210 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002211 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002212 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2213 DAG.getConstant(0, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002214 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002215 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002216
Christian Pirkerb5728192014-05-08 14:06:24 +00002217 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2218 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2219 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002220 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002221 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002222 VA = RVLocs[++i]; // skip ahead to next loc
2223 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002224 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2225 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002226 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002227 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002228 VA = RVLocs[++i]; // skip ahead to next loc
2229
2230 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002231 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2232 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002233 }
2234 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2235 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002236 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002237 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002238 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2239 fmrrd.getValue(isLittleEndian ? 0 : 1),
2240 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002241 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002242 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002243 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002244 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2245 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002246 Flag);
2247 } else
2248 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2249
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002250 // Guarantee that all emitted copies are
2251 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002252 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002253 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002254 }
2255
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002256 // Update chain and glue.
2257 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002258 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002259 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002260
Tim Northoverd8407452013-10-01 14:33:28 +00002261 // CPUs which aren't M-class use a special sequence to return from
2262 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2263 // though we use "subs pc, lr, #N").
2264 //
2265 // M-class CPUs actually use a normal return sequence with a special
2266 // (hardware-provided) value in LR, so the normal code path works.
2267 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2268 !Subtarget->isMClass()) {
2269 if (Subtarget->isThumb1Only())
2270 report_fatal_error("interrupt attribute is not supported in Thumb1");
2271 return LowerInterruptReturn(RetOps, dl, DAG);
2272 }
2273
Craig Topper48d114b2014-04-26 18:35:24 +00002274 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002275}
2276
Evan Chengf8bad082012-04-10 01:51:00 +00002277bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002278 if (N->getNumValues() != 1)
2279 return false;
2280 if (!N->hasNUsesOfValue(1, 0))
2281 return false;
2282
Evan Chengf8bad082012-04-10 01:51:00 +00002283 SDValue TCChain = Chain;
2284 SDNode *Copy = *N->use_begin();
2285 if (Copy->getOpcode() == ISD::CopyToReg) {
2286 // If the copy has a glue operand, we conservatively assume it isn't safe to
2287 // perform a tail call.
2288 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2289 return false;
2290 TCChain = Copy->getOperand(0);
2291 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2292 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002293 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002294 SmallPtrSet<SDNode*, 2> Copies;
2295 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002296 UI != UE; ++UI) {
2297 if (UI->getOpcode() != ISD::CopyToReg)
2298 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002299 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002300 }
Evan Chengf8bad082012-04-10 01:51:00 +00002301 if (Copies.size() > 2)
2302 return false;
2303
2304 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2305 UI != UE; ++UI) {
2306 SDValue UseChain = UI->getOperand(0);
2307 if (Copies.count(UseChain.getNode()))
2308 // Second CopyToReg
2309 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002310 else {
2311 // We are at the top of this chain.
2312 // If the copy has a glue operand, we conservatively assume it
2313 // isn't safe to perform a tail call.
2314 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2315 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002316 // First CopyToReg
2317 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002318 }
Evan Chengf8bad082012-04-10 01:51:00 +00002319 }
2320 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002321 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002322 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002323 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002324 Copy = *Copy->use_begin();
2325 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002326 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002327 // If the copy has a glue operand, we conservatively assume it isn't safe to
2328 // perform a tail call.
2329 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2330 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002331 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002332 } else {
2333 return false;
2334 }
2335
Evan Cheng419ea282010-12-01 22:59:46 +00002336 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002337 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2338 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002339 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2340 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002341 return false;
2342 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002343 }
2344
Evan Chengf8bad082012-04-10 01:51:00 +00002345 if (!HasRet)
2346 return false;
2347
2348 Chain = TCChain;
2349 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002350}
2351
Evan Cheng0663f232011-03-21 01:19:09 +00002352bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002353 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002354 return false;
2355
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00002356 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng0663f232011-03-21 01:19:09 +00002357 return false;
2358
2359 return !Subtarget->isThumb1Only();
2360}
2361
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002362// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2363// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2364// one of the above mentioned nodes. It has to be wrapped because otherwise
2365// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2366// be used to form addressing mode. These wrapped nodes will be selected
2367// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002368static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002369 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002370 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002371 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002372 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002373 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002374 if (CP->isMachineConstantPoolEntry())
2375 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2376 CP->getAlignment());
2377 else
2378 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2379 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002380 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002381}
2382
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002383unsigned ARMTargetLowering::getJumpTableEncoding() const {
2384 return MachineJumpTableInfo::EK_Inline;
2385}
2386
Dan Gohman21cea8a2010-04-17 15:26:15 +00002387SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2388 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002389 MachineFunction &MF = DAG.getMachineFunction();
2390 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2391 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002392 SDLoc DL(Op);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002393 EVT PtrVT = getPointerTy();
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002394 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002395 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2396 SDValue CPAddr;
2397 if (RelocM == Reloc::Static) {
2398 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2399 } else {
2400 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002401 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002402 ARMConstantPoolValue *CPV =
2403 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2404 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002405 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2406 }
2407 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2408 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002409 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002410 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002411 if (RelocM == Reloc::Static)
2412 return Result;
Evan Cheng408aa562009-11-06 22:24:13 +00002413 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002414 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002415}
2416
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002417// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002418SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002419ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002420 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002421 SDLoc dl(GA);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002422 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002423 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002424 MachineFunction &MF = DAG.getMachineFunction();
2425 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002426 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002427 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002428 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2429 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002430 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002431 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002432 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattner7727d052010-09-21 06:44:06 +00002433 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002434 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002435 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002436
Evan Cheng408aa562009-11-06 22:24:13 +00002437 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002438 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002439
2440 // call __tls_get_addr.
2441 ArgListTy Args;
2442 ArgListEntry Entry;
2443 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002444 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002445 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002446
Dale Johannesen555a3752009-01-30 23:10:59 +00002447 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002448 TargetLowering::CallLoweringInfo CLI(DAG);
2449 CLI.setDebugLoc(dl).setChain(Chain)
2450 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002451 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args),
2452 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002453
Justin Holewinskiaa583972012-05-25 16:35:28 +00002454 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002455 return CallResult.first;
2456}
2457
2458// Lower ISD::GlobalTLSAddress using the "initial exec" or
2459// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002460SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002461ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002462 SelectionDAG &DAG,
2463 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002464 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002465 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002466 SDValue Offset;
2467 SDValue Chain = DAG.getEntryNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002468 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002469 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002470 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002471
Hans Wennborgaea41202012-05-04 09:40:39 +00002472 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002473 MachineFunction &MF = DAG.getMachineFunction();
2474 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002475 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002476 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002477 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2478 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002479 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2480 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2481 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002482 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002483 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002484 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002485 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002486 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002487 Chain = Offset.getValue(1);
2488
Evan Cheng408aa562009-11-06 22:24:13 +00002489 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002490 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002491
Evan Chengcdbb70c2009-10-31 03:39:36 +00002492 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002493 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002494 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002495 } else {
2496 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002497 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002498 ARMConstantPoolValue *CPV =
2499 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002500 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002501 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002502 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002503 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002504 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002505 }
2506
2507 // The address of the thread local variable is the add of the thread
2508 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002509 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002510}
2511
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002512SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002513ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002514 // TODO: implement the "local dynamic" model
2515 assert(Subtarget->isTargetELF() &&
2516 "TLS not implemented for non-ELF targets");
2517 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgaea41202012-05-04 09:40:39 +00002518
2519 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2520
2521 switch (model) {
2522 case TLSModel::GeneralDynamic:
2523 case TLSModel::LocalDynamic:
2524 return LowerToTLSGeneralDynamicModel(GA, DAG);
2525 case TLSModel::InitialExec:
2526 case TLSModel::LocalExec:
2527 return LowerToTLSExecModels(GA, DAG, model);
2528 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002529 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002530}
2531
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002532SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002533 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002534 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002535 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002536 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002537 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002538 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002539 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002540 ARMConstantPoolConstant::Create(GV,
2541 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002542 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002543 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson7117a912009-03-20 22:42:55 +00002544 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002545 CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002546 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002547 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002548 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002549 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002550 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002551 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002552 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002553 MachinePointerInfo::getGOT(),
2554 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002555 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002556 }
2557
2558 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002559 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002560 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002561 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002562 // FIXME: Once remat is capable of dealing with instructions with register
2563 // operands, expand this into two nodes.
2564 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2565 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002566 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002567 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2568 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2569 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2570 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002571 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002572 }
2573}
2574
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002575SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002576 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002577 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002578 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002579 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002580 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002581
Eric Christopherc1058df2014-07-04 01:55:26 +00002582 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002583 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002584
Tim Northover72360d22013-12-02 10:35:41 +00002585 // FIXME: Once remat is capable of dealing with instructions with register
2586 // operands, expand this into multiple nodes
2587 unsigned Wrapper =
2588 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002589
Tim Northover72360d22013-12-02 10:35:41 +00002590 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2591 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002592
Evan Cheng1b389522009-09-03 07:04:02 +00002593 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002594 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2595 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002596 return Result;
2597}
2598
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002599SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2600 SelectionDAG &DAG) const {
2601 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002602 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2603 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002604
2605 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002606 const ARMII::TOF TargetFlags =
2607 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002608 EVT PtrVT = getPointerTy();
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002609 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002610 SDLoc DL(Op);
2611
2612 ++NumMovwMovt;
2613
2614 // FIXME: Once remat is capable of dealing with instructions with register
2615 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002616 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2617 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
2618 TargetFlags));
2619 if (GV->hasDLLImportStorageClass())
2620 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
2621 MachinePointerInfo::getGOT(), false, false, false, 0);
2622 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002623}
2624
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002625SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002626 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002627 assert(Subtarget->isTargetELF() &&
2628 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002629 MachineFunction &MF = DAG.getMachineFunction();
2630 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002631 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002632 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002633 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002634 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002635 ARMConstantPoolValue *CPV =
2636 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2637 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002638 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002639 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002640 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002641 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002642 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00002643 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002644 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002645}
2646
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002647SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002648ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002649 SDLoc dl(Op);
Jim Grosbachfaa3abb2010-05-27 23:49:24 +00002650 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002651 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2652 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002653 Op.getOperand(1), Val);
2654}
2655
2656SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002657ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002658 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002659 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2660 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2661}
2662
2663SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002664ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002665 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002666 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002667 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002668 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002669 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002670 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00002671 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00002672 "RBIT intrinsic must have i32 type!");
Yi Kongc655f0c2014-08-20 10:40:20 +00002673 return DAG.getNode(ARMISD::RBIT, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00002674 }
Bob Wilson17f88782009-08-04 00:25:01 +00002675 case Intrinsic::arm_thread_pointer: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002676 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson17f88782009-08-04 00:25:01 +00002677 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2678 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002679 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002680 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002681 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002682 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002683 EVT PtrVT = getPointerTy();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002684 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2685 SDValue CPAddr;
2686 unsigned PCAdj = (RelocM != Reloc::PIC_)
2687 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002688 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002689 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2690 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002691 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002692 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002693 SDValue Result =
Evan Chengcdbb70c2009-10-31 03:39:36 +00002694 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002695 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002696 false, false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002697
2698 if (RelocM == Reloc::PIC_) {
Evan Cheng408aa562009-11-06 22:24:13 +00002699 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002700 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2701 }
2702 return Result;
2703 }
Evan Cheng18381b42011-03-29 23:06:19 +00002704 case Intrinsic::arm_neon_vmulls:
2705 case Intrinsic::arm_neon_vmullu: {
2706 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2707 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002708 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002709 Op.getOperand(1), Op.getOperand(2));
2710 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002711 }
2712}
2713
Eli Friedman30a49e92011-08-03 21:06:02 +00002714static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2715 const ARMSubtarget *Subtarget) {
2716 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002717 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002718 if (!Subtarget->hasDataBarrier()) {
2719 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2720 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2721 // here.
2722 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002723 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002724 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002725 DAG.getConstant(0, MVT::i32));
2726 }
2727
Tim Northover36b24172013-07-03 09:20:36 +00002728 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2729 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00002730 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002731 if (Subtarget->isMClass()) {
2732 // Only a full system barrier exists in the M-class architectures.
2733 Domain = ARM_MB::SY;
2734 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002735 // Swift happens to implement ISHST barriers in a way that's compatible with
2736 // Release semantics but weaker than ISH so we'd be fools not to use
2737 // it. Beware: other processors probably don't!
2738 Domain = ARM_MB::ISHST;
2739 }
2740
Joey Gouly926d3f52013-09-05 15:35:24 +00002741 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
2742 DAG.getConstant(Intrinsic::arm_dmb, MVT::i32),
Tim Northover36b24172013-07-03 09:20:36 +00002743 DAG.getConstant(Domain, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002744}
2745
Evan Cheng8740ee32010-11-03 06:34:55 +00002746static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2747 const ARMSubtarget *Subtarget) {
2748 // ARM pre v5TE and Thumb1 does not have preload instructions.
2749 if (!(Subtarget->isThumb2() ||
2750 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2751 // Just preserve the chain.
2752 return Op.getOperand(0);
2753
Andrew Trickef9de2a2013-05-25 02:42:55 +00002754 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002755 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2756 if (!isRead &&
2757 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2758 // ARMv7 with MP extension has PLDW.
2759 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002760
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002761 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2762 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002763 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002764 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002765 isData = ~isData & 1;
2766 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002767
2768 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng21acf9f2010-11-04 05:19:35 +00002769 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2770 DAG.getConstant(isData, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002771}
2772
Dan Gohman31ae5862010-04-17 14:41:14 +00002773static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2774 MachineFunction &MF = DAG.getMachineFunction();
2775 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2776
Evan Cheng10043e22007-01-19 07:51:42 +00002777 // vastart just stores the address of the VarArgsFrameIndex slot into the
2778 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002779 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002780 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002781 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002782 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002783 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2784 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002785}
2786
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002787SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002788ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2789 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002790 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002791 MachineFunction &MF = DAG.getMachineFunction();
2792 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2793
Craig Topper760b1342012-02-22 05:59:10 +00002794 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002795 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002796 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002797 else
Craig Topperc7242e02012-04-20 07:30:17 +00002798 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002799
2800 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002801 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002802 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002803
2804 SDValue ArgValue2;
2805 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002806 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002807 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002808
2809 // Create load node to retrieve arguments from the stack.
2810 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Chengcdbb70c2009-10-31 03:39:36 +00002811 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002812 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002813 false, false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002814 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002815 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002816 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002817 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002818 if (!Subtarget->isLittle())
2819 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002820 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002821}
2822
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002823void
2824ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002825 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002826 unsigned ArgSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002827 unsigned &ArgRegsSize,
2828 unsigned &ArgRegsSaveSize)
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002829 const {
2830 unsigned NumGPRs;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002831 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2832 unsigned RBegin, REnd;
2833 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2834 NumGPRs = REnd - RBegin;
2835 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002836 unsigned int firstUnalloced;
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002837 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002838 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2839 }
2840
Eric Christopher1889fdc2015-01-29 00:19:39 +00002841 unsigned Align = Subtarget->getFrameLowering()->getStackAlignment();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002842 ArgRegsSize = NumGPRs * 4;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002843
2844 // If parameter is split between stack and GPRs...
Mark Seabornbe266aa2014-02-16 18:59:48 +00002845 if (NumGPRs && Align > 4 &&
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002846 (ArgRegsSize < ArgSize ||
2847 InRegsParamRecordIdx >= CCInfo.getInRegsParamsCount())) {
Mark Seabornbe266aa2014-02-16 18:59:48 +00002848 // Add padding for part of param recovered from GPRs. For example,
2849 // if Align == 8, its last byte must be at address K*8 - 1.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002850 // We need to do it, since remained (stack) part of parameter has
2851 // stack alignment, and we need to "attach" "GPRs head" without gaps
2852 // to it:
2853 // Stack:
2854 // |---- 8 bytes block ----| |---- 8 bytes block ----| |---- 8 bytes...
2855 // [ [padding] [GPRs head] ] [ Tail passed via stack ....
2856 //
2857 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2858 unsigned Padding =
Mark Seabornbe266aa2014-02-16 18:59:48 +00002859 OffsetToAlignment(ArgRegsSize + AFI->getArgRegsSaveSize(), Align);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002860 ArgRegsSaveSize = ArgRegsSize + Padding;
2861 } else
2862 // We don't need to extend regs save size for byval parameters if they
2863 // are passed via GPRs only.
2864 ArgRegsSaveSize = ArgRegsSize;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002865}
2866
2867// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002868// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002869// byval). Either way, we allocate stack slots adjacent to the data
2870// provided by our caller, and store the unallocated registers there.
2871// If this is a variadic function, the va_list pointer will begin with
2872// these values; otherwise, this reassembles a (byval) structure that
2873// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002874// Return: The frame index registers were stored into.
2875int
2876ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002877 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002878 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002879 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002880 unsigned OffsetFromOrigArg,
2881 unsigned ArgOffset,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002882 unsigned ArgSize,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002883 bool ForceMutable,
2884 unsigned ByValStoreOffset,
2885 unsigned TotalArgRegsSaveSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002886
2887 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002888 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002889 // Setup first unallocated register as first byval register;
2890 // eat all remained registers
2891 // (these two actions are performed by HandleByVal method).
2892 // Then, here, we initialize stack frame with
2893 // "store-reg" instructions.
2894 // Case #2. Var-args function, that doesn't contain byval parameters.
2895 // The same: eat all remained unallocated registers,
2896 // initialize stack frame.
2897
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002898 MachineFunction &MF = DAG.getMachineFunction();
2899 MachineFrameInfo *MFI = MF.getFrameInfo();
2900 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002901 unsigned firstRegToSaveIndex, lastRegToSaveIndex;
2902 unsigned RBegin, REnd;
2903 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2904 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2905 firstRegToSaveIndex = RBegin - ARM::R0;
2906 lastRegToSaveIndex = REnd - ARM::R0;
2907 } else {
Tim Northover3b6b7ca2015-02-21 02:11:17 +00002908 firstRegToSaveIndex = CCInfo.getFirstUnallocated(GPRArgRegs);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002909 lastRegToSaveIndex = 4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002910 }
2911
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002912 unsigned ArgRegsSize, ArgRegsSaveSize;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002913 computeRegArea(CCInfo, MF, InRegsParamRecordIdx, ArgSize,
2914 ArgRegsSize, ArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002915
2916 // Store any by-val regs to their spots on the stack so that they may be
2917 // loaded by deferencing the result of formal parameter pointer or va_next.
2918 // Note: once stack area for byval/varargs registers
2919 // was initialized, it can't be initialized again.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002920 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002921 unsigned Padding = ArgRegsSaveSize - ArgRegsSize;
2922
2923 if (Padding) {
2924 assert(AFI->getStoredByValParamsPadding() == 0 &&
2925 "The only parameter may be padded.");
2926 AFI->setStoredByValParamsPadding(Padding);
2927 }
2928
Oliver Stannardd55e1152014-03-05 15:25:27 +00002929 int FrameIndex = MFI->CreateFixedObject(ArgRegsSaveSize,
2930 Padding +
2931 ByValStoreOffset -
2932 (int64_t)TotalArgRegsSaveSize,
2933 false);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002934 SDValue FIN = DAG.getFrameIndex(FrameIndex, getPointerTy());
Oliver Stannardd55e1152014-03-05 15:25:27 +00002935 if (Padding) {
2936 MFI->CreateFixedObject(Padding,
2937 ArgOffset + ByValStoreOffset -
2938 (int64_t)ArgRegsSaveSize,
2939 false);
2940 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002941
2942 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002943 for (unsigned i = 0; firstRegToSaveIndex < lastRegToSaveIndex;
2944 ++firstRegToSaveIndex, ++i) {
Craig Topper760b1342012-02-22 05:59:10 +00002945 const TargetRegisterClass *RC;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002946 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002947 RC = &ARM::tGPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002948 else
Craig Topperc7242e02012-04-20 07:30:17 +00002949 RC = &ARM::GPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002950
2951 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2952 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2953 SDValue Store =
2954 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002955 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002956 false, false, 0);
2957 MemOps.push_back(Store);
2958 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2959 DAG.getConstant(4, getPointerTy()));
2960 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002961
2962 AFI->setArgRegsSaveSize(ArgRegsSaveSize + AFI->getArgRegsSaveSize());
2963
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002964 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002965 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002966 return FrameIndex;
Oliver Stannardd55e1152014-03-05 15:25:27 +00002967 } else {
2968 if (ArgSize == 0) {
2969 // We cannot allocate a zero-byte object for the first variadic argument,
2970 // so just make up a size.
2971 ArgSize = 4;
2972 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002973 // This will point to the next argument passed via stack.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002974 return MFI->CreateFixedObject(
Oliver Stannardd55e1152014-03-05 15:25:27 +00002975 ArgSize, ArgOffset, !ForceMutable);
2976 }
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002977}
2978
2979// Setup stack frame, the va_list pointer will start from.
2980void
2981ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002982 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002983 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002984 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002985 bool ForceMutable) const {
2986 MachineFunction &MF = DAG.getMachineFunction();
2987 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2988
2989 // Try to store any remaining integer argument regs
2990 // to their spots on the stack so that they may be loaded by deferencing
2991 // the result of va_next.
2992 // If there is no regs to be stored, just point address after last
2993 // argument passed via stack.
2994 int FrameIndex =
Craig Topper062a2ba2014-04-25 05:30:21 +00002995 StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
2996 CCInfo.getInRegsParamsCount(), 0, ArgOffset, 0, ForceMutable,
2997 0, TotalArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002998
2999 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003000}
3001
Bob Wilson2e076c42009-06-22 23:27:02 +00003002SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003003ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003004 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003005 const SmallVectorImpl<ISD::InputArg>
3006 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003007 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003008 SmallVectorImpl<SDValue> &InVals)
3009 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00003010 MachineFunction &MF = DAG.getMachineFunction();
3011 MachineFrameInfo *MFI = MF.getFrameInfo();
3012
Bob Wilsona4c22902009-04-17 19:07:39 +00003013 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3014
3015 // Assign locations to all of the incoming arguments.
3016 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003017 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3018 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003019 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003020 CCAssignFnForNode(CallConv, /* Return*/ false,
3021 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00003022
Bob Wilsona4c22902009-04-17 19:07:39 +00003023 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003024 int lastInsIndex = -1;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003025 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003026 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
3027 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003028
3029 // Initially ArgRegsSaveSize is zero.
3030 // Then we increase this value each time we meet byval parameter.
3031 // We also increase this value in case of varargs function.
3032 AFI->setArgRegsSaveSize(0);
3033
Oliver Stannardd55e1152014-03-05 15:25:27 +00003034 unsigned ByValStoreOffset = 0;
3035 unsigned TotalArgRegsSaveSize = 0;
3036 unsigned ArgRegsSaveSizeMaxAlign = 4;
3037
3038 // Calculate the amount of stack space that we need to allocate to store
3039 // byval and variadic arguments that are passed in registers.
3040 // We need to know this before we allocate the first byval or variadic
3041 // argument, as they will be allocated a stack slot below the CFA (Canonical
3042 // Frame Address, the stack pointer at entry to the function).
3043 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3044 CCValAssign &VA = ArgLocs[i];
3045 if (VA.isMemLoc()) {
3046 int index = VA.getValNo();
3047 if (index != lastInsIndex) {
3048 ISD::ArgFlagsTy Flags = Ins[index].Flags;
3049 if (Flags.isByVal()) {
3050 unsigned ExtraArgRegsSize;
3051 unsigned ExtraArgRegsSaveSize;
Daniel Sanders8104b752014-11-01 19:32:23 +00003052 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsProcessed(),
Oliver Stannardd55e1152014-03-05 15:25:27 +00003053 Flags.getByValSize(),
3054 ExtraArgRegsSize, ExtraArgRegsSaveSize);
3055
3056 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
3057 if (Flags.getByValAlign() > ArgRegsSaveSizeMaxAlign)
3058 ArgRegsSaveSizeMaxAlign = Flags.getByValAlign();
3059 CCInfo.nextInRegsParam();
3060 }
3061 lastInsIndex = index;
3062 }
3063 }
3064 }
3065 CCInfo.rewindByValRegsInfo();
3066 lastInsIndex = -1;
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003067 if (isVarArg && MFI->hasVAStart()) {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003068 unsigned ExtraArgRegsSize;
3069 unsigned ExtraArgRegsSaveSize;
3070 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsCount(), 0,
3071 ExtraArgRegsSize, ExtraArgRegsSaveSize);
3072 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
3073 }
3074 // If the arg regs save area contains N-byte aligned values, the
3075 // bottom of it must be at least N-byte aligned.
3076 TotalArgRegsSaveSize = RoundUpToAlignment(TotalArgRegsSaveSize, ArgRegsSaveSizeMaxAlign);
3077 TotalArgRegsSaveSize = std::min(TotalArgRegsSaveSize, 16U);
3078
Bob Wilsona4c22902009-04-17 19:07:39 +00003079 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3080 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00003081 if (Ins[VA.getValNo()].isOrigArg()) {
3082 std::advance(CurOrigArg,
3083 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
3084 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
3085 }
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003086 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003087 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003088 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003089
Bob Wilsona4c22902009-04-17 19:07:39 +00003090 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003091 // f64 and vector types are split up into multiple registers or
3092 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003093 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003094 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003095 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003096 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003097 SDValue ArgValue2;
3098 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003099 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson699bdf72010-04-13 22:03:22 +00003100 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3101 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003102 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003103 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003104 } else {
3105 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3106 Chain, DAG, dl);
3107 }
Owen Anderson9f944592009-08-11 20:47:22 +00003108 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3109 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00003110 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson9f944592009-08-11 20:47:22 +00003111 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00003112 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
3113 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003114 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003115
Bob Wilson2e076c42009-06-22 23:27:02 +00003116 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003117 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003118
Owen Anderson9f944592009-08-11 20:47:22 +00003119 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003120 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003121 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003122 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003123 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003124 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003125 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003126 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3127 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003128 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003129 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003130
3131 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003132 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003133 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003134 }
3135
3136 // If this is an 8 or 16-bit value, it is really passed promoted
3137 // to 32 bits. Insert an assert[sz]ext to capture this, then
3138 // truncate to the right size.
3139 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003140 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003141 case CCValAssign::Full: break;
3142 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003143 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003144 break;
3145 case CCValAssign::SExt:
3146 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3147 DAG.getValueType(VA.getValVT()));
3148 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3149 break;
3150 case CCValAssign::ZExt:
3151 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3152 DAG.getValueType(VA.getValVT()));
3153 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3154 break;
3155 }
3156
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003157 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003158
3159 } else { // VA.isRegLoc()
3160
3161 // sanity check
3162 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003163 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003164
Andrew Trick05938a52015-02-16 18:10:47 +00003165 int index = VA.getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003166
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003167 // Some Ins[] entries become multiple ArgLoc[] entries.
3168 // Process them only once.
3169 if (index != lastInsIndex)
3170 {
3171 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003172 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003173 // This can be changed with more analysis.
3174 // In case of tail call optimization mark all arguments mutable.
3175 // Since they could be overwritten by lowering of arguments in case of
3176 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003177 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003178 assert(Ins[index].isOrigArg() &&
3179 "Byval arguments cannot be implicit");
Daniel Sanders8104b752014-11-01 19:32:23 +00003180 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003181
3182 ByValStoreOffset = RoundUpToAlignment(ByValStoreOffset, Flags.getByValAlign());
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003183 int FrameIndex = StoreByValRegs(
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003184 CCInfo, DAG, dl, Chain, CurOrigArg,
3185 CurByValIndex,
3186 Ins[VA.getValNo()].PartOffset,
3187 VA.getLocMemOffset(),
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003188 Flags.getByValSize(),
Oliver Stannardd55e1152014-03-05 15:25:27 +00003189 true /*force mutable frames*/,
3190 ByValStoreOffset,
3191 TotalArgRegsSaveSize);
3192 ByValStoreOffset += Flags.getByValSize();
3193 ByValStoreOffset = std::min(ByValStoreOffset, 16U);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003194 InVals.push_back(DAG.getFrameIndex(FrameIndex, getPointerTy()));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003195 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003196 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003197 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003198 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003199 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003200
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003201 // Create load nodes to retrieve arguments from the stack.
3202 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3203 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3204 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003205 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003206 }
3207 lastInsIndex = index;
3208 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003209 }
3210 }
3211
3212 // varargs
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003213 if (isVarArg && MFI->hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003214 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003215 CCInfo.getNextStackOffset(),
3216 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003217
Oliver Stannardb14c6252014-04-02 16:10:33 +00003218 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3219
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003220 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003221}
3222
3223/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003224static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003225 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003226 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003227 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003228 // Maybe this has already been legalized into the constant pool?
3229 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003230 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003231 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003232 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003233 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003234 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003235 } else if (Op->getOpcode() == ISD::BITCAST &&
3236 Op->getValueType(0) == MVT::f64) {
3237 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3238 // created by LowerConstantFP().
3239 SDValue BitcastOp = Op->getOperand(0);
3240 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM) {
3241 SDValue MoveOp = BitcastOp->getOperand(0);
3242 if (MoveOp->getOpcode() == ISD::TargetConstant &&
3243 cast<ConstantSDNode>(MoveOp)->getZExtValue() == 0) {
3244 return true;
3245 }
3246 }
Evan Cheng10043e22007-01-19 07:51:42 +00003247 }
3248 return false;
3249}
3250
Evan Cheng10043e22007-01-19 07:51:42 +00003251/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3252/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003253SDValue
3254ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003255 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003256 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003257 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003258 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003259 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003260 // Constant does not fit, try adjusting it by one?
3261 switch (CC) {
3262 default: break;
3263 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003264 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003265 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003266 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003267 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003268 }
3269 break;
3270 case ISD::SETULT:
3271 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003272 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003273 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003274 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003275 }
3276 break;
3277 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003278 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003279 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003280 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003281 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003282 }
3283 break;
3284 case ISD::SETULE:
3285 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003286 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003287 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003288 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003289 }
3290 break;
3291 }
3292 }
3293 }
3294
3295 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003296 ARMISD::NodeType CompareType;
3297 switch (CondCode) {
3298 default:
3299 CompareType = ARMISD::CMP;
3300 break;
3301 case ARMCC::EQ:
3302 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003303 // Uses only Z Flag
3304 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003305 break;
3306 }
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003307 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003308 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003309}
3310
3311/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003312SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003313ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003314 SDLoc dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003315 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003316 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003317 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003318 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003319 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003320 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3321 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003322}
3323
Bob Wilson45acbd02011-03-08 01:17:20 +00003324/// duplicateCmp - Glue values can have only one use, so this function
3325/// duplicates a comparison node.
3326SDValue
3327ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3328 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003329 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003330 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3331 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3332
3333 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3334 Cmp = Cmp.getOperand(0);
3335 Opc = Cmp.getOpcode();
3336 if (Opc == ARMISD::CMPFP)
3337 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3338 else {
3339 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3340 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3341 }
3342 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3343}
3344
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003345std::pair<SDValue, SDValue>
3346ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3347 SDValue &ARMcc) const {
3348 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3349
3350 SDValue Value, OverflowCmp;
3351 SDValue LHS = Op.getOperand(0);
3352 SDValue RHS = Op.getOperand(1);
3353
3354
3355 // FIXME: We are currently always generating CMPs because we don't support
3356 // generating CMN through the backend. This is not as good as the natural
3357 // CMP case because it causes a register dependency and cannot be folded
3358 // later.
3359
3360 switch (Op.getOpcode()) {
3361 default:
3362 llvm_unreachable("Unknown overflow instruction!");
3363 case ISD::SADDO:
3364 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3365 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3366 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3367 break;
3368 case ISD::UADDO:
3369 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3370 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3371 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3372 break;
3373 case ISD::SSUBO:
3374 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3375 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3376 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3377 break;
3378 case ISD::USUBO:
3379 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3380 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3381 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3382 break;
3383 } // switch (...)
3384
3385 return std::make_pair(Value, OverflowCmp);
3386}
3387
3388
3389SDValue
3390ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3391 // Let legalize expand this if it isn't a legal type yet.
3392 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3393 return SDValue();
3394
3395 SDValue Value, OverflowCmp;
3396 SDValue ARMcc;
3397 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3398 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3399 // We use 0 and 1 as false and true values.
3400 SDValue TVal = DAG.getConstant(1, MVT::i32);
3401 SDValue FVal = DAG.getConstant(0, MVT::i32);
3402 EVT VT = Op.getValueType();
3403
3404 SDValue Overflow = DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, TVal, FVal,
3405 ARMcc, CCR, OverflowCmp);
3406
3407 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
3408 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), VTs, Value, Overflow);
3409}
3410
3411
Bill Wendling6a981312010-08-11 08:43:16 +00003412SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3413 SDValue Cond = Op.getOperand(0);
3414 SDValue SelectTrue = Op.getOperand(1);
3415 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003416 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003417 unsigned Opc = Cond.getOpcode();
3418
3419 if (Cond.getResNo() == 1 &&
3420 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3421 Opc == ISD::USUBO)) {
3422 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3423 return SDValue();
3424
3425 SDValue Value, OverflowCmp;
3426 SDValue ARMcc;
3427 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3428 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3429 EVT VT = Op.getValueType();
3430
Oliver Stannard51b1d462014-08-21 12:50:31 +00003431 return getCMOV(SDLoc(Op), VT, SelectTrue, SelectFalse, ARMcc, CCR,
3432 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003433 }
Bill Wendling6a981312010-08-11 08:43:16 +00003434
3435 // Convert:
3436 //
3437 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3438 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3439 //
3440 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3441 const ConstantSDNode *CMOVTrue =
3442 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3443 const ConstantSDNode *CMOVFalse =
3444 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3445
3446 if (CMOVTrue && CMOVFalse) {
3447 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3448 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3449
3450 SDValue True;
3451 SDValue False;
3452 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3453 True = SelectTrue;
3454 False = SelectFalse;
3455 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3456 True = SelectFalse;
3457 False = SelectTrue;
3458 }
3459
3460 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003461 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003462 SDValue ARMcc = Cond.getOperand(2);
3463 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003464 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003465 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003466 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00003467 }
3468 }
3469 }
3470
Dan Gohmand4a77c42012-02-24 00:09:36 +00003471 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3472 // undefined bits before doing a full-word comparison with zero.
3473 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
3474 DAG.getConstant(1, Cond.getValueType()));
3475
Bill Wendling6a981312010-08-11 08:43:16 +00003476 return DAG.getSelectCC(dl, Cond,
3477 DAG.getConstant(0, Cond.getValueType()),
3478 SelectTrue, SelectFalse, ISD::SETNE);
3479}
3480
Joey Gouly881eab52013-08-22 15:29:11 +00003481static ISD::CondCode getInverseCCForVSEL(ISD::CondCode CC) {
3482 if (CC == ISD::SETNE)
3483 return ISD::SETEQ;
Weiming Zhao63871d22013-12-18 22:25:17 +00003484 return ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003485}
3486
3487static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3488 bool &swpCmpOps, bool &swpVselOps) {
3489 // Start by selecting the GE condition code for opcodes that return true for
3490 // 'equality'
3491 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3492 CC == ISD::SETULE)
3493 CondCode = ARMCC::GE;
3494
3495 // and GT for opcodes that return false for 'equality'.
3496 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3497 CC == ISD::SETULT)
3498 CondCode = ARMCC::GT;
3499
3500 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3501 // to swap the compare operands.
3502 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3503 CC == ISD::SETULT)
3504 swpCmpOps = true;
3505
3506 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3507 // If we have an unordered opcode, we need to swap the operands to the VSEL
3508 // instruction (effectively negating the condition).
3509 //
3510 // This also has the effect of swapping which one of 'less' or 'greater'
3511 // returns true, so we also swap the compare operands. It also switches
3512 // whether we return true for 'equality', so we compensate by picking the
3513 // opposite condition code to our original choice.
3514 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3515 CC == ISD::SETUGT) {
3516 swpCmpOps = !swpCmpOps;
3517 swpVselOps = !swpVselOps;
3518 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3519 }
3520
3521 // 'ordered' is 'anything but unordered', so use the VS condition code and
3522 // swap the VSEL operands.
3523 if (CC == ISD::SETO) {
3524 CondCode = ARMCC::VS;
3525 swpVselOps = true;
3526 }
3527
3528 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3529 // code and swap the VSEL operands.
3530 if (CC == ISD::SETUNE) {
3531 CondCode = ARMCC::EQ;
3532 swpVselOps = true;
3533 }
3534}
3535
Oliver Stannard51b1d462014-08-21 12:50:31 +00003536SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal,
3537 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
3538 SDValue Cmp, SelectionDAG &DAG) const {
3539 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
3540 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3541 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
3542 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3543 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
3544
3545 SDValue TrueLow = TrueVal.getValue(0);
3546 SDValue TrueHigh = TrueVal.getValue(1);
3547 SDValue FalseLow = FalseVal.getValue(0);
3548 SDValue FalseHigh = FalseVal.getValue(1);
3549
3550 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
3551 ARMcc, CCR, Cmp);
3552 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
3553 ARMcc, CCR, duplicateCmp(Cmp, DAG));
3554
3555 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
3556 } else {
3557 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3558 Cmp);
3559 }
3560}
3561
Dan Gohman21cea8a2010-04-17 15:26:15 +00003562SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003563 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003564 SDValue LHS = Op.getOperand(0);
3565 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003566 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003567 SDValue TrueVal = Op.getOperand(2);
3568 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003569 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003570
Oliver Stannard51b1d462014-08-21 12:50:31 +00003571 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3572 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3573 dl);
3574
3575 // If softenSetCCOperands only returned one value, we should compare it to
3576 // zero.
3577 if (!RHS.getNode()) {
3578 RHS = DAG.getConstant(0, LHS.getValueType());
3579 CC = ISD::SETNE;
3580 }
3581 }
3582
Owen Anderson9f944592009-08-11 20:47:22 +00003583 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003584 // Try to generate VSEL on ARMv8.
3585 // The VSEL instruction can't use all the usual ARM condition
3586 // codes: it only has two bits to select the condition code, so it's
3587 // constrained to use only GE, GT, VS and EQ.
3588 //
3589 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3590 // swap the operands of the previous compare instruction (effectively
3591 // inverting the compare condition, swapping 'less' and 'greater') and
3592 // sometimes need to swap the operands to the VSEL (which inverts the
3593 // condition in the sense of firing whenever the previous condition didn't)
Eric Christopher1889fdc2015-01-29 00:19:39 +00003594 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3595 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003596 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3597 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3598 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
3599 CC = getInverseCCForVSEL(CC);
3600 std::swap(TrueVal, FalseVal);
3601 }
3602 }
3603
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003604 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003605 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003606 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003607 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003608 }
3609
3610 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003611 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003612
Joey Gouly881eab52013-08-22 15:29:11 +00003613 // Try to generate VSEL on ARMv8.
Eric Christopher1889fdc2015-01-29 00:19:39 +00003614 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3615 TrueVal.getValueType() == MVT::f64)) {
Joey Goulye3dd6842013-08-23 12:01:13 +00003616 // We can select VMAXNM/VMINNM from a compare followed by a select with the
3617 // same operands, as follows:
3618 // c = fcmp [ogt, olt, ugt, ult] a, b
3619 // select c, a, b
3620 // We only do this in unsafe-fp-math, because signed zeros and NaNs are
3621 // handled differently than the original code sequence.
Oliver Stannard79efe412014-10-27 09:23:02 +00003622 if (getTargetMachine().Options.UnsafeFPMath) {
3623 if (LHS == TrueVal && RHS == FalseVal) {
3624 if (CC == ISD::SETOGT || CC == ISD::SETUGT)
3625 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal);
3626 if (CC == ISD::SETOLT || CC == ISD::SETULT)
3627 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal);
3628 } else if (LHS == FalseVal && RHS == TrueVal) {
3629 if (CC == ISD::SETOLT || CC == ISD::SETULT)
3630 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal);
3631 if (CC == ISD::SETOGT || CC == ISD::SETUGT)
3632 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal);
3633 }
Joey Goulye3dd6842013-08-23 12:01:13 +00003634 }
3635
Joey Gouly881eab52013-08-22 15:29:11 +00003636 bool swpCmpOps = false;
3637 bool swpVselOps = false;
3638 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3639
3640 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3641 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3642 if (swpCmpOps)
3643 std::swap(LHS, RHS);
3644 if (swpVselOps)
3645 std::swap(TrueVal, FalseVal);
3646 }
3647 }
3648
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003649 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3650 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003651 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003652 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003653 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003654 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003655 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003656 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003657 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003658 }
3659 return Result;
3660}
3661
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003662/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3663/// to morph to an integer compare sequence.
3664static bool canChangeToInt(SDValue Op, bool &SeenZero,
3665 const ARMSubtarget *Subtarget) {
3666 SDNode *N = Op.getNode();
3667 if (!N->hasOneUse())
3668 // Otherwise it requires moving the value from fp to integer registers.
3669 return false;
3670 if (!N->getNumValues())
3671 return false;
3672 EVT VT = Op.getValueType();
3673 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3674 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3675 // vmrs are very slow, e.g. cortex-a8.
3676 return false;
3677
3678 if (isFloatingPointZero(Op)) {
3679 SeenZero = true;
3680 return true;
3681 }
3682 return ISD::isNormalLoad(N);
3683}
3684
3685static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3686 if (isFloatingPointZero(Op))
3687 return DAG.getConstant(0, MVT::i32);
3688
3689 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003690 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003691 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003692 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003693 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003694
3695 llvm_unreachable("Unknown VFP cmp argument!");
3696}
3697
3698static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3699 SDValue &RetVal1, SDValue &RetVal2) {
3700 if (isFloatingPointZero(Op)) {
3701 RetVal1 = DAG.getConstant(0, MVT::i32);
3702 RetVal2 = DAG.getConstant(0, MVT::i32);
3703 return;
3704 }
3705
3706 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3707 SDValue Ptr = Ld->getBasePtr();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003708 RetVal1 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003709 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003710 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003711 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003712 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003713
3714 EVT PtrType = Ptr.getValueType();
3715 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003716 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003717 PtrType, Ptr, DAG.getConstant(4, PtrType));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003718 RetVal2 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003719 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003720 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003721 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003722 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003723 return;
3724 }
3725
3726 llvm_unreachable("Unknown VFP cmp argument!");
3727}
3728
3729/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3730/// f32 and even f64 comparisons to integer ones.
3731SDValue
3732ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3733 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003734 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003735 SDValue LHS = Op.getOperand(2);
3736 SDValue RHS = Op.getOperand(3);
3737 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003738 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003739
Evan Chengd12af5d2012-03-01 23:27:13 +00003740 bool LHSSeenZero = false;
3741 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3742 bool RHSSeenZero = false;
3743 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3744 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003745 // If unsafe fp math optimization is enabled and there are no other uses of
3746 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003747 // to an integer comparison.
3748 if (CC == ISD::SETOEQ)
3749 CC = ISD::SETEQ;
3750 else if (CC == ISD::SETUNE)
3751 CC = ISD::SETNE;
3752
Evan Chengd12af5d2012-03-01 23:27:13 +00003753 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003754 SDValue ARMcc;
3755 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003756 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3757 bitcastf32Toi32(LHS, DAG), Mask);
3758 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3759 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003760 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3761 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3762 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3763 Chain, Dest, ARMcc, CCR, Cmp);
3764 }
3765
3766 SDValue LHS1, LHS2;
3767 SDValue RHS1, RHS2;
3768 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3769 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003770 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3771 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003772 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3773 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003774 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003775 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003776 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003777 }
3778
3779 return SDValue();
3780}
3781
3782SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3783 SDValue Chain = Op.getOperand(0);
3784 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3785 SDValue LHS = Op.getOperand(2);
3786 SDValue RHS = Op.getOperand(3);
3787 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003788 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003789
Oliver Stannard51b1d462014-08-21 12:50:31 +00003790 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3791 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3792 dl);
3793
3794 // If softenSetCCOperands only returned one value, we should compare it to
3795 // zero.
3796 if (!RHS.getNode()) {
3797 RHS = DAG.getConstant(0, LHS.getValueType());
3798 CC = ISD::SETNE;
3799 }
3800 }
3801
Owen Anderson9f944592009-08-11 20:47:22 +00003802 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003803 SDValue ARMcc;
3804 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003805 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003806 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003807 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003808 }
3809
Owen Anderson9f944592009-08-11 20:47:22 +00003810 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003811
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003812 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003813 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3814 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3815 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3816 if (Result.getNode())
3817 return Result;
3818 }
3819
Evan Cheng10043e22007-01-19 07:51:42 +00003820 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003821 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003822
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003823 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3824 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003825 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003826 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003827 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003828 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003829 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003830 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3831 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003832 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003833 }
3834 return Res;
3835}
3836
Dan Gohman21cea8a2010-04-17 15:26:15 +00003837SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003838 SDValue Chain = Op.getOperand(0);
3839 SDValue Table = Op.getOperand(1);
3840 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003841 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003842
Owen Anderson53aa7a92009-08-10 22:56:29 +00003843 EVT PTy = getPointerTy();
Evan Cheng10043e22007-01-19 07:51:42 +00003844 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3845 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3f17aee2009-07-14 18:44:34 +00003846 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003847 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson9f944592009-08-11 20:47:22 +00003848 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chengc8bed032009-07-28 20:53:24 +00003849 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3850 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003851 if (Subtarget->isThumb2()) {
3852 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3853 // which does another jump to the destination. This also makes it easier
3854 // to translate it to TBB / TBH later.
3855 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003856 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Chengc6d70ae2009-07-29 02:18:14 +00003857 Addr, Op.getOperand(2), JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003858 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003859 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003860 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattner7727d052010-09-21 06:44:06 +00003861 MachinePointerInfo::getJumpTable(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003862 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003863 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003864 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson9f944592009-08-11 20:47:22 +00003865 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003866 } else {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003867 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003868 MachinePointerInfo::getJumpTable(),
3869 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003870 Chain = Addr.getValue(1);
Owen Anderson9f944592009-08-11 20:47:22 +00003871 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003872 }
Evan Cheng10043e22007-01-19 07:51:42 +00003873}
3874
Eli Friedman2d4055b2011-11-09 23:36:02 +00003875static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003876 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003877 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003878
James Molloy547d4c02012-02-20 09:24:05 +00003879 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3880 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3881 return Op;
3882 return DAG.UnrollVectorOp(Op.getNode());
3883 }
3884
3885 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3886 "Invalid type for custom lowering!");
3887 if (VT != MVT::v4i16)
3888 return DAG.UnrollVectorOp(Op.getNode());
3889
3890 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3891 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003892}
3893
Oliver Stannard51b1d462014-08-21 12:50:31 +00003894SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003895 EVT VT = Op.getValueType();
3896 if (VT.isVector())
3897 return LowerVectorFP_TO_INT(Op, DAG);
3898
Oliver Stannard51b1d462014-08-21 12:50:31 +00003899 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
3900 RTLIB::Libcall LC;
3901 if (Op.getOpcode() == ISD::FP_TO_SINT)
3902 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
3903 Op.getValueType());
3904 else
3905 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
3906 Op.getValueType());
3907 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3908 /*isSigned*/ false, SDLoc(Op)).first;
3909 }
3910
Andrew Trickef9de2a2013-05-25 02:42:55 +00003911 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003912 unsigned Opc;
3913
3914 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003915 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003916 case ISD::FP_TO_SINT:
3917 Opc = ARMISD::FTOSI;
3918 break;
3919 case ISD::FP_TO_UINT:
3920 Opc = ARMISD::FTOUI;
3921 break;
3922 }
3923 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peck527da1b2010-11-23 03:31:01 +00003924 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003925}
3926
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003927static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3928 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003929 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003930
Eli Friedman2d4055b2011-11-09 23:36:02 +00003931 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3932 if (VT.getVectorElementType() == MVT::f32)
3933 return Op;
3934 return DAG.UnrollVectorOp(Op.getNode());
3935 }
3936
Duncan Sandsa41634e2011-08-12 14:54:45 +00003937 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3938 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003939 if (VT != MVT::v4f32)
3940 return DAG.UnrollVectorOp(Op.getNode());
3941
3942 unsigned CastOpc;
3943 unsigned Opc;
3944 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003945 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003946 case ISD::SINT_TO_FP:
3947 CastOpc = ISD::SIGN_EXTEND;
3948 Opc = ISD::SINT_TO_FP;
3949 break;
3950 case ISD::UINT_TO_FP:
3951 CastOpc = ISD::ZERO_EXTEND;
3952 Opc = ISD::UINT_TO_FP;
3953 break;
3954 }
3955
3956 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3957 return DAG.getNode(Opc, dl, VT, Op);
3958}
3959
Oliver Stannard51b1d462014-08-21 12:50:31 +00003960SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00003961 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003962 if (VT.isVector())
3963 return LowerVectorINT_TO_FP(Op, DAG);
3964
Oliver Stannard51b1d462014-08-21 12:50:31 +00003965 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
3966 RTLIB::Libcall LC;
3967 if (Op.getOpcode() == ISD::SINT_TO_FP)
3968 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
3969 Op.getValueType());
3970 else
3971 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
3972 Op.getValueType());
3973 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3974 /*isSigned*/ false, SDLoc(Op)).first;
3975 }
3976
Andrew Trickef9de2a2013-05-25 02:42:55 +00003977 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003978 unsigned Opc;
3979
3980 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003981 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003982 case ISD::SINT_TO_FP:
3983 Opc = ARMISD::SITOF;
3984 break;
3985 case ISD::UINT_TO_FP:
3986 Opc = ARMISD::UITOF;
3987 break;
3988 }
3989
Wesley Peck527da1b2010-11-23 03:31:01 +00003990 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilsone4191e72010-03-19 22:51:32 +00003991 return DAG.getNode(Opc, dl, VT, Op);
3992}
3993
Evan Cheng25f93642010-07-08 02:08:50 +00003994SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00003995 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003996 SDValue Tmp0 = Op.getOperand(0);
3997 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003998 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003999 EVT VT = Op.getValueType();
4000 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00004001 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
4002 Tmp0.getOpcode() == ARMISD::VMOVDRR;
4003 bool UseNEON = !InGPR && Subtarget->hasNEON();
4004
4005 if (UseNEON) {
4006 // Use VBSL to copy the sign bit.
4007 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
4008 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
4009 DAG.getTargetConstant(EncodedVal, MVT::i32));
4010 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
4011 if (VT == MVT::f64)
4012 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4013 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
4014 DAG.getConstant(32, MVT::i32));
4015 else /*if (VT == MVT::f32)*/
4016 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
4017 if (SrcVT == MVT::f32) {
4018 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
4019 if (VT == MVT::f64)
4020 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4021 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
4022 DAG.getConstant(32, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00004023 } else if (VT == MVT::f32)
4024 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
4025 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
4026 DAG.getConstant(32, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004027 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4028 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4029
4030 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
4031 MVT::i32);
4032 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4033 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4034 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00004035
Evan Chengd6b641e2011-02-23 02:24:55 +00004036 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4037 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4038 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004039 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004040 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4041 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
4042 DAG.getConstant(0, MVT::i32));
4043 } else {
4044 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4045 }
4046
4047 return Res;
4048 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004049
4050 // Bitcast operand 1 to i32.
4051 if (SrcVT == MVT::f64)
4052 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004053 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004054 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4055
Evan Chengd6b641e2011-02-23 02:24:55 +00004056 // Or in the signbit with integer operations.
4057 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
4058 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
4059 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4060 if (VT == MVT::f32) {
4061 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4062 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4063 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4064 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004065 }
4066
Evan Chengd6b641e2011-02-23 02:24:55 +00004067 // f64: Or the high part with signbit and then combine two parts.
4068 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004069 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004070 SDValue Lo = Tmp0.getValue(0);
4071 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4072 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4073 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004074}
4075
Evan Cheng168ced92010-05-22 01:47:14 +00004076SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4077 MachineFunction &MF = DAG.getMachineFunction();
4078 MachineFrameInfo *MFI = MF.getFrameInfo();
4079 MFI->setReturnAddressIsTaken(true);
4080
Bill Wendling908bf812014-01-06 00:43:20 +00004081 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004082 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004083
Evan Cheng168ced92010-05-22 01:47:14 +00004084 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004085 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004086 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4087 if (Depth) {
4088 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
4089 SDValue Offset = DAG.getConstant(4, MVT::i32);
4090 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4091 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004092 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00004093 }
4094
4095 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004096 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004097 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4098}
4099
Dan Gohman21cea8a2010-04-17 15:26:15 +00004100SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004101 const ARMBaseRegisterInfo &ARI =
4102 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4103 MachineFunction &MF = DAG.getMachineFunction();
4104 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004105 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004106
Owen Anderson53aa7a92009-08-10 22:56:29 +00004107 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004108 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004109 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004110 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004111 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4112 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004113 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
4114 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004115 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004116 return FrameAddr;
4117}
4118
Renato Golinc7aea402014-05-06 16:51:25 +00004119// FIXME? Maybe this could be a TableGen attribute on some registers and
4120// this table could be generated automatically from RegInfo.
Hal Finkelf0e086a2014-05-11 19:29:07 +00004121unsigned ARMTargetLowering::getRegisterByName(const char* RegName,
4122 EVT VT) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004123 unsigned Reg = StringSwitch<unsigned>(RegName)
4124 .Case("sp", ARM::SP)
4125 .Default(0);
4126 if (Reg)
4127 return Reg;
4128 report_fatal_error("Invalid register name global variable");
4129}
4130
Wesley Peck527da1b2010-11-23 03:31:01 +00004131/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004132/// expand a bit convert where either the source or destination type is i64 to
4133/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4134/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4135/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004136static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004137 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004138 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004139 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004140
Bob Wilson59b70ea2010-04-17 05:30:19 +00004141 // This function is only supposed to be called for i64 types, either as the
4142 // source or destination of the bit convert.
4143 EVT SrcVT = Op.getValueType();
4144 EVT DstVT = N->getValueType(0);
4145 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004146 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004147
Bob Wilson59b70ea2010-04-17 05:30:19 +00004148 // Turn i64->f64 into VMOVDRR.
4149 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00004150 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
4151 DAG.getConstant(0, MVT::i32));
4152 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
4153 DAG.getConstant(1, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004154 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004155 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004156 }
Bob Wilson7117a912009-03-20 22:42:55 +00004157
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004158 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004159 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004160 SDValue Cvt;
Christian Pirker6692e7c2014-05-14 16:59:44 +00004161 if (TLI.isBigEndian() && SrcVT.isVector() &&
4162 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004163 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4164 DAG.getVTList(MVT::i32, MVT::i32),
4165 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4166 else
4167 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4168 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004169 // Merge the pieces into a single i64 value.
4170 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4171 }
Bob Wilson7117a912009-03-20 22:42:55 +00004172
Bob Wilson59b70ea2010-04-17 05:30:19 +00004173 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004174}
4175
Bob Wilson2e076c42009-06-22 23:27:02 +00004176/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004177/// Zero vectors are used to represent vector negation and in those cases
4178/// will be implemented with the NEON VNEG instruction. However, VNEG does
4179/// not support i64 elements, so sometimes the zero vectors will need to be
4180/// explicitly constructed. Regardless, use a canonical VMOV to create the
4181/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004182static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004183 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004184 // The canonical modified immediate encoding of a zero vector is....0!
4185 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
4186 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4187 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004188 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004189}
4190
Jim Grosbach624fcb22009-10-31 21:00:56 +00004191/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4192/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004193SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4194 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004195 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4196 EVT VT = Op.getValueType();
4197 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004198 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004199 SDValue ShOpLo = Op.getOperand(0);
4200 SDValue ShOpHi = Op.getOperand(1);
4201 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004202 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004203 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004204
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004205 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4206
Jim Grosbach624fcb22009-10-31 21:00:56 +00004207 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4208 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4209 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4210 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4211 DAG.getConstant(VTBits, MVT::i32));
4212 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4213 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004214 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004215
4216 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4217 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004218 ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004219 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004220 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004221 CCR, Cmp);
4222
4223 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004224 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004225}
4226
Jim Grosbach5d994042009-10-31 19:38:01 +00004227/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4228/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004229SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4230 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004231 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4232 EVT VT = Op.getValueType();
4233 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004234 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004235 SDValue ShOpLo = Op.getOperand(0);
4236 SDValue ShOpHi = Op.getOperand(1);
4237 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004238 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004239
4240 assert(Op.getOpcode() == ISD::SHL_PARTS);
4241 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4242 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4243 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4244 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4245 DAG.getConstant(VTBits, MVT::i32));
4246 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4247 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4248
4249 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4250 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4251 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004252 ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004253 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004254 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004255 CCR, Cmp);
4256
4257 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004258 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004259}
4260
Jim Grosbach535d3b42010-09-08 03:54:02 +00004261SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004262 SelectionDAG &DAG) const {
4263 // The rounding mode is in bits 23:22 of the FPSCR.
4264 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4265 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4266 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004267 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004268 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
4269 DAG.getConstant(Intrinsic::arm_get_fpscr,
4270 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004271 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemanb69b1822010-08-03 21:31:55 +00004272 DAG.getConstant(1U << 22, MVT::i32));
4273 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
4274 DAG.getConstant(22, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004275 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemanb69b1822010-08-03 21:31:55 +00004276 DAG.getConstant(3, MVT::i32));
4277}
4278
Jim Grosbach8546ec92010-01-18 19:58:49 +00004279static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4280 const ARMSubtarget *ST) {
4281 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004282 SDLoc dl(N);
Jim Grosbach8546ec92010-01-18 19:58:49 +00004283
4284 if (!ST->hasV6T2Ops())
4285 return SDValue();
4286
4287 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
4288 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4289}
4290
Evan Chengb4eae132012-12-04 22:41:50 +00004291/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4292/// for each 16-bit element from operand, repeated. The basic idea is to
4293/// leverage vcnt to get the 8-bit counts, gather and add the results.
4294///
4295/// Trace for v4i16:
4296/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4297/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4298/// 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 +00004299/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004300/// [b0 b1 b2 b3 b4 b5 b6 b7]
4301/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4302/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4303/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4304static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4305 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004306 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004307
4308 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4309 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4310 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4311 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4312 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4313 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4314}
4315
4316/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4317/// bit-count for each 16-bit element from the operand. We need slightly
4318/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4319/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004320///
Evan Chengb4eae132012-12-04 22:41:50 +00004321/// Trace for v4i16:
4322/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4323/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4324/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4325/// v4i16:Extracted = [k0 k1 k2 k3 ]
4326static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4327 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004328 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004329
4330 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4331 if (VT.is64BitVector()) {
4332 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4333 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
4334 DAG.getIntPtrConstant(0));
4335 } else {
4336 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
4337 BitCounts, DAG.getIntPtrConstant(0));
4338 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4339 }
4340}
4341
4342/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4343/// bit-count for each 32-bit element from the operand. The idea here is
4344/// to split the vector into 16-bit elements, leverage the 16-bit count
4345/// routine, and then combine the results.
4346///
4347/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4348/// input = [v0 v1 ] (vi: 32-bit elements)
4349/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4350/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004351/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004352/// [k0 k1 k2 k3 ]
4353/// N1 =+[k1 k0 k3 k2 ]
4354/// [k0 k2 k1 k3 ]
4355/// N2 =+[k1 k3 k0 k2 ]
4356/// [k0 k2 k1 k3 ]
4357/// Extended =+[k1 k3 k0 k2 ]
4358/// [k0 k2 ]
4359/// Extracted=+[k1 k3 ]
4360///
4361static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4362 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004363 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004364
4365 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4366
4367 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4368 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4369 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4370 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4371 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4372
4373 if (VT.is64BitVector()) {
4374 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4375 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
4376 DAG.getIntPtrConstant(0));
4377 } else {
4378 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
4379 DAG.getIntPtrConstant(0));
4380 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4381 }
4382}
4383
4384static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4385 const ARMSubtarget *ST) {
4386 EVT VT = N->getValueType(0);
4387
4388 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004389 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4390 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004391 "Unexpected type for custom ctpop lowering");
4392
4393 if (VT.getVectorElementType() == MVT::i32)
4394 return lowerCTPOP32BitElements(N, DAG);
4395 else
4396 return lowerCTPOP16BitElements(N, DAG);
4397}
4398
Bob Wilson2e076c42009-06-22 23:27:02 +00004399static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4400 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004401 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004402 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004403
Bob Wilson7d471332010-11-18 21:16:28 +00004404 if (!VT.isVector())
4405 return SDValue();
4406
Bob Wilson2e076c42009-06-22 23:27:02 +00004407 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004408 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004409
Bob Wilson7d471332010-11-18 21:16:28 +00004410 // Left shifts translate directly to the vshiftu intrinsic.
4411 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004412 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilson7d471332010-11-18 21:16:28 +00004413 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
4414 N->getOperand(0), N->getOperand(1));
4415
4416 assert((N->getOpcode() == ISD::SRA ||
4417 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4418
4419 // NEON uses the same intrinsics for both left and right shifts. For
4420 // right shifts, the shift amounts are negative, so negate the vector of
4421 // shift amounts.
4422 EVT ShiftVT = N->getOperand(1).getValueType();
4423 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4424 getZeroVector(ShiftVT, DAG, dl),
4425 N->getOperand(1));
4426 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4427 Intrinsic::arm_neon_vshifts :
4428 Intrinsic::arm_neon_vshiftu);
4429 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4430 DAG.getConstant(vshiftInt, MVT::i32),
4431 N->getOperand(0), NegatedCount);
4432}
4433
4434static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4435 const ARMSubtarget *ST) {
4436 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004437 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004438
Eli Friedman682d8c12009-08-22 03:13:10 +00004439 // We can get here for a node like i32 = ISD::SHL i32, i64
4440 if (VT != MVT::i64)
4441 return SDValue();
4442
4443 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004444 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004445
Chris Lattnerf81d5882007-11-24 07:07:01 +00004446 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4447 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004448 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004449 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004450
Chris Lattnerf81d5882007-11-24 07:07:01 +00004451 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004452 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004453
Chris Lattnerf81d5882007-11-24 07:07:01 +00004454 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004455 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004456 DAG.getConstant(0, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004457 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004458 DAG.getConstant(1, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004459
Chris Lattnerf81d5882007-11-24 07:07:01 +00004460 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4461 // captures the result into a carry flag.
4462 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004463 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004464
Chris Lattnerf81d5882007-11-24 07:07:01 +00004465 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004466 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004467
Chris Lattnerf81d5882007-11-24 07:07:01 +00004468 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004469 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004470}
4471
Bob Wilson2e076c42009-06-22 23:27:02 +00004472static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4473 SDValue TmpOp0, TmpOp1;
4474 bool Invert = false;
4475 bool Swap = false;
4476 unsigned Opc = 0;
4477
4478 SDValue Op0 = Op.getOperand(0);
4479 SDValue Op1 = Op.getOperand(1);
4480 SDValue CC = Op.getOperand(2);
Tim Northover45aa89c2015-02-08 00:50:47 +00004481 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004482 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004483 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004484 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004485
Oliver Stannard51b1d462014-08-21 12:50:31 +00004486 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004487 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004488 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004489 case ISD::SETUNE:
4490 case ISD::SETNE: Invert = true; // Fallthrough
4491 case ISD::SETOEQ:
4492 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4493 case ISD::SETOLT:
4494 case ISD::SETLT: Swap = true; // Fallthrough
4495 case ISD::SETOGT:
4496 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4497 case ISD::SETOLE:
4498 case ISD::SETLE: Swap = true; // Fallthrough
4499 case ISD::SETOGE:
4500 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4501 case ISD::SETUGE: Swap = true; // Fallthrough
4502 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4503 case ISD::SETUGT: Swap = true; // Fallthrough
4504 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4505 case ISD::SETUEQ: Invert = true; // Fallthrough
4506 case ISD::SETONE:
4507 // Expand this to (OLT | OGT).
4508 TmpOp0 = Op0;
4509 TmpOp1 = Op1;
4510 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004511 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4512 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004513 break;
4514 case ISD::SETUO: Invert = true; // Fallthrough
4515 case ISD::SETO:
4516 // Expand this to (OLT | OGE).
4517 TmpOp0 = Op0;
4518 TmpOp1 = Op1;
4519 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004520 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4521 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004522 break;
4523 }
4524 } else {
4525 // Integer comparisons.
4526 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004527 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004528 case ISD::SETNE: Invert = true;
4529 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4530 case ISD::SETLT: Swap = true;
4531 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4532 case ISD::SETLE: Swap = true;
4533 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4534 case ISD::SETULT: Swap = true;
4535 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4536 case ISD::SETULE: Swap = true;
4537 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4538 }
4539
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004540 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004541 if (Opc == ARMISD::VCEQ) {
4542
4543 SDValue AndOp;
4544 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4545 AndOp = Op0;
4546 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4547 AndOp = Op1;
4548
4549 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004550 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004551 AndOp = AndOp.getOperand(0);
4552
4553 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4554 Opc = ARMISD::VTST;
Tim Northover45aa89c2015-02-08 00:50:47 +00004555 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
4556 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004557 Invert = !Invert;
4558 }
4559 }
4560 }
4561
4562 if (Swap)
4563 std::swap(Op0, Op1);
4564
Owen Andersonc7baee32010-11-08 23:21:22 +00004565 // If one of the operands is a constant vector zero, attempt to fold the
4566 // comparison to a specialized compare-against-zero form.
4567 SDValue SingleOp;
4568 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4569 SingleOp = Op0;
4570 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4571 if (Opc == ARMISD::VCGE)
4572 Opc = ARMISD::VCLEZ;
4573 else if (Opc == ARMISD::VCGT)
4574 Opc = ARMISD::VCLTZ;
4575 SingleOp = Op1;
4576 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004577
Owen Andersonc7baee32010-11-08 23:21:22 +00004578 SDValue Result;
4579 if (SingleOp.getNode()) {
4580 switch (Opc) {
4581 case ARMISD::VCEQ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004582 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004583 case ARMISD::VCGE:
Tim Northover45aa89c2015-02-08 00:50:47 +00004584 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004585 case ARMISD::VCLEZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004586 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004587 case ARMISD::VCGT:
Tim Northover45aa89c2015-02-08 00:50:47 +00004588 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004589 case ARMISD::VCLTZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004590 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004591 default:
Tim Northover45aa89c2015-02-08 00:50:47 +00004592 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004593 }
4594 } else {
Tim Northover45aa89c2015-02-08 00:50:47 +00004595 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004596 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004597
Tim Northover45aa89c2015-02-08 00:50:47 +00004598 Result = DAG.getSExtOrTrunc(Result, dl, VT);
4599
Bob Wilson2e076c42009-06-22 23:27:02 +00004600 if (Invert)
4601 Result = DAG.getNOT(dl, Result, VT);
4602
4603 return Result;
4604}
4605
Bob Wilson5b2b5042010-06-14 22:19:57 +00004606/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4607/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004608/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004609static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4610 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Andersona4076922010-11-05 21:57:54 +00004611 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004612 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004613
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004614 // SplatBitSize is set to the smallest size that splats the vector, so a
4615 // zero vector will always have SplatBitSize == 8. However, NEON modified
4616 // immediate instructions others than VMOV do not support the 8-bit encoding
4617 // of a zero vector, and the default encoding of zero is supposed to be the
4618 // 32-bit version.
4619 if (SplatBits == 0)
4620 SplatBitSize = 32;
4621
Bob Wilson2e076c42009-06-22 23:27:02 +00004622 switch (SplatBitSize) {
4623 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004624 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004625 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004626 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004627 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004628 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004629 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004630 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004631 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004632
4633 case 16:
4634 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004635 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004636 if ((SplatBits & ~0xff) == 0) {
4637 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004638 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004639 Imm = SplatBits;
4640 break;
4641 }
4642 if ((SplatBits & ~0xff00) == 0) {
4643 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004644 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004645 Imm = SplatBits >> 8;
4646 break;
4647 }
4648 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004649
4650 case 32:
4651 // NEON's 32-bit VMOV supports splat values where:
4652 // * only one byte is nonzero, or
4653 // * the least significant byte is 0xff and the second byte is nonzero, or
4654 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004655 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004656 if ((SplatBits & ~0xff) == 0) {
4657 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004658 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004659 Imm = SplatBits;
4660 break;
4661 }
4662 if ((SplatBits & ~0xff00) == 0) {
4663 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004664 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004665 Imm = SplatBits >> 8;
4666 break;
4667 }
4668 if ((SplatBits & ~0xff0000) == 0) {
4669 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004670 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004671 Imm = SplatBits >> 16;
4672 break;
4673 }
4674 if ((SplatBits & ~0xff000000) == 0) {
4675 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004676 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004677 Imm = SplatBits >> 24;
4678 break;
4679 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004680
Owen Andersona4076922010-11-05 21:57:54 +00004681 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4682 if (type == OtherModImm) return SDValue();
4683
Bob Wilson2e076c42009-06-22 23:27:02 +00004684 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004685 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4686 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004687 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004688 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004689 break;
4690 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004691
4692 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004693 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4694 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004695 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004696 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004697 break;
4698 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004699
4700 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4701 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4702 // VMOV.I32. A (very) minor optimization would be to replicate the value
4703 // and fall through here to test for a valid 64-bit splat. But, then the
4704 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004705 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004706
4707 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004708 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004709 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004710 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004711 uint64_t BitMask = 0xff;
4712 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004713 unsigned ImmMask = 1;
4714 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004715 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004716 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004717 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004718 Imm |= ImmMask;
4719 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004720 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004721 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004722 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004723 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004724 }
Christian Pirker6f81e752014-06-23 18:05:53 +00004725
4726 if (DAG.getTargetLoweringInfo().isBigEndian())
4727 // swap higher and lower 32 bit word
4728 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
4729
Bob Wilson6eae5202010-06-11 21:34:50 +00004730 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004731 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004732 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004733 break;
4734 }
4735
Bob Wilson6eae5202010-06-11 21:34:50 +00004736 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004737 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004738 }
4739
Bob Wilsona3f19012010-07-13 21:16:48 +00004740 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4741 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004742}
4743
Lang Hames591cdaf2012-03-29 21:56:11 +00004744SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4745 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004746 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004747 return SDValue();
4748
Tim Northoverf79c3a52013-08-20 08:57:11 +00004749 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004750 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004751
Oliver Stannard51b1d462014-08-21 12:50:31 +00004752 // Use the default (constant pool) lowering for double constants when we have
4753 // an SP-only FPU
4754 if (IsDouble && Subtarget->isFPOnlySP())
4755 return SDValue();
4756
Lang Hames591cdaf2012-03-29 21:56:11 +00004757 // Try splatting with a VMOV.f32...
4758 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004759 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4760
Lang Hames591cdaf2012-03-29 21:56:11 +00004761 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004762 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4763 // We have code in place to select a valid ConstantFP already, no need to
4764 // do any mangling.
4765 return Op;
4766 }
4767
4768 // It's a float and we are trying to use NEON operations where
4769 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004770 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004771 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4772 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4773 NewVal);
4774 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4775 DAG.getConstant(0, MVT::i32));
4776 }
4777
Tim Northoverf79c3a52013-08-20 08:57:11 +00004778 // The rest of our options are NEON only, make sure that's allowed before
4779 // proceeding..
4780 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4781 return SDValue();
4782
Lang Hames591cdaf2012-03-29 21:56:11 +00004783 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004784 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4785
4786 // It wouldn't really be worth bothering for doubles except for one very
4787 // important value, which does happen to match: 0.0. So make sure we don't do
4788 // anything stupid.
4789 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4790 return SDValue();
4791
4792 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
4793 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4794 false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004795 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004796 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004797 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4798 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004799 if (IsDouble)
4800 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4801
4802 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004803 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4804 VecConstant);
4805 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4806 DAG.getConstant(0, MVT::i32));
4807 }
4808
4809 // Finally, try a VMVN.i32
Tim Northoverf79c3a52013-08-20 08:57:11 +00004810 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4811 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004812 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004813 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004814 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004815
4816 if (IsDouble)
4817 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4818
4819 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004820 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4821 VecConstant);
4822 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4823 DAG.getConstant(0, MVT::i32));
4824 }
4825
4826 return SDValue();
4827}
4828
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004829// check if an VEXT instruction can handle the shuffle mask when the
4830// vector sources of the shuffle are the same.
4831static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4832 unsigned NumElts = VT.getVectorNumElements();
4833
4834 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4835 if (M[0] < 0)
4836 return false;
4837
4838 Imm = M[0];
4839
4840 // If this is a VEXT shuffle, the immediate value is the index of the first
4841 // element. The other shuffle indices must be the successive elements after
4842 // the first one.
4843 unsigned ExpectedElt = Imm;
4844 for (unsigned i = 1; i < NumElts; ++i) {
4845 // Increment the expected index. If it wraps around, just follow it
4846 // back to index zero and keep going.
4847 ++ExpectedElt;
4848 if (ExpectedElt == NumElts)
4849 ExpectedElt = 0;
4850
4851 if (M[i] < 0) continue; // ignore UNDEF indices
4852 if (ExpectedElt != static_cast<unsigned>(M[i]))
4853 return false;
4854 }
4855
4856 return true;
4857}
4858
Lang Hames591cdaf2012-03-29 21:56:11 +00004859
Benjamin Kramer339ced42012-01-15 13:16:05 +00004860static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004861 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004862 unsigned NumElts = VT.getVectorNumElements();
4863 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004864
4865 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4866 if (M[0] < 0)
4867 return false;
4868
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004869 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004870
4871 // If this is a VEXT shuffle, the immediate value is the index of the first
4872 // element. The other shuffle indices must be the successive elements after
4873 // the first one.
4874 unsigned ExpectedElt = Imm;
4875 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004876 // Increment the expected index. If it wraps around, it may still be
4877 // a VEXT but the source vectors must be swapped.
4878 ExpectedElt += 1;
4879 if (ExpectedElt == NumElts * 2) {
4880 ExpectedElt = 0;
4881 ReverseVEXT = true;
4882 }
4883
Bob Wilson411dfad2010-08-17 05:54:34 +00004884 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004885 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004886 return false;
4887 }
4888
4889 // Adjust the index value if the source operands will be swapped.
4890 if (ReverseVEXT)
4891 Imm -= NumElts;
4892
Bob Wilson32cd8552009-08-19 17:03:43 +00004893 return true;
4894}
4895
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004896/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4897/// instruction with the specified blocksize. (The order of the elements
4898/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00004899static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004900 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4901 "Only possible block sizes for VREV are: 16, 32, 64");
4902
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004903 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00004904 if (EltSz == 64)
4905 return false;
4906
4907 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004908 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00004909 // If the first shuffle index is UNDEF, be optimistic.
4910 if (M[0] < 0)
4911 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004912
4913 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4914 return false;
4915
4916 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004917 if (M[i] < 0) continue; // ignore UNDEF indices
4918 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004919 return false;
4920 }
4921
4922 return true;
4923}
4924
Benjamin Kramer339ced42012-01-15 13:16:05 +00004925static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00004926 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4927 // range, then 0 is placed into the resulting vector. So pretty much any mask
4928 // of 8 elements can work here.
4929 return VT == MVT::v8i8 && M.size() == 8;
4930}
4931
Benjamin Kramer339ced42012-01-15 13:16:05 +00004932static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004933 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4934 if (EltSz == 64)
4935 return false;
4936
Bob Wilsona7062312009-08-21 20:54:19 +00004937 unsigned NumElts = VT.getVectorNumElements();
4938 WhichResult = (M[0] == 0 ? 0 : 1);
4939 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004940 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4941 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsona7062312009-08-21 20:54:19 +00004942 return false;
4943 }
4944 return true;
4945}
4946
Bob Wilson0bbd3072009-12-03 06:40:55 +00004947/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4948/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4949/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004950static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004951 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4952 if (EltSz == 64)
4953 return false;
4954
4955 unsigned NumElts = VT.getVectorNumElements();
4956 WhichResult = (M[0] == 0 ? 0 : 1);
4957 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004958 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4959 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004960 return false;
4961 }
4962 return true;
4963}
4964
Benjamin Kramer339ced42012-01-15 13:16:05 +00004965static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004966 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4967 if (EltSz == 64)
4968 return false;
4969
Bob Wilsona7062312009-08-21 20:54:19 +00004970 unsigned NumElts = VT.getVectorNumElements();
4971 WhichResult = (M[0] == 0 ? 0 : 1);
4972 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004973 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsona7062312009-08-21 20:54:19 +00004974 if ((unsigned) M[i] != 2 * i + WhichResult)
4975 return false;
4976 }
4977
4978 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004979 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004980 return false;
4981
4982 return true;
4983}
4984
Bob Wilson0bbd3072009-12-03 06:40:55 +00004985/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4986/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4987/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00004988static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004989 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4990 if (EltSz == 64)
4991 return false;
4992
4993 unsigned Half = VT.getVectorNumElements() / 2;
4994 WhichResult = (M[0] == 0 ? 0 : 1);
4995 for (unsigned j = 0; j != 2; ++j) {
4996 unsigned Idx = WhichResult;
4997 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004998 int MIdx = M[i + j * Half];
4999 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson0bbd3072009-12-03 06:40:55 +00005000 return false;
5001 Idx += 2;
5002 }
5003 }
5004
5005 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5006 if (VT.is64BitVector() && EltSz == 32)
5007 return false;
5008
5009 return true;
5010}
5011
Benjamin Kramer339ced42012-01-15 13:16:05 +00005012static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005013 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5014 if (EltSz == 64)
5015 return false;
5016
Bob Wilsona7062312009-08-21 20:54:19 +00005017 unsigned NumElts = VT.getVectorNumElements();
5018 WhichResult = (M[0] == 0 ? 0 : 1);
5019 unsigned Idx = WhichResult * NumElts / 2;
5020 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005021 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
5022 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsona7062312009-08-21 20:54:19 +00005023 return false;
5024 Idx += 1;
5025 }
5026
5027 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005028 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005029 return false;
5030
5031 return true;
5032}
5033
Bob Wilson0bbd3072009-12-03 06:40:55 +00005034/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
5035/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5036/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005037static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005038 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5039 if (EltSz == 64)
5040 return false;
5041
5042 unsigned NumElts = VT.getVectorNumElements();
5043 WhichResult = (M[0] == 0 ? 0 : 1);
5044 unsigned Idx = WhichResult * NumElts / 2;
5045 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005046 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
5047 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson0bbd3072009-12-03 06:40:55 +00005048 return false;
5049 Idx += 1;
5050 }
5051
5052 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5053 if (VT.is64BitVector() && EltSz == 32)
5054 return false;
5055
5056 return true;
5057}
5058
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005059/// \return true if this is a reverse operation on an vector.
5060static bool isReverseMask(ArrayRef<int> M, EVT VT) {
5061 unsigned NumElts = VT.getVectorNumElements();
5062 // Make sure the mask has the right size.
5063 if (NumElts != M.size())
5064 return false;
5065
5066 // Look for <15, ..., 3, -1, 1, 0>.
5067 for (unsigned i = 0; i != NumElts; ++i)
5068 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
5069 return false;
5070
5071 return true;
5072}
5073
Dale Johannesen2bff5052010-07-29 20:10:08 +00005074// If N is an integer constant that can be moved into a register in one
5075// instruction, return an SDValue of such a constant (will become a MOV
5076// instruction). Otherwise return null.
5077static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005078 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00005079 uint64_t Val;
5080 if (!isa<ConstantSDNode>(N))
5081 return SDValue();
5082 Val = cast<ConstantSDNode>(N)->getZExtValue();
5083
5084 if (ST->isThumb1Only()) {
5085 if (Val <= 255 || ~Val <= 255)
5086 return DAG.getConstant(Val, MVT::i32);
5087 } else {
5088 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
5089 return DAG.getConstant(Val, MVT::i32);
5090 }
5091 return SDValue();
5092}
5093
Bob Wilson2e076c42009-06-22 23:27:02 +00005094// If this is a case we can't handle, return null and let the default
5095// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005096SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
5097 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00005098 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005099 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005100 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005101
5102 APInt SplatBits, SplatUndef;
5103 unsigned SplatBitSize;
5104 bool HasAnyUndefs;
5105 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005106 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00005107 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00005108 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00005109 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00005110 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00005111 DAG, VmovVT, VT.is128BitVector(),
5112 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00005113 if (Val.getNode()) {
5114 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005115 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00005116 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00005117
5118 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00005119 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005120 Val = isNEONModifiedImm(NegatedImm,
5121 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00005122 DAG, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005123 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005124 if (Val.getNode()) {
5125 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005126 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005127 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005128
5129 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00005130 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00005131 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005132 if (ImmVal != -1) {
5133 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
5134 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
5135 }
5136 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005137 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00005138 }
5139
Bob Wilson91fdf682010-05-22 00:23:12 +00005140 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00005141 //
5142 // As an optimisation, even if more than one value is used it may be more
5143 // profitable to splat with one value then change some lanes.
5144 //
5145 // Heuristically we decide to do this if the vector has a "dominant" value,
5146 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00005147 unsigned NumElts = VT.getVectorNumElements();
5148 bool isOnlyLowElement = true;
5149 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005150 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00005151 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005152
5153 // Map of the number of times a particular SDValue appears in the
5154 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00005155 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00005156 SDValue Value;
5157 for (unsigned i = 0; i < NumElts; ++i) {
5158 SDValue V = Op.getOperand(i);
5159 if (V.getOpcode() == ISD::UNDEF)
5160 continue;
5161 if (i > 0)
5162 isOnlyLowElement = false;
5163 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5164 isConstant = false;
5165
James Molloy49bdbce2012-09-06 09:55:02 +00005166 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005167 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005168
James Molloy49bdbce2012-09-06 09:55:02 +00005169 // Is this value dominant? (takes up more than half of the lanes)
5170 if (++Count > (NumElts / 2)) {
5171 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005172 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005173 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005174 }
James Molloy49bdbce2012-09-06 09:55:02 +00005175 if (ValueCounts.size() != 1)
5176 usesOnlyOneValue = false;
5177 if (!Value.getNode() && ValueCounts.size() > 0)
5178 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005179
James Molloy49bdbce2012-09-06 09:55:02 +00005180 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005181 return DAG.getUNDEF(VT);
5182
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005183 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5184 // Keep going if we are hitting this case.
5185 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005186 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5187
Dale Johannesen2bff5052010-07-29 20:10:08 +00005188 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5189
Dale Johannesen710a2d92010-10-19 20:00:17 +00005190 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5191 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005192 if (hasDominantValue && EltSize <= 32) {
5193 if (!isConstant) {
5194 SDValue N;
5195
5196 // If we are VDUPing a value that comes directly from a vector, that will
5197 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005198 // just use VDUPLANE. We can only do this if the lane being extracted
5199 // is at a constant index, as the VDUP from lane instructions only have
5200 // constant-index forms.
5201 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5202 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005203 // We need to create a new undef vector to use for the VDUPLANE if the
5204 // size of the vector from which we get the value is different than the
5205 // size of the vector that we need to create. We will insert the element
5206 // such that the register coalescer will remove unnecessary copies.
5207 if (VT != Value->getOperand(0).getValueType()) {
5208 ConstantSDNode *constIndex;
5209 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
5210 assert(constIndex && "The index is not a constant!");
5211 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5212 VT.getVectorNumElements();
5213 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5214 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
5215 Value, DAG.getConstant(index, MVT::i32)),
5216 DAG.getConstant(index, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005217 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005218 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005219 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005220 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005221 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5222
5223 if (!usesOnlyOneValue) {
5224 // The dominant value was splatted as 'N', but we now have to insert
5225 // all differing elements.
5226 for (unsigned I = 0; I < NumElts; ++I) {
5227 if (Op.getOperand(I) == Value)
5228 continue;
5229 SmallVector<SDValue, 3> Ops;
5230 Ops.push_back(N);
5231 Ops.push_back(Op.getOperand(I));
5232 Ops.push_back(DAG.getConstant(I, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005233 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005234 }
5235 }
5236 return N;
5237 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005238 if (VT.getVectorElementType().isFloatingPoint()) {
5239 SmallVector<SDValue, 8> Ops;
5240 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005241 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005242 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005243 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005244 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005245 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5246 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005247 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005248 }
James Molloy49bdbce2012-09-06 09:55:02 +00005249 if (usesOnlyOneValue) {
5250 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5251 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005252 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005253 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005254 }
5255
5256 // If all elements are constants and the case above didn't get hit, fall back
5257 // to the default expansion, which will generate a load from the constant
5258 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005259 if (isConstant)
5260 return SDValue();
5261
Bob Wilson6f2b8962011-01-07 21:37:30 +00005262 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5263 if (NumElts >= 4) {
5264 SDValue shuffle = ReconstructShuffle(Op, DAG);
5265 if (shuffle != SDValue())
5266 return shuffle;
5267 }
5268
Bob Wilson91fdf682010-05-22 00:23:12 +00005269 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005270 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5271 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005272 if (EltSize >= 32) {
5273 // Do the expansion with floating-point types, since that is what the VFP
5274 // registers are defined to use, and since i64 is not legal.
5275 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5276 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005277 SmallVector<SDValue, 8> Ops;
5278 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005279 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005280 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005281 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005282 }
5283
Jim Grosbach24e102a2013-07-08 18:18:52 +00005284 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5285 // know the default expansion would otherwise fall back on something even
5286 // worse. For a vector with one or two non-undef values, that's
5287 // scalar_to_vector for the elements followed by a shuffle (provided the
5288 // shuffle is valid for the target) and materialization element by element
5289 // on the stack followed by a load for everything else.
5290 if (!isConstant && !usesOnlyOneValue) {
5291 SDValue Vec = DAG.getUNDEF(VT);
5292 for (unsigned i = 0 ; i < NumElts; ++i) {
5293 SDValue V = Op.getOperand(i);
5294 if (V.getOpcode() == ISD::UNDEF)
5295 continue;
5296 SDValue LaneIdx = DAG.getConstant(i, MVT::i32);
5297 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5298 }
5299 return Vec;
5300 }
5301
Bob Wilson2e076c42009-06-22 23:27:02 +00005302 return SDValue();
5303}
5304
Bob Wilson6f2b8962011-01-07 21:37:30 +00005305// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005306// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005307SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5308 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005309 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005310 EVT VT = Op.getValueType();
5311 unsigned NumElts = VT.getVectorNumElements();
5312
5313 SmallVector<SDValue, 2> SourceVecs;
5314 SmallVector<unsigned, 2> MinElts;
5315 SmallVector<unsigned, 2> MaxElts;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005316
Bob Wilson6f2b8962011-01-07 21:37:30 +00005317 for (unsigned i = 0; i < NumElts; ++i) {
5318 SDValue V = Op.getOperand(i);
5319 if (V.getOpcode() == ISD::UNDEF)
5320 continue;
5321 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5322 // A shuffle can only come from building a vector from various
5323 // elements of other vectors.
5324 return SDValue();
Eli Friedman74d1da52011-10-14 23:58:49 +00005325 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
5326 VT.getVectorElementType()) {
5327 // This code doesn't know how to handle shuffles where the vector
5328 // element types do not match (this happens because type legalization
5329 // promotes the return type of EXTRACT_VECTOR_ELT).
5330 // FIXME: It might be appropriate to extend this code to handle
5331 // mismatched types.
5332 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005333 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005334
Bob Wilson6f2b8962011-01-07 21:37:30 +00005335 // Record this extraction against the appropriate vector if possible...
5336 SDValue SourceVec = V.getOperand(0);
Jim Grosbach6df755c2012-07-25 17:02:47 +00005337 // If the element number isn't a constant, we can't effectively
5338 // analyze what's going on.
5339 if (!isa<ConstantSDNode>(V.getOperand(1)))
5340 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005341 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5342 bool FoundSource = false;
5343 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
5344 if (SourceVecs[j] == SourceVec) {
5345 if (MinElts[j] > EltNo)
5346 MinElts[j] = EltNo;
5347 if (MaxElts[j] < EltNo)
5348 MaxElts[j] = EltNo;
5349 FoundSource = true;
5350 break;
5351 }
5352 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005353
Bob Wilson6f2b8962011-01-07 21:37:30 +00005354 // Or record a new source if not...
5355 if (!FoundSource) {
5356 SourceVecs.push_back(SourceVec);
5357 MinElts.push_back(EltNo);
5358 MaxElts.push_back(EltNo);
5359 }
5360 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005361
Bob Wilson6f2b8962011-01-07 21:37:30 +00005362 // Currently only do something sane when at most two source vectors
5363 // involved.
5364 if (SourceVecs.size() > 2)
5365 return SDValue();
5366
5367 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
5368 int VEXTOffsets[2] = {0, 0};
Andrew Trick5eb0a302011-01-19 02:26:13 +00005369
Bob Wilson6f2b8962011-01-07 21:37:30 +00005370 // This loop extracts the usage patterns of the source vectors
5371 // and prepares appropriate SDValues for a shuffle if possible.
5372 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
5373 if (SourceVecs[i].getValueType() == VT) {
5374 // No VEXT necessary
5375 ShuffleSrcs[i] = SourceVecs[i];
5376 VEXTOffsets[i] = 0;
5377 continue;
5378 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
5379 // It probably isn't worth padding out a smaller vector just to
5380 // break it down again in a shuffle.
5381 return SDValue();
5382 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005383
Bob Wilson6f2b8962011-01-07 21:37:30 +00005384 // Since only 64-bit and 128-bit vectors are legal on ARM and
5385 // we've eliminated the other cases...
Bob Wilson3fa9c062011-01-07 23:40:46 +00005386 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
5387 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick5eb0a302011-01-19 02:26:13 +00005388
Bob Wilson6f2b8962011-01-07 21:37:30 +00005389 if (MaxElts[i] - MinElts[i] >= NumElts) {
5390 // Span too large for a VEXT to cope
5391 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005392 }
5393
Bob Wilson6f2b8962011-01-07 21:37:30 +00005394 if (MinElts[i] >= NumElts) {
5395 // The extraction can just take the second half
5396 VEXTOffsets[i] = NumElts;
Eric Christopher2af95512011-01-14 23:50:53 +00005397 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5398 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005399 DAG.getIntPtrConstant(NumElts));
5400 } else if (MaxElts[i] < NumElts) {
5401 // The extraction can just take the first half
5402 VEXTOffsets[i] = 0;
Eric Christopher2af95512011-01-14 23:50:53 +00005403 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5404 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005405 DAG.getIntPtrConstant(0));
5406 } else {
5407 // An actual VEXT is needed
5408 VEXTOffsets[i] = MinElts[i];
Eric Christopher2af95512011-01-14 23:50:53 +00005409 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5410 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005411 DAG.getIntPtrConstant(0));
Eric Christopher2af95512011-01-14 23:50:53 +00005412 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5413 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005414 DAG.getIntPtrConstant(NumElts));
5415 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
5416 DAG.getConstant(VEXTOffsets[i], MVT::i32));
5417 }
5418 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005419
Bob Wilson6f2b8962011-01-07 21:37:30 +00005420 SmallVector<int, 8> Mask;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005421
Bob Wilson6f2b8962011-01-07 21:37:30 +00005422 for (unsigned i = 0; i < NumElts; ++i) {
5423 SDValue Entry = Op.getOperand(i);
5424 if (Entry.getOpcode() == ISD::UNDEF) {
5425 Mask.push_back(-1);
5426 continue;
5427 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005428
Bob Wilson6f2b8962011-01-07 21:37:30 +00005429 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher2af95512011-01-14 23:50:53 +00005430 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
5431 .getOperand(1))->getSExtValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005432 if (ExtractVec == SourceVecs[0]) {
5433 Mask.push_back(ExtractElt - VEXTOffsets[0]);
5434 } else {
5435 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
5436 }
5437 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005438
Bob Wilson6f2b8962011-01-07 21:37:30 +00005439 // Final check before we try to produce nonsense...
5440 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher2af95512011-01-14 23:50:53 +00005441 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
5442 &Mask[0]);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005443
Bob Wilson6f2b8962011-01-07 21:37:30 +00005444 return SDValue();
5445}
5446
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005447/// isShuffleMaskLegal - Targets can use this to indicate that they only
5448/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5449/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5450/// are assumed to be legal.
5451bool
5452ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5453 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005454 if (VT.getVectorNumElements() == 4 &&
5455 (VT.is128BitVector() || VT.is64BitVector())) {
5456 unsigned PFIndexes[4];
5457 for (unsigned i = 0; i != 4; ++i) {
5458 if (M[i] < 0)
5459 PFIndexes[i] = 8;
5460 else
5461 PFIndexes[i] = M[i];
5462 }
5463
5464 // Compute the index in the perfect shuffle table.
5465 unsigned PFTableIndex =
5466 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5467 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5468 unsigned Cost = (PFEntry >> 30);
5469
5470 if (Cost <= 4)
5471 return true;
5472 }
5473
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005474 bool ReverseVEXT;
Bob Wilsona7062312009-08-21 20:54:19 +00005475 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005476
Bob Wilson846bd792010-06-07 23:53:38 +00005477 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5478 return (EltSize >= 32 ||
5479 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005480 isVREVMask(M, VT, 64) ||
5481 isVREVMask(M, VT, 32) ||
5482 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005483 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005484 isVTBLMask(M, VT) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005485 isVTRNMask(M, VT, WhichResult) ||
5486 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson0bbd3072009-12-03 06:40:55 +00005487 isVZIPMask(M, VT, WhichResult) ||
5488 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
5489 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005490 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
5491 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005492}
5493
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005494/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5495/// the specified operations to build the shuffle.
5496static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5497 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005498 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005499 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5500 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5501 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5502
5503 enum {
5504 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5505 OP_VREV,
5506 OP_VDUP0,
5507 OP_VDUP1,
5508 OP_VDUP2,
5509 OP_VDUP3,
5510 OP_VEXT1,
5511 OP_VEXT2,
5512 OP_VEXT3,
5513 OP_VUZPL, // VUZP, left result
5514 OP_VUZPR, // VUZP, right result
5515 OP_VZIPL, // VZIP, left result
5516 OP_VZIPR, // VZIP, right result
5517 OP_VTRNL, // VTRN, left result
5518 OP_VTRNR // VTRN, right result
5519 };
5520
5521 if (OpNum == OP_COPY) {
5522 if (LHSID == (1*9+2)*9+3) return LHS;
5523 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5524 return RHS;
5525 }
5526
5527 SDValue OpLHS, OpRHS;
5528 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5529 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5530 EVT VT = OpLHS.getValueType();
5531
5532 switch (OpNum) {
5533 default: llvm_unreachable("Unknown shuffle opcode!");
5534 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005535 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005536 if (VT.getVectorElementType() == MVT::i32 ||
5537 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005538 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5539 // vrev <4 x i16> -> VREV32
5540 if (VT.getVectorElementType() == MVT::i16)
5541 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5542 // vrev <4 x i8> -> VREV16
5543 assert(VT.getVectorElementType() == MVT::i8);
5544 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005545 case OP_VDUP0:
5546 case OP_VDUP1:
5547 case OP_VDUP2:
5548 case OP_VDUP3:
5549 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005550 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005551 case OP_VEXT1:
5552 case OP_VEXT2:
5553 case OP_VEXT3:
5554 return DAG.getNode(ARMISD::VEXT, dl, VT,
5555 OpLHS, OpRHS,
5556 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
5557 case OP_VUZPL:
5558 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005559 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005560 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5561 case OP_VZIPL:
5562 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005563 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005564 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5565 case OP_VTRNL:
5566 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005567 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5568 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005569 }
5570}
5571
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005572static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005573 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005574 SelectionDAG &DAG) {
5575 // Check to see if we can use the VTBL instruction.
5576 SDValue V1 = Op.getOperand(0);
5577 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005578 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005579
5580 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005581 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005582 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
5583 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
5584
5585 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5586 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005587 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005588
Owen Anderson77aa2662011-04-05 21:48:57 +00005589 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005590 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005591}
5592
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005593static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5594 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005595 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005596 SDValue OpLHS = Op.getOperand(0);
5597 EVT VT = OpLHS.getValueType();
5598
5599 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5600 "Expect an v8i16/v16i8 type");
5601 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5602 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5603 // extract the first 8 bytes into the top double word and the last 8 bytes
5604 // into the bottom double word. The v8i16 case is similar.
5605 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5606 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
5607 DAG.getConstant(ExtractNum, MVT::i32));
5608}
5609
Bob Wilson2e076c42009-06-22 23:27:02 +00005610static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005611 SDValue V1 = Op.getOperand(0);
5612 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005613 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005614 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005615 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005616
Bob Wilsonc6800b52009-08-13 02:13:04 +00005617 // Convert shuffles that are directly supported on NEON to target-specific
5618 // DAG nodes, instead of keeping them as shuffles and matching them again
5619 // during code selection. This is more efficient and avoids the possibility
5620 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005621 // FIXME: floating-point vectors should be canonicalized to integer vectors
5622 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005623 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005624
Bob Wilson846bd792010-06-07 23:53:38 +00005625 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5626 if (EltSize <= 32) {
5627 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5628 int Lane = SVN->getSplatIndex();
5629 // If this is undef splat, generate it via "just" vdup, if possible.
5630 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005631
Dan Gohman198b7ff2011-11-03 21:49:52 +00005632 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005633 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5634 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5635 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005636 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5637 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5638 // reaches it).
5639 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5640 !isa<ConstantSDNode>(V1.getOperand(0))) {
5641 bool IsScalarToVector = true;
5642 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5643 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5644 IsScalarToVector = false;
5645 break;
5646 }
5647 if (IsScalarToVector)
5648 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5649 }
Bob Wilson846bd792010-06-07 23:53:38 +00005650 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
5651 DAG.getConstant(Lane, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005652 }
Bob Wilson846bd792010-06-07 23:53:38 +00005653
5654 bool ReverseVEXT;
5655 unsigned Imm;
5656 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5657 if (ReverseVEXT)
5658 std::swap(V1, V2);
5659 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
5660 DAG.getConstant(Imm, MVT::i32));
5661 }
5662
5663 if (isVREVMask(ShuffleMask, VT, 64))
5664 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5665 if (isVREVMask(ShuffleMask, VT, 32))
5666 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5667 if (isVREVMask(ShuffleMask, VT, 16))
5668 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5669
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005670 if (V2->getOpcode() == ISD::UNDEF &&
5671 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5672 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
5673 DAG.getConstant(Imm, MVT::i32));
5674 }
5675
Bob Wilson846bd792010-06-07 23:53:38 +00005676 // Check for Neon shuffles that modify both input vectors in place.
5677 // If both results are used, i.e., if there are two shuffles with the same
5678 // source operands and with masks corresponding to both results of one of
5679 // these operations, DAG memoization will ensure that a single node is
5680 // used for both shuffles.
5681 unsigned WhichResult;
5682 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5683 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5684 V1, V2).getValue(WhichResult);
5685 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5686 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5687 V1, V2).getValue(WhichResult);
5688 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5689 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5690 V1, V2).getValue(WhichResult);
5691
5692 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5693 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5694 V1, V1).getValue(WhichResult);
5695 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5696 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5697 V1, V1).getValue(WhichResult);
5698 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5699 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5700 V1, V1).getValue(WhichResult);
Bob Wilsoncce31f62009-08-14 05:08:32 +00005701 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005702
Bob Wilsona7062312009-08-21 20:54:19 +00005703 // If the shuffle is not directly supported and it has 4 elements, use
5704 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005705 unsigned NumElts = VT.getVectorNumElements();
5706 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005707 unsigned PFIndexes[4];
5708 for (unsigned i = 0; i != 4; ++i) {
5709 if (ShuffleMask[i] < 0)
5710 PFIndexes[i] = 8;
5711 else
5712 PFIndexes[i] = ShuffleMask[i];
5713 }
5714
5715 // Compute the index in the perfect shuffle table.
5716 unsigned PFTableIndex =
5717 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005718 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5719 unsigned Cost = (PFEntry >> 30);
5720
5721 if (Cost <= 4)
5722 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5723 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00005724
Bob Wilsond8a9a042010-06-04 00:04:02 +00005725 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00005726 if (EltSize >= 32) {
5727 // Do the expansion with floating-point types, since that is what the VFP
5728 // registers are defined to use, and since i64 is not legal.
5729 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5730 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005731 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5732 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005733 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00005734 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00005735 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00005736 Ops.push_back(DAG.getUNDEF(EltVT));
5737 else
5738 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5739 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5740 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
5741 MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00005742 }
Craig Topper48d114b2014-04-26 18:35:24 +00005743 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005744 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00005745 }
5746
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005747 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5748 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
5749
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005750 if (VT == MVT::v8i8) {
5751 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
5752 if (NewOp.getNode())
5753 return NewOp;
5754 }
5755
Bob Wilson6f34e272009-08-14 05:16:33 +00005756 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005757}
5758
Eli Friedmana5e244c2011-10-24 23:08:52 +00005759static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5760 // INSERT_VECTOR_ELT is legal only for immediate indexes.
5761 SDValue Lane = Op.getOperand(2);
5762 if (!isa<ConstantSDNode>(Lane))
5763 return SDValue();
5764
5765 return Op;
5766}
5767
Bob Wilson2e076c42009-06-22 23:27:02 +00005768static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00005769 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00005770 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00005771 if (!isa<ConstantSDNode>(Lane))
5772 return SDValue();
5773
5774 SDValue Vec = Op.getOperand(0);
5775 if (Op.getValueType() == MVT::i32 &&
5776 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005777 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00005778 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5779 }
5780
5781 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00005782}
5783
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005784static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5785 // The only time a CONCAT_VECTORS operation can have legal types is when
5786 // two 64-bit vectors are concatenated to a 128-bit vector.
5787 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5788 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005789 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005790 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005791 SDValue Op0 = Op.getOperand(0);
5792 SDValue Op1 = Op.getOperand(1);
5793 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005794 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005795 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005796 DAG.getIntPtrConstant(0));
5797 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005798 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005799 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005800 DAG.getIntPtrConstant(1));
Wesley Peck527da1b2010-11-23 03:31:01 +00005801 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005802}
5803
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005804/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5805/// element has been zero/sign-extended, depending on the isSigned parameter,
5806/// from an integer type half its size.
5807static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5808 bool isSigned) {
5809 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5810 EVT VT = N->getValueType(0);
5811 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5812 SDNode *BVN = N->getOperand(0).getNode();
5813 if (BVN->getValueType(0) != MVT::v4i32 ||
5814 BVN->getOpcode() != ISD::BUILD_VECTOR)
5815 return false;
5816 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5817 unsigned HiElt = 1 - LoElt;
5818 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5819 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5820 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5821 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5822 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5823 return false;
5824 if (isSigned) {
5825 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5826 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5827 return true;
5828 } else {
5829 if (Hi0->isNullValue() && Hi1->isNullValue())
5830 return true;
5831 }
5832 return false;
5833 }
5834
5835 if (N->getOpcode() != ISD::BUILD_VECTOR)
5836 return false;
5837
5838 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5839 SDNode *Elt = N->getOperand(i).getNode();
5840 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5841 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5842 unsigned HalfSize = EltSize / 2;
5843 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005844 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005845 return false;
5846 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005847 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005848 return false;
5849 }
5850 continue;
5851 }
5852 return false;
5853 }
5854
5855 return true;
5856}
5857
5858/// isSignExtended - Check if a node is a vector value that is sign-extended
5859/// or a constant BUILD_VECTOR with sign-extended elements.
5860static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5861 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5862 return true;
5863 if (isExtendedBUILD_VECTOR(N, DAG, true))
5864 return true;
5865 return false;
5866}
5867
5868/// isZeroExtended - Check if a node is a vector value that is zero-extended
5869/// or a constant BUILD_VECTOR with zero-extended elements.
5870static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5871 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5872 return true;
5873 if (isExtendedBUILD_VECTOR(N, DAG, false))
5874 return true;
5875 return false;
5876}
5877
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005878static EVT getExtensionTo64Bits(const EVT &OrigVT) {
5879 if (OrigVT.getSizeInBits() >= 64)
5880 return OrigVT;
5881
5882 assert(OrigVT.isSimple() && "Expecting a simple value type");
5883
5884 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
5885 switch (OrigSimpleTy) {
5886 default: llvm_unreachable("Unexpected Vector Type");
5887 case MVT::v2i8:
5888 case MVT::v2i16:
5889 return MVT::v2i32;
5890 case MVT::v4i8:
5891 return MVT::v4i16;
5892 }
5893}
5894
Sebastian Popa204f722012-11-30 19:08:04 +00005895/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5896/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5897/// We insert the required extension here to get the vector to fill a D register.
5898static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5899 const EVT &OrigTy,
5900 const EVT &ExtTy,
5901 unsigned ExtOpcode) {
5902 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5903 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5904 // 64-bits we need to insert a new extension so that it will be 64-bits.
5905 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5906 if (OrigTy.getSizeInBits() >= 64)
5907 return N;
5908
5909 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005910 EVT NewVT = getExtensionTo64Bits(OrigTy);
5911
Andrew Trickef9de2a2013-05-25 02:42:55 +00005912 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00005913}
5914
5915/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5916/// does not do any sign/zero extension. If the original vector is less
5917/// than 64 bits, an appropriate extension will be added after the load to
5918/// reach a total size of 64 bits. We have to add the extension separately
5919/// because ARM does not have a sign/zero extending load for vectors.
5920static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005921 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
5922
5923 // The load already has the right type.
5924 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005925 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00005926 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5927 LD->isNonTemporal(), LD->isInvariant(),
5928 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005929
5930 // We need to create a zextload/sextload. We cannot just create a load
5931 // followed by a zext/zext node because LowerMUL is also run during normal
5932 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005933 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005934 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00005935 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(),
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005936 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00005937}
5938
5939/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5940/// extending load, or BUILD_VECTOR with extended elements, return the
5941/// unextended value. The unextended vector should be 64 bits so that it can
5942/// be used as an operand to a VMULL instruction. If the original vector size
5943/// before extension is less than 64 bits we add a an extension to resize
5944/// the vector to 64 bits.
5945static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00005946 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00005947 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5948 N->getOperand(0)->getValueType(0),
5949 N->getValueType(0),
5950 N->getOpcode());
5951
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005952 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00005953 return SkipLoadExtensionForVMULL(LD, DAG);
5954
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005955 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5956 // have been legalized as a BITCAST from v4i32.
5957 if (N->getOpcode() == ISD::BITCAST) {
5958 SDNode *BVN = N->getOperand(0).getNode();
5959 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5960 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5961 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00005962 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005963 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5964 }
5965 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5966 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5967 EVT VT = N->getValueType(0);
5968 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5969 unsigned NumElts = VT.getVectorNumElements();
5970 MVT TruncVT = MVT::getIntegerVT(EltSize);
5971 SmallVector<SDValue, 8> Ops;
5972 for (unsigned i = 0; i != NumElts; ++i) {
5973 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5974 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00005975 // Element types smaller than 32 bits are not legal, so use i32 elements.
5976 // The values are implicitly truncated so sext vs. zext doesn't matter.
5977 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005978 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005979 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topper48d114b2014-04-26 18:35:24 +00005980 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00005981}
5982
Evan Chenge2086e72011-03-29 01:56:09 +00005983static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5984 unsigned Opcode = N->getOpcode();
5985 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5986 SDNode *N0 = N->getOperand(0).getNode();
5987 SDNode *N1 = N->getOperand(1).getNode();
5988 return N0->hasOneUse() && N1->hasOneUse() &&
5989 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5990 }
5991 return false;
5992}
5993
5994static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5995 unsigned Opcode = N->getOpcode();
5996 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5997 SDNode *N0 = N->getOperand(0).getNode();
5998 SDNode *N1 = N->getOperand(1).getNode();
5999 return N0->hasOneUse() && N1->hasOneUse() &&
6000 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
6001 }
6002 return false;
6003}
6004
Bob Wilson38ab35a2010-09-01 23:50:19 +00006005static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
6006 // Multiplications are only custom-lowered for 128-bit vectors so that
6007 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
6008 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00006009 assert(VT.is128BitVector() && VT.isInteger() &&
6010 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00006011 SDNode *N0 = Op.getOperand(0).getNode();
6012 SDNode *N1 = Op.getOperand(1).getNode();
6013 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00006014 bool isMLA = false;
6015 bool isN0SExt = isSignExtended(N0, DAG);
6016 bool isN1SExt = isSignExtended(N1, DAG);
6017 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00006018 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00006019 else {
6020 bool isN0ZExt = isZeroExtended(N0, DAG);
6021 bool isN1ZExt = isZeroExtended(N1, DAG);
6022 if (isN0ZExt && isN1ZExt)
6023 NewOpc = ARMISD::VMULLu;
6024 else if (isN1SExt || isN1ZExt) {
6025 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
6026 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
6027 if (isN1SExt && isAddSubSExt(N0, DAG)) {
6028 NewOpc = ARMISD::VMULLs;
6029 isMLA = true;
6030 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
6031 NewOpc = ARMISD::VMULLu;
6032 isMLA = true;
6033 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
6034 std::swap(N0, N1);
6035 NewOpc = ARMISD::VMULLu;
6036 isMLA = true;
6037 }
6038 }
6039
6040 if (!NewOpc) {
6041 if (VT == MVT::v2i64)
6042 // Fall through to expand this. It is not legal.
6043 return SDValue();
6044 else
6045 // Other vector multiplications are legal.
6046 return Op;
6047 }
6048 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006049
6050 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006051 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00006052 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00006053 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006054 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00006055 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006056 assert(Op0.getValueType().is64BitVector() &&
6057 Op1.getValueType().is64BitVector() &&
6058 "unexpected types for extended operands to VMULL");
6059 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
6060 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006061
Evan Chenge2086e72011-03-29 01:56:09 +00006062 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
6063 // isel lowering to take advantage of no-stall back to back vmul + vmla.
6064 // vmull q0, d4, d6
6065 // vmlal q0, d5, d6
6066 // is faster than
6067 // vaddl q0, d4, d5
6068 // vmovl q1, d6
6069 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00006070 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
6071 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006072 EVT Op1VT = Op1.getValueType();
6073 return DAG.getNode(N0->getOpcode(), DL, VT,
6074 DAG.getNode(NewOpc, DL, VT,
6075 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
6076 DAG.getNode(NewOpc, DL, VT,
6077 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00006078}
6079
Owen Anderson77aa2662011-04-05 21:48:57 +00006080static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006081LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00006082 // Convert to float
6083 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
6084 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
6085 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
6086 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
6087 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
6088 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
6089 // Get reciprocal estimate.
6090 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00006091 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006092 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
6093 // Because char has a smaller range than uchar, we can actually get away
6094 // without any newton steps. This requires that we use a weird bias
6095 // of 0xb000, however (again, this has been exhaustively tested).
6096 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
6097 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
6098 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
6099 Y = DAG.getConstant(0xb000, MVT::i32);
6100 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
6101 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
6102 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
6103 // Convert back to short.
6104 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
6105 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
6106 return X;
6107}
6108
Owen Anderson77aa2662011-04-05 21:48:57 +00006109static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006110LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00006111 SDValue N2;
6112 // Convert to float.
6113 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
6114 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
6115 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
6116 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
6117 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
6118 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006119
Nate Begemanfa62d502011-02-11 20:53:29 +00006120 // Use reciprocal estimate and one refinement step.
6121 // float4 recip = vrecpeq_f32(yf);
6122 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006123 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006124 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006125 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006126 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
6127 N1, N2);
6128 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6129 // Because short has a smaller range than ushort, we can actually get away
6130 // with only a single newton step. This requires that we use a weird bias
6131 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006132 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00006133 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6134 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006135 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006136 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6137 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6138 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6139 // Convert back to integer and return.
6140 // return vmovn_s32(vcvt_s32_f32(result));
6141 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6142 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6143 return N0;
6144}
6145
6146static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
6147 EVT VT = Op.getValueType();
6148 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6149 "unexpected type for custom-lowering ISD::SDIV");
6150
Andrew Trickef9de2a2013-05-25 02:42:55 +00006151 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006152 SDValue N0 = Op.getOperand(0);
6153 SDValue N1 = Op.getOperand(1);
6154 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006155
Nate Begemanfa62d502011-02-11 20:53:29 +00006156 if (VT == MVT::v8i8) {
6157 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6158 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006159
Nate Begemanfa62d502011-02-11 20:53:29 +00006160 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6161 DAG.getIntPtrConstant(4));
6162 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00006163 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00006164 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6165 DAG.getIntPtrConstant(0));
6166 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
6167 DAG.getIntPtrConstant(0));
6168
6169 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6170 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6171
6172 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6173 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006174
Nate Begemanfa62d502011-02-11 20:53:29 +00006175 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6176 return N0;
6177 }
6178 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6179}
6180
6181static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
6182 EVT VT = Op.getValueType();
6183 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6184 "unexpected type for custom-lowering ISD::UDIV");
6185
Andrew Trickef9de2a2013-05-25 02:42:55 +00006186 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006187 SDValue N0 = Op.getOperand(0);
6188 SDValue N1 = Op.getOperand(1);
6189 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006190
Nate Begemanfa62d502011-02-11 20:53:29 +00006191 if (VT == MVT::v8i8) {
6192 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6193 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006194
Nate Begemanfa62d502011-02-11 20:53:29 +00006195 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6196 DAG.getIntPtrConstant(4));
6197 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00006198 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00006199 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6200 DAG.getIntPtrConstant(0));
6201 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
6202 DAG.getIntPtrConstant(0));
Owen Anderson77aa2662011-04-05 21:48:57 +00006203
Nate Begemanfa62d502011-02-11 20:53:29 +00006204 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6205 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006206
Nate Begemanfa62d502011-02-11 20:53:29 +00006207 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6208 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006209
6210 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begemanfa62d502011-02-11 20:53:29 +00006211 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
6212 N0);
6213 return N0;
6214 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006215
Nate Begemanfa62d502011-02-11 20:53:29 +00006216 // v4i16 sdiv ... Convert to float.
6217 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6218 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6219 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6220 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6221 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006222 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006223
6224 // Use reciprocal estimate and two refinement steps.
6225 // float4 recip = vrecpeq_f32(yf);
6226 // recip *= vrecpsq_f32(yf, recip);
6227 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006228 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006229 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006230 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006231 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006232 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006233 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006234 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006235 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006236 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006237 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6238 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6239 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6240 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006241 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006242 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6243 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
6244 N1 = DAG.getConstant(2, MVT::i32);
6245 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6246 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6247 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6248 // Convert back to integer and return.
6249 // return vmovn_u32(vcvt_s32_f32(result));
6250 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6251 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6252 return N0;
6253}
6254
Evan Chenge8916542011-08-30 01:34:54 +00006255static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6256 EVT VT = Op.getNode()->getValueType(0);
6257 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6258
6259 unsigned Opc;
6260 bool ExtraOp = false;
6261 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006262 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006263 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6264 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6265 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6266 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6267 }
6268
6269 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006270 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006271 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006272 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006273 Op.getOperand(1), Op.getOperand(2));
6274}
6275
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006276SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6277 assert(Subtarget->isTargetDarwin());
6278
6279 // For iOS, we want to call an alternative entry point: __sincos_stret,
6280 // return values are passed via sret.
6281 SDLoc dl(Op);
6282 SDValue Arg = Op.getOperand(0);
6283 EVT ArgVT = Arg.getValueType();
6284 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
6285
6286 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
6287 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6288
6289 // Pair of floats / doubles used to pass the result.
Reid Kleckner343c3952014-11-20 23:51:47 +00006290 StructType *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006291
6292 // Create stack object for sret.
6293 const uint64_t ByteSize = TLI.getDataLayout()->getTypeAllocSize(RetTy);
6294 const unsigned StackAlign = TLI.getDataLayout()->getPrefTypeAlignment(RetTy);
6295 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6296 SDValue SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
6297
6298 ArgListTy Args;
6299 ArgListEntry Entry;
6300
6301 Entry.Node = SRet;
6302 Entry.Ty = RetTy->getPointerTo();
6303 Entry.isSExt = false;
6304 Entry.isZExt = false;
6305 Entry.isSRet = true;
6306 Args.push_back(Entry);
6307
6308 Entry.Node = Arg;
6309 Entry.Ty = ArgTy;
6310 Entry.isSExt = false;
6311 Entry.isZExt = false;
6312 Args.push_back(Entry);
6313
6314 const char *LibcallName = (ArgVT == MVT::f64)
6315 ? "__sincos_stret" : "__sincosf_stret";
6316 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
6317
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006318 TargetLowering::CallLoweringInfo CLI(DAG);
6319 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
6320 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), Callee,
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00006321 std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006322 .setDiscardResult();
6323
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006324 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6325
6326 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6327 MachinePointerInfo(), false, false, false, 0);
6328
6329 // Address of cos field.
6330 SDValue Add = DAG.getNode(ISD::ADD, dl, getPointerTy(), SRet,
6331 DAG.getIntPtrConstant(ArgVT.getStoreSize()));
6332 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6333 MachinePointerInfo(), false, false, false, 0);
6334
6335 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6336 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6337 LoadSin.getValue(0), LoadCos.getValue(0));
6338}
6339
Eli Friedman10f9ce22011-09-15 22:26:18 +00006340static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006341 // Monotonic load/store is legal for all targets
6342 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6343 return Op;
6344
Alp Tokercb402912014-01-24 17:20:08 +00006345 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006346 // dmb or equivalent available.
6347 return SDValue();
6348}
6349
Tim Northoverbc933082013-05-23 19:11:20 +00006350static void ReplaceREADCYCLECOUNTER(SDNode *N,
6351 SmallVectorImpl<SDValue> &Results,
6352 SelectionDAG &DAG,
6353 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006354 SDLoc DL(N);
Tim Northoverbc933082013-05-23 19:11:20 +00006355 SDValue Cycles32, OutChain;
6356
6357 if (Subtarget->hasPerfMon()) {
6358 // Under Power Management extensions, the cycle-count is:
6359 // mrc p15, #0, <Rt>, c9, c13, #0
6360 SDValue Ops[] = { N->getOperand(0), // Chain
6361 DAG.getConstant(Intrinsic::arm_mrc, MVT::i32),
6362 DAG.getConstant(15, MVT::i32),
6363 DAG.getConstant(0, MVT::i32),
6364 DAG.getConstant(9, MVT::i32),
6365 DAG.getConstant(13, MVT::i32),
6366 DAG.getConstant(0, MVT::i32)
6367 };
6368
6369 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00006370 DAG.getVTList(MVT::i32, MVT::Other), Ops);
Tim Northoverbc933082013-05-23 19:11:20 +00006371 OutChain = Cycles32.getValue(1);
6372 } else {
6373 // Intrinsic is defined to return 0 on unsupported platforms. Technically
6374 // there are older ARM CPUs that have implementation-specific ways of
6375 // obtaining this information (FIXME!).
6376 Cycles32 = DAG.getConstant(0, MVT::i32);
6377 OutChain = DAG.getEntryNode();
6378 }
6379
6380
6381 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64,
6382 Cycles32, DAG.getConstant(0, MVT::i32));
6383 Results.push_back(Cycles64);
6384 Results.push_back(OutChain);
6385}
6386
Dan Gohman21cea8a2010-04-17 15:26:15 +00006387SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006388 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006389 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Cheng10043e22007-01-19 07:51:42 +00006390 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006391 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006392 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006393 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6394 default: llvm_unreachable("unknown object format");
6395 case Triple::COFF:
6396 return LowerGlobalAddressWindows(Op, DAG);
6397 case Triple::ELF:
6398 return LowerGlobalAddressELF(Op, DAG);
6399 case Triple::MachO:
6400 return LowerGlobalAddressDarwin(Op, DAG);
6401 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006402 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006403 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006404 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6405 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006406 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006407 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006408 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006409 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006410 case ISD::SINT_TO_FP:
6411 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6412 case ISD::FP_TO_SINT:
6413 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006414 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006415 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006416 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006417 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006418 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006419 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006420 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6421 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006422 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006423 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006424 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006425 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng15b80e42009-11-12 07:13:11 +00006426 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006427 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006428 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach8546ec92010-01-18 19:58:49 +00006429 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006430 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006431 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006432 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006433 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006434 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006435 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006436 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006437 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006438 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006439 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanfa62d502011-02-11 20:53:29 +00006440 case ISD::SDIV: return LowerSDIV(Op, DAG);
6441 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006442 case ISD::ADDC:
6443 case ISD::ADDE:
6444 case ISD::SUBC:
6445 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006446 case ISD::SADDO:
6447 case ISD::UADDO:
6448 case ISD::SSUBO:
6449 case ISD::USUBO:
6450 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006451 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006452 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006453 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006454 case ISD::SDIVREM:
6455 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00006456 case ISD::DYNAMIC_STACKALLOC:
6457 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
6458 return LowerDYNAMIC_STACKALLOC(Op, DAG);
6459 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00006460 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
6461 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006462 }
Evan Cheng10043e22007-01-19 07:51:42 +00006463}
6464
Duncan Sands6ed40142008-12-01 11:39:25 +00006465/// ReplaceNodeResults - Replace the results of node with an illegal result
6466/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006467void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
6468 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006469 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006470 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006471 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006472 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006473 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peck527da1b2010-11-23 03:31:01 +00006474 case ISD::BITCAST:
6475 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006476 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006477 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006478 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006479 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006480 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006481 case ISD::READCYCLECOUNTER:
6482 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6483 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00006484 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006485 if (Res.getNode())
6486 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006487}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006488
Evan Cheng10043e22007-01-19 07:51:42 +00006489//===----------------------------------------------------------------------===//
6490// ARM Scheduler Hooks
6491//===----------------------------------------------------------------------===//
6492
Bill Wendling030b58e2011-10-06 22:18:16 +00006493/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6494/// registers the function context.
6495void ARMTargetLowering::
6496SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6497 MachineBasicBlock *DispatchBB, int FI) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006498 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling374ee192011-10-03 21:25:38 +00006499 DebugLoc dl = MI->getDebugLoc();
6500 MachineFunction *MF = MBB->getParent();
6501 MachineRegisterInfo *MRI = &MF->getRegInfo();
6502 MachineConstantPool *MCP = MF->getConstantPool();
6503 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6504 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006505
Bill Wendling374ee192011-10-03 21:25:38 +00006506 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006507 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006508
Bill Wendling374ee192011-10-03 21:25:38 +00006509 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006510 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006511 ARMConstantPoolValue *CPV =
6512 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6513 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6514
Craig Topper61e88f42014-11-21 05:58:21 +00006515 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
6516 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006517
Bill Wendling030b58e2011-10-06 22:18:16 +00006518 // Grab constant pool and fixed stack memory operands.
6519 MachineMemOperand *CPMMO =
6520 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6521 MachineMemOperand::MOLoad, 4, 4);
6522
6523 MachineMemOperand *FIMMOSt =
6524 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6525 MachineMemOperand::MOStore, 4, 4);
6526
6527 // Load the address of the dispatch MBB into the jump buffer.
6528 if (isThumb2) {
6529 // Incoming value: jbuf
6530 // ldr.n r5, LCPI1_1
6531 // orr r5, r5, #1
6532 // add r5, pc
6533 // str r5, [$jbuf, #+4] ; &jbuf[1]
6534 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6535 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6536 .addConstantPoolIndex(CPI)
6537 .addMemOperand(CPMMO));
6538 // Set the low bit because of thumb mode.
6539 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6540 AddDefaultCC(
6541 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6542 .addReg(NewVReg1, RegState::Kill)
6543 .addImm(0x01)));
6544 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6545 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6546 .addReg(NewVReg2, RegState::Kill)
6547 .addImm(PCLabelId);
6548 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6549 .addReg(NewVReg3, RegState::Kill)
6550 .addFrameIndex(FI)
6551 .addImm(36) // &jbuf[1] :: pc
6552 .addMemOperand(FIMMOSt));
6553 } else if (isThumb) {
6554 // Incoming value: jbuf
6555 // ldr.n r1, LCPI1_4
6556 // add r1, pc
6557 // mov r2, #1
6558 // orrs r1, r2
6559 // add r2, $jbuf, #+4 ; &jbuf[1]
6560 // str r1, [r2]
6561 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6562 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6563 .addConstantPoolIndex(CPI)
6564 .addMemOperand(CPMMO));
6565 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6566 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6567 .addReg(NewVReg1, RegState::Kill)
6568 .addImm(PCLabelId);
6569 // Set the low bit because of thumb mode.
6570 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6571 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6572 .addReg(ARM::CPSR, RegState::Define)
6573 .addImm(1));
6574 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6575 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6576 .addReg(ARM::CPSR, RegState::Define)
6577 .addReg(NewVReg2, RegState::Kill)
6578 .addReg(NewVReg3, RegState::Kill));
6579 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00006580 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
6581 .addFrameIndex(FI)
6582 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00006583 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6584 .addReg(NewVReg4, RegState::Kill)
6585 .addReg(NewVReg5, RegState::Kill)
6586 .addImm(0)
6587 .addMemOperand(FIMMOSt));
6588 } else {
6589 // Incoming value: jbuf
6590 // ldr r1, LCPI1_1
6591 // add r1, pc, r1
6592 // str r1, [$jbuf, #+4] ; &jbuf[1]
6593 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6594 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6595 .addConstantPoolIndex(CPI)
6596 .addImm(0)
6597 .addMemOperand(CPMMO));
6598 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6599 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6600 .addReg(NewVReg1, RegState::Kill)
6601 .addImm(PCLabelId));
6602 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6603 .addReg(NewVReg2, RegState::Kill)
6604 .addFrameIndex(FI)
6605 .addImm(36) // &jbuf[1] :: pc
6606 .addMemOperand(FIMMOSt));
6607 }
6608}
6609
6610MachineBasicBlock *ARMTargetLowering::
6611EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006612 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00006613 DebugLoc dl = MI->getDebugLoc();
6614 MachineFunction *MF = MBB->getParent();
6615 MachineRegisterInfo *MRI = &MF->getRegInfo();
6616 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6617 MachineFrameInfo *MFI = MF->getFrameInfo();
6618 int FI = MFI->getFunctionContextIndex();
6619
Craig Topper61e88f42014-11-21 05:58:21 +00006620 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
6621 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00006622
Bill Wendling362c1b02011-10-06 21:29:56 +00006623 // Get a mapping of the call site numbers to all of the landing pads they're
6624 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00006625 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6626 unsigned MaxCSNum = 0;
6627 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00006628 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6629 ++BB) {
Bill Wendling202803e2011-10-05 00:02:33 +00006630 if (!BB->isLandingPad()) continue;
6631
6632 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6633 // pad.
6634 for (MachineBasicBlock::iterator
6635 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6636 if (!II->isEHLabel()) continue;
6637
6638 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006639 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006640
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006641 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6642 for (SmallVectorImpl<unsigned>::iterator
6643 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6644 CSI != CSE; ++CSI) {
6645 CallSiteNumToLPad[*CSI].push_back(BB);
6646 MaxCSNum = std::max(MaxCSNum, *CSI);
6647 }
Bill Wendling202803e2011-10-05 00:02:33 +00006648 break;
6649 }
6650 }
6651
6652 // Get an ordered list of the machine basic blocks for the jump table.
6653 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00006654 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00006655 LPadList.reserve(CallSiteNumToLPad.size());
6656 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6657 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6658 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006659 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00006660 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00006661 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6662 }
Bill Wendling202803e2011-10-05 00:02:33 +00006663 }
6664
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006665 assert(!LPadList.empty() &&
6666 "No landing pad destinations for the dispatch jump table!");
6667
Bill Wendling362c1b02011-10-06 21:29:56 +00006668 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00006669 MachineJumpTableInfo *JTI =
6670 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6671 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6672 unsigned UId = AFI->createJumpTableUId();
Chad Rosier96603432013-03-01 18:30:38 +00006673 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00006674
Bill Wendling362c1b02011-10-06 21:29:56 +00006675 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00006676
6677 // Shove the dispatch's address into the return slot in the function context.
6678 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6679 DispatchBB->setIsLandingPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00006680
Bill Wendling324be982011-10-05 00:39:32 +00006681 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006682 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00006683 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006684 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00006685 else
6686 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6687
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006688 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00006689 DispatchBB->addSuccessor(TrapBB);
6690
6691 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6692 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00006693
Bill Wendling510fbcd2011-10-17 21:32:56 +00006694 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00006695 MF->insert(MF->end(), DispatchBB);
6696 MF->insert(MF->end(), DispContBB);
6697 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00006698
Bill Wendling030b58e2011-10-06 22:18:16 +00006699 // Insert code into the entry block that creates and registers the function
6700 // context.
6701 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6702
Bill Wendling030b58e2011-10-06 22:18:16 +00006703 MachineMemOperand *FIMMOLd =
Bill Wendling362c1b02011-10-06 21:29:56 +00006704 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendlingb3d46782011-10-06 23:37:36 +00006705 MachineMemOperand::MOLoad |
6706 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00006707
Chad Rosier1ec8e402012-11-06 23:05:24 +00006708 MachineInstrBuilder MIB;
6709 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6710
6711 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6712 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6713
6714 // Add a register mask with no preserved registers. This results in all
6715 // registers being marked as clobbered.
6716 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00006717
Bill Wendling85833f72011-10-18 22:49:07 +00006718 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00006719 if (Subtarget->isThumb2()) {
6720 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6721 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6722 .addFrameIndex(FI)
6723 .addImm(4)
6724 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006725
Bill Wendling85833f72011-10-18 22:49:07 +00006726 if (NumLPads < 256) {
6727 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6728 .addReg(NewVReg1)
6729 .addImm(LPadList.size()));
6730 } else {
6731 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6732 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006733 .addImm(NumLPads & 0xFFFF));
6734
6735 unsigned VReg2 = VReg1;
6736 if ((NumLPads & 0xFFFF0000) != 0) {
6737 VReg2 = MRI->createVirtualRegister(TRC);
6738 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6739 .addReg(VReg1)
6740 .addImm(NumLPads >> 16));
6741 }
6742
Bill Wendling85833f72011-10-18 22:49:07 +00006743 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6744 .addReg(NewVReg1)
6745 .addReg(VReg2));
6746 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006747
Bill Wendling5626c662011-10-06 22:53:00 +00006748 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6749 .addMBB(TrapBB)
6750 .addImm(ARMCC::HI)
6751 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00006752
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006753 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6754 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006755 .addJumpTableIndex(MJTI)
6756 .addImm(UId));
Bill Wendling202803e2011-10-05 00:02:33 +00006757
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006758 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006759 AddDefaultCC(
6760 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006761 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6762 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00006763 .addReg(NewVReg1)
6764 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6765
6766 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006767 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00006768 .addReg(NewVReg1)
Bill Wendling5626c662011-10-06 22:53:00 +00006769 .addJumpTableIndex(MJTI)
6770 .addImm(UId);
6771 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00006772 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6773 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6774 .addFrameIndex(FI)
6775 .addImm(1)
6776 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00006777
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006778 if (NumLPads < 256) {
6779 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6780 .addReg(NewVReg1)
6781 .addImm(NumLPads));
6782 } else {
6783 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00006784 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6785 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6786
6787 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006788 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006789 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006790 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006791 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006792
6793 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6794 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6795 .addReg(VReg1, RegState::Define)
6796 .addConstantPoolIndex(Idx));
6797 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6798 .addReg(NewVReg1)
6799 .addReg(VReg1));
6800 }
6801
Bill Wendlingb3d46782011-10-06 23:37:36 +00006802 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6803 .addMBB(TrapBB)
6804 .addImm(ARMCC::HI)
6805 .addReg(ARM::CPSR);
6806
6807 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6808 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6809 .addReg(ARM::CPSR, RegState::Define)
6810 .addReg(NewVReg1)
6811 .addImm(2));
6812
6813 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00006814 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendlingb3d46782011-10-06 23:37:36 +00006815 .addJumpTableIndex(MJTI)
6816 .addImm(UId));
6817
6818 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6819 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6820 .addReg(ARM::CPSR, RegState::Define)
6821 .addReg(NewVReg2, RegState::Kill)
6822 .addReg(NewVReg3));
6823
6824 MachineMemOperand *JTMMOLd =
6825 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6826 MachineMemOperand::MOLoad, 4, 4);
6827
6828 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6829 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6830 .addReg(NewVReg4, RegState::Kill)
6831 .addImm(0)
6832 .addMemOperand(JTMMOLd));
6833
Chad Rosier96603432013-03-01 18:30:38 +00006834 unsigned NewVReg6 = NewVReg5;
6835 if (RelocM == Reloc::PIC_) {
6836 NewVReg6 = MRI->createVirtualRegister(TRC);
6837 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6838 .addReg(ARM::CPSR, RegState::Define)
6839 .addReg(NewVReg5, RegState::Kill)
6840 .addReg(NewVReg3));
6841 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00006842
6843 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6844 .addReg(NewVReg6, RegState::Kill)
6845 .addJumpTableIndex(MJTI)
6846 .addImm(UId);
Bill Wendling5626c662011-10-06 22:53:00 +00006847 } else {
6848 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6849 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6850 .addFrameIndex(FI)
6851 .addImm(4)
6852 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00006853
Bill Wendling4969dcd2011-10-18 22:52:20 +00006854 if (NumLPads < 256) {
6855 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6856 .addReg(NewVReg1)
6857 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00006858 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00006859 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6860 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006861 .addImm(NumLPads & 0xFFFF));
6862
6863 unsigned VReg2 = VReg1;
6864 if ((NumLPads & 0xFFFF0000) != 0) {
6865 VReg2 = MRI->createVirtualRegister(TRC);
6866 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6867 .addReg(VReg1)
6868 .addImm(NumLPads >> 16));
6869 }
6870
Bill Wendling4969dcd2011-10-18 22:52:20 +00006871 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6872 .addReg(NewVReg1)
6873 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00006874 } else {
6875 MachineConstantPool *ConstantPool = MF->getConstantPool();
6876 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6877 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6878
6879 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006880 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006881 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006882 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006883 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6884
6885 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6886 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6887 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00006888 .addConstantPoolIndex(Idx)
6889 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00006890 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6891 .addReg(NewVReg1)
6892 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00006893 }
6894
Bill Wendling5626c662011-10-06 22:53:00 +00006895 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6896 .addMBB(TrapBB)
6897 .addImm(ARMCC::HI)
6898 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00006899
Bill Wendling973c8172011-10-18 22:11:18 +00006900 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006901 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00006902 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006903 .addReg(NewVReg1)
6904 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00006905 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6906 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006907 .addJumpTableIndex(MJTI)
6908 .addImm(UId));
6909
6910 MachineMemOperand *JTMMOLd =
6911 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6912 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00006913 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006914 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00006915 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6916 .addReg(NewVReg3, RegState::Kill)
6917 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006918 .addImm(0)
6919 .addMemOperand(JTMMOLd));
6920
Chad Rosier96603432013-03-01 18:30:38 +00006921 if (RelocM == Reloc::PIC_) {
6922 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
6923 .addReg(NewVReg5, RegState::Kill)
6924 .addReg(NewVReg4)
6925 .addJumpTableIndex(MJTI)
6926 .addImm(UId);
6927 } else {
6928 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
6929 .addReg(NewVReg5, RegState::Kill)
6930 .addJumpTableIndex(MJTI)
6931 .addImm(UId);
6932 }
Bill Wendling5626c662011-10-06 22:53:00 +00006933 }
Bill Wendling202803e2011-10-05 00:02:33 +00006934
Bill Wendling324be982011-10-05 00:39:32 +00006935 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006936 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00006937 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006938 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6939 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00006940 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00006941 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006942 }
6943
Bill Wendling26d27802011-10-17 05:25:09 +00006944 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00006945 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00006946 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00006947 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006948
6949 // Remove the landing pad successor from the invoke block and replace it
6950 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00006951 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6952 BB->succ_end());
6953 while (!Successors.empty()) {
6954 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling883ec972011-10-07 23:18:02 +00006955 if (SMBB->isLandingPad()) {
6956 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00006957 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006958 }
6959 }
6960
6961 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006962
6963 // Find the invoke call and mark all of the callee-saved registers as
6964 // 'implicit defined' so that they're spilled. This prevents code from
6965 // moving instructions to before the EH block, where they will never be
6966 // executed.
6967 for (MachineBasicBlock::reverse_iterator
6968 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00006969 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006970
6971 DenseMap<unsigned, bool> DefRegs;
6972 for (MachineInstr::mop_iterator
6973 OI = II->operands_begin(), OE = II->operands_end();
6974 OI != OE; ++OI) {
6975 if (!OI->isReg()) continue;
6976 DefRegs[OI->getReg()] = true;
6977 }
6978
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00006979 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006980
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006981 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00006982 unsigned Reg = SavedRegs[i];
6983 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00006984 !ARM::tGPRRegClass.contains(Reg) &&
6985 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006986 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006987 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006988 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006989 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006990 continue;
6991 if (!DefRegs[Reg])
6992 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006993 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006994
6995 break;
6996 }
Bill Wendling883ec972011-10-07 23:18:02 +00006997 }
Bill Wendling324be982011-10-05 00:39:32 +00006998
Bill Wendling617075f2011-10-18 18:30:49 +00006999 // Mark all former landing pads as non-landing pads. The dispatch is the only
7000 // landing pad now.
7001 for (SmallVectorImpl<MachineBasicBlock*>::iterator
7002 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
7003 (*I)->setIsLandingPad(false);
7004
Bill Wendling324be982011-10-05 00:39:32 +00007005 // The instruction is gone now.
7006 MI->eraseFromParent();
7007
Bill Wendling374ee192011-10-03 21:25:38 +00007008 return MBB;
7009}
7010
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007011static
7012MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
7013 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
7014 E = MBB->succ_end(); I != E; ++I)
7015 if (*I != Succ)
7016 return *I;
7017 llvm_unreachable("Expecting a BB with two successors!");
7018}
7019
Manman Renb504f492013-10-29 22:27:32 +00007020/// Return the load opcode for a given load size. If load size >= 8,
7021/// neon opcode will be returned.
7022static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
7023 if (LdSize >= 8)
7024 return LdSize == 16 ? ARM::VLD1q32wb_fixed
7025 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
7026 if (IsThumb1)
7027 return LdSize == 4 ? ARM::tLDRi
7028 : LdSize == 2 ? ARM::tLDRHi
7029 : LdSize == 1 ? ARM::tLDRBi : 0;
7030 if (IsThumb2)
7031 return LdSize == 4 ? ARM::t2LDR_POST
7032 : LdSize == 2 ? ARM::t2LDRH_POST
7033 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
7034 return LdSize == 4 ? ARM::LDR_POST_IMM
7035 : LdSize == 2 ? ARM::LDRH_POST
7036 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
7037}
7038
7039/// Return the store opcode for a given store size. If store size >= 8,
7040/// neon opcode will be returned.
7041static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
7042 if (StSize >= 8)
7043 return StSize == 16 ? ARM::VST1q32wb_fixed
7044 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
7045 if (IsThumb1)
7046 return StSize == 4 ? ARM::tSTRi
7047 : StSize == 2 ? ARM::tSTRHi
7048 : StSize == 1 ? ARM::tSTRBi : 0;
7049 if (IsThumb2)
7050 return StSize == 4 ? ARM::t2STR_POST
7051 : StSize == 2 ? ARM::t2STRH_POST
7052 : StSize == 1 ? ARM::t2STRB_POST : 0;
7053 return StSize == 4 ? ARM::STR_POST_IMM
7054 : StSize == 2 ? ARM::STRH_POST
7055 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
7056}
7057
7058/// Emit a post-increment load operation with given size. The instructions
7059/// will be added to BB at Pos.
7060static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
7061 const TargetInstrInfo *TII, DebugLoc dl,
7062 unsigned LdSize, unsigned Data, unsigned AddrIn,
7063 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7064 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
7065 assert(LdOpc != 0 && "Should have a load opcode");
7066 if (LdSize >= 8) {
7067 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7068 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7069 .addImm(0));
7070 } else if (IsThumb1) {
7071 // load + update AddrIn
7072 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7073 .addReg(AddrIn).addImm(0));
7074 MachineInstrBuilder MIB =
7075 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7076 MIB = AddDefaultT1CC(MIB);
7077 MIB.addReg(AddrIn).addImm(LdSize);
7078 AddDefaultPred(MIB);
7079 } else if (IsThumb2) {
7080 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7081 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7082 .addImm(LdSize));
7083 } else { // arm
7084 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7085 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7086 .addReg(0).addImm(LdSize));
7087 }
7088}
7089
7090/// Emit a post-increment store operation with given size. The instructions
7091/// will be added to BB at Pos.
7092static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
7093 const TargetInstrInfo *TII, DebugLoc dl,
7094 unsigned StSize, unsigned Data, unsigned AddrIn,
7095 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7096 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
7097 assert(StOpc != 0 && "Should have a store opcode");
7098 if (StSize >= 8) {
7099 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7100 .addReg(AddrIn).addImm(0).addReg(Data));
7101 } else if (IsThumb1) {
7102 // store + update AddrIn
7103 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
7104 .addReg(AddrIn).addImm(0));
7105 MachineInstrBuilder MIB =
7106 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7107 MIB = AddDefaultT1CC(MIB);
7108 MIB.addReg(AddrIn).addImm(StSize);
7109 AddDefaultPred(MIB);
7110 } else if (IsThumb2) {
7111 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7112 .addReg(Data).addReg(AddrIn).addImm(StSize));
7113 } else { // arm
7114 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7115 .addReg(Data).addReg(AddrIn).addReg(0)
7116 .addImm(StSize));
7117 }
7118}
7119
David Peixottoc32e24a2013-10-17 19:49:22 +00007120MachineBasicBlock *
7121ARMTargetLowering::EmitStructByval(MachineInstr *MI,
7122 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00007123 // This pseudo instruction has 3 operands: dst, src, size
7124 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7125 // Otherwise, we will generate unrolled scalar copies.
Eric Christopher1889fdc2015-01-29 00:19:39 +00007126 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00007127 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7128 MachineFunction::iterator It = BB;
7129 ++It;
7130
7131 unsigned dest = MI->getOperand(0).getReg();
7132 unsigned src = MI->getOperand(1).getReg();
7133 unsigned SizeVal = MI->getOperand(2).getImm();
7134 unsigned Align = MI->getOperand(3).getImm();
7135 DebugLoc dl = MI->getDebugLoc();
7136
Manman Rene8735522012-06-01 19:33:18 +00007137 MachineFunction *MF = BB->getParent();
7138 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00007139 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00007140 const TargetRegisterClass *TRC = nullptr;
7141 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007142
7143 bool IsThumb1 = Subtarget->isThumb1Only();
7144 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00007145
7146 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00007147 UnitSize = 1;
7148 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00007149 UnitSize = 2;
7150 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00007151 // Check whether we can use NEON instructions.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00007152 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007153 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007154 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007155 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007156 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007157 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007158 }
7159 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007160 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007161 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007162 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007163
David Peixottob0653e532013-10-24 16:39:36 +00007164 // Select the correct opcode and register class for unit size load/store
7165 bool IsNeon = UnitSize >= 8;
Craig Topper61e88f42014-11-21 05:58:21 +00007166 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007167 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00007168 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
7169 : UnitSize == 8 ? &ARM::DPRRegClass
7170 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007171
Manman Rene8735522012-06-01 19:33:18 +00007172 unsigned BytesLeft = SizeVal % UnitSize;
7173 unsigned LoopSize = SizeVal - BytesLeft;
7174
7175 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7176 // Use LDR and STR to copy.
7177 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7178 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7179 unsigned srcIn = src;
7180 unsigned destIn = dest;
7181 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007182 unsigned srcOut = MRI.createVirtualRegister(TRC);
7183 unsigned destOut = MRI.createVirtualRegister(TRC);
7184 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007185 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7186 IsThumb1, IsThumb2);
7187 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7188 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007189 srcIn = srcOut;
7190 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007191 }
7192
7193 // Handle the leftover bytes with LDRB and STRB.
7194 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7195 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007196 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007197 unsigned srcOut = MRI.createVirtualRegister(TRC);
7198 unsigned destOut = MRI.createVirtualRegister(TRC);
7199 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007200 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7201 IsThumb1, IsThumb2);
7202 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7203 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007204 srcIn = srcOut;
7205 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007206 }
7207 MI->eraseFromParent(); // The instruction is gone now.
7208 return BB;
7209 }
7210
7211 // Expand the pseudo op to a loop.
7212 // thisMBB:
7213 // ...
7214 // movw varEnd, # --> with thumb2
7215 // movt varEnd, #
7216 // ldrcp varEnd, idx --> without thumb2
7217 // fallthrough --> loopMBB
7218 // loopMBB:
7219 // PHI varPhi, varEnd, varLoop
7220 // PHI srcPhi, src, srcLoop
7221 // PHI destPhi, dst, destLoop
7222 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7223 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7224 // subs varLoop, varPhi, #UnitSize
7225 // bne loopMBB
7226 // fallthrough --> exitMBB
7227 // exitMBB:
7228 // epilogue to handle left-over bytes
7229 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7230 // [destOut] = STRB_POST(scratch, destLoop, 1)
7231 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7232 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7233 MF->insert(It, loopMBB);
7234 MF->insert(It, exitMBB);
7235
7236 // Transfer the remainder of BB and its successor edges to exitMBB.
7237 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007238 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007239 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7240
7241 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007242 unsigned varEnd = MRI.createVirtualRegister(TRC);
7243 if (IsThumb2) {
7244 unsigned Vtmp = varEnd;
7245 if ((LoopSize & 0xFFFF0000) != 0)
7246 Vtmp = MRI.createVirtualRegister(TRC);
7247 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), Vtmp)
7248 .addImm(LoopSize & 0xFFFF));
7249
7250 if ((LoopSize & 0xFFFF0000) != 0)
7251 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
7252 .addReg(Vtmp).addImm(LoopSize >> 16));
7253 } else {
7254 MachineConstantPool *ConstantPool = MF->getConstantPool();
7255 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7256 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7257
7258 // MachineConstantPool wants an explicit alignment.
7259 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
7260 if (Align == 0)
7261 Align = getDataLayout()->getTypeAllocSize(C->getType());
7262 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7263
7264 if (IsThumb1)
7265 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7266 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7267 else
7268 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7269 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7270 }
Manman Rene8735522012-06-01 19:33:18 +00007271 BB->addSuccessor(loopMBB);
7272
7273 // Generate the loop body:
7274 // varPhi = PHI(varLoop, varEnd)
7275 // srcPhi = PHI(srcLoop, src)
7276 // destPhi = PHI(destLoop, dst)
7277 MachineBasicBlock *entryBB = BB;
7278 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007279 unsigned varLoop = MRI.createVirtualRegister(TRC);
7280 unsigned varPhi = MRI.createVirtualRegister(TRC);
7281 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7282 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7283 unsigned destLoop = MRI.createVirtualRegister(TRC);
7284 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007285
7286 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7287 .addReg(varLoop).addMBB(loopMBB)
7288 .addReg(varEnd).addMBB(entryBB);
7289 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7290 .addReg(srcLoop).addMBB(loopMBB)
7291 .addReg(src).addMBB(entryBB);
7292 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7293 .addReg(destLoop).addMBB(loopMBB)
7294 .addReg(dest).addMBB(entryBB);
7295
7296 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7297 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007298 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007299 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7300 IsThumb1, IsThumb2);
7301 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7302 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007303
7304 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007305 if (IsThumb1) {
7306 MachineInstrBuilder MIB =
7307 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7308 MIB = AddDefaultT1CC(MIB);
7309 MIB.addReg(varPhi).addImm(UnitSize);
7310 AddDefaultPred(MIB);
7311 } else {
7312 MachineInstrBuilder MIB =
7313 BuildMI(*BB, BB->end(), dl,
7314 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7315 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7316 MIB->getOperand(5).setReg(ARM::CPSR);
7317 MIB->getOperand(5).setIsDef(true);
7318 }
7319 BuildMI(*BB, BB->end(), dl,
7320 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7321 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007322
7323 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7324 BB->addSuccessor(loopMBB);
7325 BB->addSuccessor(exitMBB);
7326
7327 // Add epilogue to handle BytesLeft.
7328 BB = exitMBB;
7329 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007330
7331 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7332 // [destOut] = STRB_POST(scratch, destLoop, 1)
7333 unsigned srcIn = srcLoop;
7334 unsigned destIn = destLoop;
7335 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007336 unsigned srcOut = MRI.createVirtualRegister(TRC);
7337 unsigned destOut = MRI.createVirtualRegister(TRC);
7338 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007339 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7340 IsThumb1, IsThumb2);
7341 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7342 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007343 srcIn = srcOut;
7344 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007345 }
7346
7347 MI->eraseFromParent(); // The instruction is gone now.
7348 return BB;
7349}
7350
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007351MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007352ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
7353 MachineBasicBlock *MBB) const {
7354 const TargetMachine &TM = getTargetMachine();
Eric Christopher1889fdc2015-01-29 00:19:39 +00007355 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007356 DebugLoc DL = MI->getDebugLoc();
7357
7358 assert(Subtarget->isTargetWindows() &&
7359 "__chkstk is only supported on Windows");
7360 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
7361
7362 // __chkstk takes the number of words to allocate on the stack in R4, and
7363 // returns the stack adjustment in number of bytes in R4. This will not
7364 // clober any other registers (other than the obvious lr).
7365 //
7366 // Although, technically, IP should be considered a register which may be
7367 // clobbered, the call itself will not touch it. Windows on ARM is a pure
7368 // thumb-2 environment, so there is no interworking required. As a result, we
7369 // do not expect a veneer to be emitted by the linker, clobbering IP.
7370 //
Alp Toker1d099d92014-06-19 19:41:26 +00007371 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007372 // required, again, ensuring that IP is not clobbered.
7373 //
7374 // Finally, although some linkers may theoretically provide a trampoline for
7375 // out of range calls (which is quite common due to a 32M range limitation of
7376 // branches for Thumb), we can generate the long-call version via
7377 // -mcmodel=large, alleviating the need for the trampoline which may clobber
7378 // IP.
7379
7380 switch (TM.getCodeModel()) {
7381 case CodeModel::Small:
7382 case CodeModel::Medium:
7383 case CodeModel::Default:
7384 case CodeModel::Kernel:
7385 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
7386 .addImm((unsigned)ARMCC::AL).addReg(0)
7387 .addExternalSymbol("__chkstk")
7388 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7389 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7390 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7391 break;
7392 case CodeModel::Large:
7393 case CodeModel::JITDefault: {
7394 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7395 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
7396
7397 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
7398 .addExternalSymbol("__chkstk");
7399 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
7400 .addImm((unsigned)ARMCC::AL).addReg(0)
7401 .addReg(Reg, RegState::Kill)
7402 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7403 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7404 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7405 break;
7406 }
7407 }
7408
7409 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
7410 ARM::SP)
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +00007411 .addReg(ARM::SP).addReg(ARM::R4)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007412
7413 MI->eraseFromParent();
7414 return MBB;
7415}
7416
7417MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007418ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007419 MachineBasicBlock *BB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007420 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007421 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007422 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007423 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007424 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007425 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007426 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007427 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007428 // The Thumb2 pre-indexed stores have the same MI operands, they just
7429 // define them differently in the .td files from the isel patterns, so
7430 // they need pseudos.
7431 case ARM::t2STR_preidx:
7432 MI->setDesc(TII->get(ARM::t2STR_PRE));
7433 return BB;
7434 case ARM::t2STRB_preidx:
7435 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7436 return BB;
7437 case ARM::t2STRH_preidx:
7438 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7439 return BB;
7440
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007441 case ARM::STRi_preidx:
7442 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007443 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007444 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7445 // Decode the offset.
7446 unsigned Offset = MI->getOperand(4).getImm();
7447 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7448 Offset = ARM_AM::getAM2Offset(Offset);
7449 if (isSub)
7450 Offset = -Offset;
7451
Jim Grosbachf402f692011-08-12 21:02:34 +00007452 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007453 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007454 .addOperand(MI->getOperand(0)) // Rn_wb
7455 .addOperand(MI->getOperand(1)) // Rt
7456 .addOperand(MI->getOperand(2)) // Rn
7457 .addImm(Offset) // offset (skip GPR==zero_reg)
7458 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007459 .addOperand(MI->getOperand(6))
7460 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007461 MI->eraseFromParent();
7462 return BB;
7463 }
7464 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007465 case ARM::STRBr_preidx:
7466 case ARM::STRH_preidx: {
7467 unsigned NewOpc;
7468 switch (MI->getOpcode()) {
7469 default: llvm_unreachable("unexpected opcode!");
7470 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7471 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7472 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7473 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007474 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7475 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7476 MIB.addOperand(MI->getOperand(i));
7477 MI->eraseFromParent();
7478 return BB;
7479 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007480
Evan Chengbb2af352009-08-12 05:17:19 +00007481 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007482 // To "insert" a SELECT_CC instruction, we actually have to insert the
7483 // diamond control-flow pattern. The incoming instruction knows the
7484 // destination vreg to set, the condition code register to branch on, the
7485 // true/false values to select between, and a branch opcode to use.
7486 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007487 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007488 ++It;
7489
7490 // thisMBB:
7491 // ...
7492 // TrueVal = ...
7493 // cmpTY ccX, r1, r2
7494 // bCC copy1MBB
7495 // fallthrough --> copy0MBB
7496 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007497 MachineFunction *F = BB->getParent();
7498 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7499 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007500 F->insert(It, copy0MBB);
7501 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007502
7503 // Transfer the remainder of BB and its successor edges to sinkMBB.
7504 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007505 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007506 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7507
Dan Gohmanf4f04102010-07-06 15:49:48 +00007508 BB->addSuccessor(copy0MBB);
7509 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007510
Dan Gohman34396292010-07-06 20:24:04 +00007511 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7512 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7513
Evan Cheng10043e22007-01-19 07:51:42 +00007514 // copy0MBB:
7515 // %FalseValue = ...
7516 // # fallthrough to sinkMBB
7517 BB = copy0MBB;
7518
7519 // Update machine-CFG edges
7520 BB->addSuccessor(sinkMBB);
7521
7522 // sinkMBB:
7523 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7524 // ...
7525 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007526 BuildMI(*BB, BB->begin(), dl,
7527 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007528 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7529 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7530
Dan Gohman34396292010-07-06 20:24:04 +00007531 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007532 return BB;
7533 }
Evan Chengb972e562009-08-07 00:34:42 +00007534
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007535 case ARM::BCCi64:
7536 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007537 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007538 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007539
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007540 // Compare both parts that make up the double comparison separately for
7541 // equality.
7542 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7543
7544 unsigned LHS1 = MI->getOperand(1).getReg();
7545 unsigned LHS2 = MI->getOperand(2).getReg();
7546 if (RHSisZero) {
7547 AddDefaultPred(BuildMI(BB, dl,
7548 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7549 .addReg(LHS1).addImm(0));
7550 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7551 .addReg(LHS2).addImm(0)
7552 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7553 } else {
7554 unsigned RHS1 = MI->getOperand(3).getReg();
7555 unsigned RHS2 = MI->getOperand(4).getReg();
7556 AddDefaultPred(BuildMI(BB, dl,
7557 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7558 .addReg(LHS1).addReg(RHS1));
7559 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7560 .addReg(LHS2).addReg(RHS2)
7561 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7562 }
7563
7564 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7565 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7566 if (MI->getOperand(0).getImm() == ARMCC::NE)
7567 std::swap(destMBB, exitMBB);
7568
7569 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7570 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007571 if (isThumb2)
7572 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7573 else
7574 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007575
7576 MI->eraseFromParent(); // The pseudo instruction is gone now.
7577 return BB;
7578 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007579
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007580 case ARM::Int_eh_sjlj_setjmp:
7581 case ARM::Int_eh_sjlj_setjmp_nofp:
7582 case ARM::tInt_eh_sjlj_setjmp:
7583 case ARM::t2Int_eh_sjlj_setjmp:
7584 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7585 EmitSjLjDispatchBlock(MI, BB);
7586 return BB;
7587
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007588 case ARM::ABS:
7589 case ARM::t2ABS: {
7590 // To insert an ABS instruction, we have to insert the
7591 // diamond control-flow pattern. The incoming instruction knows the
7592 // source vreg to test against 0, the destination vreg to set,
7593 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007594 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007595 // It transforms
7596 // V1 = ABS V0
7597 // into
7598 // V2 = MOVS V0
7599 // BCC (branch to SinkBB if V0 >= 0)
7600 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007601 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007602 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7603 MachineFunction::iterator BBI = BB;
7604 ++BBI;
7605 MachineFunction *Fn = BB->getParent();
7606 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7607 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7608 Fn->insert(BBI, RSBBB);
7609 Fn->insert(BBI, SinkBB);
7610
7611 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7612 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7613 bool isThumb2 = Subtarget->isThumb2();
7614 MachineRegisterInfo &MRI = Fn->getRegInfo();
7615 // In Thumb mode S must not be specified if source register is the SP or
7616 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00007617 unsigned NewRsbDstReg =
7618 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007619
7620 // Transfer the remainder of BB and its successor edges to sinkMBB.
7621 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007622 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007623 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7624
7625 BB->addSuccessor(RSBBB);
7626 BB->addSuccessor(SinkBB);
7627
7628 // fall through to SinkMBB
7629 RSBBB->addSuccessor(SinkBB);
7630
Manman Rene0763c72012-06-15 21:32:12 +00007631 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00007632 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00007633 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7634 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007635
7636 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00007637 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007638 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7639 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7640
7641 // insert rsbri in RSBBB
7642 // Note: BCC and rsbri will be converted into predicated rsbmi
7643 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00007644 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007645 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Rene0763c72012-06-15 21:32:12 +00007646 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007647 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7648
Andrew Trick3f07c422011-10-18 18:40:53 +00007649 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007650 // reuse ABSDstReg to not change uses of ABS instruction
7651 BuildMI(*SinkBB, SinkBB->begin(), dl,
7652 TII->get(ARM::PHI), ABSDstReg)
7653 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00007654 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007655
7656 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00007657 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007658
7659 // return last added BB
7660 return SinkBB;
7661 }
Manman Rene8735522012-06-01 19:33:18 +00007662 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00007663 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00007664 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007665 case ARM::WIN__CHKSTK:
7666 return EmitLowered__chkstk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00007667 }
7668}
7669
Evan Chenge6fba772011-08-30 19:09:48 +00007670void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7671 SDNode *Node) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007672 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00007673 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7674 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7675 // operand is still set to noreg. If needed, set the optional operand's
7676 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00007677 //
Andrew Trick88b24502011-10-18 19:18:52 +00007678 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00007679
Andrew Trick924123a2011-09-21 02:20:46 +00007680 // Rename pseudo opcodes.
7681 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7682 if (NewOpc) {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007683 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
Andrew Trick88b24502011-10-18 19:18:52 +00007684 MCID = &TII->get(NewOpc);
7685
7686 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7687 "converted opcode should be the same except for cc_out");
7688
7689 MI->setDesc(*MCID);
7690
7691 // Add the optional cc_out operand
7692 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00007693 }
Andrew Trick88b24502011-10-18 19:18:52 +00007694 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00007695
7696 // Any ARM instruction that sets the 's' bit should specify an optional
7697 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00007698 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007699 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007700 return;
7701 }
Andrew Trick924123a2011-09-21 02:20:46 +00007702 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7703 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007704 bool definesCPSR = false;
7705 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00007706 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00007707 i != e; ++i) {
7708 const MachineOperand &MO = MI->getOperand(i);
7709 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7710 definesCPSR = true;
7711 if (MO.isDead())
7712 deadCPSR = true;
7713 MI->RemoveOperand(i);
7714 break;
Evan Chenge6fba772011-08-30 19:09:48 +00007715 }
7716 }
Andrew Trick8586e622011-09-20 03:17:40 +00007717 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00007718 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007719 return;
7720 }
7721 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00007722 if (deadCPSR) {
7723 assert(!MI->getOperand(ccOutIdx).getReg() &&
7724 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00007725 return;
Andrew Trick924123a2011-09-21 02:20:46 +00007726 }
Andrew Trick8586e622011-09-20 03:17:40 +00007727
Andrew Trick924123a2011-09-21 02:20:46 +00007728 // If this instruction was defined with an optional CPSR def and its dag node
7729 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007730 MachineOperand &MO = MI->getOperand(ccOutIdx);
7731 MO.setReg(ARM::CPSR);
7732 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00007733}
7734
Evan Cheng10043e22007-01-19 07:51:42 +00007735//===----------------------------------------------------------------------===//
7736// ARM Optimization Hooks
7737//===----------------------------------------------------------------------===//
7738
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007739// Helper function that checks if N is a null or all ones constant.
7740static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7741 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7742 if (!C)
7743 return false;
7744 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7745}
7746
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007747// Return true if N is conditionally 0 or all ones.
7748// Detects these expressions where cc is an i1 value:
7749//
7750// (select cc 0, y) [AllOnes=0]
7751// (select cc y, 0) [AllOnes=0]
7752// (zext cc) [AllOnes=0]
7753// (sext cc) [AllOnes=0/1]
7754// (select cc -1, y) [AllOnes=1]
7755// (select cc y, -1) [AllOnes=1]
7756//
7757// Invert is set when N is the null/all ones constant when CC is false.
7758// OtherOp is set to the alternative value of N.
7759static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7760 SDValue &CC, bool &Invert,
7761 SDValue &OtherOp,
7762 SelectionDAG &DAG) {
7763 switch (N->getOpcode()) {
7764 default: return false;
7765 case ISD::SELECT: {
7766 CC = N->getOperand(0);
7767 SDValue N1 = N->getOperand(1);
7768 SDValue N2 = N->getOperand(2);
7769 if (isZeroOrAllOnes(N1, AllOnes)) {
7770 Invert = false;
7771 OtherOp = N2;
7772 return true;
7773 }
7774 if (isZeroOrAllOnes(N2, AllOnes)) {
7775 Invert = true;
7776 OtherOp = N1;
7777 return true;
7778 }
7779 return false;
7780 }
7781 case ISD::ZERO_EXTEND:
7782 // (zext cc) can never be the all ones value.
7783 if (AllOnes)
7784 return false;
7785 // Fall through.
7786 case ISD::SIGN_EXTEND: {
7787 EVT VT = N->getValueType(0);
7788 CC = N->getOperand(0);
7789 if (CC.getValueType() != MVT::i1)
7790 return false;
7791 Invert = !AllOnes;
7792 if (AllOnes)
7793 // When looking for an AllOnes constant, N is an sext, and the 'other'
7794 // value is 0.
7795 OtherOp = DAG.getConstant(0, VT);
7796 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7797 // When looking for a 0 constant, N can be zext or sext.
7798 OtherOp = DAG.getConstant(1, VT);
7799 else
7800 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7801 return true;
7802 }
7803 }
7804}
7805
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007806// Combine a constant select operand into its use:
7807//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007808// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7809// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7810// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7811// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7812// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007813//
7814// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007815// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007816//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007817// Also recognize sext/zext from i1:
7818//
7819// (add (zext cc), x) -> (select cc (add x, 1), x)
7820// (add (sext cc), x) -> (select cc (add x, -1), x)
7821//
7822// These transformations eventually create predicated instructions.
7823//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007824// @param N The node to transform.
7825// @param Slct The N operand that is a select.
7826// @param OtherOp The other N operand (x above).
7827// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007828// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007829// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00007830static
7831SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007832 TargetLowering::DAGCombinerInfo &DCI,
7833 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00007834 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007835 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007836 SDValue NonConstantVal;
7837 SDValue CCOp;
7838 bool SwapSelectOps;
7839 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7840 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007841 return SDValue();
7842
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007843 // Slct is now know to be the desired identity constant when CC is true.
7844 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007845 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007846 OtherOp, NonConstantVal);
7847 // Unless SwapSelectOps says CC should be false.
7848 if (SwapSelectOps)
7849 std::swap(TrueVal, FalseVal);
7850
Andrew Trickef9de2a2013-05-25 02:42:55 +00007851 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007852 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00007853}
7854
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007855// Attempt combineSelectAndUse on each operand of a commutative operator N.
7856static
7857SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7858 TargetLowering::DAGCombinerInfo &DCI) {
7859 SDValue N0 = N->getOperand(0);
7860 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007861 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007862 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7863 if (Result.getNode())
7864 return Result;
7865 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007866 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007867 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7868 if (Result.getNode())
7869 return Result;
7870 }
7871 return SDValue();
7872}
7873
Eric Christopher1b8b94192011-06-29 21:10:36 +00007874// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00007875// (only after legalization).
7876static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7877 TargetLowering::DAGCombinerInfo &DCI,
7878 const ARMSubtarget *Subtarget) {
7879
7880 // Only perform optimization if after legalize, and if NEON is available. We
7881 // also expected both operands to be BUILD_VECTORs.
7882 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7883 || N0.getOpcode() != ISD::BUILD_VECTOR
7884 || N1.getOpcode() != ISD::BUILD_VECTOR)
7885 return SDValue();
7886
7887 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7888 EVT VT = N->getValueType(0);
7889 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7890 return SDValue();
7891
7892 // Check that the vector operands are of the right form.
7893 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7894 // operands, where N is the size of the formed vector.
7895 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7896 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007897
7898 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00007899 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00007900 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00007901 SDValue Vec = N0->getOperand(0)->getOperand(0);
7902 SDNode *V = Vec.getNode();
7903 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00007904
Eric Christopher1b8b94192011-06-29 21:10:36 +00007905 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007906 // check to see if each of their operands are an EXTRACT_VECTOR with
7907 // the same vector and appropriate index.
7908 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7909 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7910 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00007911
Tanya Lattnere9e67052011-06-14 23:48:48 +00007912 SDValue ExtVec0 = N0->getOperand(i);
7913 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007914
Tanya Lattnere9e67052011-06-14 23:48:48 +00007915 // First operand is the vector, verify its the same.
7916 if (V != ExtVec0->getOperand(0).getNode() ||
7917 V != ExtVec1->getOperand(0).getNode())
7918 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00007919
Tanya Lattnere9e67052011-06-14 23:48:48 +00007920 // Second is the constant, verify its correct.
7921 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7922 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00007923
Tanya Lattnere9e67052011-06-14 23:48:48 +00007924 // For the constant, we want to see all the even or all the odd.
7925 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7926 || C1->getZExtValue() != nextIndex+1)
7927 return SDValue();
7928
7929 // Increment index.
7930 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007931 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00007932 return SDValue();
7933 }
7934
7935 // Create VPADDL node.
7936 SelectionDAG &DAG = DCI.DAG;
7937 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00007938
7939 // Build operand list.
7940 SmallVector<SDValue, 8> Ops;
7941 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7942 TLI.getPointerTy()));
7943
7944 // Input is the vector.
7945 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007946
Tanya Lattnere9e67052011-06-14 23:48:48 +00007947 // Get widened type and narrowed type.
7948 MVT widenType;
7949 unsigned numElem = VT.getVectorNumElements();
Silviu Barangaa3106e62014-04-03 10:44:27 +00007950
7951 EVT inputLaneType = Vec.getValueType().getVectorElementType();
7952 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00007953 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7954 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7955 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7956 default:
Craig Toppere55c5562012-02-07 02:50:20 +00007957 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00007958 }
7959
Craig Topper48d114b2014-04-26 18:35:24 +00007960 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00007961 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
7962 return DAG.getNode(ExtOp, SDLoc(N), VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00007963}
7964
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007965static SDValue findMUL_LOHI(SDValue V) {
7966 if (V->getOpcode() == ISD::UMUL_LOHI ||
7967 V->getOpcode() == ISD::SMUL_LOHI)
7968 return V;
7969 return SDValue();
7970}
7971
7972static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7973 TargetLowering::DAGCombinerInfo &DCI,
7974 const ARMSubtarget *Subtarget) {
7975
7976 if (Subtarget->isThumb1Only()) return SDValue();
7977
7978 // Only perform the checks after legalize when the pattern is available.
7979 if (DCI.isBeforeLegalize()) return SDValue();
7980
7981 // Look for multiply add opportunities.
7982 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7983 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7984 // a glue link from the first add to the second add.
7985 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7986 // a S/UMLAL instruction.
7987 // loAdd UMUL_LOHI
7988 // \ / :lo \ :hi
7989 // \ / \ [no multiline comment]
7990 // ADDC | hiAdd
7991 // \ :glue / /
7992 // \ / /
7993 // ADDE
7994 //
7995 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7996 SDValue AddcOp0 = AddcNode->getOperand(0);
7997 SDValue AddcOp1 = AddcNode->getOperand(1);
7998
7999 // Check if the two operands are from the same mul_lohi node.
8000 if (AddcOp0.getNode() == AddcOp1.getNode())
8001 return SDValue();
8002
8003 assert(AddcNode->getNumValues() == 2 &&
8004 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00008005 "Expect ADDC with two result values. First: i32");
8006
8007 // Check that we have a glued ADDC node.
8008 if (AddcNode->getValueType(1) != MVT::Glue)
8009 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008010
8011 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
8012 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
8013 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
8014 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
8015 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
8016 return SDValue();
8017
8018 // Look for the glued ADDE.
8019 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00008020 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008021 return SDValue();
8022
8023 // Make sure it is really an ADDE.
8024 if (AddeNode->getOpcode() != ISD::ADDE)
8025 return SDValue();
8026
8027 assert(AddeNode->getNumOperands() == 3 &&
8028 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
8029 "ADDE node has the wrong inputs");
8030
8031 // Check for the triangle shape.
8032 SDValue AddeOp0 = AddeNode->getOperand(0);
8033 SDValue AddeOp1 = AddeNode->getOperand(1);
8034
8035 // Make sure that the ADDE operands are not coming from the same node.
8036 if (AddeOp0.getNode() == AddeOp1.getNode())
8037 return SDValue();
8038
8039 // Find the MUL_LOHI node walking up ADDE's operands.
8040 bool IsLeftOperandMUL = false;
8041 SDValue MULOp = findMUL_LOHI(AddeOp0);
8042 if (MULOp == SDValue())
8043 MULOp = findMUL_LOHI(AddeOp1);
8044 else
8045 IsLeftOperandMUL = true;
8046 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00008047 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008048
8049 // Figure out the right opcode.
8050 unsigned Opc = MULOp->getOpcode();
8051 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
8052
8053 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00008054 SDValue* HiAdd = nullptr;
8055 SDValue* LoMul = nullptr;
8056 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008057
Jyoti Allurf1d70502015-01-23 09:10:03 +00008058 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
8059 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
8060 return SDValue();
8061
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008062 if (IsLeftOperandMUL)
8063 HiAdd = &AddeOp1;
8064 else
8065 HiAdd = &AddeOp0;
8066
8067
Jyoti Allurf1d70502015-01-23 09:10:03 +00008068 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
8069 // whose low result is fed to the ADDC we are checking.
8070
8071 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008072 LoMul = &AddcOp0;
8073 LowAdd = &AddcOp1;
8074 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00008075 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008076 LoMul = &AddcOp1;
8077 LowAdd = &AddcOp0;
8078 }
8079
Craig Topper062a2ba2014-04-25 05:30:21 +00008080 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008081 return SDValue();
8082
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008083 // Create the merged node.
8084 SelectionDAG &DAG = DCI.DAG;
8085
8086 // Build operand list.
8087 SmallVector<SDValue, 8> Ops;
8088 Ops.push_back(LoMul->getOperand(0));
8089 Ops.push_back(LoMul->getOperand(1));
8090 Ops.push_back(*LowAdd);
8091 Ops.push_back(*HiAdd);
8092
Andrew Trickef9de2a2013-05-25 02:42:55 +00008093 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00008094 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008095
8096 // Replace the ADDs' nodes uses by the MLA node's values.
8097 SDValue HiMLALResult(MLALNode.getNode(), 1);
8098 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
8099
8100 SDValue LoMLALResult(MLALNode.getNode(), 0);
8101 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
8102
8103 // Return original node to notify the driver to stop replacing.
8104 SDValue resNode(AddcNode, 0);
8105 return resNode;
8106}
8107
8108/// PerformADDCCombine - Target-specific dag combine transform from
8109/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
8110static SDValue PerformADDCCombine(SDNode *N,
8111 TargetLowering::DAGCombinerInfo &DCI,
8112 const ARMSubtarget *Subtarget) {
8113
8114 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
8115
8116}
8117
Bob Wilson728eb292010-07-29 20:34:14 +00008118/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
8119/// operands N0 and N1. This is a helper for PerformADDCombine that is
8120/// called with the default operands, and if that fails, with commuted
8121/// operands.
8122static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008123 TargetLowering::DAGCombinerInfo &DCI,
8124 const ARMSubtarget *Subtarget){
8125
8126 // Attempt to create vpaddl for this add.
8127 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
8128 if (Result.getNode())
8129 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008130
Chris Lattner4147f082009-03-12 06:52:53 +00008131 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008132 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008133 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
8134 if (Result.getNode()) return Result;
8135 }
Chris Lattner4147f082009-03-12 06:52:53 +00008136 return SDValue();
8137}
8138
Bob Wilson728eb292010-07-29 20:34:14 +00008139/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
8140///
8141static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008142 TargetLowering::DAGCombinerInfo &DCI,
8143 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00008144 SDValue N0 = N->getOperand(0);
8145 SDValue N1 = N->getOperand(1);
8146
8147 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008148 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008149 if (Result.getNode())
8150 return Result;
8151
8152 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008153 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008154}
8155
Chris Lattner4147f082009-03-12 06:52:53 +00008156/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00008157///
Chris Lattner4147f082009-03-12 06:52:53 +00008158static SDValue PerformSUBCombine(SDNode *N,
8159 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00008160 SDValue N0 = N->getOperand(0);
8161 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00008162
Chris Lattner4147f082009-03-12 06:52:53 +00008163 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008164 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008165 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
8166 if (Result.getNode()) return Result;
8167 }
Bob Wilson7117a912009-03-20 22:42:55 +00008168
Chris Lattner4147f082009-03-12 06:52:53 +00008169 return SDValue();
8170}
8171
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008172/// PerformVMULCombine
8173/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8174/// special multiplier accumulator forwarding.
8175/// vmul d3, d0, d2
8176/// vmla d3, d1, d2
8177/// is faster than
8178/// vadd d3, d0, d1
8179/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00008180// However, for (A + B) * (A + B),
8181// vadd d2, d0, d1
8182// vmul d3, d0, d2
8183// vmla d3, d1, d2
8184// is slower than
8185// vadd d2, d0, d1
8186// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008187static SDValue PerformVMULCombine(SDNode *N,
8188 TargetLowering::DAGCombinerInfo &DCI,
8189 const ARMSubtarget *Subtarget) {
8190 if (!Subtarget->hasVMLxForwarding())
8191 return SDValue();
8192
8193 SelectionDAG &DAG = DCI.DAG;
8194 SDValue N0 = N->getOperand(0);
8195 SDValue N1 = N->getOperand(1);
8196 unsigned Opcode = N0.getOpcode();
8197 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8198 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00008199 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008200 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8201 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8202 return SDValue();
8203 std::swap(N0, N1);
8204 }
8205
Weiming Zhao2052f482013-09-25 23:12:06 +00008206 if (N0 == N1)
8207 return SDValue();
8208
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008209 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008210 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008211 SDValue N00 = N0->getOperand(0);
8212 SDValue N01 = N0->getOperand(1);
8213 return DAG.getNode(Opcode, DL, VT,
8214 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8215 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8216}
8217
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008218static SDValue PerformMULCombine(SDNode *N,
8219 TargetLowering::DAGCombinerInfo &DCI,
8220 const ARMSubtarget *Subtarget) {
8221 SelectionDAG &DAG = DCI.DAG;
8222
8223 if (Subtarget->isThumb1Only())
8224 return SDValue();
8225
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008226 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8227 return SDValue();
8228
8229 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008230 if (VT.is64BitVector() || VT.is128BitVector())
8231 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008232 if (VT != MVT::i32)
8233 return SDValue();
8234
8235 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8236 if (!C)
8237 return SDValue();
8238
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008239 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008240 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008241
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008242 ShiftAmt = ShiftAmt & (32 - 1);
8243 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008244 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008245
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008246 SDValue Res;
8247 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008248
8249 if (MulAmt >= 0) {
8250 if (isPowerOf2_32(MulAmt - 1)) {
8251 // (mul x, 2^N + 1) => (add (shl x, N), x)
8252 Res = DAG.getNode(ISD::ADD, DL, VT,
8253 V,
8254 DAG.getNode(ISD::SHL, DL, VT,
8255 V,
8256 DAG.getConstant(Log2_32(MulAmt - 1),
8257 MVT::i32)));
8258 } else if (isPowerOf2_32(MulAmt + 1)) {
8259 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8260 Res = DAG.getNode(ISD::SUB, DL, VT,
8261 DAG.getNode(ISD::SHL, DL, VT,
8262 V,
8263 DAG.getConstant(Log2_32(MulAmt + 1),
8264 MVT::i32)),
8265 V);
8266 } else
8267 return SDValue();
8268 } else {
8269 uint64_t MulAmtAbs = -MulAmt;
8270 if (isPowerOf2_32(MulAmtAbs + 1)) {
8271 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8272 Res = DAG.getNode(ISD::SUB, DL, VT,
8273 V,
8274 DAG.getNode(ISD::SHL, DL, VT,
8275 V,
8276 DAG.getConstant(Log2_32(MulAmtAbs + 1),
8277 MVT::i32)));
8278 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8279 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8280 Res = DAG.getNode(ISD::ADD, DL, VT,
8281 V,
8282 DAG.getNode(ISD::SHL, DL, VT,
8283 V,
8284 DAG.getConstant(Log2_32(MulAmtAbs-1),
8285 MVT::i32)));
8286 Res = DAG.getNode(ISD::SUB, DL, VT,
8287 DAG.getConstant(0, MVT::i32),Res);
8288
8289 } else
8290 return SDValue();
8291 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008292
8293 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008294 Res = DAG.getNode(ISD::SHL, DL, VT,
8295 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008296
8297 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008298 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008299 return SDValue();
8300}
8301
Owen Anderson30c48922010-11-05 19:27:46 +00008302static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008303 TargetLowering::DAGCombinerInfo &DCI,
8304 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008305
Owen Anderson30c48922010-11-05 19:27:46 +00008306 // Attempt to use immediate-form VBIC
8307 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008308 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008309 EVT VT = N->getValueType(0);
8310 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008311
Tanya Lattner266792a2011-04-07 15:24:20 +00008312 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8313 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008314
Owen Anderson30c48922010-11-05 19:27:46 +00008315 APInt SplatBits, SplatUndef;
8316 unsigned SplatBitSize;
8317 bool HasAnyUndefs;
8318 if (BVN &&
8319 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8320 if (SplatBitSize <= 64) {
8321 EVT VbicVT;
8322 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8323 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00008324 DAG, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008325 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008326 if (Val.getNode()) {
8327 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008328 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008329 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008330 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008331 }
8332 }
8333 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008334
Evan Chenge87681c2012-02-23 01:19:06 +00008335 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008336 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8337 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8338 if (Result.getNode())
8339 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008340 }
8341
Owen Anderson30c48922010-11-05 19:27:46 +00008342 return SDValue();
8343}
8344
Jim Grosbach11013ed2010-07-16 23:05:05 +00008345/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8346static SDValue PerformORCombine(SDNode *N,
8347 TargetLowering::DAGCombinerInfo &DCI,
8348 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008349 // Attempt to use immediate-form VORR
8350 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008351 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008352 EVT VT = N->getValueType(0);
8353 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008354
Tanya Lattner266792a2011-04-07 15:24:20 +00008355 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8356 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008357
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008358 APInt SplatBits, SplatUndef;
8359 unsigned SplatBitSize;
8360 bool HasAnyUndefs;
8361 if (BVN && Subtarget->hasNEON() &&
8362 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8363 if (SplatBitSize <= 64) {
8364 EVT VorrVT;
8365 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8366 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00008367 DAG, VorrVT, VT.is128BitVector(),
8368 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008369 if (Val.getNode()) {
8370 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008371 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008372 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008373 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008374 }
8375 }
8376 }
8377
Evan Chenge87681c2012-02-23 01:19:06 +00008378 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008379 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8380 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8381 if (Result.getNode())
8382 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008383 }
8384
Nadav Rotem3a94c542012-08-13 18:52:44 +00008385 // The code below optimizes (or (and X, Y), Z).
8386 // The AND operand needs to have a single user to make these optimizations
8387 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008388 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008389 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008390 return SDValue();
8391 SDValue N1 = N->getOperand(1);
8392
8393 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8394 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8395 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8396 APInt SplatUndef;
8397 unsigned SplatBitSize;
8398 bool HasAnyUndefs;
8399
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008400 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008401 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008402 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8403 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008404 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008405 HasAnyUndefs) && !HasAnyUndefs) {
8406 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8407 HasAnyUndefs) && !HasAnyUndefs) {
8408 // Ensure that the bit width of the constants are the same and that
8409 // the splat arguments are logical inverses as per the pattern we
8410 // are trying to simplify.
8411 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8412 SplatBits0 == ~SplatBits1) {
8413 // Canonicalize the vector type to make instruction selection
8414 // simpler.
8415 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8416 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8417 N0->getOperand(1),
8418 N0->getOperand(0),
8419 N1->getOperand(0));
8420 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8421 }
8422 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008423 }
8424 }
8425
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008426 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8427 // reasonable.
8428
Jim Grosbach11013ed2010-07-16 23:05:05 +00008429 // BFI is only available on V6T2+
8430 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8431 return SDValue();
8432
Andrew Trickef9de2a2013-05-25 02:42:55 +00008433 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008434 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008435 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008436 //
8437 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008438 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008439 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008440 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008441 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008442 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008443
Jim Grosbach11013ed2010-07-16 23:05:05 +00008444 if (VT != MVT::i32)
8445 return SDValue();
8446
Evan Cheng2e51bb42010-12-13 20:32:54 +00008447 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008448
Jim Grosbach11013ed2010-07-16 23:05:05 +00008449 // The value and the mask need to be constants so we can verify this is
8450 // actually a bitfield set. If the mask is 0xffff, we can do better
8451 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008452 SDValue MaskOp = N0.getOperand(1);
8453 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8454 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008455 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008456 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008457 if (Mask == 0xffff)
8458 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008459 SDValue Res;
8460 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008461 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8462 if (N1C) {
8463 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008464 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008465 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008466
Evan Cheng34345752010-12-11 04:11:38 +00008467 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008468 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008469
Evan Cheng2e51bb42010-12-13 20:32:54 +00008470 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Cheng34345752010-12-11 04:11:38 +00008471 DAG.getConstant(Val, MVT::i32),
8472 DAG.getConstant(Mask, MVT::i32));
8473
8474 // Do not add new nodes to DAG combiner worklist.
8475 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008476 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008477 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008478 } else if (N1.getOpcode() == ISD::AND) {
8479 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008480 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8481 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008482 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008483 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008484
Eric Christopherd5530962011-03-26 01:21:03 +00008485 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8486 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008487 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008488 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008489 // The pack halfword instruction works better for masks that fit it,
8490 // so use that when it's available.
8491 if (Subtarget->hasT2ExtractPack() &&
8492 (Mask == 0xffff || Mask == 0xffff0000))
8493 return SDValue();
8494 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008495 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008496 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopherd5530962011-03-26 01:21:03 +00008497 DAG.getConstant(amt, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008498 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008499 DAG.getConstant(Mask, MVT::i32));
8500 // Do not add new nodes to DAG combiner worklist.
8501 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008502 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008503 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008504 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008505 // The pack halfword instruction works better for masks that fit it,
8506 // so use that when it's available.
8507 if (Subtarget->hasT2ExtractPack() &&
8508 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8509 return SDValue();
8510 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008511 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008512 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008513 DAG.getConstant(lsb, MVT::i32));
8514 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopherd5530962011-03-26 01:21:03 +00008515 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008516 // Do not add new nodes to DAG combiner worklist.
8517 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008518 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008519 }
8520 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008521
Evan Cheng2e51bb42010-12-13 20:32:54 +00008522 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8523 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8524 ARM::isBitFieldInvertedMask(~Mask)) {
8525 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8526 // where lsb(mask) == #shamt and masked bits of B are known zero.
8527 SDValue ShAmt = N00.getOperand(1);
8528 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008529 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008530 if (ShAmtC != LSB)
8531 return SDValue();
8532
8533 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8534 DAG.getConstant(~Mask, MVT::i32));
8535
8536 // Do not add new nodes to DAG combiner worklist.
8537 DCI.CombineTo(N, Res, false);
8538 }
8539
Jim Grosbach11013ed2010-07-16 23:05:05 +00008540 return SDValue();
8541}
8542
Evan Chenge87681c2012-02-23 01:19:06 +00008543static SDValue PerformXORCombine(SDNode *N,
8544 TargetLowering::DAGCombinerInfo &DCI,
8545 const ARMSubtarget *Subtarget) {
8546 EVT VT = N->getValueType(0);
8547 SelectionDAG &DAG = DCI.DAG;
8548
8549 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8550 return SDValue();
8551
8552 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008553 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8554 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8555 if (Result.getNode())
8556 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008557 }
8558
8559 return SDValue();
8560}
8561
Evan Cheng6d02d902011-06-15 01:12:31 +00008562/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8563/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00008564static SDValue PerformBFICombine(SDNode *N,
8565 TargetLowering::DAGCombinerInfo &DCI) {
8566 SDValue N1 = N->getOperand(1);
8567 if (N1.getOpcode() == ISD::AND) {
8568 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8569 if (!N11C)
8570 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008571 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008572 unsigned LSB = countTrailingZeros(~InvMask);
8573 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +00008574 assert(Width <
8575 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00008576 "undefined behavior");
8577 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +00008578 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008579 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008580 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00008581 N->getOperand(0), N1.getOperand(0),
8582 N->getOperand(2));
8583 }
8584 return SDValue();
8585}
8586
Bob Wilson22806742010-09-22 22:09:21 +00008587/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8588/// ARMISD::VMOVRRD.
8589static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008590 TargetLowering::DAGCombinerInfo &DCI,
8591 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +00008592 // vmovrrd(vmovdrr x, y) -> x,y
8593 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00008594 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +00008595 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008596
8597 // vmovrrd(load f64) -> (load i32), (load i32)
8598 SDNode *InNode = InDouble.getNode();
8599 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8600 InNode->getValueType(0) == MVT::f64 &&
8601 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8602 !cast<LoadSDNode>(InNode)->isVolatile()) {
8603 // TODO: Should this be done for non-FrameIndex operands?
8604 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8605
8606 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008607 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008608 SDValue BasePtr = LD->getBasePtr();
8609 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8610 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008611 LD->isNonTemporal(), LD->isInvariant(),
8612 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008613
8614 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8615 DAG.getConstant(4, MVT::i32));
8616 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8617 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008618 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008619 std::min(4U, LD->getAlignment() / 2));
8620
8621 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Christian Pirker762b2c62014-06-01 09:30:52 +00008622 if (DCI.DAG.getTargetLoweringInfo().isBigEndian())
8623 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008624 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008625 return Result;
8626 }
8627
Bob Wilson22806742010-09-22 22:09:21 +00008628 return SDValue();
8629}
8630
8631/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8632/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8633static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8634 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8635 SDValue Op0 = N->getOperand(0);
8636 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00008637 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008638 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008639 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008640 Op1 = Op1.getOperand(0);
8641 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8642 Op0.getNode() == Op1.getNode() &&
8643 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008644 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00008645 N->getValueType(0), Op0.getOperand(0));
8646 return SDValue();
8647}
8648
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008649/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8650/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8651/// i64 vector to have f64 elements, since the value can then be loaded
8652/// directly into a VFP register.
8653static bool hasNormalLoadOperand(SDNode *N) {
8654 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8655 for (unsigned i = 0; i < NumElts; ++i) {
8656 SDNode *Elt = N->getOperand(i).getNode();
8657 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8658 return true;
8659 }
8660 return false;
8661}
8662
Bob Wilsoncb6db982010-09-17 22:59:05 +00008663/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8664/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008665static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008666 TargetLowering::DAGCombinerInfo &DCI,
8667 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +00008668 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8669 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8670 // into a pair of GPRs, which is fine when the value is used as a scalar,
8671 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008672 SelectionDAG &DAG = DCI.DAG;
8673 if (N->getNumOperands() == 2) {
8674 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8675 if (RV.getNode())
8676 return RV;
8677 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00008678
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008679 // Load i64 elements as f64 values so that type legalization does not split
8680 // them up into i32 values.
8681 EVT VT = N->getValueType(0);
8682 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8683 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008684 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008685 SmallVector<SDValue, 8> Ops;
8686 unsigned NumElts = VT.getVectorNumElements();
8687 for (unsigned i = 0; i < NumElts; ++i) {
8688 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8689 Ops.push_back(V);
8690 // Make the DAGCombiner fold the bitcast.
8691 DCI.AddToWorklist(V.getNode());
8692 }
8693 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00008694 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008695 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8696}
8697
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008698/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
8699static SDValue
8700PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8701 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
8702 // At that time, we may have inserted bitcasts from integer to float.
8703 // If these bitcasts have survived DAGCombine, change the lowering of this
8704 // BUILD_VECTOR in something more vector friendly, i.e., that does not
8705 // force to use floating point types.
8706
8707 // Make sure we can change the type of the vector.
8708 // This is possible iff:
8709 // 1. The vector is only used in a bitcast to a integer type. I.e.,
8710 // 1.1. Vector is used only once.
8711 // 1.2. Use is a bit convert to an integer type.
8712 // 2. The size of its operands are 32-bits (64-bits are not legal).
8713 EVT VT = N->getValueType(0);
8714 EVT EltVT = VT.getVectorElementType();
8715
8716 // Check 1.1. and 2.
8717 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
8718 return SDValue();
8719
8720 // By construction, the input type must be float.
8721 assert(EltVT == MVT::f32 && "Unexpected type!");
8722
8723 // Check 1.2.
8724 SDNode *Use = *N->use_begin();
8725 if (Use->getOpcode() != ISD::BITCAST ||
8726 Use->getValueType(0).isFloatingPoint())
8727 return SDValue();
8728
8729 // Check profitability.
8730 // Model is, if more than half of the relevant operands are bitcast from
8731 // i32, turn the build_vector into a sequence of insert_vector_elt.
8732 // Relevant operands are everything that is not statically
8733 // (i.e., at compile time) bitcasted.
8734 unsigned NumOfBitCastedElts = 0;
8735 unsigned NumElts = VT.getVectorNumElements();
8736 unsigned NumOfRelevantElts = NumElts;
8737 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
8738 SDValue Elt = N->getOperand(Idx);
8739 if (Elt->getOpcode() == ISD::BITCAST) {
8740 // Assume only bit cast to i32 will go away.
8741 if (Elt->getOperand(0).getValueType() == MVT::i32)
8742 ++NumOfBitCastedElts;
8743 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
8744 // Constants are statically casted, thus do not count them as
8745 // relevant operands.
8746 --NumOfRelevantElts;
8747 }
8748
8749 // Check if more than half of the elements require a non-free bitcast.
8750 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
8751 return SDValue();
8752
8753 SelectionDAG &DAG = DCI.DAG;
8754 // Create the new vector type.
8755 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
8756 // Check if the type is legal.
8757 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8758 if (!TLI.isTypeLegal(VecVT))
8759 return SDValue();
8760
8761 // Combine:
8762 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
8763 // => BITCAST INSERT_VECTOR_ELT
8764 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
8765 // (BITCAST EN), N.
8766 SDValue Vec = DAG.getUNDEF(VecVT);
8767 SDLoc dl(N);
8768 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
8769 SDValue V = N->getOperand(Idx);
8770 if (V.getOpcode() == ISD::UNDEF)
8771 continue;
8772 if (V.getOpcode() == ISD::BITCAST &&
8773 V->getOperand(0).getValueType() == MVT::i32)
8774 // Fold obvious case.
8775 V = V.getOperand(0);
8776 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00008777 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008778 // Make the DAGCombiner fold the bitcasts.
8779 DCI.AddToWorklist(V.getNode());
8780 }
8781 SDValue LaneIdx = DAG.getConstant(Idx, MVT::i32);
8782 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
8783 }
8784 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
8785 // Make the DAGCombiner fold the bitcasts.
8786 DCI.AddToWorklist(Vec.getNode());
8787 return Vec;
8788}
8789
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008790/// PerformInsertEltCombine - Target-specific dag combine xforms for
8791/// ISD::INSERT_VECTOR_ELT.
8792static SDValue PerformInsertEltCombine(SDNode *N,
8793 TargetLowering::DAGCombinerInfo &DCI) {
8794 // Bitcast an i64 load inserted into a vector to f64.
8795 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8796 EVT VT = N->getValueType(0);
8797 SDNode *Elt = N->getOperand(1).getNode();
8798 if (VT.getVectorElementType() != MVT::i64 ||
8799 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8800 return SDValue();
8801
8802 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008803 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008804 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8805 VT.getVectorNumElements());
8806 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8807 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8808 // Make the DAGCombiner fold the bitcasts.
8809 DCI.AddToWorklist(Vec.getNode());
8810 DCI.AddToWorklist(V.getNode());
8811 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8812 Vec, V, N->getOperand(2));
8813 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00008814}
8815
Bob Wilsonc7334a12010-10-27 20:38:28 +00008816/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8817/// ISD::VECTOR_SHUFFLE.
8818static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8819 // The LLVM shufflevector instruction does not require the shuffle mask
8820 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8821 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8822 // operands do not match the mask length, they are extended by concatenating
8823 // them with undef vectors. That is probably the right thing for other
8824 // targets, but for NEON it is better to concatenate two double-register
8825 // size vector operands into a single quad-register size vector. Do that
8826 // transformation here:
8827 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8828 // shuffle(concat(v1, v2), undef)
8829 SDValue Op0 = N->getOperand(0);
8830 SDValue Op1 = N->getOperand(1);
8831 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8832 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8833 Op0.getNumOperands() != 2 ||
8834 Op1.getNumOperands() != 2)
8835 return SDValue();
8836 SDValue Concat0Op1 = Op0.getOperand(1);
8837 SDValue Concat1Op1 = Op1.getOperand(1);
8838 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8839 Concat1Op1.getOpcode() != ISD::UNDEF)
8840 return SDValue();
8841 // Skip the transformation if any of the types are illegal.
8842 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8843 EVT VT = N->getValueType(0);
8844 if (!TLI.isTypeLegal(VT) ||
8845 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8846 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8847 return SDValue();
8848
Andrew Trickef9de2a2013-05-25 02:42:55 +00008849 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008850 Op0.getOperand(0), Op1.getOperand(0));
8851 // Translate the shuffle mask.
8852 SmallVector<int, 16> NewMask;
8853 unsigned NumElts = VT.getVectorNumElements();
8854 unsigned HalfElts = NumElts/2;
8855 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8856 for (unsigned n = 0; n < NumElts; ++n) {
8857 int MaskElt = SVN->getMaskElt(n);
8858 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00008859 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00008860 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00008861 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00008862 NewElt = HalfElts + MaskElt - NumElts;
8863 NewMask.push_back(NewElt);
8864 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00008865 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008866 DAG.getUNDEF(VT), NewMask.data());
8867}
8868
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008869/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
8870/// NEON load/store intrinsics, and generic vector load/stores, to merge
8871/// base address updates.
8872/// For generic load/stores, the memory type is assumed to be a vector.
8873/// The caller is assumed to have checked legality.
Bob Wilson06fce872011-02-07 17:43:21 +00008874static SDValue CombineBaseUpdate(SDNode *N,
8875 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson06fce872011-02-07 17:43:21 +00008876 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008877 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8878 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008879 const bool isStore = N->getOpcode() == ISD::STORE;
8880 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +00008881 SDValue Addr = N->getOperand(AddrOpIdx);
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008882 MemSDNode *MemN = cast<MemSDNode>(N);
Bob Wilson06fce872011-02-07 17:43:21 +00008883
8884 // Search for a use of the address operand that is an increment.
8885 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8886 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8887 SDNode *User = *UI;
8888 if (User->getOpcode() != ISD::ADD ||
8889 UI.getUse().getResNo() != Addr.getResNo())
8890 continue;
8891
8892 // Check that the add is independent of the load/store. Otherwise, folding
8893 // it would create a cycle.
8894 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8895 continue;
8896
8897 // Find the new opcode for the updating load/store.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008898 bool isLoadOp = true;
Bob Wilson06fce872011-02-07 17:43:21 +00008899 bool isLaneOp = false;
8900 unsigned NewOpc = 0;
8901 unsigned NumVecs = 0;
8902 if (isIntrinsic) {
8903 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8904 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00008905 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008906 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8907 NumVecs = 1; break;
8908 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8909 NumVecs = 2; break;
8910 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8911 NumVecs = 3; break;
8912 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8913 NumVecs = 4; break;
8914 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8915 NumVecs = 2; isLaneOp = true; break;
8916 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8917 NumVecs = 3; isLaneOp = true; break;
8918 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8919 NumVecs = 4; isLaneOp = true; break;
8920 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008921 NumVecs = 1; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008922 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008923 NumVecs = 2; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008924 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008925 NumVecs = 3; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008926 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008927 NumVecs = 4; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008928 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008929 NumVecs = 2; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008930 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008931 NumVecs = 3; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008932 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008933 NumVecs = 4; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008934 }
8935 } else {
8936 isLaneOp = true;
8937 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00008938 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008939 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8940 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8941 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008942 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD;
8943 NumVecs = 1; isLaneOp = false; break;
8944 case ISD::STORE: NewOpc = ARMISD::VST1_UPD;
8945 NumVecs = 1; isLaneOp = false; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008946 }
8947 }
8948
8949 // Find the size of memory referenced by the load/store.
8950 EVT VecTy;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008951 if (isLoadOp) {
Bob Wilson06fce872011-02-07 17:43:21 +00008952 VecTy = N->getValueType(0);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008953 } else if (isIntrinsic) {
Renato Golin2a5c0a52015-02-04 10:11:59 +00008954 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008955 } else {
8956 assert(isStore && "Node has to be a load, a store, or an intrinsic!");
8957 VecTy = N->getOperand(1).getValueType();
8958 }
8959
Bob Wilson06fce872011-02-07 17:43:21 +00008960 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8961 if (isLaneOp)
8962 NumBytes /= VecTy.getVectorNumElements();
8963
8964 // If the increment is a constant, it must match the memory ref size.
8965 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8966 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8967 uint64_t IncVal = CInc->getZExtValue();
8968 if (IncVal != NumBytes)
8969 continue;
8970 } else if (NumBytes >= 3 * 16) {
8971 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8972 // separate instructions that make it harder to use a non-constant update.
8973 continue;
8974 }
8975
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008976 // OK, we found an ADD we can fold into the base update.
8977 // Now, create a _UPD node, taking care of not breaking alignment.
8978
8979 EVT AlignedVecTy = VecTy;
8980 unsigned Alignment = MemN->getAlignment();
8981
8982 // If this is a less-than-standard-aligned load/store, change the type to
8983 // match the standard alignment.
8984 // The alignment is overlooked when selecting _UPD variants; and it's
8985 // easier to introduce bitcasts here than fix that.
8986 // There are 3 ways to get to this base-update combine:
8987 // - intrinsics: they are assumed to be properly aligned (to the standard
8988 // alignment of the memory type), so we don't need to do anything.
8989 // - ARMISD::VLDx nodes: they are only generated from the aforementioned
8990 // intrinsics, so, likewise, there's nothing to do.
8991 // - generic load/store instructions: the alignment is specified as an
8992 // explicit operand, rather than implicitly as the standard alignment
8993 // of the memory type (like the intrisics). We need to change the
8994 // memory type to match the explicit alignment. That way, we don't
8995 // generate non-standard-aligned ARMISD::VLDx nodes.
8996 if (isa<LSBaseSDNode>(N)) {
8997 if (Alignment == 0)
8998 Alignment = 1;
8999 if (Alignment < VecTy.getScalarSizeInBits() / 8) {
9000 MVT EltTy = MVT::getIntegerVT(Alignment * 8);
9001 assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
9002 assert(!isLaneOp && "Unexpected generic load/store lane.");
9003 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
9004 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
9005 }
9006 // Don't set an explicit alignment on regular load/stores that we want
9007 // to transform to VLD/VST 1_UPD nodes.
9008 // This matches the behavior of regular load/stores, which only get an
9009 // explicit alignment if the MMO alignment is larger than the standard
9010 // alignment of the memory type.
9011 // Intrinsics, however, always get an explicit alignment, set to the
9012 // alignment of the MMO.
9013 Alignment = 1;
9014 }
9015
Bob Wilson06fce872011-02-07 17:43:21 +00009016 // Create the new updating load/store node.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009017 // First, create an SDVTList for the new updating node's results.
Bob Wilson06fce872011-02-07 17:43:21 +00009018 EVT Tys[6];
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009019 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0);
Bob Wilson06fce872011-02-07 17:43:21 +00009020 unsigned n;
9021 for (n = 0; n < NumResultVecs; ++n)
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009022 Tys[n] = AlignedVecTy;
Bob Wilson06fce872011-02-07 17:43:21 +00009023 Tys[n++] = MVT::i32;
9024 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009025 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009026
9027 // Then, gather the new node's operands.
Bob Wilson06fce872011-02-07 17:43:21 +00009028 SmallVector<SDValue, 8> Ops;
9029 Ops.push_back(N->getOperand(0)); // incoming chain
9030 Ops.push_back(N->getOperand(AddrOpIdx));
9031 Ops.push_back(Inc);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009032
9033 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
9034 // Try to match the intrinsic's signature
9035 Ops.push_back(StN->getValue());
9036 } else {
9037 // Loads (and of course intrinsics) match the intrinsics' signature,
9038 // so just add all but the alignment operand.
9039 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i)
9040 Ops.push_back(N->getOperand(i));
9041 }
9042
9043 // For all node types, the alignment operand is always the last one.
9044 Ops.push_back(DAG.getConstant(Alignment, MVT::i32));
9045
9046 // If this is a non-standard-aligned STORE, the penultimate operand is the
9047 // stored value. Bitcast it to the aligned type.
9048 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
9049 SDValue &StVal = Ops[Ops.size()-2];
9050 StVal = DAG.getNode(ISD::BITCAST, SDLoc(N), AlignedVecTy, StVal);
9051 }
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009052
Andrew Trickef9de2a2013-05-25 02:42:55 +00009053 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys,
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009054 Ops, AlignedVecTy,
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009055 MemN->getMemOperand());
Bob Wilson06fce872011-02-07 17:43:21 +00009056
9057 // Update the uses.
Ahmed Bougacha4c2b0782015-02-19 23:13:10 +00009058 SmallVector<SDValue, 5> NewResults;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009059 for (unsigned i = 0; i < NumResultVecs; ++i)
Bob Wilson06fce872011-02-07 17:43:21 +00009060 NewResults.push_back(SDValue(UpdN.getNode(), i));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009061
9062 // If this is an non-standard-aligned LOAD, the first result is the loaded
9063 // value. Bitcast it to the expected result type.
9064 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
9065 SDValue &LdVal = NewResults[0];
9066 LdVal = DAG.getNode(ISD::BITCAST, SDLoc(N), VecTy, LdVal);
9067 }
9068
Bob Wilson06fce872011-02-07 17:43:21 +00009069 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
9070 DCI.CombineTo(N, NewResults);
9071 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
9072
9073 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00009074 }
Bob Wilson06fce872011-02-07 17:43:21 +00009075 return SDValue();
9076}
9077
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009078static SDValue PerformVLDCombine(SDNode *N,
9079 TargetLowering::DAGCombinerInfo &DCI) {
9080 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9081 return SDValue();
9082
9083 return CombineBaseUpdate(N, DCI);
9084}
9085
Bob Wilson2d790df2010-11-28 06:51:26 +00009086/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
9087/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
9088/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
9089/// return true.
9090static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9091 SelectionDAG &DAG = DCI.DAG;
9092 EVT VT = N->getValueType(0);
9093 // vldN-dup instructions only support 64-bit vectors for N > 1.
9094 if (!VT.is64BitVector())
9095 return false;
9096
9097 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
9098 SDNode *VLD = N->getOperand(0).getNode();
9099 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
9100 return false;
9101 unsigned NumVecs = 0;
9102 unsigned NewOpc = 0;
9103 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
9104 if (IntNo == Intrinsic::arm_neon_vld2lane) {
9105 NumVecs = 2;
9106 NewOpc = ARMISD::VLD2DUP;
9107 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
9108 NumVecs = 3;
9109 NewOpc = ARMISD::VLD3DUP;
9110 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
9111 NumVecs = 4;
9112 NewOpc = ARMISD::VLD4DUP;
9113 } else {
9114 return false;
9115 }
9116
9117 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9118 // numbers match the load.
9119 unsigned VLDLaneNo =
9120 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9121 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9122 UI != UE; ++UI) {
9123 // Ignore uses of the chain result.
9124 if (UI.getUse().getResNo() == NumVecs)
9125 continue;
9126 SDNode *User = *UI;
9127 if (User->getOpcode() != ARMISD::VDUPLANE ||
9128 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9129 return false;
9130 }
9131
9132 // Create the vldN-dup node.
9133 EVT Tys[5];
9134 unsigned n;
9135 for (n = 0; n < NumVecs; ++n)
9136 Tys[n] = VT;
9137 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009138 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00009139 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9140 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009141 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009142 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00009143 VLDMemInt->getMemOperand());
9144
9145 // Update the uses.
9146 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9147 UI != UE; ++UI) {
9148 unsigned ResNo = UI.getUse().getResNo();
9149 // Ignore uses of the chain result.
9150 if (ResNo == NumVecs)
9151 continue;
9152 SDNode *User = *UI;
9153 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9154 }
9155
9156 // Now the vldN-lane intrinsic is dead except for its chain result.
9157 // Update uses of the chain.
9158 std::vector<SDValue> VLDDupResults;
9159 for (unsigned n = 0; n < NumVecs; ++n)
9160 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9161 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9162 DCI.CombineTo(VLD, VLDDupResults);
9163
9164 return true;
9165}
9166
Bob Wilson103a0dc2010-07-14 01:22:12 +00009167/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9168/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009169static SDValue PerformVDUPLANECombine(SDNode *N,
9170 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009171 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009172
Bob Wilson2d790df2010-11-28 06:51:26 +00009173 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9174 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9175 if (CombineVLDDUP(N, DCI))
9176 return SDValue(N, 0);
9177
9178 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9179 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009180 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009181 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009182 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009183 return SDValue();
9184
9185 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9186 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9187 // The canonical VMOV for a zero vector uses a 32-bit element size.
9188 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9189 unsigned EltBits;
9190 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9191 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00009192 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009193 if (EltSize > VT.getVectorElementType().getSizeInBits())
9194 return SDValue();
9195
Andrew Trickef9de2a2013-05-25 02:42:55 +00009196 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009197}
9198
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009199static SDValue PerformLOADCombine(SDNode *N,
9200 TargetLowering::DAGCombinerInfo &DCI) {
9201 EVT VT = N->getValueType(0);
9202
9203 // If this is a legal vector load, try to combine it into a VLD1_UPD.
9204 if (ISD::isNormalLoad(N) && VT.isVector() &&
9205 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9206 return CombineBaseUpdate(N, DCI);
9207
9208 return SDValue();
9209}
9210
Ahmed Bougacha23167462014-12-09 21:26:53 +00009211/// PerformSTORECombine - Target-specific dag combine xforms for
9212/// ISD::STORE.
9213static SDValue PerformSTORECombine(SDNode *N,
9214 TargetLowering::DAGCombinerInfo &DCI) {
9215 StoreSDNode *St = cast<StoreSDNode>(N);
9216 if (St->isVolatile())
9217 return SDValue();
9218
9219 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
9220 // pack all of the elements in one place. Next, store to memory in fewer
9221 // chunks.
9222 SDValue StVal = St->getValue();
9223 EVT VT = StVal.getValueType();
9224 if (St->isTruncatingStore() && VT.isVector()) {
9225 SelectionDAG &DAG = DCI.DAG;
9226 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9227 EVT StVT = St->getMemoryVT();
9228 unsigned NumElems = VT.getVectorNumElements();
9229 assert(StVT != VT && "Cannot truncate to the same type");
9230 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
9231 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
9232
9233 // From, To sizes and ElemCount must be pow of two
9234 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
9235
9236 // We are going to use the original vector elt for storing.
9237 // Accumulated smaller vector elements must be a multiple of the store size.
9238 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
9239
9240 unsigned SizeRatio = FromEltSz / ToEltSz;
9241 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
9242
9243 // Create a type on which we perform the shuffle.
9244 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
9245 NumElems*SizeRatio);
9246 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
9247
9248 SDLoc DL(St);
9249 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
9250 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
9251 for (unsigned i = 0; i < NumElems; ++i)
9252 ShuffleVec[i] = TLI.isBigEndian() ? (i+1) * SizeRatio - 1 : i * SizeRatio;
9253
9254 // Can't shuffle using an illegal type.
9255 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
9256
9257 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
9258 DAG.getUNDEF(WideVec.getValueType()),
9259 ShuffleVec.data());
9260 // At this point all of the data is stored at the bottom of the
9261 // register. We now need to save it to mem.
9262
9263 // Find the largest store unit
9264 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +00009265 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +00009266 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
9267 StoreType = Tp;
9268 }
9269 // Didn't find a legal store type.
9270 if (!TLI.isTypeLegal(StoreType))
9271 return SDValue();
9272
9273 // Bitcast the original vector into a vector of store-size units
9274 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
9275 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
9276 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
9277 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
9278 SmallVector<SDValue, 8> Chains;
9279 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
9280 TLI.getPointerTy());
9281 SDValue BasePtr = St->getBasePtr();
9282
9283 // Perform one or more big stores into memory.
9284 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
9285 for (unsigned I = 0; I < E; I++) {
9286 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
9287 StoreType, ShuffWide,
9288 DAG.getIntPtrConstant(I));
9289 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
9290 St->getPointerInfo(), St->isVolatile(),
9291 St->isNonTemporal(), St->getAlignment());
9292 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
9293 Increment);
9294 Chains.push_back(Ch);
9295 }
9296 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
9297 }
9298
9299 if (!ISD::isNormalStore(St))
9300 return SDValue();
9301
9302 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
9303 // ARM stores of arguments in the same cache line.
9304 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
9305 StVal.getNode()->hasOneUse()) {
9306 SelectionDAG &DAG = DCI.DAG;
9307 bool isBigEndian = DAG.getTargetLoweringInfo().isBigEndian();
9308 SDLoc DL(St);
9309 SDValue BasePtr = St->getBasePtr();
9310 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
9311 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
9312 BasePtr, St->getPointerInfo(), St->isVolatile(),
9313 St->isNonTemporal(), St->getAlignment());
9314
9315 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
9316 DAG.getConstant(4, MVT::i32));
9317 return DAG.getStore(NewST1.getValue(0), DL,
9318 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
9319 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
9320 St->isNonTemporal(),
9321 std::min(4U, St->getAlignment() / 2));
9322 }
9323
9324 if (StVal.getValueType() == MVT::i64 &&
9325 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9326
9327 // Bitcast an i64 store extracted from a vector to f64.
9328 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9329 SelectionDAG &DAG = DCI.DAG;
9330 SDLoc dl(StVal);
9331 SDValue IntVec = StVal.getOperand(0);
9332 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9333 IntVec.getValueType().getVectorNumElements());
9334 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
9335 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
9336 Vec, StVal.getOperand(1));
9337 dl = SDLoc(N);
9338 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
9339 // Make the DAGCombiner fold the bitcasts.
9340 DCI.AddToWorklist(Vec.getNode());
9341 DCI.AddToWorklist(ExtElt.getNode());
9342 DCI.AddToWorklist(V.getNode());
9343 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
9344 St->getPointerInfo(), St->isVolatile(),
9345 St->isNonTemporal(), St->getAlignment(),
9346 St->getAAInfo());
9347 }
9348
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009349 // If this is a legal vector store, try to combine it into a VST1_UPD.
9350 if (ISD::isNormalStore(N) && VT.isVector() &&
9351 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9352 return CombineBaseUpdate(N, DCI);
9353
Ahmed Bougacha23167462014-12-09 21:26:53 +00009354 return SDValue();
9355}
9356
Eric Christopher1b8b94192011-06-29 21:10:36 +00009357// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00009358// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
9359static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
9360{
Chad Rosier6b610b32011-06-28 17:26:57 +00009361 integerPart cN;
9362 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009363 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
9364 I != E; I++) {
9365 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
9366 if (!C)
9367 return false;
9368
Eric Christopher1b8b94192011-06-29 21:10:36 +00009369 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009370 APFloat APF = C->getValueAPF();
9371 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
9372 != APFloat::opOK || !isExact)
9373 return false;
9374
9375 c0 = (I == 0) ? cN : c0;
9376 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
9377 return false;
9378 }
9379 C = c0;
9380 return true;
9381}
9382
9383/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
9384/// can replace combinations of VMUL and VCVT (floating-point to integer)
9385/// when the VMUL has a constant operand that is a power of 2.
9386///
9387/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9388/// vmul.f32 d16, d17, d16
9389/// vcvt.s32.f32 d16, d16
9390/// becomes:
9391/// vcvt.s32.f32 d16, d16, #3
9392static SDValue PerformVCVTCombine(SDNode *N,
9393 TargetLowering::DAGCombinerInfo &DCI,
9394 const ARMSubtarget *Subtarget) {
9395 SelectionDAG &DAG = DCI.DAG;
9396 SDValue Op = N->getOperand(0);
9397
9398 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
9399 Op.getOpcode() != ISD::FMUL)
9400 return SDValue();
9401
9402 uint64_t C;
9403 SDValue N0 = Op->getOperand(0);
9404 SDValue ConstVec = Op->getOperand(1);
9405 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
9406
Eric Christopher1b8b94192011-06-29 21:10:36 +00009407 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00009408 !isConstVecPow2(ConstVec, isSigned, C))
9409 return SDValue();
9410
Tim Northover7cbc2152013-06-28 15:29:25 +00009411 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
9412 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Bradley Smithececb7f2014-12-16 10:59:27 +00009413 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9414 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32 ||
9415 NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +00009416 // These instructions only exist converting from f32 to i32. We can handle
9417 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +00009418 // be lossy. We also can't handle more then 4 lanes, since these intructions
9419 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +00009420 return SDValue();
9421 }
9422
Chad Rosierfa8d8932011-06-24 19:23:04 +00009423 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9424 Intrinsic::arm_neon_vcvtfp2fxu;
Tim Northover7cbc2152013-06-28 15:29:25 +00009425 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
9426 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9427 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
9428 DAG.getConstant(Log2_64(C), MVT::i32));
9429
9430 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9431 FixConv = DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), FixConv);
9432
9433 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009434}
9435
9436/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9437/// can replace combinations of VCVT (integer to floating-point) and VDIV
9438/// when the VDIV has a constant operand that is a power of 2.
9439///
9440/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9441/// vcvt.f32.s32 d16, d16
9442/// vdiv.f32 d16, d17, d16
9443/// becomes:
9444/// vcvt.f32.s32 d16, d16, #3
9445static SDValue PerformVDIVCombine(SDNode *N,
9446 TargetLowering::DAGCombinerInfo &DCI,
9447 const ARMSubtarget *Subtarget) {
9448 SelectionDAG &DAG = DCI.DAG;
9449 SDValue Op = N->getOperand(0);
9450 unsigned OpOpcode = Op.getNode()->getOpcode();
9451
9452 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
9453 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9454 return SDValue();
9455
9456 uint64_t C;
9457 SDValue ConstVec = N->getOperand(1);
9458 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
9459
9460 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9461 !isConstVecPow2(ConstVec, isSigned, C))
9462 return SDValue();
9463
Tim Northover7cbc2152013-06-28 15:29:25 +00009464 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
9465 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
9466 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9467 // These instructions only exist converting from i32 to f32. We can handle
9468 // smaller integers by generating an extra extend, but larger ones would
9469 // be lossy.
9470 return SDValue();
9471 }
9472
9473 SDValue ConvInput = Op.getOperand(0);
9474 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9475 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9476 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
9477 SDLoc(N), NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9478 ConvInput);
9479
Eric Christopher1b8b94192011-06-29 21:10:36 +00009480 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00009481 Intrinsic::arm_neon_vcvtfxu2fp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009482 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
Chad Rosierfa8d8932011-06-24 19:23:04 +00009483 Op.getValueType(),
Eric Christopher1b8b94192011-06-29 21:10:36 +00009484 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Tim Northover7cbc2152013-06-28 15:29:25 +00009485 ConvInput, DAG.getConstant(Log2_64(C), MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00009486}
9487
9488/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00009489/// operand of a vector shift operation, where all the elements of the
9490/// build_vector must have the same constant integer value.
9491static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9492 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00009493 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00009494 Op = Op.getOperand(0);
9495 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9496 APInt SplatBits, SplatUndef;
9497 unsigned SplatBitSize;
9498 bool HasAnyUndefs;
9499 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9500 HasAnyUndefs, ElementBits) ||
9501 SplatBitSize > ElementBits)
9502 return false;
9503 Cnt = SplatBits.getSExtValue();
9504 return true;
9505}
9506
9507/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9508/// operand of a vector shift left operation. That value must be in the range:
9509/// 0 <= Value < ElementBits for a left shift; or
9510/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009511static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009512 assert(VT.isVector() && "vector shift count is not a vector type");
9513 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9514 if (! getVShiftImm(Op, ElementBits, Cnt))
9515 return false;
9516 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9517}
9518
9519/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9520/// operand of a vector shift right operation. For a shift opcode, the value
9521/// is positive, but for an intrinsic the value count must be negative. The
9522/// absolute value must be in the range:
9523/// 1 <= |Value| <= ElementBits for a right shift; or
9524/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009525static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00009526 int64_t &Cnt) {
9527 assert(VT.isVector() && "vector shift count is not a vector type");
9528 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9529 if (! getVShiftImm(Op, ElementBits, Cnt))
9530 return false;
9531 if (isIntrinsic)
9532 Cnt = -Cnt;
9533 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9534}
9535
9536/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9537static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9538 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9539 switch (IntNo) {
9540 default:
9541 // Don't do anything for most intrinsics.
9542 break;
9543
9544 // Vector shifts: check for immediate versions and lower them.
9545 // Note: This is done during DAG combining instead of DAG legalizing because
9546 // the build_vectors for 64-bit vector element shift counts are generally
9547 // not legal, and it is hard to see their values after they get legalized to
9548 // loads from a constant pool.
9549 case Intrinsic::arm_neon_vshifts:
9550 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009551 case Intrinsic::arm_neon_vrshifts:
9552 case Intrinsic::arm_neon_vrshiftu:
9553 case Intrinsic::arm_neon_vrshiftn:
9554 case Intrinsic::arm_neon_vqshifts:
9555 case Intrinsic::arm_neon_vqshiftu:
9556 case Intrinsic::arm_neon_vqshiftsu:
9557 case Intrinsic::arm_neon_vqshiftns:
9558 case Intrinsic::arm_neon_vqshiftnu:
9559 case Intrinsic::arm_neon_vqshiftnsu:
9560 case Intrinsic::arm_neon_vqrshiftns:
9561 case Intrinsic::arm_neon_vqrshiftnu:
9562 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009563 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009564 int64_t Cnt;
9565 unsigned VShiftOpc = 0;
9566
9567 switch (IntNo) {
9568 case Intrinsic::arm_neon_vshifts:
9569 case Intrinsic::arm_neon_vshiftu:
9570 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9571 VShiftOpc = ARMISD::VSHL;
9572 break;
9573 }
9574 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9575 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9576 ARMISD::VSHRs : ARMISD::VSHRu);
9577 break;
9578 }
9579 return SDValue();
9580
Bob Wilson2e076c42009-06-22 23:27:02 +00009581 case Intrinsic::arm_neon_vrshifts:
9582 case Intrinsic::arm_neon_vrshiftu:
9583 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9584 break;
9585 return SDValue();
9586
9587 case Intrinsic::arm_neon_vqshifts:
9588 case Intrinsic::arm_neon_vqshiftu:
9589 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9590 break;
9591 return SDValue();
9592
9593 case Intrinsic::arm_neon_vqshiftsu:
9594 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9595 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +00009596 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009597
Bob Wilson2e076c42009-06-22 23:27:02 +00009598 case Intrinsic::arm_neon_vrshiftn:
9599 case Intrinsic::arm_neon_vqshiftns:
9600 case Intrinsic::arm_neon_vqshiftnu:
9601 case Intrinsic::arm_neon_vqshiftnsu:
9602 case Intrinsic::arm_neon_vqrshiftns:
9603 case Intrinsic::arm_neon_vqrshiftnu:
9604 case Intrinsic::arm_neon_vqrshiftnsu:
9605 // Narrowing shifts require an immediate right shift.
9606 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9607 break;
Jim Grosbach84511e12010-06-02 21:53:11 +00009608 llvm_unreachable("invalid shift count for narrowing vector shift "
9609 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009610
9611 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00009612 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00009613 }
9614
9615 switch (IntNo) {
9616 case Intrinsic::arm_neon_vshifts:
9617 case Intrinsic::arm_neon_vshiftu:
9618 // Opcode already set above.
9619 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00009620 case Intrinsic::arm_neon_vrshifts:
9621 VShiftOpc = ARMISD::VRSHRs; break;
9622 case Intrinsic::arm_neon_vrshiftu:
9623 VShiftOpc = ARMISD::VRSHRu; break;
9624 case Intrinsic::arm_neon_vrshiftn:
9625 VShiftOpc = ARMISD::VRSHRN; break;
9626 case Intrinsic::arm_neon_vqshifts:
9627 VShiftOpc = ARMISD::VQSHLs; break;
9628 case Intrinsic::arm_neon_vqshiftu:
9629 VShiftOpc = ARMISD::VQSHLu; break;
9630 case Intrinsic::arm_neon_vqshiftsu:
9631 VShiftOpc = ARMISD::VQSHLsu; break;
9632 case Intrinsic::arm_neon_vqshiftns:
9633 VShiftOpc = ARMISD::VQSHRNs; break;
9634 case Intrinsic::arm_neon_vqshiftnu:
9635 VShiftOpc = ARMISD::VQSHRNu; break;
9636 case Intrinsic::arm_neon_vqshiftnsu:
9637 VShiftOpc = ARMISD::VQSHRNsu; break;
9638 case Intrinsic::arm_neon_vqrshiftns:
9639 VShiftOpc = ARMISD::VQRSHRNs; break;
9640 case Intrinsic::arm_neon_vqrshiftnu:
9641 VShiftOpc = ARMISD::VQRSHRNu; break;
9642 case Intrinsic::arm_neon_vqrshiftnsu:
9643 VShiftOpc = ARMISD::VQRSHRNsu; break;
9644 }
9645
Andrew Trickef9de2a2013-05-25 02:42:55 +00009646 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009647 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009648 }
9649
9650 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009651 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009652 int64_t Cnt;
9653 unsigned VShiftOpc = 0;
9654
9655 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9656 VShiftOpc = ARMISD::VSLI;
9657 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9658 VShiftOpc = ARMISD::VSRI;
9659 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009660 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009661 }
9662
Andrew Trickef9de2a2013-05-25 02:42:55 +00009663 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +00009664 N->getOperand(1), N->getOperand(2),
Owen Anderson9f944592009-08-11 20:47:22 +00009665 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009666 }
9667
9668 case Intrinsic::arm_neon_vqrshifts:
9669 case Intrinsic::arm_neon_vqrshiftu:
9670 // No immediate versions of these to check for.
9671 break;
9672 }
9673
9674 return SDValue();
9675}
9676
9677/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9678/// lowers them. As with the vector shift intrinsics, this is done during DAG
9679/// combining instead of DAG legalizing because the build_vectors for 64-bit
9680/// vector element shift counts are generally not legal, and it is hard to see
9681/// their values after they get legalized to loads from a constant pool.
9682static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9683 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009684 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +00009685 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9686 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9687 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9688 SDValue N1 = N->getOperand(1);
9689 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9690 SDValue N0 = N->getOperand(0);
9691 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9692 DAG.MaskedValueIsZero(N0.getOperand(0),
9693 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009694 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +00009695 }
9696 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009697
9698 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +00009699 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9700 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +00009701 return SDValue();
9702
9703 assert(ST->hasNEON() && "unexpected vector shift");
9704 int64_t Cnt;
9705
9706 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009707 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009708
9709 case ISD::SHL:
9710 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009711 return DAG.getNode(ARMISD::VSHL, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009712 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009713 break;
9714
9715 case ISD::SRA:
9716 case ISD::SRL:
9717 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9718 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9719 ARMISD::VSHRs : ARMISD::VSHRu);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009720 return DAG.getNode(VShiftOpc, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009721 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009722 }
9723 }
9724 return SDValue();
9725}
9726
9727/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9728/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9729static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9730 const ARMSubtarget *ST) {
9731 SDValue N0 = N->getOperand(0);
9732
9733 // Check for sign- and zero-extensions of vector extract operations of 8-
9734 // and 16-bit vector elements. NEON supports these directly. They are
9735 // handled during DAG combining because type legalization will promote them
9736 // to 32-bit types and it is messy to recognize the operations after that.
9737 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9738 SDValue Vec = N0.getOperand(0);
9739 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009740 EVT VT = N->getValueType(0);
9741 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009742 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9743
Owen Anderson9f944592009-08-11 20:47:22 +00009744 if (VT == MVT::i32 &&
9745 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +00009746 TLI.isTypeLegal(Vec.getValueType()) &&
9747 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009748
9749 unsigned Opc = 0;
9750 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009751 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009752 case ISD::SIGN_EXTEND:
9753 Opc = ARMISD::VGETLANEs;
9754 break;
9755 case ISD::ZERO_EXTEND:
9756 case ISD::ANY_EXTEND:
9757 Opc = ARMISD::VGETLANEu;
9758 break;
9759 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009760 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +00009761 }
9762 }
9763
9764 return SDValue();
9765}
9766
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009767/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9768/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9769static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9770 const ARMSubtarget *ST) {
9771 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng55f0c6b2010-07-15 22:07:12 +00009772 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009773 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9774 // a NaN; only do the transformation when it matches that behavior.
9775
9776 // For now only do this when using NEON for FP operations; if using VFP, it
9777 // is not obvious that the benefit outweighs the cost of switching to the
9778 // NEON pipeline.
9779 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9780 N->getValueType(0) != MVT::f32)
9781 return SDValue();
9782
9783 SDValue CondLHS = N->getOperand(0);
9784 SDValue CondRHS = N->getOperand(1);
9785 SDValue LHS = N->getOperand(2);
9786 SDValue RHS = N->getOperand(3);
9787 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9788
9789 unsigned Opcode = 0;
9790 bool IsReversed;
Bob Wilsonba8ac742010-02-24 22:15:53 +00009791 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009792 IsReversed = false; // x CC y ? x : y
Bob Wilsonba8ac742010-02-24 22:15:53 +00009793 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009794 IsReversed = true ; // x CC y ? y : x
9795 } else {
9796 return SDValue();
9797 }
9798
Bob Wilsonba8ac742010-02-24 22:15:53 +00009799 bool IsUnordered;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009800 switch (CC) {
9801 default: break;
9802 case ISD::SETOLT:
9803 case ISD::SETOLE:
9804 case ISD::SETLT:
9805 case ISD::SETLE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009806 case ISD::SETULT:
9807 case ISD::SETULE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009808 // If LHS is NaN, an ordered comparison will be false and the result will
9809 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9810 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9811 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9812 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9813 break;
9814 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9815 // will return -0, so vmin can only be used for unsafe math or if one of
9816 // the operands is known to be nonzero.
9817 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009818 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009819 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9820 break;
9821 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009822 break;
9823
9824 case ISD::SETOGT:
9825 case ISD::SETOGE:
9826 case ISD::SETGT:
9827 case ISD::SETGE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009828 case ISD::SETUGT:
9829 case ISD::SETUGE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009830 // If LHS is NaN, an ordered comparison will be false and the result will
9831 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9832 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9833 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9834 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9835 break;
9836 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9837 // will return +0, so vmax can only be used for unsafe math or if one of
9838 // the operands is known to be nonzero.
9839 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009840 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009841 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9842 break;
9843 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009844 break;
9845 }
9846
9847 if (!Opcode)
9848 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009849 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009850}
9851
Evan Chengf863e3f2011-07-13 00:42:17 +00009852/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9853SDValue
9854ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9855 SDValue Cmp = N->getOperand(4);
9856 if (Cmp.getOpcode() != ARMISD::CMPZ)
9857 // Only looking at EQ and NE cases.
9858 return SDValue();
9859
9860 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009861 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +00009862 SDValue LHS = Cmp.getOperand(0);
9863 SDValue RHS = Cmp.getOperand(1);
9864 SDValue FalseVal = N->getOperand(0);
9865 SDValue TrueVal = N->getOperand(1);
9866 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +00009867 ARMCC::CondCodes CC =
9868 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +00009869
9870 // Simplify
9871 // mov r1, r0
9872 // cmp r1, x
9873 // mov r0, y
9874 // moveq r0, x
9875 // to
9876 // cmp r0, x
9877 // movne r0, y
9878 //
9879 // mov r1, r0
9880 // cmp r1, x
9881 // mov r0, x
9882 // movne r0, y
9883 // to
9884 // cmp r0, x
9885 // movne r0, y
9886 /// FIXME: Turn this into a target neutral optimization?
9887 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +00009888 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +00009889 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9890 N->getOperand(3), Cmp);
9891 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9892 SDValue ARMcc;
9893 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9894 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9895 N->getOperand(3), NewCmp);
9896 }
9897
9898 if (Res.getNode()) {
9899 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00009900 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +00009901 // Capture demanded bits information that would be otherwise lost.
9902 if (KnownZero == 0xfffffffe)
9903 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9904 DAG.getValueType(MVT::i1));
9905 else if (KnownZero == 0xffffff00)
9906 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9907 DAG.getValueType(MVT::i8));
9908 else if (KnownZero == 0xffff0000)
9909 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9910 DAG.getValueType(MVT::i16));
9911 }
9912
9913 return Res;
9914}
9915
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009916SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +00009917 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009918 switch (N->getOpcode()) {
9919 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009920 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +00009921 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009922 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009923 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009924 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +00009925 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9926 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +00009927 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009928 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +00009929 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009930 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009931 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009932 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +00009933 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +00009934 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009935 case ISD::FP_TO_SINT:
9936 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9937 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009938 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00009939 case ISD::SHL:
9940 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009941 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00009942 case ISD::SIGN_EXTEND:
9943 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009944 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9945 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +00009946 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009947 case ISD::LOAD: return PerformLOADCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009948 case ARMISD::VLD2DUP:
9949 case ARMISD::VLD3DUP:
9950 case ARMISD::VLD4DUP:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009951 return PerformVLDCombine(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009952 case ARMISD::BUILD_VECTOR:
9953 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009954 case ISD::INTRINSIC_VOID:
9955 case ISD::INTRINSIC_W_CHAIN:
9956 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9957 case Intrinsic::arm_neon_vld1:
9958 case Intrinsic::arm_neon_vld2:
9959 case Intrinsic::arm_neon_vld3:
9960 case Intrinsic::arm_neon_vld4:
9961 case Intrinsic::arm_neon_vld2lane:
9962 case Intrinsic::arm_neon_vld3lane:
9963 case Intrinsic::arm_neon_vld4lane:
9964 case Intrinsic::arm_neon_vst1:
9965 case Intrinsic::arm_neon_vst2:
9966 case Intrinsic::arm_neon_vst3:
9967 case Intrinsic::arm_neon_vst4:
9968 case Intrinsic::arm_neon_vst2lane:
9969 case Intrinsic::arm_neon_vst3lane:
9970 case Intrinsic::arm_neon_vst4lane:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009971 return PerformVLDCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009972 default: break;
9973 }
9974 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009975 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009976 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009977}
9978
Evan Chengd42641c2011-02-02 01:06:55 +00009979bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9980 EVT VT) const {
9981 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9982}
9983
Matt Arsenault6f2a5262014-07-27 17:46:40 +00009984bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
9985 unsigned,
9986 unsigned,
9987 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009988 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +00009989 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009990
9991 switch (VT.getSimpleVT().SimpleTy) {
9992 default:
9993 return false;
9994 case MVT::i8:
9995 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009996 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009997 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +00009998 if (AllowsUnaligned) {
9999 if (Fast)
10000 *Fast = Subtarget->hasV7Ops();
10001 return true;
10002 }
10003 return false;
10004 }
Evan Chengeec6bc62012-08-15 17:44:53 +000010005 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010006 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010007 // For any little-endian targets with neon, we can support unaligned ld/st
10008 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +000010009 // A big-endian target may also explicitly support unaligned accesses
Evan Cheng79e2ca92012-12-10 23:21:26 +000010010 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
10011 if (Fast)
10012 *Fast = true;
10013 return true;
10014 }
10015 return false;
10016 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010017 }
10018}
10019
Lang Hames9929c422011-11-02 22:52:45 +000010020static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
10021 unsigned AlignCheck) {
10022 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
10023 (DstAlign == 0 || DstAlign % AlignCheck == 0));
10024}
10025
10026EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
10027 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000010028 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +000010029 bool MemcpyStrSrc,
10030 MachineFunction &MF) const {
10031 const Function *F = MF.getFunction();
10032
10033 // See if we can use NEON instructions for this...
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +000010034 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
10035 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010036 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +000010037 if (Size >= 16 &&
10038 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010039 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010040 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +000010041 } else if (Size >= 8 &&
10042 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010043 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
10044 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010045 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +000010046 }
10047 }
10048
Lang Hamesb85fcd02011-11-08 18:56:23 +000010049 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +000010050 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010051 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +000010052 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010053 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +000010054
Lang Hames9929c422011-11-02 22:52:45 +000010055 // Let the target-independent logic figure it out.
10056 return MVT::Other;
10057}
10058
Evan Cheng9ec512d2012-12-06 19:13:27 +000010059bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
10060 if (Val.getOpcode() != ISD::LOAD)
10061 return false;
10062
10063 EVT VT1 = Val.getValueType();
10064 if (!VT1.isSimple() || !VT1.isInteger() ||
10065 !VT2.isSimple() || !VT2.isInteger())
10066 return false;
10067
10068 switch (VT1.getSimpleVT().SimpleTy) {
10069 default: break;
10070 case MVT::i1:
10071 case MVT::i8:
10072 case MVT::i16:
10073 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
10074 return true;
10075 }
10076
10077 return false;
10078}
10079
Ahmed Bougacha4200cc92015-03-05 19:37:53 +000010080bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
10081 EVT VT = ExtVal.getValueType();
10082
10083 if (!isTypeLegal(VT))
10084 return false;
10085
10086 // Don't create a loadext if we can fold the extension into a wide/long
10087 // instruction.
10088 // If there's more than one user instruction, the loadext is desirable no
10089 // matter what. There can be two uses by the same instruction.
10090 if (ExtVal->use_empty() ||
10091 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode()))
10092 return true;
10093
10094 SDNode *U = *ExtVal->use_begin();
10095 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB ||
10096 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL))
10097 return false;
10098
10099 return true;
10100}
10101
Tim Northovercc2e9032013-08-06 13:58:03 +000010102bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
10103 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
10104 return false;
10105
10106 if (!isTypeLegal(EVT::getEVT(Ty1)))
10107 return false;
10108
10109 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
10110
10111 // Assuming the caller doesn't have a zeroext or signext return parameter,
10112 // truncation all the way down to i1 is valid.
10113 return true;
10114}
10115
10116
Evan Chengdc49a8d2009-08-14 20:09:37 +000010117static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
10118 if (V < 0)
10119 return false;
10120
10121 unsigned Scale = 1;
10122 switch (VT.getSimpleVT().SimpleTy) {
10123 default: return false;
10124 case MVT::i1:
10125 case MVT::i8:
10126 // Scale == 1;
10127 break;
10128 case MVT::i16:
10129 // Scale == 2;
10130 Scale = 2;
10131 break;
10132 case MVT::i32:
10133 // Scale == 4;
10134 Scale = 4;
10135 break;
10136 }
10137
10138 if ((V & (Scale - 1)) != 0)
10139 return false;
10140 V /= Scale;
10141 return V == (V & ((1LL << 5) - 1));
10142}
10143
10144static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
10145 const ARMSubtarget *Subtarget) {
10146 bool isNeg = false;
10147 if (V < 0) {
10148 isNeg = true;
10149 V = - V;
10150 }
10151
10152 switch (VT.getSimpleVT().SimpleTy) {
10153 default: return false;
10154 case MVT::i1:
10155 case MVT::i8:
10156 case MVT::i16:
10157 case MVT::i32:
10158 // + imm12 or - imm8
10159 if (isNeg)
10160 return V == (V & ((1LL << 8) - 1));
10161 return V == (V & ((1LL << 12) - 1));
10162 case MVT::f32:
10163 case MVT::f64:
10164 // Same as ARM mode. FIXME: NEON?
10165 if (!Subtarget->hasVFP2())
10166 return false;
10167 if ((V & 3) != 0)
10168 return false;
10169 V >>= 2;
10170 return V == (V & ((1LL << 8) - 1));
10171 }
10172}
10173
Evan Cheng2150b922007-03-12 23:30:29 +000010174/// isLegalAddressImmediate - Return true if the integer value can be used
10175/// as the offset of the target addressing mode for load / store of the
10176/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010177static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010178 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000010179 if (V == 0)
10180 return true;
10181
Evan Chengce5dfb62009-03-09 19:15:00 +000010182 if (!VT.isSimple())
10183 return false;
10184
Evan Chengdc49a8d2009-08-14 20:09:37 +000010185 if (Subtarget->isThumb1Only())
10186 return isLegalT1AddressImmediate(V, VT);
10187 else if (Subtarget->isThumb2())
10188 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000010189
Evan Chengdc49a8d2009-08-14 20:09:37 +000010190 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000010191 if (V < 0)
10192 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000010193 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000010194 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010195 case MVT::i1:
10196 case MVT::i8:
10197 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000010198 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010199 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010200 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000010201 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010202 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010203 case MVT::f32:
10204 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010205 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000010206 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000010207 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000010208 return false;
10209 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010210 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000010211 }
Evan Cheng10043e22007-01-19 07:51:42 +000010212}
10213
Evan Chengdc49a8d2009-08-14 20:09:37 +000010214bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
10215 EVT VT) const {
10216 int Scale = AM.Scale;
10217 if (Scale < 0)
10218 return false;
10219
10220 switch (VT.getSimpleVT().SimpleTy) {
10221 default: return false;
10222 case MVT::i1:
10223 case MVT::i8:
10224 case MVT::i16:
10225 case MVT::i32:
10226 if (Scale == 1)
10227 return true;
10228 // r + r << imm
10229 Scale = Scale & ~1;
10230 return Scale == 2 || Scale == 4 || Scale == 8;
10231 case MVT::i64:
10232 // r + r
10233 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
10234 return true;
10235 return false;
10236 case MVT::isVoid:
10237 // Note, we allow "void" uses (basically, uses that aren't loads or
10238 // stores), because arm allows folding a scale into many arithmetic
10239 // operations. This should be made more precise and revisited later.
10240
10241 // Allow r << imm, but the imm has to be a multiple of two.
10242 if (Scale & 1) return false;
10243 return isPowerOf2_32(Scale);
10244 }
10245}
10246
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010247/// isLegalAddressingMode - Return true if the addressing mode represented
10248/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson7117a912009-03-20 22:42:55 +000010249bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +000010250 Type *Ty) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010251 EVT VT = getValueType(Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000010252 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000010253 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010254
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010255 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000010256 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010257 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010258
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010259 switch (AM.Scale) {
10260 case 0: // no scale reg, must be "r+i" or "r", or "i".
10261 break;
10262 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010263 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010264 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +000010265 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010266 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000010267 // ARM doesn't support any R+R*scale+imm addr modes.
10268 if (AM.BaseOffs)
10269 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010270
Bob Wilson866c1742009-04-08 17:55:28 +000010271 if (!VT.isSimple())
10272 return false;
10273
Evan Chengdc49a8d2009-08-14 20:09:37 +000010274 if (Subtarget->isThumb2())
10275 return isLegalT2ScaledAddressingMode(AM, VT);
10276
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010277 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000010278 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010279 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010280 case MVT::i1:
10281 case MVT::i8:
10282 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010283 if (Scale < 0) Scale = -Scale;
10284 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010285 return true;
10286 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000010287 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000010288 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010289 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010290 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010291 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010292 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000010293 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010294
Owen Anderson9f944592009-08-11 20:47:22 +000010295 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010296 // Note, we allow "void" uses (basically, uses that aren't loads or
10297 // stores), because arm allows folding a scale into many arithmetic
10298 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000010299
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010300 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000010301 if (Scale & 1) return false;
10302 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010303 }
Evan Cheng2150b922007-03-12 23:30:29 +000010304 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010305 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000010306}
10307
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010308/// isLegalICmpImmediate - Return true if the specified immediate is legal
10309/// icmp immediate, that is the target has icmp instructions which can compare
10310/// a register against the immediate without having to materialize the
10311/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000010312bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010313 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010314 if (!Subtarget->isThumb())
Chandler Carruth8a102c22012-04-06 20:10:52 +000010315 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010316 if (Subtarget->isThumb2())
Chandler Carruth8a102c22012-04-06 20:10:52 +000010317 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010318 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000010319 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010320}
10321
Andrew Tricka22cdb72012-07-18 18:34:27 +000010322/// isLegalAddImmediate - Return true if the specified immediate is a legal add
10323/// *or sub* immediate, that is the target has add or sub instructions which can
10324/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000010325/// immediate into a register.
10326bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000010327 // Same encoding for add/sub, just flip the sign.
10328 int64_t AbsImm = llvm::abs64(Imm);
10329 if (!Subtarget->isThumb())
10330 return ARM_AM::getSOImmVal(AbsImm) != -1;
10331 if (Subtarget->isThumb2())
10332 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
10333 // Thumb1 only has 8-bit unsigned immediate.
10334 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000010335}
10336
Owen Anderson53aa7a92009-08-10 22:56:29 +000010337static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010338 bool isSEXTLoad, SDValue &Base,
10339 SDValue &Offset, bool &isInc,
10340 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000010341 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10342 return false;
10343
Owen Anderson9f944592009-08-11 20:47:22 +000010344 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000010345 // AddressingMode 3
10346 Base = Ptr->getOperand(0);
10347 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010348 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010349 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010350 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010351 isInc = false;
10352 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10353 return true;
10354 }
10355 }
10356 isInc = (Ptr->getOpcode() == ISD::ADD);
10357 Offset = Ptr->getOperand(1);
10358 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000010359 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000010360 // AddressingMode 2
10361 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010362 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010363 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010364 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010365 isInc = false;
10366 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10367 Base = Ptr->getOperand(0);
10368 return true;
10369 }
10370 }
10371
10372 if (Ptr->getOpcode() == ISD::ADD) {
10373 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000010374 ARM_AM::ShiftOpc ShOpcVal=
10375 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000010376 if (ShOpcVal != ARM_AM::no_shift) {
10377 Base = Ptr->getOperand(1);
10378 Offset = Ptr->getOperand(0);
10379 } else {
10380 Base = Ptr->getOperand(0);
10381 Offset = Ptr->getOperand(1);
10382 }
10383 return true;
10384 }
10385
10386 isInc = (Ptr->getOpcode() == ISD::ADD);
10387 Base = Ptr->getOperand(0);
10388 Offset = Ptr->getOperand(1);
10389 return true;
10390 }
10391
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000010392 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000010393 return false;
10394}
10395
Owen Anderson53aa7a92009-08-10 22:56:29 +000010396static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010397 bool isSEXTLoad, SDValue &Base,
10398 SDValue &Offset, bool &isInc,
10399 SelectionDAG &DAG) {
10400 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10401 return false;
10402
10403 Base = Ptr->getOperand(0);
10404 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
10405 int RHSC = (int)RHS->getZExtValue();
10406 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
10407 assert(Ptr->getOpcode() == ISD::ADD);
10408 isInc = false;
10409 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10410 return true;
10411 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
10412 isInc = Ptr->getOpcode() == ISD::ADD;
10413 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
10414 return true;
10415 }
10416 }
10417
10418 return false;
10419}
10420
Evan Cheng10043e22007-01-19 07:51:42 +000010421/// getPreIndexedAddressParts - returns true by value, base pointer and
10422/// offset pointer and addressing mode by reference if the node's address
10423/// can be legally represented as pre-indexed load / store address.
10424bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010425ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
10426 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010427 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010428 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010429 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010430 return false;
10431
Owen Anderson53aa7a92009-08-10 22:56:29 +000010432 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010433 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010434 bool isSEXTLoad = false;
10435 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10436 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010437 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010438 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10439 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10440 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010441 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010442 } else
10443 return false;
10444
10445 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010446 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010447 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010448 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10449 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010450 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010451 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000010452 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000010453 if (!isLegal)
10454 return false;
10455
10456 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10457 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010458}
10459
10460/// getPostIndexedAddressParts - returns true by value, base pointer and
10461/// offset pointer and addressing mode by reference if this node can be
10462/// combined with a load / store to form a post-indexed load / store.
10463bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010464 SDValue &Base,
10465 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010466 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010467 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010468 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010469 return false;
10470
Owen Anderson53aa7a92009-08-10 22:56:29 +000010471 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010472 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010473 bool isSEXTLoad = false;
10474 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010475 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010476 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010477 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10478 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010479 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010480 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010481 } else
10482 return false;
10483
10484 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010485 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010486 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010487 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000010488 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010489 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010490 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10491 isInc, DAG);
10492 if (!isLegal)
10493 return false;
10494
Evan Chengf19384d2010-05-18 21:31:17 +000010495 if (Ptr != Base) {
10496 // Swap base ptr and offset to catch more post-index load / store when
10497 // it's legal. In Thumb2 mode, offset must be an immediate.
10498 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10499 !Subtarget->isThumb2())
10500 std::swap(Base, Offset);
10501
10502 // Post-indexed load / store update the base pointer.
10503 if (Ptr != Base)
10504 return false;
10505 }
10506
Evan Cheng84c6cda2009-07-02 07:28:31 +000010507 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10508 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010509}
10510
Jay Foada0653a32014-05-14 21:14:37 +000010511void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10512 APInt &KnownZero,
10513 APInt &KnownOne,
10514 const SelectionDAG &DAG,
10515 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000010516 unsigned BitWidth = KnownOne.getBitWidth();
10517 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000010518 switch (Op.getOpcode()) {
10519 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000010520 case ARMISD::ADDC:
10521 case ARMISD::ADDE:
10522 case ARMISD::SUBC:
10523 case ARMISD::SUBE:
10524 // These nodes' second result is a boolean
10525 if (Op.getResNo() == 0)
10526 break;
10527 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10528 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010529 case ARMISD::CMOV: {
10530 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000010531 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010532 if (KnownZero == 0 && KnownOne == 0) return;
10533
Dan Gohmanf990faf2008-02-13 00:35:47 +000010534 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000010535 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010536 KnownZero &= KnownZeroRHS;
10537 KnownOne &= KnownOneRHS;
10538 return;
10539 }
Tim Northover01b4aa92014-04-03 15:10:35 +000010540 case ISD::INTRINSIC_W_CHAIN: {
10541 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
10542 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
10543 switch (IntID) {
10544 default: return;
10545 case Intrinsic::arm_ldaex:
10546 case Intrinsic::arm_ldrex: {
10547 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
10548 unsigned MemBits = VT.getScalarType().getSizeInBits();
10549 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
10550 return;
10551 }
10552 }
10553 }
Evan Cheng10043e22007-01-19 07:51:42 +000010554 }
10555}
10556
10557//===----------------------------------------------------------------------===//
10558// ARM Inline Assembly Support
10559//===----------------------------------------------------------------------===//
10560
Evan Cheng078b0b02011-01-08 01:24:27 +000010561bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10562 // Looking for "rev" which is V6+.
10563 if (!Subtarget->hasV6Ops())
10564 return false;
10565
10566 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10567 std::string AsmStr = IA->getAsmString();
10568 SmallVector<StringRef, 4> AsmPieces;
10569 SplitString(AsmStr, AsmPieces, ";\n");
10570
10571 switch (AsmPieces.size()) {
10572 default: return false;
10573 case 1:
10574 AsmStr = AsmPieces[0];
10575 AsmPieces.clear();
10576 SplitString(AsmStr, AsmPieces, " \t,");
10577
10578 // rev $0, $1
10579 if (AsmPieces.size() == 3 &&
10580 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10581 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010582 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010583 if (Ty && Ty->getBitWidth() == 32)
10584 return IntrinsicLowering::LowerToByteSwap(CI);
10585 }
10586 break;
10587 }
10588
10589 return false;
10590}
10591
Evan Cheng10043e22007-01-19 07:51:42 +000010592/// getConstraintType - Given a constraint letter, return the type of
10593/// constraint it is for this target.
10594ARMTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +000010595ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
10596 if (Constraint.size() == 1) {
10597 switch (Constraint[0]) {
10598 default: break;
10599 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010600 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010601 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010602 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010603 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010604 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010605 // An address with a single base register. Due to the way we
10606 // currently handle addresses it is the same as an 'r' memory constraint.
10607 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010608 }
Eric Christophere256cd02011-06-21 22:10:57 +000010609 } else if (Constraint.size() == 2) {
10610 switch (Constraint[0]) {
10611 default: break;
10612 // All 'U+' constraints are addresses.
10613 case 'U': return C_Memory;
10614 }
Evan Cheng10043e22007-01-19 07:51:42 +000010615 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010616 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010617}
10618
John Thompsone8360b72010-10-29 17:29:13 +000010619/// Examine constraint type and operand type and determine a weight value.
10620/// This object must already have been set up with the operand type
10621/// and the current alternative constraint selected.
10622TargetLowering::ConstraintWeight
10623ARMTargetLowering::getSingleConstraintMatchWeight(
10624 AsmOperandInfo &info, const char *constraint) const {
10625 ConstraintWeight weight = CW_Invalid;
10626 Value *CallOperandVal = info.CallOperandVal;
10627 // If we don't have a value, we can't do a match,
10628 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010629 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010630 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010631 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010632 // Look at the constraint type.
10633 switch (*constraint) {
10634 default:
10635 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10636 break;
10637 case 'l':
10638 if (type->isIntegerTy()) {
10639 if (Subtarget->isThumb())
10640 weight = CW_SpecificReg;
10641 else
10642 weight = CW_Register;
10643 }
10644 break;
10645 case 'w':
10646 if (type->isFloatingPointTy())
10647 weight = CW_Register;
10648 break;
10649 }
10650 return weight;
10651}
10652
Eric Christophercf2007c2011-06-30 23:50:52 +000010653typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10654RCPair
Eric Christopher11e4df72015-02-26 22:38:43 +000010655ARMTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
10656 const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000010657 MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000010658 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010659 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000010660 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000010661 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010662 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010663 return RCPair(0U, &ARM::tGPRRegClass);
10664 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000010665 case 'h': // High regs or no regs.
10666 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010667 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000010668 break;
Chris Lattner6223e832007-04-02 17:24:08 +000010669 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000010670 if (Subtarget->isThumb1Only())
10671 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000010672 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010673 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000010674 if (VT == MVT::Other)
10675 break;
Owen Anderson9f944592009-08-11 20:47:22 +000010676 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010677 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000010678 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010679 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000010680 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010681 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010682 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010683 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000010684 if (VT == MVT::Other)
10685 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010686 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010687 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010688 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010689 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010690 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010691 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010692 break;
Eric Christopherc011d312011-07-01 00:30:46 +000010693 case 't':
10694 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010695 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000010696 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010697 }
10698 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000010699 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000010700 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000010701
Eric Christopher11e4df72015-02-26 22:38:43 +000010702 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Evan Cheng10043e22007-01-19 07:51:42 +000010703}
10704
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010705/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10706/// vector. If it is invalid, don't add anything to Ops.
10707void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010708 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010709 std::vector<SDValue>&Ops,
10710 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000010711 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010712
Eric Christopherde9399b2011-06-02 23:16:42 +000010713 // Currently only support length 1 constraints.
10714 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010715
Eric Christopherde9399b2011-06-02 23:16:42 +000010716 char ConstraintLetter = Constraint[0];
10717 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010718 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000010719 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010720 case 'I': case 'J': case 'K': case 'L':
10721 case 'M': case 'N': case 'O':
10722 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10723 if (!C)
10724 return;
10725
10726 int64_t CVal64 = C->getSExtValue();
10727 int CVal = (int) CVal64;
10728 // None of these constraints allow values larger than 32 bits. Check
10729 // that the value fits in an int.
10730 if (CVal != CVal64)
10731 return;
10732
Eric Christopherde9399b2011-06-02 23:16:42 +000010733 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000010734 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000010735 // Constant suitable for movw, must be between 0 and
10736 // 65535.
10737 if (Subtarget->hasV6T2Ops())
10738 if (CVal >= 0 && CVal <= 65535)
10739 break;
10740 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010741 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000010742 if (Subtarget->isThumb1Only()) {
10743 // This must be a constant between 0 and 255, for ADD
10744 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010745 if (CVal >= 0 && CVal <= 255)
10746 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010747 } else if (Subtarget->isThumb2()) {
10748 // A constant that can be used as an immediate value in a
10749 // data-processing instruction.
10750 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10751 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010752 } else {
10753 // A constant that can be used as an immediate value in a
10754 // data-processing instruction.
10755 if (ARM_AM::getSOImmVal(CVal) != -1)
10756 break;
10757 }
10758 return;
10759
10760 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000010761 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010762 // This must be a constant between -255 and -1, for negated ADD
10763 // immediates. This can be used in GCC with an "n" modifier that
10764 // prints the negated value, for use with SUB instructions. It is
10765 // not useful otherwise but is implemented for compatibility.
10766 if (CVal >= -255 && CVal <= -1)
10767 break;
10768 } else {
10769 // This must be a constant between -4095 and 4095. It is not clear
10770 // what this constraint is intended for. Implemented for
10771 // compatibility with GCC.
10772 if (CVal >= -4095 && CVal <= 4095)
10773 break;
10774 }
10775 return;
10776
10777 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000010778 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010779 // A 32-bit value where only one byte has a nonzero value. Exclude
10780 // zero to match GCC. This constraint is used by GCC internally for
10781 // constants that can be loaded with a move/shift combination.
10782 // It is not useful otherwise but is implemented for compatibility.
10783 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10784 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010785 } else if (Subtarget->isThumb2()) {
10786 // A constant whose bitwise inverse can be used as an immediate
10787 // value in a data-processing instruction. This can be used in GCC
10788 // with a "B" modifier that prints the inverted value, for use with
10789 // BIC and MVN instructions. It is not useful otherwise but is
10790 // implemented for compatibility.
10791 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10792 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010793 } else {
10794 // A constant whose bitwise inverse can be used as an immediate
10795 // value in a data-processing instruction. This can be used in GCC
10796 // with a "B" modifier that prints the inverted value, for use with
10797 // BIC and MVN instructions. It is not useful otherwise but is
10798 // implemented for compatibility.
10799 if (ARM_AM::getSOImmVal(~CVal) != -1)
10800 break;
10801 }
10802 return;
10803
10804 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000010805 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010806 // This must be a constant between -7 and 7,
10807 // for 3-operand ADD/SUB immediate instructions.
10808 if (CVal >= -7 && CVal < 7)
10809 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010810 } else if (Subtarget->isThumb2()) {
10811 // A constant whose negation can be used as an immediate value in a
10812 // data-processing instruction. This can be used in GCC with an "n"
10813 // modifier that prints the negated value, for use with SUB
10814 // instructions. It is not useful otherwise but is implemented for
10815 // compatibility.
10816 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10817 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010818 } else {
10819 // A constant whose negation can be used as an immediate value in a
10820 // data-processing instruction. This can be used in GCC with an "n"
10821 // modifier that prints the negated value, for use with SUB
10822 // instructions. It is not useful otherwise but is implemented for
10823 // compatibility.
10824 if (ARM_AM::getSOImmVal(-CVal) != -1)
10825 break;
10826 }
10827 return;
10828
10829 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000010830 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010831 // This must be a multiple of 4 between 0 and 1020, for
10832 // ADD sp + immediate.
10833 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10834 break;
10835 } else {
10836 // A power of two or a constant between 0 and 32. This is used in
10837 // GCC for the shift amount on shifted register operands, but it is
10838 // useful in general for any shift amounts.
10839 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10840 break;
10841 }
10842 return;
10843
10844 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000010845 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010846 // This must be a constant between 0 and 31, for shift amounts.
10847 if (CVal >= 0 && CVal <= 31)
10848 break;
10849 }
10850 return;
10851
10852 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000010853 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010854 // This must be a multiple of 4 between -508 and 508, for
10855 // ADD/SUB sp = sp + immediate.
10856 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10857 break;
10858 }
10859 return;
10860 }
10861 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10862 break;
10863 }
10864
10865 if (Result.getNode()) {
10866 Ops.push_back(Result);
10867 return;
10868 }
Dale Johannesence97d552010-06-25 21:55:36 +000010869 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010870}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010871
Renato Golin87610692013-07-16 09:32:17 +000010872SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
10873 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only");
10874 unsigned Opcode = Op->getOpcode();
10875 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000010876 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000010877 bool isSigned = (Opcode == ISD::SDIVREM);
10878 EVT VT = Op->getValueType(0);
10879 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
10880
10881 RTLIB::Libcall LC;
10882 switch (VT.getSimpleVT().SimpleTy) {
10883 default: llvm_unreachable("Unexpected request for libcall!");
Saleem Abdulrasool740be892014-08-17 22:50:59 +000010884 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
10885 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
10886 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
10887 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
Renato Golin87610692013-07-16 09:32:17 +000010888 }
10889
10890 SDValue InChain = DAG.getEntryNode();
10891
10892 TargetLowering::ArgListTy Args;
10893 TargetLowering::ArgListEntry Entry;
10894 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
10895 EVT ArgVT = Op->getOperand(i).getValueType();
10896 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
10897 Entry.Node = Op->getOperand(i);
10898 Entry.Ty = ArgTy;
10899 Entry.isSExt = isSigned;
10900 Entry.isZExt = !isSigned;
10901 Args.push_back(Entry);
10902 }
10903
10904 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
10905 getPointerTy());
10906
Reid Kleckner343c3952014-11-20 23:51:47 +000010907 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000010908
10909 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010910 TargetLowering::CallLoweringInfo CLI(DAG);
10911 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000010912 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010913 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000010914
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010915 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000010916 return CallInfo.first;
10917}
10918
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000010919SDValue
10920ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
10921 assert(Subtarget->isTargetWindows() && "unsupported target platform");
10922 SDLoc DL(Op);
10923
10924 // Get the inputs.
10925 SDValue Chain = Op.getOperand(0);
10926 SDValue Size = Op.getOperand(1);
10927
10928 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
10929 DAG.getConstant(2, MVT::i32));
10930
10931 SDValue Flag;
10932 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
10933 Flag = Chain.getValue(1);
10934
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000010935 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000010936 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
10937
10938 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
10939 Chain = NewSP.getValue(1);
10940
10941 SDValue Ops[2] = { NewSP, Chain };
10942 return DAG.getMergeValues(Ops, DL);
10943}
10944
Oliver Stannard51b1d462014-08-21 12:50:31 +000010945SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
10946 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
10947 "Unexpected type for custom-lowering FP_EXTEND");
10948
10949 RTLIB::Libcall LC;
10950 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
10951
10952 SDValue SrcVal = Op.getOperand(0);
10953 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
10954 /*isSigned*/ false, SDLoc(Op)).first;
10955}
10956
10957SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
10958 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
10959 Subtarget->isFPOnlySP() &&
10960 "Unexpected type for custom-lowering FP_ROUND");
10961
10962 RTLIB::Libcall LC;
10963 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
10964
10965 SDValue SrcVal = Op.getOperand(0);
10966 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
10967 /*isSigned*/ false, SDLoc(Op)).first;
10968}
10969
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010970bool
10971ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10972 // The ARM target isn't yet aware of offsets.
10973 return false;
10974}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010975
Jim Grosbach11013ed2010-07-16 23:05:05 +000010976bool ARM::isBitFieldInvertedMask(unsigned v) {
10977 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010978 return false;
10979
Jim Grosbach11013ed2010-07-16 23:05:05 +000010980 // there can be 1's on either or both "outsides", all the "inside"
10981 // bits must be 0's
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000010982 return isShiftedMask_32(~v);
Jim Grosbach11013ed2010-07-16 23:05:05 +000010983}
10984
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010985/// isFPImmLegal - Returns true if the target can instruction select the
10986/// specified FP immediate natively. If false, the legalizer will
10987/// materialize the FP immediate as a load from a constant pool.
10988bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10989 if (!Subtarget->hasVFP3())
10990 return false;
10991 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010992 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000010993 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000010994 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010995 return false;
10996}
Bob Wilson5549d492010-09-21 17:56:22 +000010997
Wesley Peck527da1b2010-11-23 03:31:01 +000010998/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000010999/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
11000/// specified in the intrinsic calls.
11001bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
11002 const CallInst &I,
11003 unsigned Intrinsic) const {
11004 switch (Intrinsic) {
11005 case Intrinsic::arm_neon_vld1:
11006 case Intrinsic::arm_neon_vld2:
11007 case Intrinsic::arm_neon_vld3:
11008 case Intrinsic::arm_neon_vld4:
11009 case Intrinsic::arm_neon_vld2lane:
11010 case Intrinsic::arm_neon_vld3lane:
11011 case Intrinsic::arm_neon_vld4lane: {
11012 Info.opc = ISD::INTRINSIC_W_CHAIN;
11013 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmowcdfe20b2012-10-08 16:38:25 +000011014 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000011015 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11016 Info.ptrVal = I.getArgOperand(0);
11017 Info.offset = 0;
11018 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11019 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11020 Info.vol = false; // volatile loads with NEON intrinsics not supported
11021 Info.readMem = true;
11022 Info.writeMem = false;
11023 return true;
11024 }
11025 case Intrinsic::arm_neon_vst1:
11026 case Intrinsic::arm_neon_vst2:
11027 case Intrinsic::arm_neon_vst3:
11028 case Intrinsic::arm_neon_vst4:
11029 case Intrinsic::arm_neon_vst2lane:
11030 case Intrinsic::arm_neon_vst3lane:
11031 case Intrinsic::arm_neon_vst4lane: {
11032 Info.opc = ISD::INTRINSIC_VOID;
11033 // Conservatively set memVT to the entire set of vectors stored.
11034 unsigned NumElts = 0;
11035 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000011036 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000011037 if (!ArgTy->isVectorTy())
11038 break;
Micah Villmowcdfe20b2012-10-08 16:38:25 +000011039 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000011040 }
11041 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11042 Info.ptrVal = I.getArgOperand(0);
11043 Info.offset = 0;
11044 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11045 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11046 Info.vol = false; // volatile stores with NEON intrinsics not supported
11047 Info.readMem = false;
11048 Info.writeMem = true;
11049 return true;
11050 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011051 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011052 case Intrinsic::arm_ldrex: {
11053 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
11054 Info.opc = ISD::INTRINSIC_W_CHAIN;
11055 Info.memVT = MVT::getVT(PtrTy->getElementType());
11056 Info.ptrVal = I.getArgOperand(0);
11057 Info.offset = 0;
11058 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
11059 Info.vol = true;
11060 Info.readMem = true;
11061 Info.writeMem = false;
11062 return true;
11063 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011064 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011065 case Intrinsic::arm_strex: {
11066 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
11067 Info.opc = ISD::INTRINSIC_W_CHAIN;
11068 Info.memVT = MVT::getVT(PtrTy->getElementType());
11069 Info.ptrVal = I.getArgOperand(1);
11070 Info.offset = 0;
11071 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
11072 Info.vol = true;
11073 Info.readMem = false;
11074 Info.writeMem = true;
11075 return true;
11076 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011077 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011078 case Intrinsic::arm_strexd: {
11079 Info.opc = ISD::INTRINSIC_W_CHAIN;
11080 Info.memVT = MVT::i64;
11081 Info.ptrVal = I.getArgOperand(2);
11082 Info.offset = 0;
11083 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011084 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011085 Info.readMem = false;
11086 Info.writeMem = true;
11087 return true;
11088 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011089 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011090 case Intrinsic::arm_ldrexd: {
11091 Info.opc = ISD::INTRINSIC_W_CHAIN;
11092 Info.memVT = MVT::i64;
11093 Info.ptrVal = I.getArgOperand(0);
11094 Info.offset = 0;
11095 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011096 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011097 Info.readMem = true;
11098 Info.writeMem = false;
11099 return true;
11100 }
Bob Wilson5549d492010-09-21 17:56:22 +000011101 default:
11102 break;
11103 }
11104
11105 return false;
11106}
Juergen Ributzka659ce002014-01-28 01:20:14 +000011107
11108/// \brief Returns true if it is beneficial to convert a load of a constant
11109/// to just the constant itself.
11110bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
11111 Type *Ty) const {
11112 assert(Ty->isIntegerTy());
11113
11114 unsigned Bits = Ty->getPrimitiveSizeInBits();
11115 if (Bits == 0 || Bits > 32)
11116 return false;
11117 return true;
11118}
Tim Northover037f26f22014-04-17 18:22:47 +000011119
Robin Morisset25c8e312014-09-17 00:06:58 +000011120bool ARMTargetLowering::hasLoadLinkedStoreConditional() const { return true; }
11121
Robin Morisset5349e8e2014-09-18 18:56:04 +000011122Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
11123 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000011124 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000011125
11126 // First, if the target has no DMB, see what fallback we can use.
11127 if (!Subtarget->hasDataBarrier()) {
11128 // Some ARMv6 cpus can support data barriers with an mcr instruction.
11129 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
11130 // here.
11131 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
11132 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
11133 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
11134 Builder.getInt32(0), Builder.getInt32(7),
11135 Builder.getInt32(10), Builder.getInt32(5)};
11136 return Builder.CreateCall(MCR, args);
11137 } else {
11138 // Instead of using barriers, atomic accesses on these subtargets use
11139 // libcalls.
11140 llvm_unreachable("makeDMB on a target so old that it has no barriers");
11141 }
11142 } else {
11143 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
11144 // Only a full system barrier exists in the M-class architectures.
11145 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
11146 Constant *CDomain = Builder.getInt32(Domain);
11147 return Builder.CreateCall(DMB, CDomain);
11148 }
Robin Morisseta47cb412014-09-03 21:01:03 +000011149}
11150
11151// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000011152Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011153 AtomicOrdering Ord, bool IsStore,
11154 bool IsLoad) const {
11155 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011156 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011157
11158 switch (Ord) {
11159 case NotAtomic:
11160 case Unordered:
11161 llvm_unreachable("Invalid fence: unordered/non-atomic");
11162 case Monotonic:
11163 case Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000011164 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011165 case SequentiallyConsistent:
11166 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000011167 return nullptr; // Nothing to do
11168 /*FALLTHROUGH*/
Robin Morisseta47cb412014-09-03 21:01:03 +000011169 case Release:
11170 case AcquireRelease:
11171 if (Subtarget->isSwift())
Robin Morissetdedef332014-09-23 20:31:14 +000011172 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000011173 // FIXME: add a comment with a link to documentation justifying this.
11174 else
Robin Morissetdedef332014-09-23 20:31:14 +000011175 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011176 }
Robin Morissetdedef332014-09-23 20:31:14 +000011177 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011178}
11179
Robin Morissetdedef332014-09-23 20:31:14 +000011180Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011181 AtomicOrdering Ord, bool IsStore,
11182 bool IsLoad) const {
11183 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011184 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011185
11186 switch (Ord) {
11187 case NotAtomic:
11188 case Unordered:
11189 llvm_unreachable("Invalid fence: unordered/not-atomic");
11190 case Monotonic:
11191 case Release:
Robin Morissetdedef332014-09-23 20:31:14 +000011192 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011193 case Acquire:
11194 case AcquireRelease:
Robin Morissetdedef332014-09-23 20:31:14 +000011195 case SequentiallyConsistent:
11196 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011197 }
Robin Morissetdedef332014-09-23 20:31:14 +000011198 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011199}
11200
Robin Morisseted3d48f2014-09-03 21:29:59 +000011201// Loads and stores less than 64-bits are already atomic; ones above that
11202// are doomed anyway, so defer to the default libcall and blame the OS when
11203// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11204// anything for those.
11205bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
11206 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
11207 return (Size == 64) && !Subtarget->isMClass();
11208}
Tim Northover037f26f22014-04-17 18:22:47 +000011209
Robin Morisseted3d48f2014-09-03 21:29:59 +000011210// Loads and stores less than 64-bits are already atomic; ones above that
11211// are doomed anyway, so defer to the default libcall and blame the OS when
11212// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11213// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000011214// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
11215// guarantee, see DDI0406C ARM architecture reference manual,
11216// sections A8.8.72-74 LDRD)
Robin Morisseted3d48f2014-09-03 21:29:59 +000011217bool ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
11218 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
11219 return (Size == 64) && !Subtarget->isMClass();
11220}
11221
11222// For the real atomic operations, we have ldrex/strex up to 32 bits,
11223// and up to 64 bits on the non-M profiles
JF Bastienf14889e2015-03-04 15:47:57 +000011224TargetLoweringBase::AtomicRMWExpansionKind
11225ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000011226 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
JF Bastienf14889e2015-03-04 15:47:57 +000011227 return (Size <= (Subtarget->isMClass() ? 32U : 64U))
11228 ? AtomicRMWExpansionKind::LLSC
11229 : AtomicRMWExpansionKind::None;
Tim Northover037f26f22014-04-17 18:22:47 +000011230}
11231
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011232// This has so far only been implemented for MachO.
11233bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000011234 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011235}
11236
Quentin Colombetc32615d2014-10-31 17:52:53 +000011237bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
11238 unsigned &Cost) const {
11239 // If we do not have NEON, vector types are not natively supported.
11240 if (!Subtarget->hasNEON())
11241 return false;
11242
11243 // Floating point values and vector values map to the same register file.
11244 // Therefore, althought we could do a store extract of a vector type, this is
11245 // better to leave at float as we have more freedom in the addressing mode for
11246 // those.
11247 if (VectorTy->isFPOrFPVectorTy())
11248 return false;
11249
11250 // If the index is unknown at compile time, this is very expensive to lower
11251 // and it is not possible to combine the store with the extract.
11252 if (!isa<ConstantInt>(Idx))
11253 return false;
11254
11255 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
11256 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
11257 // We can do a store + vector extract on any vector that fits perfectly in a D
11258 // or Q register.
11259 if (BitWidth == 64 || BitWidth == 128) {
11260 Cost = 0;
11261 return true;
11262 }
11263 return false;
11264}
11265
Tim Northover037f26f22014-04-17 18:22:47 +000011266Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
11267 AtomicOrdering Ord) const {
11268 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
11269 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
Robin Morissetb155f522014-08-18 16:48:58 +000011270 bool IsAcquire = isAtLeastAcquire(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011271
11272 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
11273 // intrinsic must return {i32, i32} and we have to recombine them into a
11274 // single i64 here.
11275 if (ValTy->getPrimitiveSizeInBits() == 64) {
11276 Intrinsic::ID Int =
11277 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
11278 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
11279
11280 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11281 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
11282
11283 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
11284 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011285 if (!Subtarget->isLittle())
11286 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011287 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
11288 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
11289 return Builder.CreateOr(
11290 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
11291 }
11292
11293 Type *Tys[] = { Addr->getType() };
11294 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
11295 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
11296
11297 return Builder.CreateTruncOrBitCast(
11298 Builder.CreateCall(Ldrex, Addr),
11299 cast<PointerType>(Addr->getType())->getElementType());
11300}
11301
11302Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
11303 Value *Addr,
11304 AtomicOrdering Ord) const {
11305 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morissetb155f522014-08-18 16:48:58 +000011306 bool IsRelease = isAtLeastRelease(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011307
11308 // Since the intrinsics must have legal type, the i64 intrinsics take two
11309 // parameters: "i32, i32". We must marshal Val into the appropriate form
11310 // before the call.
11311 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
11312 Intrinsic::ID Int =
11313 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
11314 Function *Strex = Intrinsic::getDeclaration(M, Int);
11315 Type *Int32Ty = Type::getInt32Ty(M->getContext());
11316
11317 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
11318 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011319 if (!Subtarget->isLittle())
11320 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011321 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11322 return Builder.CreateCall3(Strex, Lo, Hi, Addr);
11323 }
11324
11325 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
11326 Type *Tys[] = { Addr->getType() };
11327 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
11328
11329 return Builder.CreateCall2(
11330 Strex, Builder.CreateZExtOrBitCast(
11331 Val, Strex->getFunctionType()->getParamType(0)),
11332 Addr);
11333}
Oliver Stannardc24f2172014-05-09 14:01:47 +000011334
11335enum HABaseType {
11336 HA_UNKNOWN = 0,
11337 HA_FLOAT,
11338 HA_DOUBLE,
11339 HA_VECT64,
11340 HA_VECT128
11341};
11342
11343static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
11344 uint64_t &Members) {
11345 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
11346 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
11347 uint64_t SubMembers = 0;
11348 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
11349 return false;
11350 Members += SubMembers;
11351 }
11352 } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
11353 uint64_t SubMembers = 0;
11354 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
11355 return false;
11356 Members += SubMembers * AT->getNumElements();
11357 } else if (Ty->isFloatTy()) {
11358 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
11359 return false;
11360 Members = 1;
11361 Base = HA_FLOAT;
11362 } else if (Ty->isDoubleTy()) {
11363 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
11364 return false;
11365 Members = 1;
11366 Base = HA_DOUBLE;
11367 } else if (const VectorType *VT = dyn_cast<VectorType>(Ty)) {
11368 Members = 1;
11369 switch (Base) {
11370 case HA_FLOAT:
11371 case HA_DOUBLE:
11372 return false;
11373 case HA_VECT64:
11374 return VT->getBitWidth() == 64;
11375 case HA_VECT128:
11376 return VT->getBitWidth() == 128;
11377 case HA_UNKNOWN:
11378 switch (VT->getBitWidth()) {
11379 case 64:
11380 Base = HA_VECT64;
11381 return true;
11382 case 128:
11383 Base = HA_VECT128;
11384 return true;
11385 default:
11386 return false;
11387 }
11388 }
11389 }
11390
11391 return (Members > 0 && Members <= 4);
11392}
11393
Tim Northovere95c5b32015-02-24 17:22:34 +000011394/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of
11395/// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when
11396/// passing according to AAPCS rules.
Oliver Stannardc24f2172014-05-09 14:01:47 +000011397bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
11398 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000011399 if (getEffectiveCallingConv(CallConv, isVarArg) !=
11400 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000011401 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000011402
11403 HABaseType Base = HA_UNKNOWN;
11404 uint64_t Members = 0;
Tim Northovere95c5b32015-02-24 17:22:34 +000011405 bool IsHA = isHomogeneousAggregate(Ty, Base, Members);
11406 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump());
11407
11408 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy();
11409 return IsHA || IsIntArray;
Oliver Stannardc24f2172014-05-09 14:01:47 +000011410}