blob: d3168617d867476272edcd9f914fc91520a3df38 [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.
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000572 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16,
573 MVT::v2i32}) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000574 for (MVT VT : MVT::integer_vector_valuetypes()) {
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000575 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal);
576 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal);
577 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000578 }
James Molloy547d4c02012-02-20 09:24:05 +0000579 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000580 }
581
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000582 // ARM and Thumb2 support UMLAL/SMLAL.
583 if (!Subtarget->isThumb1Only())
584 setTargetDAGCombine(ISD::ADDC);
585
Oliver Stannard51b1d462014-08-21 12:50:31 +0000586 if (Subtarget->isFPOnlySP()) {
587 // When targetting a floating-point unit with only single-precision
588 // operations, f64 is legal for the few double-precision instructions which
589 // are present However, no double-precision operations other than moves,
590 // loads and stores are provided by the hardware.
591 setOperationAction(ISD::FADD, MVT::f64, Expand);
592 setOperationAction(ISD::FSUB, MVT::f64, Expand);
593 setOperationAction(ISD::FMUL, MVT::f64, Expand);
594 setOperationAction(ISD::FMA, MVT::f64, Expand);
595 setOperationAction(ISD::FDIV, MVT::f64, Expand);
596 setOperationAction(ISD::FREM, MVT::f64, Expand);
597 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
598 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
599 setOperationAction(ISD::FNEG, MVT::f64, Expand);
600 setOperationAction(ISD::FABS, MVT::f64, Expand);
601 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
602 setOperationAction(ISD::FSIN, MVT::f64, Expand);
603 setOperationAction(ISD::FCOS, MVT::f64, Expand);
604 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
605 setOperationAction(ISD::FPOW, MVT::f64, Expand);
606 setOperationAction(ISD::FLOG, MVT::f64, Expand);
607 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
608 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
609 setOperationAction(ISD::FEXP, MVT::f64, Expand);
610 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
611 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
612 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
613 setOperationAction(ISD::FRINT, MVT::f64, Expand);
614 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
615 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
616 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
617 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
618 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000619
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000620 computeRegisterProperties(Subtarget->getRegisterInfo());
Evan Cheng10043e22007-01-19 07:51:42 +0000621
Tim Northover4e80b582014-07-18 13:01:19 +0000622 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000623 for (MVT VT : MVT::fp_valuetypes()) {
624 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
625 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
626 }
Tim Northover4e80b582014-07-18 13:01:19 +0000627
628 // ... or truncating stores
629 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
630 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
631 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000632
Duncan Sands95d46ef2008-01-23 20:39:46 +0000633 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000634 for (MVT VT : MVT::integer_valuetypes())
635 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000636
Evan Cheng10043e22007-01-19 07:51:42 +0000637 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000638 if (!Subtarget->isThumb1Only()) {
639 for (unsigned im = (unsigned)ISD::PRE_INC;
640 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000641 setIndexedLoadAction(im, MVT::i1, Legal);
642 setIndexedLoadAction(im, MVT::i8, Legal);
643 setIndexedLoadAction(im, MVT::i16, Legal);
644 setIndexedLoadAction(im, MVT::i32, Legal);
645 setIndexedStoreAction(im, MVT::i1, Legal);
646 setIndexedStoreAction(im, MVT::i8, Legal);
647 setIndexedStoreAction(im, MVT::i16, Legal);
648 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000649 }
Evan Cheng10043e22007-01-19 07:51:42 +0000650 }
651
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000652 setOperationAction(ISD::SADDO, MVT::i32, Custom);
653 setOperationAction(ISD::UADDO, MVT::i32, Custom);
654 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
655 setOperationAction(ISD::USUBO, MVT::i32, Custom);
656
Evan Cheng10043e22007-01-19 07:51:42 +0000657 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000658 setOperationAction(ISD::MUL, MVT::i64, Expand);
659 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000660 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000661 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
662 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000663 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000664 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
665 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000666 setOperationAction(ISD::MULHS, MVT::i32, Expand);
667
Jim Grosbach5d994042009-10-31 19:38:01 +0000668 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000669 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000670 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000671 setOperationAction(ISD::SRL, MVT::i64, Custom);
672 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000673
Evan Chenge8916542011-08-30 01:34:54 +0000674 if (!Subtarget->isThumb1Only()) {
675 // FIXME: We should do this for Thumb1 as well.
676 setOperationAction(ISD::ADDC, MVT::i32, Custom);
677 setOperationAction(ISD::ADDE, MVT::i32, Custom);
678 setOperationAction(ISD::SUBC, MVT::i32, Custom);
679 setOperationAction(ISD::SUBE, MVT::i32, Custom);
680 }
681
Evan Cheng10043e22007-01-19 07:51:42 +0000682 // ARM does not have ROTL.
Owen Anderson9f944592009-08-11 20:47:22 +0000683 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach8546ec92010-01-18 19:58:49 +0000684 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000685 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000686 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000687 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000688
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000689 // These just redirect to CTTZ and CTLZ on ARM.
690 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
691 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
692
Tim Northoverbc933082013-05-23 19:11:20 +0000693 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
694
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000695 // Only ARMv6 has BSWAP.
696 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000697 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000698
Bob Wilsone8a549c2012-09-29 21:43:49 +0000699 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
700 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
701 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbach92d999002010-05-05 20:44:35 +0000702 setOperationAction(ISD::SDIV, MVT::i32, Expand);
703 setOperationAction(ISD::UDIV, MVT::i32, Expand);
704 }
Renato Golin87610692013-07-16 09:32:17 +0000705
706 // FIXME: Also set divmod for SREM on EABI
Chad Rosierad7c9102014-08-23 18:29:43 +0000707 setOperationAction(ISD::SREM, MVT::i32, Expand);
708 setOperationAction(ISD::UREM, MVT::i32, Expand);
709 // Register based DivRem for AEABI (RTABI 4.2)
710 if (Subtarget->isTargetAEABI()) {
711 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
712 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
713 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
714 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
715 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
716 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
717 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
718 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
719
720 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
721 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
722 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
723 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
724 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
725 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
726 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
727 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
728
729 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
730 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
731 } else {
Renato Golin87610692013-07-16 09:32:17 +0000732 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
733 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
734 }
Bob Wilson7117a912009-03-20 22:42:55 +0000735
Owen Anderson9f944592009-08-11 20:47:22 +0000736 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
737 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
738 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
739 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000740 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000741
Evan Cheng74d92c12011-04-08 21:37:21 +0000742 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000743
Evan Cheng10043e22007-01-19 07:51:42 +0000744 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000745 setOperationAction(ISD::VASTART, MVT::Other, Custom);
746 setOperationAction(ISD::VAARG, MVT::Other, Expand);
747 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
748 setOperationAction(ISD::VAEND, MVT::Other, Expand);
749 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
750 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000751
Tim Northoverd6a729b2014-01-06 14:28:05 +0000752 if (!Subtarget->isTargetMachO()) {
753 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000754 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000755 setExceptionPointerRegister(ARM::R0);
756 setExceptionSelectorRegister(ARM::R1);
757 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000758
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000759 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
760 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
761 else
762 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
763
Evan Cheng6e809de2010-08-11 06:22:01 +0000764 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000765 // the default expansion. If we are targeting a single threaded system,
766 // then set them all for expand so we can lower them later into their
767 // non-atomic form.
768 if (TM.Options.ThreadModel == ThreadModel::Single)
769 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
770 else if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000771 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
772 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000773 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000774
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000775 // On v8, we have particularly efficient implementations of atomic fences
776 // if they can be combined with nearby atomic loads and stores.
777 if (!Subtarget->hasV8Ops()) {
Robin Morissetd18cda62014-08-15 22:17:28 +0000778 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000779 setInsertFencesForAtomic(true);
780 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000781 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000782 // If there's anything we can use as a barrier, go through custom lowering
783 // for ATOMIC_FENCE.
784 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
785 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
786
Jim Grosbach6860bb72010-06-18 22:35:32 +0000787 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000788 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000789 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000790 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000791 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000792 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000793 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000794 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000795 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000796 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000797 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000798 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000799 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000800 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
801 // Unordered/Monotonic case.
802 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
803 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000804 }
Evan Cheng10043e22007-01-19 07:51:42 +0000805
Evan Cheng21acf9f2010-11-04 05:19:35 +0000806 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000807
Eli Friedman8cfa7712010-06-26 04:36:50 +0000808 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
809 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000810 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
811 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000812 }
Owen Anderson9f944592009-08-11 20:47:22 +0000813 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000814
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000815 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
816 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000817 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000818 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000819 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000820 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
821 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000822
823 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000824 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbach31984832010-07-07 00:07:57 +0000825 if (Subtarget->isTargetDarwin()) {
826 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
827 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall7d84ece2011-05-29 19:50:32 +0000828 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbach31984832010-07-07 00:07:57 +0000829 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000830
Owen Anderson9f944592009-08-11 20:47:22 +0000831 setOperationAction(ISD::SETCC, MVT::i32, Expand);
832 setOperationAction(ISD::SETCC, MVT::f32, Expand);
833 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000834 setOperationAction(ISD::SELECT, MVT::i32, Custom);
835 setOperationAction(ISD::SELECT, MVT::f32, Custom);
836 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000837 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
838 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
839 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000840
Owen Anderson9f944592009-08-11 20:47:22 +0000841 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
842 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
843 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
844 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
845 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000846
Dan Gohman482732a2007-10-11 23:21:31 +0000847 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000848 setOperationAction(ISD::FSIN, MVT::f64, Expand);
849 setOperationAction(ISD::FSIN, MVT::f32, Expand);
850 setOperationAction(ISD::FCOS, MVT::f32, Expand);
851 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000852 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
853 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000854 setOperationAction(ISD::FREM, MVT::f64, Expand);
855 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000856 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
857 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000858 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
859 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000860 }
Owen Anderson9f944592009-08-11 20:47:22 +0000861 setOperationAction(ISD::FPOW, MVT::f64, Expand);
862 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000863
Evan Chengd0007f32012-04-10 21:40:28 +0000864 if (!Subtarget->hasVFP4()) {
865 setOperationAction(ISD::FMA, MVT::f64, Expand);
866 setOperationAction(ISD::FMA, MVT::f32, Expand);
867 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000868
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000869 // Various VFP goodness
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000870 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilsone4191e72010-03-19 22:51:32 +0000871 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
872 if (Subtarget->hasVFP2()) {
873 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
874 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
875 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
876 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
877 }
Tim Northover53f3bcf2014-07-17 11:27:04 +0000878
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000879 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
880 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
Tim Northover53f3bcf2014-07-17 11:27:04 +0000881 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
882 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
883 }
884
885 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000886 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +0000887 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
888 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000889 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000890 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000891
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000892 // Combine sin / cos into one node or libcall if possible.
893 if (Subtarget->hasSinCos()) {
894 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
895 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Bob Wilson9868d712014-10-09 05:43:30 +0000896 if (Subtarget->getTargetTriple().isiOS()) {
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000897 // For iOS, we don't want to the normal expansion of a libcall to
898 // sincos. We want to issue a libcall to __sincos_stret.
899 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
900 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
901 }
902 }
Evan Cheng10043e22007-01-19 07:51:42 +0000903
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000904 // FP-ARMv8 implements a lot of rounding-like FP operations.
905 if (Subtarget->hasFPARMv8()) {
906 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
907 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
908 setOperationAction(ISD::FROUND, MVT::f32, Legal);
909 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
910 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
911 setOperationAction(ISD::FRINT, MVT::f32, Legal);
912 if (!Subtarget->isFPOnlySP()) {
913 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
914 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
915 setOperationAction(ISD::FROUND, MVT::f64, Legal);
916 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
917 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
918 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Chad Rosierb1bbf6f2014-08-15 21:38:16 +0000919 }
920 }
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000921 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000922 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000923 setTargetDAGCombine(ISD::ADD);
924 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000925 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000926 setTargetDAGCombine(ISD::AND);
927 setTargetDAGCombine(ISD::OR);
928 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000929
Evan Chengf258a152012-02-23 02:58:19 +0000930 if (Subtarget->hasV6Ops())
931 setTargetDAGCombine(ISD::SRL);
932
Evan Cheng10043e22007-01-19 07:51:42 +0000933 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +0000934
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000935 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
936 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +0000937 setSchedulingPreference(Sched::RegPressure);
938 else
939 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +0000940
Evan Cheng3ae2b792011-01-06 06:52:41 +0000941 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000942 MaxStoresPerMemset = 8;
943 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
944 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
945 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
946 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
947 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengb71233f2010-06-26 01:52:05 +0000948
Rafael Espindolaa76eccf2010-07-11 04:01:49 +0000949 // On ARM arguments smaller than 4 bytes are extended, so all arguments
950 // are at least 4 bytes aligned.
951 setMinStackArgumentAlignment(4);
952
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000953 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000954 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000955
Eli Friedman2518f832011-05-06 20:34:06 +0000956 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +0000957}
958
Andrew Trick43f25632011-01-19 02:35:27 +0000959// FIXME: It might make sense to define the representative register class as the
960// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
961// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
962// SPR's representative would be DPR_VFP2. This should work well if register
963// pressure tracking were modified such that a register use would increment the
964// pressure of the register class's representative and all of it's super
965// classes' representatives transitively. We have not implemented this because
966// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000967// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +0000968// and extractions.
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000969std::pair<const TargetRegisterClass *, uint8_t>
970ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
971 MVT VT) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000972 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +0000973 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000974 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +0000975 default:
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000976 return TargetLowering::findRepresentativeClass(TRI, VT);
Evan Cheng28590382010-07-21 23:53:58 +0000977 // Use DPR as representative register class for all floating point
978 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
979 // the cost is 1 for both f32 and f64.
980 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +0000981 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +0000982 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +0000983 // When NEON is used for SP, only half of the register file is available
984 // because operations that define both SP and DP results will be constrained
985 // to the VFP2 class (D0-D15). We currently model this constraint prior to
986 // coalescing by double-counting the SP regs. See the FIXME above.
987 if (Subtarget->useNEONForSinglePrecisionFP())
988 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000989 break;
990 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
991 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +0000992 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000993 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000994 break;
995 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000996 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000997 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +0000998 break;
999 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001000 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001001 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001002 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001003 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001004 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001005}
1006
Evan Cheng10043e22007-01-19 07:51:42 +00001007const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
1008 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001009 default: return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001010 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001011 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001012 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
1013 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001014 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001015 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
1016 case ARMISD::tCALL: return "ARMISD::tCALL";
1017 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1018 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001019 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001020 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001021 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001022 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1023 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001024 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001025 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001026 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1027 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001028 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001029 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001030
Evan Cheng10043e22007-01-19 07:51:42 +00001031 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001032
Jim Grosbach8546ec92010-01-18 19:58:49 +00001033 case ARMISD::RBIT: return "ARMISD::RBIT";
1034
Bob Wilsone4191e72010-03-19 22:51:32 +00001035 case ARMISD::FTOSI: return "ARMISD::FTOSI";
1036 case ARMISD::FTOUI: return "ARMISD::FTOUI";
1037 case ARMISD::SITOF: return "ARMISD::SITOF";
1038 case ARMISD::UITOF: return "ARMISD::UITOF";
1039
Evan Cheng10043e22007-01-19 07:51:42 +00001040 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1041 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1042 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001043
Evan Chenge8916542011-08-30 01:34:54 +00001044 case ARMISD::ADDC: return "ARMISD::ADDC";
1045 case ARMISD::ADDE: return "ARMISD::ADDE";
1046 case ARMISD::SUBC: return "ARMISD::SUBC";
1047 case ARMISD::SUBE: return "ARMISD::SUBE";
1048
Bob Wilson22806742010-09-22 22:09:21 +00001049 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1050 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001051
Evan Chengec6d7c92009-10-28 06:55:03 +00001052 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
1053 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
1054
Dale Johannesend679ff72010-06-03 21:09:53 +00001055 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001056
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001057 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001058
Evan Chengb972e562009-08-07 00:34:42 +00001059 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1060
Bob Wilson7ed59712010-10-30 00:54:37 +00001061 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001062
Evan Cheng8740ee32010-11-03 06:34:55 +00001063 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1064
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001065 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
1066
Bob Wilson2e076c42009-06-22 23:27:02 +00001067 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001068 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001069 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001070 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1071 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001072 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1073 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001074 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1075 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001076 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1077 case ARMISD::VTST: return "ARMISD::VTST";
1078
1079 case ARMISD::VSHL: return "ARMISD::VSHL";
1080 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1081 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001082 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1083 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1084 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1085 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1086 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1087 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1088 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1089 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1090 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1091 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1092 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1093 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
1094 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1095 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001096 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001097 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001098 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001099 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001100 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001101 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001102 case ARMISD::VREV64: return "ARMISD::VREV64";
1103 case ARMISD::VREV32: return "ARMISD::VREV32";
1104 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001105 case ARMISD::VZIP: return "ARMISD::VZIP";
1106 case ARMISD::VUZP: return "ARMISD::VUZP";
1107 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001108 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1109 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001110 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1111 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001112 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1113 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001114 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilsonc6c13a32010-02-18 06:05:53 +00001115 case ARMISD::FMAX: return "ARMISD::FMAX";
1116 case ARMISD::FMIN: return "ARMISD::FMIN";
Joey Goulye3dd6842013-08-23 12:01:13 +00001117 case ARMISD::VMAXNM: return "ARMISD::VMAX";
1118 case ARMISD::VMINNM: return "ARMISD::VMIN";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001119 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001120 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1121 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001122 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilson2d790df2010-11-28 06:51:26 +00001123 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1124 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1125 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001126 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1127 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1128 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1129 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1130 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1131 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1132 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1133 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1134 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1135 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1136 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1137 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1138 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1139 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1140 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1141 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1142 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001143 }
1144}
1145
Matt Arsenault758659232013-05-18 00:21:46 +00001146EVT ARMTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Duncan Sandsf2641e12011-09-06 19:07:46 +00001147 if (!VT.isVector()) return getPointerTy();
1148 return VT.changeVectorElementTypeToInteger();
1149}
1150
Evan Cheng4cad68e2010-05-15 02:18:07 +00001151/// getRegClassFor - Return the register class that should be used for the
1152/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001153const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001154 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1155 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1156 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001157 if (Subtarget->hasNEON()) {
1158 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001159 return &ARM::QQPRRegClass;
1160 if (VT == MVT::v8i64)
1161 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001162 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001163 return TargetLowering::getRegClassFor(VT);
1164}
1165
Eric Christopher84bdfd82010-07-21 22:26:11 +00001166// Create a fast isel object.
1167FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001168ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1169 const TargetLibraryInfo *libInfo) const {
1170 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001171}
1172
Evan Cheng4401f882010-05-20 23:26:43 +00001173Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001174 unsigned NumVals = N->getNumValues();
1175 if (!NumVals)
1176 return Sched::RegPressure;
1177
1178 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001179 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001180 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001181 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001182 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001183 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001184 }
Evan Chengbf914992010-05-28 23:25:23 +00001185
1186 if (!N->isMachineOpcode())
1187 return Sched::RegPressure;
1188
1189 // Load are scheduled for latency even if there instruction itinerary
1190 // is not available.
Eric Christopher1889fdc2015-01-29 00:19:39 +00001191 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001192 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001193
Evan Cheng6cc775f2011-06-28 19:10:37 +00001194 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001195 return Sched::RegPressure;
1196 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001197 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001198 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001199
Evan Cheng4401f882010-05-20 23:26:43 +00001200 return Sched::RegPressure;
1201}
1202
Evan Cheng10043e22007-01-19 07:51:42 +00001203//===----------------------------------------------------------------------===//
1204// Lowering Code
1205//===----------------------------------------------------------------------===//
1206
Evan Cheng10043e22007-01-19 07:51:42 +00001207/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1208static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1209 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001210 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001211 case ISD::SETNE: return ARMCC::NE;
1212 case ISD::SETEQ: return ARMCC::EQ;
1213 case ISD::SETGT: return ARMCC::GT;
1214 case ISD::SETGE: return ARMCC::GE;
1215 case ISD::SETLT: return ARMCC::LT;
1216 case ISD::SETLE: return ARMCC::LE;
1217 case ISD::SETUGT: return ARMCC::HI;
1218 case ISD::SETUGE: return ARMCC::HS;
1219 case ISD::SETULT: return ARMCC::LO;
1220 case ISD::SETULE: return ARMCC::LS;
1221 }
1222}
1223
Bob Wilsona2e83332009-09-09 23:14:54 +00001224/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1225static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001226 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001227 CondCode2 = ARMCC::AL;
1228 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001229 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001230 case ISD::SETEQ:
1231 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1232 case ISD::SETGT:
1233 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1234 case ISD::SETGE:
1235 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1236 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001237 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001238 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1239 case ISD::SETO: CondCode = ARMCC::VC; break;
1240 case ISD::SETUO: CondCode = ARMCC::VS; break;
1241 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1242 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1243 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1244 case ISD::SETLT:
1245 case ISD::SETULT: CondCode = ARMCC::LT; break;
1246 case ISD::SETLE:
1247 case ISD::SETULE: CondCode = ARMCC::LE; break;
1248 case ISD::SETNE:
1249 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1250 }
Evan Cheng10043e22007-01-19 07:51:42 +00001251}
1252
Bob Wilsona4c22902009-04-17 19:07:39 +00001253//===----------------------------------------------------------------------===//
1254// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001255//===----------------------------------------------------------------------===//
1256
1257#include "ARMGenCallingConv.inc"
1258
Oliver Stannardc24f2172014-05-09 14:01:47 +00001259/// getEffectiveCallingConv - Get the effective calling convention, taking into
1260/// account presence of floating point hardware and calling convention
1261/// limitations, such as support for variadic functions.
1262CallingConv::ID
1263ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1264 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001265 switch (CC) {
1266 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001267 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001268 case CallingConv::ARM_AAPCS:
1269 case CallingConv::ARM_APCS:
1270 case CallingConv::GHC:
1271 return CC;
1272 case CallingConv::ARM_AAPCS_VFP:
1273 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1274 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001275 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001276 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001277 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001278 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1279 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001280 return CallingConv::ARM_AAPCS_VFP;
1281 else
1282 return CallingConv::ARM_AAPCS;
1283 case CallingConv::Fast:
1284 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001285 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001286 return CallingConv::Fast;
1287 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001288 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001289 return CallingConv::ARM_AAPCS_VFP;
1290 else
1291 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001292 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001293}
1294
1295/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1296/// CallingConvention.
1297CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1298 bool Return,
1299 bool isVarArg) const {
1300 switch (getEffectiveCallingConv(CC, isVarArg)) {
1301 default:
1302 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001303 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001304 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001305 case CallingConv::ARM_AAPCS:
1306 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1307 case CallingConv::ARM_AAPCS_VFP:
1308 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1309 case CallingConv::Fast:
1310 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001311 case CallingConv::GHC:
1312 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001313 }
1314}
1315
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001316/// LowerCallResult - Lower the result values of a call into the
1317/// appropriate copies out of appropriate physical registers.
1318SDValue
1319ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001320 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001321 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001322 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001323 SmallVectorImpl<SDValue> &InVals,
1324 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001325
Bob Wilsona4c22902009-04-17 19:07:39 +00001326 // Assign locations to each value returned by this call.
1327 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001328 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1329 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001330 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001331 CCAssignFnForNode(CallConv, /* Return*/ true,
1332 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001333
1334 // Copy all of the result registers out of their specified physreg.
1335 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1336 CCValAssign VA = RVLocs[i];
1337
Stephen Linb8bd2322013-04-20 05:14:40 +00001338 // Pass 'this' value directly from the argument to return value, to avoid
1339 // reg unit interference
1340 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001341 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1342 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001343 InVals.push_back(ThisVal);
1344 continue;
1345 }
1346
Bob Wilson0041bd32009-04-25 00:33:20 +00001347 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001348 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001349 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001350 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001351 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001352 Chain = Lo.getValue(1);
1353 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001354 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001355 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001356 InFlag);
1357 Chain = Hi.getValue(1);
1358 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001359 if (!Subtarget->isLittle())
1360 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001361 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001362
Owen Anderson9f944592009-08-11 20:47:22 +00001363 if (VA.getLocVT() == MVT::v2f64) {
1364 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1365 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1366 DAG.getConstant(0, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001367
1368 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001369 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001370 Chain = Lo.getValue(1);
1371 InFlag = Lo.getValue(2);
1372 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001373 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001374 Chain = Hi.getValue(1);
1375 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001376 if (!Subtarget->isLittle())
1377 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001378 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001379 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1380 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001381 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001382 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001383 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1384 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001385 Chain = Val.getValue(1);
1386 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001387 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001388
1389 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001390 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001391 case CCValAssign::Full: break;
1392 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001393 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001394 break;
1395 }
1396
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001397 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001398 }
1399
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001400 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001401}
1402
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001403/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001404SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001405ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1406 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001407 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001408 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001409 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001410 unsigned LocMemOffset = VA.getLocMemOffset();
1411 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1412 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilsona4c22902009-04-17 19:07:39 +00001413 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner886250c2010-09-21 18:51:21 +00001414 MachinePointerInfo::getStack(LocMemOffset),
David Greene0d0149f2010-02-15 16:55:24 +00001415 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001416}
1417
Andrew Trickef9de2a2013-05-25 02:42:55 +00001418void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001419 SDValue Chain, SDValue &Arg,
1420 RegsToPassVector &RegsToPass,
1421 CCValAssign &VA, CCValAssign &NextVA,
1422 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001423 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001424 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001425
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001426 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001427 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001428 unsigned id = Subtarget->isLittle() ? 0 : 1;
1429 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001430
1431 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001432 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001433 else {
1434 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001435 if (!StackPtr.getNode())
Bob Wilson2e076c42009-06-22 23:27:02 +00001436 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1437
Christian Pirkerb5728192014-05-08 14:06:24 +00001438 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001439 dl, DAG, NextVA,
1440 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001441 }
1442}
1443
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001444/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001445/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1446/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001447SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001448ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001449 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001450 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001451 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001452 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1453 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1454 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001455 SDValue Chain = CLI.Chain;
1456 SDValue Callee = CLI.Callee;
1457 bool &isTailCall = CLI.IsTailCall;
1458 CallingConv::ID CallConv = CLI.CallConv;
1459 bool doesNotRet = CLI.DoesNotReturn;
1460 bool isVarArg = CLI.IsVarArg;
1461
Dale Johannesend679ff72010-06-03 21:09:53 +00001462 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001463 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1464 bool isThisReturn = false;
1465 bool isSibCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001466
Bob Wilson8decdc42011-10-07 17:17:49 +00001467 // Disable tail calls if they're not supported.
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001468 if (!Subtarget->supportsTailCall() || MF.getTarget().Options.DisableTailCalls)
Bob Wilson3c9ed762010-08-13 22:43:33 +00001469 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001470
Dale Johannesend679ff72010-06-03 21:09:53 +00001471 if (isTailCall) {
1472 // Check if it's really possible to do a tail call.
1473 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001474 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001475 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001476 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1477 report_fatal_error("failed to perform tail call elimination on a call "
1478 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001479 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1480 // detected sibcalls.
1481 if (isTailCall) {
1482 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001483 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001484 }
1485 }
Evan Cheng10043e22007-01-19 07:51:42 +00001486
Bob Wilsona4c22902009-04-17 19:07:39 +00001487 // Analyze operands of the call, assigning locations to each operand.
1488 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001489 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1490 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001491 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001492 CCAssignFnForNode(CallConv, /* Return*/ false,
1493 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001494
Bob Wilsona4c22902009-04-17 19:07:39 +00001495 // Get a count of how many bytes are to be pushed on the stack.
1496 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001497
Dale Johannesend679ff72010-06-03 21:09:53 +00001498 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001499 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001500 NumBytes = 0;
1501
Evan Cheng10043e22007-01-19 07:51:42 +00001502 // Adjust the stack pointer for the new arguments...
1503 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001504 if (!isSibCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00001505 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
1506 dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001507
Jim Grosbach6ad4bcb2010-02-24 01:43:03 +00001508 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +00001509
Bob Wilson2e076c42009-06-22 23:27:02 +00001510 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001511 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001512
Bob Wilsona4c22902009-04-17 19:07:39 +00001513 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001514 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001515 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1516 i != e;
1517 ++i, ++realArgIdx) {
1518 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001519 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001520 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001521 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001522
Bob Wilsona4c22902009-04-17 19:07:39 +00001523 // Promote the value if needed.
1524 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001525 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001526 case CCValAssign::Full: break;
1527 case CCValAssign::SExt:
1528 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1529 break;
1530 case CCValAssign::ZExt:
1531 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1532 break;
1533 case CCValAssign::AExt:
1534 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1535 break;
1536 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001537 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001538 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001539 }
1540
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001541 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001542 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001543 if (VA.getLocVT() == MVT::v2f64) {
1544 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1545 DAG.getConstant(0, MVT::i32));
1546 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1547 DAG.getConstant(1, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001548
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001549 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001550 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1551
1552 VA = ArgLocs[++i]; // skip ahead to next loc
1553 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001554 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001555 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1556 } else {
1557 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001558
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001559 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1560 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001561 }
1562 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001563 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001564 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001565 }
1566 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001567 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1568 assert(VA.getLocVT() == MVT::i32 &&
1569 "unexpected calling convention register assignment");
1570 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001571 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001572 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001573 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001574 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001575 } else if (isByVal) {
1576 assert(VA.isMemLoc());
1577 unsigned offset = 0;
1578
1579 // True if this byval aggregate will be split between registers
1580 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001581 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001582 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001583
1584 if (CurByValIdx < ByValArgsCount) {
1585
1586 unsigned RegBegin, RegEnd;
1587 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1588
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001589 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1590 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001591 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001592 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1593 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1594 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1595 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001596 false, false, false,
1597 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001598 MemOpChains.push_back(Load.getValue(1));
1599 RegsToPass.push_back(std::make_pair(j, Load));
1600 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001601
1602 // If parameter size outsides register area, "offset" value
1603 // helps us to calculate stack slot for remained part properly.
1604 offset = RegEnd - RegBegin;
1605
1606 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001607 }
1608
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001609 if (Flags.getByValSize() > 4*offset) {
Manman Ren9f911162012-06-01 02:44:42 +00001610 unsigned LocMemOffset = VA.getLocMemOffset();
1611 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1612 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1613 StkPtrOff);
1614 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1615 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1616 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1617 MVT::i32);
Manman Rene8735522012-06-01 19:33:18 +00001618 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001619
Manman Ren9f911162012-06-01 02:44:42 +00001620 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001621 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001622 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001623 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001624 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001625 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001626 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001627
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001628 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1629 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001630 }
Evan Cheng10043e22007-01-19 07:51:42 +00001631 }
1632
1633 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001634 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001635
1636 // Build a sequence of copy-to-reg nodes chained together with token chain
1637 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001638 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001639 // Tail call byval lowering might overwrite argument registers so in case of
1640 // tail call optimization the copies to registers are lowered later.
1641 if (!isTailCall)
1642 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1643 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1644 RegsToPass[i].second, InFlag);
1645 InFlag = Chain.getValue(1);
1646 }
Evan Cheng10043e22007-01-19 07:51:42 +00001647
Dale Johannesend679ff72010-06-03 21:09:53 +00001648 // For tail calls lower the arguments to the 'real' stack slot.
1649 if (isTailCall) {
1650 // Force all the incoming stack arguments to be loaded from the stack
1651 // before any new outgoing arguments are stored to the stack, because the
1652 // outgoing stack slots may alias the incoming argument stack slots, and
1653 // the alias isn't otherwise explicit. This is slightly more conservative
1654 // than necessary, because it means that each store effectively depends
1655 // on every argument instead of just those arguments it would clobber.
1656
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001657 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001658 InFlag = SDValue();
1659 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1660 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1661 RegsToPass[i].second, InFlag);
1662 InFlag = Chain.getValue(1);
1663 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001664 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001665 }
1666
Bill Wendling24c79f22008-09-16 21:48:12 +00001667 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1668 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1669 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001670 bool isDirect = false;
1671 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001672 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001673 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001674
1675 if (EnableARMLongCalls) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001676 assert((Subtarget->isTargetWindows() ||
1677 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1678 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001679 // Handle a global address or an external symbol. If it's not one of
1680 // those, the target's already in a register, so we don't need to do
1681 // anything extra.
1682 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001683 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001684 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001685 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001686 ARMConstantPoolValue *CPV =
1687 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1688
Jim Grosbach32bb3622010-04-14 22:28:31 +00001689 // Get the address of the callee into a register
1690 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1691 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1692 Callee = DAG.getLoad(getPointerTy(), dl,
1693 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001694 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001695 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001696 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1697 const char *Sym = S->getSymbol();
1698
1699 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001700 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001701 ARMConstantPoolValue *CPV =
1702 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1703 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001704 // Get the address of the callee into a register
1705 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1706 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1707 Callee = DAG.getLoad(getPointerTy(), dl,
1708 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001709 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001710 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001711 }
1712 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001713 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001714 isDirect = true;
Chris Lattner55452c22009-07-15 04:12:33 +00001715 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Tim Northoverd6a729b2014-01-06 14:28:05 +00001716 bool isStub = (isExt && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001717 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001718 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00001719 // ARM call to a local ARM function is predicable.
Evan Chengf128bdc2010-06-16 07:35:02 +00001720 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001721 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001722 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001723 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Tim Northover72360d22013-12-02 10:35:41 +00001724 Callee = DAG.getNode(ARMISD::WrapperPIC, dl, getPointerTy(),
Tim Northoverd4d294d2014-08-06 11:13:06 +00001725 DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
1726 0, ARMII::MO_NONLAZY));
1727 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
1728 MachinePointerInfo::getGOT(), false, false, true, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001729 } else if (Subtarget->isTargetCOFF()) {
1730 assert(Subtarget->isTargetWindows() &&
1731 "Windows is the only supported COFF target");
1732 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1733 ? ARMII::MO_DLLIMPORT
1734 : ARMII::MO_NO_FLAG;
1735 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), /*Offset=*/0,
1736 TargetFlags);
1737 if (GV->hasDLLImportStorageClass())
1738 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
1739 DAG.getNode(ARMISD::Wrapper, dl, getPointerTy(),
1740 Callee), MachinePointerInfo::getGOT(),
1741 false, false, false, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001742 } else {
1743 // On ELF targets for PIC code, direct calls should go through the PLT
1744 unsigned OpFlags = 0;
1745 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001746 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001747 OpFlags = ARMII::MO_PLT;
1748 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1749 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001750 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001751 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001752 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001753 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001754 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Cheng83f35172007-01-30 20:37:08 +00001755 // tBX takes a register source operand.
1756 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001757 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001758 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001759 ARMConstantPoolValue *CPV =
1760 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1761 ARMPCLabelIndex, 4);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001762 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001763 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen021052a2009-02-04 20:06:27 +00001764 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Chengcdbb70c2009-10-31 03:39:36 +00001765 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001766 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001767 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00001768 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson7117a912009-03-20 22:42:55 +00001769 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen021052a2009-02-04 20:06:27 +00001770 getPointerTy(), Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001771 } else {
1772 unsigned OpFlags = 0;
1773 // On ELF targets for PIC code, direct calls should go through the PLT
1774 if (Subtarget->isTargetELF() &&
1775 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1776 OpFlags = ARMII::MO_PLT;
1777 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1778 }
Evan Cheng10043e22007-01-19 07:51:42 +00001779 }
1780
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001781 // FIXME: handle tail calls differently.
1782 unsigned CallOpc;
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00001783 bool HasMinSizeAttr = MF.getFunction()->hasFnAttribute(Attribute::MinSize);
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001784 if (Subtarget->isThumb()) {
1785 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001786 CallOpc = ARMISD::CALL_NOLINK;
1787 else
1788 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1789 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001790 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001791 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001792 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet8e1fe842012-11-02 21:32:17 +00001793 // Emit regular call when code size is the priority
1794 !HasMinSizeAttr)
Evan Cheng65f9d192012-02-28 18:51:51 +00001795 // "mov lr, pc; b _foo" to avoid confusing the RSP
1796 CallOpc = ARMISD::CALL_NOLINK;
1797 else
1798 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001799 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001800
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001801 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001802 Ops.push_back(Chain);
1803 Ops.push_back(Callee);
1804
1805 // Add argument registers to the end of the list so that they are known live
1806 // into the call.
1807 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1808 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1809 RegsToPass[i].second.getValueType()));
1810
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001811 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001812 if (!isTailCall) {
1813 const uint32_t *Mask;
Eric Christopher1889fdc2015-01-29 00:19:39 +00001814 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001815 if (isThisReturn) {
1816 // For 'this' returns, use the R0-preserving mask if applicable
Eric Christopher9deb75d2015-03-11 22:42:13 +00001817 Mask = ARI->getThisReturnPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001818 if (!Mask) {
1819 // Set isThisReturn to false if the calling convention is not one that
1820 // allows 'returned' to be modeled in this way, so LowerCallResult does
1821 // not try to pass 'this' straight through
1822 isThisReturn = false;
Eric Christopher9deb75d2015-03-11 22:42:13 +00001823 Mask = ARI->getCallPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001824 }
1825 } else
Eric Christopher9deb75d2015-03-11 22:42:13 +00001826 Mask = ARI->getCallPreservedMask(MF, CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001827
Matthias Braunc22630e2013-10-04 16:52:54 +00001828 assert(Mask && "Missing call preserved mask for calling convention");
1829 Ops.push_back(DAG.getRegisterMask(Mask));
1830 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001831
Gabor Greiff304a7a2008-08-28 21:40:38 +00001832 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001833 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001834
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001835 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001836 if (isTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001837 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Dale Johannesend679ff72010-06-03 21:09:53 +00001838
Duncan Sands739a0542008-07-02 17:40:58 +00001839 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001840 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001841 InFlag = Chain.getValue(1);
1842
Chris Lattner27539552008-10-11 22:08:30 +00001843 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001844 DAG.getIntPtrConstant(0, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001845 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001846 InFlag = Chain.getValue(1);
1847
Bob Wilsona4c22902009-04-17 19:07:39 +00001848 // Handle result values, copying them out of physregs into vregs that we
1849 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001850 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001851 InVals, isThisReturn,
1852 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001853}
1854
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001855/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001856/// on the stack. Remember the next parameter register to allocate,
1857/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001858/// this.
Tim Northover8cda34f2015-03-11 18:54:22 +00001859void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size,
1860 unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001861 assert((State->getCallOrPrologue() == Prologue ||
1862 State->getCallOrPrologue() == Call) &&
1863 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001864
Tim Northover8cda34f2015-03-11 18:54:22 +00001865 // Byval (as with any stack) slots are always at least 4 byte aligned.
1866 Align = std::max(Align, 4U);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001867
Tim Northover8cda34f2015-03-11 18:54:22 +00001868 unsigned Reg = State->AllocateReg(GPRArgRegs);
1869 if (!Reg)
1870 return;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001871
Tim Northover8cda34f2015-03-11 18:54:22 +00001872 unsigned AlignInRegs = Align / 4;
1873 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs;
1874 for (unsigned i = 0; i < Waste; ++i)
1875 Reg = State->AllocateReg(GPRArgRegs);
1876
1877 if (!Reg)
1878 return;
1879
1880 unsigned Excess = 4 * (ARM::R4 - Reg);
1881
1882 // Special case when NSAA != SP and parameter size greater than size of
1883 // all remained GPR regs. In that case we can't split parameter, we must
1884 // send it to stack. We also must set NCRN to R4, so waste all
1885 // remained registers.
1886 const unsigned NSAAOffset = State->getNextStackOffset();
1887 if (NSAAOffset != 0 && Size > Excess) {
1888 while (State->AllocateReg(GPRArgRegs))
1889 ;
1890 return;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001891 }
Tim Northover8cda34f2015-03-11 18:54:22 +00001892
1893 // First register for byval parameter is the first register that wasn't
1894 // allocated before this method call, so it would be "reg".
1895 // If parameter is small enough to be saved in range [reg, r4), then
1896 // the end (first after last) register would be reg + param-size-in-regs,
1897 // else parameter would be splitted between registers and stack,
1898 // end register would be r4 in this case.
1899 unsigned ByValRegBegin = Reg;
1900 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4);
1901 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1902 // Note, first register is allocated in the beginning of function already,
1903 // allocate remained amount of registers we need.
1904 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i)
1905 State->AllocateReg(GPRArgRegs);
1906 // A byval parameter that is split between registers and memory needs its
1907 // size truncated here.
1908 // In the case where the entire structure fits in registers, we set the
1909 // size in memory to zero.
1910 Size = std::max<int>(Size - Excess, 0);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001911}
1912
Tim Northover8cda34f2015-03-11 18:54:22 +00001913
Dale Johannesend679ff72010-06-03 21:09:53 +00001914/// MatchingStackOffset - Return true if the given stack call argument is
1915/// already available in the same position (relatively) of the caller's
1916/// incoming argument stack.
1917static
1918bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1919 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00001920 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001921 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1922 int FI = INT_MAX;
1923 if (Arg.getOpcode() == ISD::CopyFromReg) {
1924 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001925 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00001926 return false;
1927 MachineInstr *Def = MRI->getVRegDef(VR);
1928 if (!Def)
1929 return false;
1930 if (!Flags.isByVal()) {
1931 if (!TII->isLoadFromStackSlot(Def, FI))
1932 return false;
1933 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00001934 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00001935 }
1936 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1937 if (Flags.isByVal())
1938 // ByVal argument is passed in as a pointer but it's now being
1939 // dereferenced. e.g.
1940 // define @foo(%struct.X* %A) {
1941 // tail call @bar(%struct.X* byval %A)
1942 // }
1943 return false;
1944 SDValue Ptr = Ld->getBasePtr();
1945 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1946 if (!FINode)
1947 return false;
1948 FI = FINode->getIndex();
1949 } else
1950 return false;
1951
1952 assert(FI != INT_MAX);
1953 if (!MFI->isFixedObjectIndex(FI))
1954 return false;
1955 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1956}
1957
1958/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1959/// for tail call optimization. Targets which want to do tail call
1960/// optimization should implement this function.
1961bool
1962ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1963 CallingConv::ID CalleeCC,
1964 bool isVarArg,
1965 bool isCalleeStructRet,
1966 bool isCallerStructRet,
1967 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001968 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00001969 const SmallVectorImpl<ISD::InputArg> &Ins,
1970 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00001971 const Function *CallerF = DAG.getMachineFunction().getFunction();
1972 CallingConv::ID CallerCC = CallerF->getCallingConv();
1973 bool CCMatch = CallerCC == CalleeCC;
1974
1975 // Look for obvious safe cases to perform tail call optimization that do not
1976 // require ABI changes. This is what gcc calls sibcall.
1977
Jim Grosbache3864cc2010-06-16 23:45:49 +00001978 // Do not sibcall optimize vararg calls unless the call site is not passing
1979 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00001980 if (isVarArg && !Outs.empty())
1981 return false;
1982
Tim Northoverd8407452013-10-01 14:33:28 +00001983 // Exception-handling functions need a special set of instructions to indicate
1984 // a return to the hardware. Tail-calling another function would probably
1985 // break this.
1986 if (CallerF->hasFnAttribute("interrupt"))
1987 return false;
1988
Dale Johannesend679ff72010-06-03 21:09:53 +00001989 // Also avoid sibcall optimization if either caller or callee uses struct
1990 // return semantics.
1991 if (isCalleeStructRet || isCallerStructRet)
1992 return false;
1993
Dale Johannesend24c66b2010-06-23 18:52:34 +00001994 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach3840c902011-07-08 20:18:11 +00001995 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1996 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1997 // support in the assembler and linker to be used. This would need to be
1998 // fixed to fully support tail calls in Thumb1.
1999 //
Dale Johannesene2289282010-07-08 01:18:23 +00002000 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
2001 // LR. This means if we need to reload LR, it takes an extra instructions,
2002 // which outweighs the value of the tail call; but here we don't know yet
2003 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach535d3b42010-09-08 03:54:02 +00002004 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesene2289282010-07-08 01:18:23 +00002005 // emitEpilogue if LR is used.
Dale Johannesene2289282010-07-08 01:18:23 +00002006
2007 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
2008 // but we need to make sure there are enough registers; the only valid
2009 // registers are the 4 used for parameters. We don't currently do this
2010 // case.
Evan Chengd4b08732010-11-30 23:55:39 +00002011 if (Subtarget->isThumb1Only())
2012 return false;
Dale Johannesen3ac52b32010-06-18 18:13:11 +00002013
Oliver Stannard12993dd2014-08-18 12:42:15 +00002014 // Externally-defined functions with weak linkage should not be
2015 // tail-called on ARM when the OS does not support dynamic
2016 // pre-emption of symbols, as the AAELF spec requires normal calls
2017 // to undefined weak functions to be replaced with a NOP or jump to the
2018 // next instruction. The behaviour of branch instructions in this
2019 // situation (as used for tail calls) is implementation-defined, so we
2020 // cannot rely on the linker replacing the tail call with a return.
2021 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2022 const GlobalValue *GV = G->getGlobal();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002023 const Triple TT(getTargetMachine().getTargetTriple());
2024 if (GV->hasExternalWeakLinkage() &&
2025 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002026 return false;
2027 }
2028
Dale Johannesend679ff72010-06-03 21:09:53 +00002029 // If the calling conventions do not match, then we'd better make sure the
2030 // results are returned in the same way as what the caller expects.
2031 if (!CCMatch) {
2032 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopherb5217502014-08-06 18:45:26 +00002033 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
2034 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002035 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
2036
2037 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopherb5217502014-08-06 18:45:26 +00002038 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
2039 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002040 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
2041
2042 if (RVLocs1.size() != RVLocs2.size())
2043 return false;
2044 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2045 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2046 return false;
2047 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2048 return false;
2049 if (RVLocs1[i].isRegLoc()) {
2050 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2051 return false;
2052 } else {
2053 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2054 return false;
2055 }
2056 }
2057 }
2058
Manman Ren7e48b252012-10-12 23:39:43 +00002059 // If Caller's vararg or byval argument has been split between registers and
2060 // stack, do not perform tail call, since part of the argument is in caller's
2061 // local frame.
2062 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
2063 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002064 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002065 return false;
2066
Dale Johannesend679ff72010-06-03 21:09:53 +00002067 // If the callee takes no arguments then go on to check the results of the
2068 // call.
2069 if (!Outs.empty()) {
2070 // Check if stack adjustment is needed. For now, do not do this if any
2071 // argument is passed on the stack.
2072 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002073 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2074 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002075 CCInfo.AnalyzeCallOperands(Outs,
2076 CCAssignFnForNode(CalleeCC, false, isVarArg));
2077 if (CCInfo.getNextStackOffset()) {
2078 MachineFunction &MF = DAG.getMachineFunction();
2079
2080 // Check if the arguments are already laid out in the right way as
2081 // the caller's fixed stack objects.
2082 MachineFrameInfo *MFI = MF.getFrameInfo();
2083 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopher1889fdc2015-01-29 00:19:39 +00002084 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002085 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2086 i != e;
2087 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002088 CCValAssign &VA = ArgLocs[i];
2089 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002090 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002091 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002092 if (VA.getLocInfo() == CCValAssign::Indirect)
2093 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002094 if (VA.needsCustom()) {
2095 // f64 and vector types are split into multiple registers or
2096 // register/stack-slot combinations. The types will not match
2097 // the registers; give up on memory f64 refs until we figure
2098 // out what to do about this.
2099 if (!VA.isRegLoc())
2100 return false;
2101 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002102 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002103 if (RegVT == MVT::v2f64) {
2104 if (!ArgLocs[++i].isRegLoc())
2105 return false;
2106 if (!ArgLocs[++i].isRegLoc())
2107 return false;
2108 }
2109 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002110 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2111 MFI, MRI, TII))
2112 return false;
2113 }
2114 }
2115 }
2116 }
2117
2118 return true;
2119}
2120
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002121bool
2122ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2123 MachineFunction &MF, bool isVarArg,
2124 const SmallVectorImpl<ISD::OutputArg> &Outs,
2125 LLVMContext &Context) const {
2126 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002127 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002128 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2129 isVarArg));
2130}
2131
Tim Northoverd8407452013-10-01 14:33:28 +00002132static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2133 SDLoc DL, SelectionDAG &DAG) {
2134 const MachineFunction &MF = DAG.getMachineFunction();
2135 const Function *F = MF.getFunction();
2136
2137 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2138
2139 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2140 // version of the "preferred return address". These offsets affect the return
2141 // instruction if this is a return from PL1 without hypervisor extensions.
2142 // IRQ/FIQ: +4 "subs pc, lr, #4"
2143 // SWI: 0 "subs pc, lr, #0"
2144 // ABORT: +4 "subs pc, lr, #4"
2145 // UNDEF: +4/+2 "subs pc, lr, #0"
2146 // UNDEF varies depending on where the exception came from ARM or Thumb
2147 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2148
2149 int64_t LROffset;
2150 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2151 IntKind == "ABORT")
2152 LROffset = 4;
2153 else if (IntKind == "SWI" || IntKind == "UNDEF")
2154 LROffset = 0;
2155 else
2156 report_fatal_error("Unsupported interrupt attribute. If present, value "
2157 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2158
2159 RetOps.insert(RetOps.begin() + 1, DAG.getConstant(LROffset, MVT::i32, false));
2160
Craig Topper48d114b2014-04-26 18:35:24 +00002161 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002162}
2163
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002164SDValue
2165ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002166 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002167 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002168 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002169 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002170
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002171 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002172 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002173
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002174 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002175 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2176 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002177
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002178 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002179 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2180 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002181
Bob Wilsona4c22902009-04-17 19:07:39 +00002182 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002183 SmallVector<SDValue, 4> RetOps;
2184 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002185 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002186
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002187 MachineFunction &MF = DAG.getMachineFunction();
2188 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2189 AFI->setReturnRegsCount(RVLocs.size());
2190
Bob Wilsona4c22902009-04-17 19:07:39 +00002191 // Copy the result values into the output registers.
2192 for (unsigned i = 0, realRVLocIdx = 0;
2193 i != RVLocs.size();
2194 ++i, ++realRVLocIdx) {
2195 CCValAssign &VA = RVLocs[i];
2196 assert(VA.isRegLoc() && "Can only return in registers!");
2197
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002198 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002199
2200 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002201 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002202 case CCValAssign::Full: break;
2203 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002204 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002205 break;
2206 }
2207
Bob Wilsona4c22902009-04-17 19:07:39 +00002208 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002209 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002210 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002211 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2212 DAG.getConstant(0, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002213 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002214 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002215
Christian Pirkerb5728192014-05-08 14:06:24 +00002216 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2217 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2218 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002219 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002220 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002221 VA = RVLocs[++i]; // skip ahead to next loc
2222 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002223 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2224 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002225 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002226 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002227 VA = RVLocs[++i]; // skip ahead to next loc
2228
2229 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002230 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2231 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002232 }
2233 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2234 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002235 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002236 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002237 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2238 fmrrd.getValue(isLittleEndian ? 0 : 1),
2239 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002240 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002241 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002242 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002243 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2244 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002245 Flag);
2246 } else
2247 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2248
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002249 // Guarantee that all emitted copies are
2250 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002251 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002252 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002253 }
2254
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002255 // Update chain and glue.
2256 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002257 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002258 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002259
Tim Northoverd8407452013-10-01 14:33:28 +00002260 // CPUs which aren't M-class use a special sequence to return from
2261 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2262 // though we use "subs pc, lr, #N").
2263 //
2264 // M-class CPUs actually use a normal return sequence with a special
2265 // (hardware-provided) value in LR, so the normal code path works.
2266 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2267 !Subtarget->isMClass()) {
2268 if (Subtarget->isThumb1Only())
2269 report_fatal_error("interrupt attribute is not supported in Thumb1");
2270 return LowerInterruptReturn(RetOps, dl, DAG);
2271 }
2272
Craig Topper48d114b2014-04-26 18:35:24 +00002273 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002274}
2275
Evan Chengf8bad082012-04-10 01:51:00 +00002276bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002277 if (N->getNumValues() != 1)
2278 return false;
2279 if (!N->hasNUsesOfValue(1, 0))
2280 return false;
2281
Evan Chengf8bad082012-04-10 01:51:00 +00002282 SDValue TCChain = Chain;
2283 SDNode *Copy = *N->use_begin();
2284 if (Copy->getOpcode() == ISD::CopyToReg) {
2285 // If the copy has a glue operand, we conservatively assume it isn't safe to
2286 // perform a tail call.
2287 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2288 return false;
2289 TCChain = Copy->getOperand(0);
2290 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2291 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002292 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002293 SmallPtrSet<SDNode*, 2> Copies;
2294 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002295 UI != UE; ++UI) {
2296 if (UI->getOpcode() != ISD::CopyToReg)
2297 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002298 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002299 }
Evan Chengf8bad082012-04-10 01:51:00 +00002300 if (Copies.size() > 2)
2301 return false;
2302
2303 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2304 UI != UE; ++UI) {
2305 SDValue UseChain = UI->getOperand(0);
2306 if (Copies.count(UseChain.getNode()))
2307 // Second CopyToReg
2308 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002309 else {
2310 // We are at the top of this chain.
2311 // If the copy has a glue operand, we conservatively assume it
2312 // isn't safe to perform a tail call.
2313 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2314 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002315 // First CopyToReg
2316 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002317 }
Evan Chengf8bad082012-04-10 01:51:00 +00002318 }
2319 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002320 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002321 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002322 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002323 Copy = *Copy->use_begin();
2324 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002325 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002326 // If the copy has a glue operand, we conservatively assume it isn't safe to
2327 // perform a tail call.
2328 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2329 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002330 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002331 } else {
2332 return false;
2333 }
2334
Evan Cheng419ea282010-12-01 22:59:46 +00002335 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002336 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2337 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002338 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2339 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002340 return false;
2341 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002342 }
2343
Evan Chengf8bad082012-04-10 01:51:00 +00002344 if (!HasRet)
2345 return false;
2346
2347 Chain = TCChain;
2348 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002349}
2350
Evan Cheng0663f232011-03-21 01:19:09 +00002351bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002352 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002353 return false;
2354
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00002355 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng0663f232011-03-21 01:19:09 +00002356 return false;
2357
2358 return !Subtarget->isThumb1Only();
2359}
2360
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002361// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2362// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2363// one of the above mentioned nodes. It has to be wrapped because otherwise
2364// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2365// be used to form addressing mode. These wrapped nodes will be selected
2366// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002367static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002368 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002369 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002370 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002371 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002372 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002373 if (CP->isMachineConstantPoolEntry())
2374 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2375 CP->getAlignment());
2376 else
2377 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2378 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002379 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002380}
2381
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002382unsigned ARMTargetLowering::getJumpTableEncoding() const {
2383 return MachineJumpTableInfo::EK_Inline;
2384}
2385
Dan Gohman21cea8a2010-04-17 15:26:15 +00002386SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2387 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002388 MachineFunction &MF = DAG.getMachineFunction();
2389 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2390 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002391 SDLoc DL(Op);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002392 EVT PtrVT = getPointerTy();
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002393 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002394 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2395 SDValue CPAddr;
2396 if (RelocM == Reloc::Static) {
2397 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2398 } else {
2399 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002400 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002401 ARMConstantPoolValue *CPV =
2402 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2403 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002404 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2405 }
2406 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2407 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002408 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002409 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002410 if (RelocM == Reloc::Static)
2411 return Result;
Evan Cheng408aa562009-11-06 22:24:13 +00002412 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002413 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002414}
2415
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002416// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002417SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002418ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002419 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002420 SDLoc dl(GA);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002421 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002422 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002423 MachineFunction &MF = DAG.getMachineFunction();
2424 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002425 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002426 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002427 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2428 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002429 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002430 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002431 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattner7727d052010-09-21 06:44:06 +00002432 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002433 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002434 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002435
Evan Cheng408aa562009-11-06 22:24:13 +00002436 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002437 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002438
2439 // call __tls_get_addr.
2440 ArgListTy Args;
2441 ArgListEntry Entry;
2442 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002443 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002444 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002445
Dale Johannesen555a3752009-01-30 23:10:59 +00002446 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002447 TargetLowering::CallLoweringInfo CLI(DAG);
2448 CLI.setDebugLoc(dl).setChain(Chain)
2449 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002450 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args),
2451 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002452
Justin Holewinskiaa583972012-05-25 16:35:28 +00002453 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002454 return CallResult.first;
2455}
2456
2457// Lower ISD::GlobalTLSAddress using the "initial exec" or
2458// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002459SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002460ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002461 SelectionDAG &DAG,
2462 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002463 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002464 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002465 SDValue Offset;
2466 SDValue Chain = DAG.getEntryNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002467 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002468 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002469 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002470
Hans Wennborgaea41202012-05-04 09:40:39 +00002471 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002472 MachineFunction &MF = DAG.getMachineFunction();
2473 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002474 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002475 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002476 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2477 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002478 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2479 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2480 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002481 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002482 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002483 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002484 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002485 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002486 Chain = Offset.getValue(1);
2487
Evan Cheng408aa562009-11-06 22:24:13 +00002488 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002489 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002490
Evan Chengcdbb70c2009-10-31 03:39:36 +00002491 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002492 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002493 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002494 } else {
2495 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002496 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002497 ARMConstantPoolValue *CPV =
2498 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002499 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002500 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002501 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002502 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002503 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002504 }
2505
2506 // The address of the thread local variable is the add of the thread
2507 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002508 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002509}
2510
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002511SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002512ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002513 // TODO: implement the "local dynamic" model
2514 assert(Subtarget->isTargetELF() &&
2515 "TLS not implemented for non-ELF targets");
2516 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgaea41202012-05-04 09:40:39 +00002517
2518 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2519
2520 switch (model) {
2521 case TLSModel::GeneralDynamic:
2522 case TLSModel::LocalDynamic:
2523 return LowerToTLSGeneralDynamicModel(GA, DAG);
2524 case TLSModel::InitialExec:
2525 case TLSModel::LocalExec:
2526 return LowerToTLSExecModels(GA, DAG, model);
2527 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002528 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002529}
2530
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002531SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002532 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002533 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002534 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002535 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002536 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002537 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002538 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002539 ARMConstantPoolConstant::Create(GV,
2540 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002541 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002542 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson7117a912009-03-20 22:42:55 +00002543 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002544 CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002545 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002546 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002547 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002548 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002549 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002550 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002551 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002552 MachinePointerInfo::getGOT(),
2553 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002554 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002555 }
2556
2557 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002558 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002559 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002560 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002561 // FIXME: Once remat is capable of dealing with instructions with register
2562 // operands, expand this into two nodes.
2563 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2564 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002565 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002566 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2567 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2568 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2569 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002570 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002571 }
2572}
2573
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002574SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002575 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002576 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002577 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002578 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002579 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002580
Eric Christopherc1058df2014-07-04 01:55:26 +00002581 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002582 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002583
Tim Northover72360d22013-12-02 10:35:41 +00002584 // FIXME: Once remat is capable of dealing with instructions with register
2585 // operands, expand this into multiple nodes
2586 unsigned Wrapper =
2587 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002588
Tim Northover72360d22013-12-02 10:35:41 +00002589 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2590 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002591
Evan Cheng1b389522009-09-03 07:04:02 +00002592 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002593 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2594 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002595 return Result;
2596}
2597
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002598SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2599 SelectionDAG &DAG) const {
2600 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002601 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2602 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002603
2604 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002605 const ARMII::TOF TargetFlags =
2606 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002607 EVT PtrVT = getPointerTy();
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002608 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002609 SDLoc DL(Op);
2610
2611 ++NumMovwMovt;
2612
2613 // FIXME: Once remat is capable of dealing with instructions with register
2614 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002615 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2616 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
2617 TargetFlags));
2618 if (GV->hasDLLImportStorageClass())
2619 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
2620 MachinePointerInfo::getGOT(), false, false, false, 0);
2621 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002622}
2623
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002624SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002625 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002626 assert(Subtarget->isTargetELF() &&
2627 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002628 MachineFunction &MF = DAG.getMachineFunction();
2629 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002630 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002631 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002632 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002633 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002634 ARMConstantPoolValue *CPV =
2635 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2636 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002637 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002638 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002639 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002640 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002641 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00002642 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002643 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002644}
2645
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002646SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002647ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002648 SDLoc dl(Op);
Jim Grosbachfaa3abb2010-05-27 23:49:24 +00002649 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002650 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2651 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002652 Op.getOperand(1), Val);
2653}
2654
2655SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002656ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002657 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002658 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2659 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2660}
2661
2662SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002663ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002664 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002665 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002666 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002667 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002668 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002669 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00002670 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00002671 "RBIT intrinsic must have i32 type!");
Yi Kongc655f0c2014-08-20 10:40:20 +00002672 return DAG.getNode(ARMISD::RBIT, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00002673 }
Bob Wilson17f88782009-08-04 00:25:01 +00002674 case Intrinsic::arm_thread_pointer: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002675 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson17f88782009-08-04 00:25:01 +00002676 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2677 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002678 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002679 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002680 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002681 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002682 EVT PtrVT = getPointerTy();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002683 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2684 SDValue CPAddr;
2685 unsigned PCAdj = (RelocM != Reloc::PIC_)
2686 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002687 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002688 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2689 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002690 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002691 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002692 SDValue Result =
Evan Chengcdbb70c2009-10-31 03:39:36 +00002693 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002694 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002695 false, false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002696
2697 if (RelocM == Reloc::PIC_) {
Evan Cheng408aa562009-11-06 22:24:13 +00002698 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002699 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2700 }
2701 return Result;
2702 }
Evan Cheng18381b42011-03-29 23:06:19 +00002703 case Intrinsic::arm_neon_vmulls:
2704 case Intrinsic::arm_neon_vmullu: {
2705 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2706 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002707 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002708 Op.getOperand(1), Op.getOperand(2));
2709 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002710 }
2711}
2712
Eli Friedman30a49e92011-08-03 21:06:02 +00002713static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2714 const ARMSubtarget *Subtarget) {
2715 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002716 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002717 if (!Subtarget->hasDataBarrier()) {
2718 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2719 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2720 // here.
2721 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002722 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002723 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002724 DAG.getConstant(0, MVT::i32));
2725 }
2726
Tim Northover36b24172013-07-03 09:20:36 +00002727 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2728 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00002729 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002730 if (Subtarget->isMClass()) {
2731 // Only a full system barrier exists in the M-class architectures.
2732 Domain = ARM_MB::SY;
2733 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002734 // Swift happens to implement ISHST barriers in a way that's compatible with
2735 // Release semantics but weaker than ISH so we'd be fools not to use
2736 // it. Beware: other processors probably don't!
2737 Domain = ARM_MB::ISHST;
2738 }
2739
Joey Gouly926d3f52013-09-05 15:35:24 +00002740 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
2741 DAG.getConstant(Intrinsic::arm_dmb, MVT::i32),
Tim Northover36b24172013-07-03 09:20:36 +00002742 DAG.getConstant(Domain, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002743}
2744
Evan Cheng8740ee32010-11-03 06:34:55 +00002745static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2746 const ARMSubtarget *Subtarget) {
2747 // ARM pre v5TE and Thumb1 does not have preload instructions.
2748 if (!(Subtarget->isThumb2() ||
2749 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2750 // Just preserve the chain.
2751 return Op.getOperand(0);
2752
Andrew Trickef9de2a2013-05-25 02:42:55 +00002753 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002754 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2755 if (!isRead &&
2756 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2757 // ARMv7 with MP extension has PLDW.
2758 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002759
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002760 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2761 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002762 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002763 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002764 isData = ~isData & 1;
2765 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002766
2767 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng21acf9f2010-11-04 05:19:35 +00002768 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2769 DAG.getConstant(isData, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002770}
2771
Dan Gohman31ae5862010-04-17 14:41:14 +00002772static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2773 MachineFunction &MF = DAG.getMachineFunction();
2774 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2775
Evan Cheng10043e22007-01-19 07:51:42 +00002776 // vastart just stores the address of the VarArgsFrameIndex slot into the
2777 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002778 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002779 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002780 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002781 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002782 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2783 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002784}
2785
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002786SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002787ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2788 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002789 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002790 MachineFunction &MF = DAG.getMachineFunction();
2791 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2792
Craig Topper760b1342012-02-22 05:59:10 +00002793 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002794 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002795 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002796 else
Craig Topperc7242e02012-04-20 07:30:17 +00002797 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002798
2799 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002800 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002801 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002802
2803 SDValue ArgValue2;
2804 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002805 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002806 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002807
2808 // Create load node to retrieve arguments from the stack.
2809 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Chengcdbb70c2009-10-31 03:39:36 +00002810 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002811 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002812 false, false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002813 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002814 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002815 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002816 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002817 if (!Subtarget->isLittle())
2818 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002819 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002820}
2821
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002822// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002823// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002824// byval). Either way, we allocate stack slots adjacent to the data
2825// provided by our caller, and store the unallocated registers there.
2826// If this is a variadic function, the va_list pointer will begin with
2827// these values; otherwise, this reassembles a (byval) structure that
2828// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002829// Return: The frame index registers were stored into.
2830int
2831ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002832 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002833 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002834 unsigned InRegsParamRecordIdx,
Tim Northover8cda34f2015-03-11 18:54:22 +00002835 int ArgOffset,
2836 unsigned ArgSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002837 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002838 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002839 // Setup first unallocated register as first byval register;
2840 // eat all remained registers
2841 // (these two actions are performed by HandleByVal method).
2842 // Then, here, we initialize stack frame with
2843 // "store-reg" instructions.
2844 // Case #2. Var-args function, that doesn't contain byval parameters.
2845 // The same: eat all remained unallocated registers,
2846 // initialize stack frame.
2847
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002848 MachineFunction &MF = DAG.getMachineFunction();
2849 MachineFrameInfo *MFI = MF.getFrameInfo();
2850 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002851 unsigned RBegin, REnd;
2852 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2853 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002854 } else {
Tim Northover8cda34f2015-03-11 18:54:22 +00002855 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
2856 RBegin = RBeginIdx == 4 ? ARM::R4 : GPRArgRegs[RBeginIdx];
2857 REnd = ARM::R4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002858 }
2859
Tim Northover8cda34f2015-03-11 18:54:22 +00002860 if (REnd != RBegin)
2861 ArgOffset = -4 * (ARM::R4 - RBegin);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002862
Tim Northover8cda34f2015-03-11 18:54:22 +00002863 int FrameIndex = MFI->CreateFixedObject(ArgSize, ArgOffset, false);
2864 SDValue FIN = DAG.getFrameIndex(FrameIndex, getPointerTy());
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002865
Tim Northover8cda34f2015-03-11 18:54:22 +00002866 SmallVector<SDValue, 4> MemOps;
2867 const TargetRegisterClass *RC =
2868 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002869
Tim Northover8cda34f2015-03-11 18:54:22 +00002870 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) {
2871 unsigned VReg = MF.addLiveIn(Reg, RC);
2872 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2873 SDValue Store =
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002874 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Tim Northover8cda34f2015-03-11 18:54:22 +00002875 MachinePointerInfo(OrigArg, 4 * i), false, false, 0);
2876 MemOps.push_back(Store);
2877 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2878 DAG.getConstant(4, getPointerTy()));
Oliver Stannardd55e1152014-03-05 15:25:27 +00002879 }
Tim Northover8cda34f2015-03-11 18:54:22 +00002880
2881 if (!MemOps.empty())
2882 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
2883 return FrameIndex;
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002884}
2885
2886// Setup stack frame, the va_list pointer will start from.
2887void
2888ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002889 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002890 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002891 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002892 bool ForceMutable) const {
2893 MachineFunction &MF = DAG.getMachineFunction();
2894 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2895
2896 // Try to store any remaining integer argument regs
2897 // to their spots on the stack so that they may be loaded by deferencing
2898 // the result of va_next.
2899 // If there is no regs to be stored, just point address after last
2900 // argument passed via stack.
Tim Northover8cda34f2015-03-11 18:54:22 +00002901 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
2902 CCInfo.getInRegsParamsCount(),
2903 CCInfo.getNextStackOffset(), 4);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002904 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002905}
2906
Bob Wilson2e076c42009-06-22 23:27:02 +00002907SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002908ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002909 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002910 const SmallVectorImpl<ISD::InputArg>
2911 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002912 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002913 SmallVectorImpl<SDValue> &InVals)
2914 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00002915 MachineFunction &MF = DAG.getMachineFunction();
2916 MachineFrameInfo *MFI = MF.getFrameInfo();
2917
Bob Wilsona4c22902009-04-17 19:07:39 +00002918 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2919
2920 // Assign locations to all of the incoming arguments.
2921 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002922 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
2923 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002924 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002925 CCAssignFnForNode(CallConv, /* Return*/ false,
2926 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00002927
Bob Wilsona4c22902009-04-17 19:07:39 +00002928 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002929 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002930 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2931 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002932
2933 // Initially ArgRegsSaveSize is zero.
2934 // Then we increase this value each time we meet byval parameter.
2935 // We also increase this value in case of varargs function.
2936 AFI->setArgRegsSaveSize(0);
2937
Oliver Stannardd55e1152014-03-05 15:25:27 +00002938 // Calculate the amount of stack space that we need to allocate to store
2939 // byval and variadic arguments that are passed in registers.
2940 // We need to know this before we allocate the first byval or variadic
2941 // argument, as they will be allocated a stack slot below the CFA (Canonical
2942 // Frame Address, the stack pointer at entry to the function).
Tim Northover8cda34f2015-03-11 18:54:22 +00002943 unsigned ArgRegBegin = ARM::R4;
Oliver Stannardd55e1152014-03-05 15:25:27 +00002944 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tim Northover8cda34f2015-03-11 18:54:22 +00002945 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount())
2946 break;
Oliver Stannardd55e1152014-03-05 15:25:27 +00002947
Tim Northover8cda34f2015-03-11 18:54:22 +00002948 CCValAssign &VA = ArgLocs[i];
2949 unsigned Index = VA.getValNo();
2950 ISD::ArgFlagsTy Flags = Ins[Index].Flags;
2951 if (!Flags.isByVal())
2952 continue;
2953
2954 assert(VA.isMemLoc() && "unexpected byval pointer in reg");
2955 unsigned RBegin, REnd;
2956 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd);
2957 ArgRegBegin = std::min(ArgRegBegin, RBegin);
2958
2959 CCInfo.nextInRegsParam();
Oliver Stannardd55e1152014-03-05 15:25:27 +00002960 }
2961 CCInfo.rewindByValRegsInfo();
Tim Northover8cda34f2015-03-11 18:54:22 +00002962
2963 int lastInsIndex = -1;
Reid Kleckner2d9bb652014-08-22 21:59:26 +00002964 if (isVarArg && MFI->hasVAStart()) {
Tim Northover8cda34f2015-03-11 18:54:22 +00002965 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
2966 if (RegIdx != array_lengthof(GPRArgRegs))
2967 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]);
Oliver Stannardd55e1152014-03-05 15:25:27 +00002968 }
Tim Northover8cda34f2015-03-11 18:54:22 +00002969
2970 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin);
2971 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize);
Oliver Stannardd55e1152014-03-05 15:25:27 +00002972
Bob Wilsona4c22902009-04-17 19:07:39 +00002973 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2974 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00002975 if (Ins[VA.getValNo()].isOrigArg()) {
2976 std::advance(CurOrigArg,
2977 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
2978 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
2979 }
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002980 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00002981 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002982 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00002983
Bob Wilsona4c22902009-04-17 19:07:39 +00002984 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002985 // f64 and vector types are split up into multiple registers or
2986 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00002987 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002988 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002989 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00002990 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00002991 SDValue ArgValue2;
2992 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00002993 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson699bdf72010-04-13 22:03:22 +00002994 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2995 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002996 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002997 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00002998 } else {
2999 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3000 Chain, DAG, dl);
3001 }
Owen Anderson9f944592009-08-11 20:47:22 +00003002 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3003 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00003004 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson9f944592009-08-11 20:47:22 +00003005 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00003006 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
3007 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003008 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003009
Bob Wilson2e076c42009-06-22 23:27:02 +00003010 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003011 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003012
Owen Anderson9f944592009-08-11 20:47:22 +00003013 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003014 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003015 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003016 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003017 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003018 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003019 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003020 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3021 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003022 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003023 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003024
3025 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003026 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003027 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003028 }
3029
3030 // If this is an 8 or 16-bit value, it is really passed promoted
3031 // to 32 bits. Insert an assert[sz]ext to capture this, then
3032 // truncate to the right size.
3033 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003034 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003035 case CCValAssign::Full: break;
3036 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003037 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003038 break;
3039 case CCValAssign::SExt:
3040 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3041 DAG.getValueType(VA.getValVT()));
3042 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3043 break;
3044 case CCValAssign::ZExt:
3045 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3046 DAG.getValueType(VA.getValVT()));
3047 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3048 break;
3049 }
3050
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003051 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003052
3053 } else { // VA.isRegLoc()
3054
3055 // sanity check
3056 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003057 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003058
Andrew Trick05938a52015-02-16 18:10:47 +00003059 int index = VA.getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003060
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003061 // Some Ins[] entries become multiple ArgLoc[] entries.
3062 // Process them only once.
3063 if (index != lastInsIndex)
3064 {
3065 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003066 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003067 // This can be changed with more analysis.
3068 // In case of tail call optimization mark all arguments mutable.
3069 // Since they could be overwritten by lowering of arguments in case of
3070 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003071 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003072 assert(Ins[index].isOrigArg() &&
3073 "Byval arguments cannot be implicit");
Daniel Sanders8104b752014-11-01 19:32:23 +00003074 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003075
Tim Northover8cda34f2015-03-11 18:54:22 +00003076 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, CurOrigArg,
3077 CurByValIndex, VA.getLocMemOffset(),
3078 Flags.getByValSize());
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003079 InVals.push_back(DAG.getFrameIndex(FrameIndex, getPointerTy()));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003080 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003081 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003082 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003083 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003084 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003085
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003086 // Create load nodes to retrieve arguments from the stack.
3087 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3088 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3089 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003090 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003091 }
3092 lastInsIndex = index;
3093 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003094 }
3095 }
3096
3097 // varargs
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003098 if (isVarArg && MFI->hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003099 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003100 CCInfo.getNextStackOffset(),
3101 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003102
Oliver Stannardb14c6252014-04-02 16:10:33 +00003103 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3104
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003105 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003106}
3107
3108/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003109static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003110 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003111 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003112 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003113 // Maybe this has already been legalized into the constant pool?
3114 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003115 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003116 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003117 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003118 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003119 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003120 } else if (Op->getOpcode() == ISD::BITCAST &&
3121 Op->getValueType(0) == MVT::f64) {
3122 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3123 // created by LowerConstantFP().
3124 SDValue BitcastOp = Op->getOperand(0);
3125 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM) {
3126 SDValue MoveOp = BitcastOp->getOperand(0);
3127 if (MoveOp->getOpcode() == ISD::TargetConstant &&
3128 cast<ConstantSDNode>(MoveOp)->getZExtValue() == 0) {
3129 return true;
3130 }
3131 }
Evan Cheng10043e22007-01-19 07:51:42 +00003132 }
3133 return false;
3134}
3135
Evan Cheng10043e22007-01-19 07:51:42 +00003136/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3137/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003138SDValue
3139ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003140 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003141 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003142 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003143 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003144 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003145 // Constant does not fit, try adjusting it by one?
3146 switch (CC) {
3147 default: break;
3148 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003149 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003150 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003151 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003152 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003153 }
3154 break;
3155 case ISD::SETULT:
3156 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003157 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003158 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003159 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003160 }
3161 break;
3162 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003163 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003164 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003165 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003166 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003167 }
3168 break;
3169 case ISD::SETULE:
3170 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003171 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003172 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003173 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003174 }
3175 break;
3176 }
3177 }
3178 }
3179
3180 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003181 ARMISD::NodeType CompareType;
3182 switch (CondCode) {
3183 default:
3184 CompareType = ARMISD::CMP;
3185 break;
3186 case ARMCC::EQ:
3187 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003188 // Uses only Z Flag
3189 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003190 break;
3191 }
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003192 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003193 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003194}
3195
3196/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003197SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003198ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003199 SDLoc dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003200 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003201 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003202 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003203 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003204 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003205 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3206 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003207}
3208
Bob Wilson45acbd02011-03-08 01:17:20 +00003209/// duplicateCmp - Glue values can have only one use, so this function
3210/// duplicates a comparison node.
3211SDValue
3212ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3213 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003214 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003215 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3216 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3217
3218 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3219 Cmp = Cmp.getOperand(0);
3220 Opc = Cmp.getOpcode();
3221 if (Opc == ARMISD::CMPFP)
3222 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3223 else {
3224 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3225 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3226 }
3227 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3228}
3229
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003230std::pair<SDValue, SDValue>
3231ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3232 SDValue &ARMcc) const {
3233 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3234
3235 SDValue Value, OverflowCmp;
3236 SDValue LHS = Op.getOperand(0);
3237 SDValue RHS = Op.getOperand(1);
3238
3239
3240 // FIXME: We are currently always generating CMPs because we don't support
3241 // generating CMN through the backend. This is not as good as the natural
3242 // CMP case because it causes a register dependency and cannot be folded
3243 // later.
3244
3245 switch (Op.getOpcode()) {
3246 default:
3247 llvm_unreachable("Unknown overflow instruction!");
3248 case ISD::SADDO:
3249 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3250 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3251 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3252 break;
3253 case ISD::UADDO:
3254 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3255 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3256 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3257 break;
3258 case ISD::SSUBO:
3259 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3260 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3261 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3262 break;
3263 case ISD::USUBO:
3264 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3265 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3266 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3267 break;
3268 } // switch (...)
3269
3270 return std::make_pair(Value, OverflowCmp);
3271}
3272
3273
3274SDValue
3275ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3276 // Let legalize expand this if it isn't a legal type yet.
3277 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3278 return SDValue();
3279
3280 SDValue Value, OverflowCmp;
3281 SDValue ARMcc;
3282 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3283 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3284 // We use 0 and 1 as false and true values.
3285 SDValue TVal = DAG.getConstant(1, MVT::i32);
3286 SDValue FVal = DAG.getConstant(0, MVT::i32);
3287 EVT VT = Op.getValueType();
3288
3289 SDValue Overflow = DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, TVal, FVal,
3290 ARMcc, CCR, OverflowCmp);
3291
3292 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
3293 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), VTs, Value, Overflow);
3294}
3295
3296
Bill Wendling6a981312010-08-11 08:43:16 +00003297SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3298 SDValue Cond = Op.getOperand(0);
3299 SDValue SelectTrue = Op.getOperand(1);
3300 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003301 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003302 unsigned Opc = Cond.getOpcode();
3303
3304 if (Cond.getResNo() == 1 &&
3305 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3306 Opc == ISD::USUBO)) {
3307 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3308 return SDValue();
3309
3310 SDValue Value, OverflowCmp;
3311 SDValue ARMcc;
3312 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3313 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3314 EVT VT = Op.getValueType();
3315
Oliver Stannard51b1d462014-08-21 12:50:31 +00003316 return getCMOV(SDLoc(Op), VT, SelectTrue, SelectFalse, ARMcc, CCR,
3317 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003318 }
Bill Wendling6a981312010-08-11 08:43:16 +00003319
3320 // Convert:
3321 //
3322 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3323 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3324 //
3325 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3326 const ConstantSDNode *CMOVTrue =
3327 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3328 const ConstantSDNode *CMOVFalse =
3329 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3330
3331 if (CMOVTrue && CMOVFalse) {
3332 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3333 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3334
3335 SDValue True;
3336 SDValue False;
3337 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3338 True = SelectTrue;
3339 False = SelectFalse;
3340 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3341 True = SelectFalse;
3342 False = SelectTrue;
3343 }
3344
3345 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003346 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003347 SDValue ARMcc = Cond.getOperand(2);
3348 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003349 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003350 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003351 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00003352 }
3353 }
3354 }
3355
Dan Gohmand4a77c42012-02-24 00:09:36 +00003356 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3357 // undefined bits before doing a full-word comparison with zero.
3358 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
3359 DAG.getConstant(1, Cond.getValueType()));
3360
Bill Wendling6a981312010-08-11 08:43:16 +00003361 return DAG.getSelectCC(dl, Cond,
3362 DAG.getConstant(0, Cond.getValueType()),
3363 SelectTrue, SelectFalse, ISD::SETNE);
3364}
3365
Joey Gouly881eab52013-08-22 15:29:11 +00003366static ISD::CondCode getInverseCCForVSEL(ISD::CondCode CC) {
3367 if (CC == ISD::SETNE)
3368 return ISD::SETEQ;
Weiming Zhao63871d22013-12-18 22:25:17 +00003369 return ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003370}
3371
3372static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3373 bool &swpCmpOps, bool &swpVselOps) {
3374 // Start by selecting the GE condition code for opcodes that return true for
3375 // 'equality'
3376 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3377 CC == ISD::SETULE)
3378 CondCode = ARMCC::GE;
3379
3380 // and GT for opcodes that return false for 'equality'.
3381 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3382 CC == ISD::SETULT)
3383 CondCode = ARMCC::GT;
3384
3385 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3386 // to swap the compare operands.
3387 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3388 CC == ISD::SETULT)
3389 swpCmpOps = true;
3390
3391 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3392 // If we have an unordered opcode, we need to swap the operands to the VSEL
3393 // instruction (effectively negating the condition).
3394 //
3395 // This also has the effect of swapping which one of 'less' or 'greater'
3396 // returns true, so we also swap the compare operands. It also switches
3397 // whether we return true for 'equality', so we compensate by picking the
3398 // opposite condition code to our original choice.
3399 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3400 CC == ISD::SETUGT) {
3401 swpCmpOps = !swpCmpOps;
3402 swpVselOps = !swpVselOps;
3403 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3404 }
3405
3406 // 'ordered' is 'anything but unordered', so use the VS condition code and
3407 // swap the VSEL operands.
3408 if (CC == ISD::SETO) {
3409 CondCode = ARMCC::VS;
3410 swpVselOps = true;
3411 }
3412
3413 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3414 // code and swap the VSEL operands.
3415 if (CC == ISD::SETUNE) {
3416 CondCode = ARMCC::EQ;
3417 swpVselOps = true;
3418 }
3419}
3420
Oliver Stannard51b1d462014-08-21 12:50:31 +00003421SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal,
3422 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
3423 SDValue Cmp, SelectionDAG &DAG) const {
3424 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
3425 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3426 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
3427 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3428 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
3429
3430 SDValue TrueLow = TrueVal.getValue(0);
3431 SDValue TrueHigh = TrueVal.getValue(1);
3432 SDValue FalseLow = FalseVal.getValue(0);
3433 SDValue FalseHigh = FalseVal.getValue(1);
3434
3435 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
3436 ARMcc, CCR, Cmp);
3437 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
3438 ARMcc, CCR, duplicateCmp(Cmp, DAG));
3439
3440 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
3441 } else {
3442 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3443 Cmp);
3444 }
3445}
3446
Dan Gohman21cea8a2010-04-17 15:26:15 +00003447SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003448 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003449 SDValue LHS = Op.getOperand(0);
3450 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003451 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003452 SDValue TrueVal = Op.getOperand(2);
3453 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003454 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003455
Oliver Stannard51b1d462014-08-21 12:50:31 +00003456 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3457 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3458 dl);
3459
3460 // If softenSetCCOperands only returned one value, we should compare it to
3461 // zero.
3462 if (!RHS.getNode()) {
3463 RHS = DAG.getConstant(0, LHS.getValueType());
3464 CC = ISD::SETNE;
3465 }
3466 }
3467
Owen Anderson9f944592009-08-11 20:47:22 +00003468 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003469 // Try to generate VSEL on ARMv8.
3470 // The VSEL instruction can't use all the usual ARM condition
3471 // codes: it only has two bits to select the condition code, so it's
3472 // constrained to use only GE, GT, VS and EQ.
3473 //
3474 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3475 // swap the operands of the previous compare instruction (effectively
3476 // inverting the compare condition, swapping 'less' and 'greater') and
3477 // sometimes need to swap the operands to the VSEL (which inverts the
3478 // condition in the sense of firing whenever the previous condition didn't)
Eric Christopher1889fdc2015-01-29 00:19:39 +00003479 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3480 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003481 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3482 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3483 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
3484 CC = getInverseCCForVSEL(CC);
3485 std::swap(TrueVal, FalseVal);
3486 }
3487 }
3488
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003489 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003490 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003491 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003492 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003493 }
3494
3495 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003496 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003497
Joey Gouly881eab52013-08-22 15:29:11 +00003498 // Try to generate VSEL on ARMv8.
Eric Christopher1889fdc2015-01-29 00:19:39 +00003499 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3500 TrueVal.getValueType() == MVT::f64)) {
Joey Goulye3dd6842013-08-23 12:01:13 +00003501 // We can select VMAXNM/VMINNM from a compare followed by a select with the
3502 // same operands, as follows:
3503 // c = fcmp [ogt, olt, ugt, ult] a, b
3504 // select c, a, b
3505 // We only do this in unsafe-fp-math, because signed zeros and NaNs are
3506 // handled differently than the original code sequence.
Oliver Stannard79efe412014-10-27 09:23:02 +00003507 if (getTargetMachine().Options.UnsafeFPMath) {
3508 if (LHS == TrueVal && RHS == FalseVal) {
3509 if (CC == ISD::SETOGT || CC == ISD::SETUGT)
3510 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal);
3511 if (CC == ISD::SETOLT || CC == ISD::SETULT)
3512 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal);
3513 } else if (LHS == FalseVal && RHS == TrueVal) {
3514 if (CC == ISD::SETOLT || CC == ISD::SETULT)
3515 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal);
3516 if (CC == ISD::SETOGT || CC == ISD::SETUGT)
3517 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal);
3518 }
Joey Goulye3dd6842013-08-23 12:01:13 +00003519 }
3520
Joey Gouly881eab52013-08-22 15:29:11 +00003521 bool swpCmpOps = false;
3522 bool swpVselOps = false;
3523 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3524
3525 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3526 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3527 if (swpCmpOps)
3528 std::swap(LHS, RHS);
3529 if (swpVselOps)
3530 std::swap(TrueVal, FalseVal);
3531 }
3532 }
3533
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003534 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3535 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003536 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003537 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003538 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003539 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003540 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003541 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003542 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003543 }
3544 return Result;
3545}
3546
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003547/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3548/// to morph to an integer compare sequence.
3549static bool canChangeToInt(SDValue Op, bool &SeenZero,
3550 const ARMSubtarget *Subtarget) {
3551 SDNode *N = Op.getNode();
3552 if (!N->hasOneUse())
3553 // Otherwise it requires moving the value from fp to integer registers.
3554 return false;
3555 if (!N->getNumValues())
3556 return false;
3557 EVT VT = Op.getValueType();
3558 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3559 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3560 // vmrs are very slow, e.g. cortex-a8.
3561 return false;
3562
3563 if (isFloatingPointZero(Op)) {
3564 SeenZero = true;
3565 return true;
3566 }
3567 return ISD::isNormalLoad(N);
3568}
3569
3570static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3571 if (isFloatingPointZero(Op))
3572 return DAG.getConstant(0, MVT::i32);
3573
3574 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003575 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003576 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003577 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003578 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003579
3580 llvm_unreachable("Unknown VFP cmp argument!");
3581}
3582
3583static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3584 SDValue &RetVal1, SDValue &RetVal2) {
3585 if (isFloatingPointZero(Op)) {
3586 RetVal1 = DAG.getConstant(0, MVT::i32);
3587 RetVal2 = DAG.getConstant(0, MVT::i32);
3588 return;
3589 }
3590
3591 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3592 SDValue Ptr = Ld->getBasePtr();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003593 RetVal1 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003594 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003595 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003596 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003597 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003598
3599 EVT PtrType = Ptr.getValueType();
3600 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003601 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003602 PtrType, Ptr, DAG.getConstant(4, PtrType));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003603 RetVal2 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003604 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003605 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003606 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003607 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003608 return;
3609 }
3610
3611 llvm_unreachable("Unknown VFP cmp argument!");
3612}
3613
3614/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3615/// f32 and even f64 comparisons to integer ones.
3616SDValue
3617ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3618 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003619 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003620 SDValue LHS = Op.getOperand(2);
3621 SDValue RHS = Op.getOperand(3);
3622 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003623 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003624
Evan Chengd12af5d2012-03-01 23:27:13 +00003625 bool LHSSeenZero = false;
3626 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3627 bool RHSSeenZero = false;
3628 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3629 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003630 // If unsafe fp math optimization is enabled and there are no other uses of
3631 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003632 // to an integer comparison.
3633 if (CC == ISD::SETOEQ)
3634 CC = ISD::SETEQ;
3635 else if (CC == ISD::SETUNE)
3636 CC = ISD::SETNE;
3637
Evan Chengd12af5d2012-03-01 23:27:13 +00003638 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003639 SDValue ARMcc;
3640 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003641 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3642 bitcastf32Toi32(LHS, DAG), Mask);
3643 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3644 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003645 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3646 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3647 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3648 Chain, Dest, ARMcc, CCR, Cmp);
3649 }
3650
3651 SDValue LHS1, LHS2;
3652 SDValue RHS1, RHS2;
3653 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3654 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003655 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3656 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003657 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3658 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003659 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003660 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003661 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003662 }
3663
3664 return SDValue();
3665}
3666
3667SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3668 SDValue Chain = Op.getOperand(0);
3669 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3670 SDValue LHS = Op.getOperand(2);
3671 SDValue RHS = Op.getOperand(3);
3672 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003673 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003674
Oliver Stannard51b1d462014-08-21 12:50:31 +00003675 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3676 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3677 dl);
3678
3679 // If softenSetCCOperands only returned one value, we should compare it to
3680 // zero.
3681 if (!RHS.getNode()) {
3682 RHS = DAG.getConstant(0, LHS.getValueType());
3683 CC = ISD::SETNE;
3684 }
3685 }
3686
Owen Anderson9f944592009-08-11 20:47:22 +00003687 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003688 SDValue ARMcc;
3689 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003690 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003691 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003692 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003693 }
3694
Owen Anderson9f944592009-08-11 20:47:22 +00003695 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003696
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003697 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003698 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3699 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3700 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3701 if (Result.getNode())
3702 return Result;
3703 }
3704
Evan Cheng10043e22007-01-19 07:51:42 +00003705 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003706 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003707
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003708 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3709 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003710 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003711 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003712 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003713 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003714 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003715 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3716 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003717 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003718 }
3719 return Res;
3720}
3721
Dan Gohman21cea8a2010-04-17 15:26:15 +00003722SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003723 SDValue Chain = Op.getOperand(0);
3724 SDValue Table = Op.getOperand(1);
3725 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003726 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003727
Owen Anderson53aa7a92009-08-10 22:56:29 +00003728 EVT PTy = getPointerTy();
Evan Cheng10043e22007-01-19 07:51:42 +00003729 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3730 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3f17aee2009-07-14 18:44:34 +00003731 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003732 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson9f944592009-08-11 20:47:22 +00003733 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chengc8bed032009-07-28 20:53:24 +00003734 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3735 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003736 if (Subtarget->isThumb2()) {
3737 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3738 // which does another jump to the destination. This also makes it easier
3739 // to translate it to TBB / TBH later.
3740 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003741 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Chengc6d70ae2009-07-29 02:18:14 +00003742 Addr, Op.getOperand(2), JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003743 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003744 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003745 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattner7727d052010-09-21 06:44:06 +00003746 MachinePointerInfo::getJumpTable(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003747 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003748 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003749 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson9f944592009-08-11 20:47:22 +00003750 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003751 } else {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003752 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003753 MachinePointerInfo::getJumpTable(),
3754 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003755 Chain = Addr.getValue(1);
Owen Anderson9f944592009-08-11 20:47:22 +00003756 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003757 }
Evan Cheng10043e22007-01-19 07:51:42 +00003758}
3759
Eli Friedman2d4055b2011-11-09 23:36:02 +00003760static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003761 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003762 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003763
James Molloy547d4c02012-02-20 09:24:05 +00003764 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3765 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3766 return Op;
3767 return DAG.UnrollVectorOp(Op.getNode());
3768 }
3769
3770 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3771 "Invalid type for custom lowering!");
3772 if (VT != MVT::v4i16)
3773 return DAG.UnrollVectorOp(Op.getNode());
3774
3775 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3776 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003777}
3778
Oliver Stannard51b1d462014-08-21 12:50:31 +00003779SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003780 EVT VT = Op.getValueType();
3781 if (VT.isVector())
3782 return LowerVectorFP_TO_INT(Op, DAG);
3783
Oliver Stannard51b1d462014-08-21 12:50:31 +00003784 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
3785 RTLIB::Libcall LC;
3786 if (Op.getOpcode() == ISD::FP_TO_SINT)
3787 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
3788 Op.getValueType());
3789 else
3790 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
3791 Op.getValueType());
3792 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3793 /*isSigned*/ false, SDLoc(Op)).first;
3794 }
3795
Andrew Trickef9de2a2013-05-25 02:42:55 +00003796 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003797 unsigned Opc;
3798
3799 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003800 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003801 case ISD::FP_TO_SINT:
3802 Opc = ARMISD::FTOSI;
3803 break;
3804 case ISD::FP_TO_UINT:
3805 Opc = ARMISD::FTOUI;
3806 break;
3807 }
3808 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peck527da1b2010-11-23 03:31:01 +00003809 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003810}
3811
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003812static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3813 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003814 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003815
Eli Friedman2d4055b2011-11-09 23:36:02 +00003816 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3817 if (VT.getVectorElementType() == MVT::f32)
3818 return Op;
3819 return DAG.UnrollVectorOp(Op.getNode());
3820 }
3821
Duncan Sandsa41634e2011-08-12 14:54:45 +00003822 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3823 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003824 if (VT != MVT::v4f32)
3825 return DAG.UnrollVectorOp(Op.getNode());
3826
3827 unsigned CastOpc;
3828 unsigned Opc;
3829 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003830 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003831 case ISD::SINT_TO_FP:
3832 CastOpc = ISD::SIGN_EXTEND;
3833 Opc = ISD::SINT_TO_FP;
3834 break;
3835 case ISD::UINT_TO_FP:
3836 CastOpc = ISD::ZERO_EXTEND;
3837 Opc = ISD::UINT_TO_FP;
3838 break;
3839 }
3840
3841 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3842 return DAG.getNode(Opc, dl, VT, Op);
3843}
3844
Oliver Stannard51b1d462014-08-21 12:50:31 +00003845SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00003846 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003847 if (VT.isVector())
3848 return LowerVectorINT_TO_FP(Op, DAG);
3849
Oliver Stannard51b1d462014-08-21 12:50:31 +00003850 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
3851 RTLIB::Libcall LC;
3852 if (Op.getOpcode() == ISD::SINT_TO_FP)
3853 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
3854 Op.getValueType());
3855 else
3856 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
3857 Op.getValueType());
3858 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3859 /*isSigned*/ false, SDLoc(Op)).first;
3860 }
3861
Andrew Trickef9de2a2013-05-25 02:42:55 +00003862 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003863 unsigned Opc;
3864
3865 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003866 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003867 case ISD::SINT_TO_FP:
3868 Opc = ARMISD::SITOF;
3869 break;
3870 case ISD::UINT_TO_FP:
3871 Opc = ARMISD::UITOF;
3872 break;
3873 }
3874
Wesley Peck527da1b2010-11-23 03:31:01 +00003875 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilsone4191e72010-03-19 22:51:32 +00003876 return DAG.getNode(Opc, dl, VT, Op);
3877}
3878
Evan Cheng25f93642010-07-08 02:08:50 +00003879SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00003880 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003881 SDValue Tmp0 = Op.getOperand(0);
3882 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003883 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003884 EVT VT = Op.getValueType();
3885 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00003886 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3887 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3888 bool UseNEON = !InGPR && Subtarget->hasNEON();
3889
3890 if (UseNEON) {
3891 // Use VBSL to copy the sign bit.
3892 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3893 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3894 DAG.getTargetConstant(EncodedVal, MVT::i32));
3895 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3896 if (VT == MVT::f64)
3897 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3898 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3899 DAG.getConstant(32, MVT::i32));
3900 else /*if (VT == MVT::f32)*/
3901 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3902 if (SrcVT == MVT::f32) {
3903 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3904 if (VT == MVT::f64)
3905 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3906 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3907 DAG.getConstant(32, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00003908 } else if (VT == MVT::f32)
3909 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3910 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3911 DAG.getConstant(32, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00003912 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3913 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3914
3915 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3916 MVT::i32);
3917 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3918 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3919 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00003920
Evan Chengd6b641e2011-02-23 02:24:55 +00003921 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3922 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3923 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00003924 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00003925 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3926 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3927 DAG.getConstant(0, MVT::i32));
3928 } else {
3929 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3930 }
3931
3932 return Res;
3933 }
Evan Cheng2da1c952011-02-11 02:28:55 +00003934
3935 // Bitcast operand 1 to i32.
3936 if (SrcVT == MVT::f64)
3937 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00003938 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00003939 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3940
Evan Chengd6b641e2011-02-23 02:24:55 +00003941 // Or in the signbit with integer operations.
3942 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3943 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3944 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3945 if (VT == MVT::f32) {
3946 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3947 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3948 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3949 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00003950 }
3951
Evan Chengd6b641e2011-02-23 02:24:55 +00003952 // f64: Or the high part with signbit and then combine two parts.
3953 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00003954 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00003955 SDValue Lo = Tmp0.getValue(0);
3956 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3957 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3958 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00003959}
3960
Evan Cheng168ced92010-05-22 01:47:14 +00003961SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3962 MachineFunction &MF = DAG.getMachineFunction();
3963 MachineFrameInfo *MFI = MF.getFrameInfo();
3964 MFI->setReturnAddressIsTaken(true);
3965
Bill Wendling908bf812014-01-06 00:43:20 +00003966 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003967 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003968
Evan Cheng168ced92010-05-22 01:47:14 +00003969 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003970 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00003971 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3972 if (Depth) {
3973 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3974 SDValue Offset = DAG.getConstant(4, MVT::i32);
3975 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3976 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003977 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00003978 }
3979
3980 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00003981 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00003982 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3983}
3984
Dan Gohman21cea8a2010-04-17 15:26:15 +00003985SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00003986 const ARMBaseRegisterInfo &ARI =
3987 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
3988 MachineFunction &MF = DAG.getMachineFunction();
3989 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003990 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00003991
Owen Anderson53aa7a92009-08-10 22:56:29 +00003992 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003993 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003994 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00003995 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003996 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3997 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00003998 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3999 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004000 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004001 return FrameAddr;
4002}
4003
Renato Golinc7aea402014-05-06 16:51:25 +00004004// FIXME? Maybe this could be a TableGen attribute on some registers and
4005// this table could be generated automatically from RegInfo.
Hal Finkelf0e086a2014-05-11 19:29:07 +00004006unsigned ARMTargetLowering::getRegisterByName(const char* RegName,
4007 EVT VT) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004008 unsigned Reg = StringSwitch<unsigned>(RegName)
4009 .Case("sp", ARM::SP)
4010 .Default(0);
4011 if (Reg)
4012 return Reg;
4013 report_fatal_error("Invalid register name global variable");
4014}
4015
Wesley Peck527da1b2010-11-23 03:31:01 +00004016/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004017/// expand a bit convert where either the source or destination type is i64 to
4018/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4019/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4020/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004021static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004022 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004023 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004024 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004025
Bob Wilson59b70ea2010-04-17 05:30:19 +00004026 // This function is only supposed to be called for i64 types, either as the
4027 // source or destination of the bit convert.
4028 EVT SrcVT = Op.getValueType();
4029 EVT DstVT = N->getValueType(0);
4030 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004031 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004032
Bob Wilson59b70ea2010-04-17 05:30:19 +00004033 // Turn i64->f64 into VMOVDRR.
4034 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00004035 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
4036 DAG.getConstant(0, MVT::i32));
4037 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
4038 DAG.getConstant(1, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004039 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004040 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004041 }
Bob Wilson7117a912009-03-20 22:42:55 +00004042
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004043 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004044 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004045 SDValue Cvt;
Christian Pirker6692e7c2014-05-14 16:59:44 +00004046 if (TLI.isBigEndian() && SrcVT.isVector() &&
4047 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004048 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4049 DAG.getVTList(MVT::i32, MVT::i32),
4050 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4051 else
4052 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4053 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004054 // Merge the pieces into a single i64 value.
4055 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4056 }
Bob Wilson7117a912009-03-20 22:42:55 +00004057
Bob Wilson59b70ea2010-04-17 05:30:19 +00004058 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004059}
4060
Bob Wilson2e076c42009-06-22 23:27:02 +00004061/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004062/// Zero vectors are used to represent vector negation and in those cases
4063/// will be implemented with the NEON VNEG instruction. However, VNEG does
4064/// not support i64 elements, so sometimes the zero vectors will need to be
4065/// explicitly constructed. Regardless, use a canonical VMOV to create the
4066/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004067static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004068 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004069 // The canonical modified immediate encoding of a zero vector is....0!
4070 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
4071 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4072 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004073 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004074}
4075
Jim Grosbach624fcb22009-10-31 21:00:56 +00004076/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4077/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004078SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4079 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004080 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4081 EVT VT = Op.getValueType();
4082 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004083 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004084 SDValue ShOpLo = Op.getOperand(0);
4085 SDValue ShOpHi = Op.getOperand(1);
4086 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004087 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004088 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004089
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004090 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4091
Jim Grosbach624fcb22009-10-31 21:00:56 +00004092 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4093 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4094 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4095 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4096 DAG.getConstant(VTBits, MVT::i32));
4097 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4098 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004099 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004100
4101 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4102 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004103 ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004104 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004105 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004106 CCR, Cmp);
4107
4108 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004109 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004110}
4111
Jim Grosbach5d994042009-10-31 19:38:01 +00004112/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4113/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004114SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4115 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004116 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4117 EVT VT = Op.getValueType();
4118 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004119 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004120 SDValue ShOpLo = Op.getOperand(0);
4121 SDValue ShOpHi = Op.getOperand(1);
4122 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004123 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004124
4125 assert(Op.getOpcode() == ISD::SHL_PARTS);
4126 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4127 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4128 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4129 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4130 DAG.getConstant(VTBits, MVT::i32));
4131 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4132 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4133
4134 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4135 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4136 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004137 ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004138 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004139 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004140 CCR, Cmp);
4141
4142 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004143 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004144}
4145
Jim Grosbach535d3b42010-09-08 03:54:02 +00004146SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004147 SelectionDAG &DAG) const {
4148 // The rounding mode is in bits 23:22 of the FPSCR.
4149 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4150 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4151 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004152 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004153 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
4154 DAG.getConstant(Intrinsic::arm_get_fpscr,
4155 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004156 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemanb69b1822010-08-03 21:31:55 +00004157 DAG.getConstant(1U << 22, MVT::i32));
4158 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
4159 DAG.getConstant(22, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004160 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemanb69b1822010-08-03 21:31:55 +00004161 DAG.getConstant(3, MVT::i32));
4162}
4163
Jim Grosbach8546ec92010-01-18 19:58:49 +00004164static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4165 const ARMSubtarget *ST) {
4166 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004167 SDLoc dl(N);
Jim Grosbach8546ec92010-01-18 19:58:49 +00004168
4169 if (!ST->hasV6T2Ops())
4170 return SDValue();
4171
4172 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
4173 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4174}
4175
Evan Chengb4eae132012-12-04 22:41:50 +00004176/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4177/// for each 16-bit element from operand, repeated. The basic idea is to
4178/// leverage vcnt to get the 8-bit counts, gather and add the results.
4179///
4180/// Trace for v4i16:
4181/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4182/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4183/// 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 +00004184/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004185/// [b0 b1 b2 b3 b4 b5 b6 b7]
4186/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4187/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4188/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4189static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4190 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004191 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004192
4193 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4194 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4195 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4196 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4197 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4198 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4199}
4200
4201/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4202/// bit-count for each 16-bit element from the operand. We need slightly
4203/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4204/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004205///
Evan Chengb4eae132012-12-04 22:41:50 +00004206/// Trace for v4i16:
4207/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4208/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4209/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4210/// v4i16:Extracted = [k0 k1 k2 k3 ]
4211static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4212 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004213 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004214
4215 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4216 if (VT.is64BitVector()) {
4217 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4218 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
4219 DAG.getIntPtrConstant(0));
4220 } else {
4221 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
4222 BitCounts, DAG.getIntPtrConstant(0));
4223 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4224 }
4225}
4226
4227/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4228/// bit-count for each 32-bit element from the operand. The idea here is
4229/// to split the vector into 16-bit elements, leverage the 16-bit count
4230/// routine, and then combine the results.
4231///
4232/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4233/// input = [v0 v1 ] (vi: 32-bit elements)
4234/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4235/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004236/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004237/// [k0 k1 k2 k3 ]
4238/// N1 =+[k1 k0 k3 k2 ]
4239/// [k0 k2 k1 k3 ]
4240/// N2 =+[k1 k3 k0 k2 ]
4241/// [k0 k2 k1 k3 ]
4242/// Extended =+[k1 k3 k0 k2 ]
4243/// [k0 k2 ]
4244/// Extracted=+[k1 k3 ]
4245///
4246static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4247 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004248 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004249
4250 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4251
4252 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4253 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4254 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4255 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4256 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4257
4258 if (VT.is64BitVector()) {
4259 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4260 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
4261 DAG.getIntPtrConstant(0));
4262 } else {
4263 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
4264 DAG.getIntPtrConstant(0));
4265 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4266 }
4267}
4268
4269static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4270 const ARMSubtarget *ST) {
4271 EVT VT = N->getValueType(0);
4272
4273 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004274 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4275 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004276 "Unexpected type for custom ctpop lowering");
4277
4278 if (VT.getVectorElementType() == MVT::i32)
4279 return lowerCTPOP32BitElements(N, DAG);
4280 else
4281 return lowerCTPOP16BitElements(N, DAG);
4282}
4283
Bob Wilson2e076c42009-06-22 23:27:02 +00004284static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4285 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004286 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004287 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004288
Bob Wilson7d471332010-11-18 21:16:28 +00004289 if (!VT.isVector())
4290 return SDValue();
4291
Bob Wilson2e076c42009-06-22 23:27:02 +00004292 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004293 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004294
Bob Wilson7d471332010-11-18 21:16:28 +00004295 // Left shifts translate directly to the vshiftu intrinsic.
4296 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004297 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilson7d471332010-11-18 21:16:28 +00004298 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
4299 N->getOperand(0), N->getOperand(1));
4300
4301 assert((N->getOpcode() == ISD::SRA ||
4302 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4303
4304 // NEON uses the same intrinsics for both left and right shifts. For
4305 // right shifts, the shift amounts are negative, so negate the vector of
4306 // shift amounts.
4307 EVT ShiftVT = N->getOperand(1).getValueType();
4308 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4309 getZeroVector(ShiftVT, DAG, dl),
4310 N->getOperand(1));
4311 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4312 Intrinsic::arm_neon_vshifts :
4313 Intrinsic::arm_neon_vshiftu);
4314 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4315 DAG.getConstant(vshiftInt, MVT::i32),
4316 N->getOperand(0), NegatedCount);
4317}
4318
4319static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4320 const ARMSubtarget *ST) {
4321 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004322 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004323
Eli Friedman682d8c12009-08-22 03:13:10 +00004324 // We can get here for a node like i32 = ISD::SHL i32, i64
4325 if (VT != MVT::i64)
4326 return SDValue();
4327
4328 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004329 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004330
Chris Lattnerf81d5882007-11-24 07:07:01 +00004331 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4332 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004333 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004334 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004335
Chris Lattnerf81d5882007-11-24 07:07:01 +00004336 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004337 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004338
Chris Lattnerf81d5882007-11-24 07:07:01 +00004339 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004340 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004341 DAG.getConstant(0, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004342 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004343 DAG.getConstant(1, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004344
Chris Lattnerf81d5882007-11-24 07:07:01 +00004345 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4346 // captures the result into a carry flag.
4347 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004348 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004349
Chris Lattnerf81d5882007-11-24 07:07:01 +00004350 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004351 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004352
Chris Lattnerf81d5882007-11-24 07:07:01 +00004353 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004354 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004355}
4356
Bob Wilson2e076c42009-06-22 23:27:02 +00004357static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4358 SDValue TmpOp0, TmpOp1;
4359 bool Invert = false;
4360 bool Swap = false;
4361 unsigned Opc = 0;
4362
4363 SDValue Op0 = Op.getOperand(0);
4364 SDValue Op1 = Op.getOperand(1);
4365 SDValue CC = Op.getOperand(2);
Tim Northover45aa89c2015-02-08 00:50:47 +00004366 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004367 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004368 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004369 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004370
Oliver Stannard51b1d462014-08-21 12:50:31 +00004371 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004372 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004373 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004374 case ISD::SETUNE:
4375 case ISD::SETNE: Invert = true; // Fallthrough
4376 case ISD::SETOEQ:
4377 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4378 case ISD::SETOLT:
4379 case ISD::SETLT: Swap = true; // Fallthrough
4380 case ISD::SETOGT:
4381 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4382 case ISD::SETOLE:
4383 case ISD::SETLE: Swap = true; // Fallthrough
4384 case ISD::SETOGE:
4385 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4386 case ISD::SETUGE: Swap = true; // Fallthrough
4387 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4388 case ISD::SETUGT: Swap = true; // Fallthrough
4389 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4390 case ISD::SETUEQ: Invert = true; // Fallthrough
4391 case ISD::SETONE:
4392 // Expand this to (OLT | OGT).
4393 TmpOp0 = Op0;
4394 TmpOp1 = Op1;
4395 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004396 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4397 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004398 break;
4399 case ISD::SETUO: Invert = true; // Fallthrough
4400 case ISD::SETO:
4401 // Expand this to (OLT | OGE).
4402 TmpOp0 = Op0;
4403 TmpOp1 = Op1;
4404 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004405 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4406 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004407 break;
4408 }
4409 } else {
4410 // Integer comparisons.
4411 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004412 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004413 case ISD::SETNE: Invert = true;
4414 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4415 case ISD::SETLT: Swap = true;
4416 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4417 case ISD::SETLE: Swap = true;
4418 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4419 case ISD::SETULT: Swap = true;
4420 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4421 case ISD::SETULE: Swap = true;
4422 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4423 }
4424
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004425 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004426 if (Opc == ARMISD::VCEQ) {
4427
4428 SDValue AndOp;
4429 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4430 AndOp = Op0;
4431 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4432 AndOp = Op1;
4433
4434 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004435 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004436 AndOp = AndOp.getOperand(0);
4437
4438 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4439 Opc = ARMISD::VTST;
Tim Northover45aa89c2015-02-08 00:50:47 +00004440 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
4441 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004442 Invert = !Invert;
4443 }
4444 }
4445 }
4446
4447 if (Swap)
4448 std::swap(Op0, Op1);
4449
Owen Andersonc7baee32010-11-08 23:21:22 +00004450 // If one of the operands is a constant vector zero, attempt to fold the
4451 // comparison to a specialized compare-against-zero form.
4452 SDValue SingleOp;
4453 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4454 SingleOp = Op0;
4455 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4456 if (Opc == ARMISD::VCGE)
4457 Opc = ARMISD::VCLEZ;
4458 else if (Opc == ARMISD::VCGT)
4459 Opc = ARMISD::VCLTZ;
4460 SingleOp = Op1;
4461 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004462
Owen Andersonc7baee32010-11-08 23:21:22 +00004463 SDValue Result;
4464 if (SingleOp.getNode()) {
4465 switch (Opc) {
4466 case ARMISD::VCEQ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004467 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004468 case ARMISD::VCGE:
Tim Northover45aa89c2015-02-08 00:50:47 +00004469 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004470 case ARMISD::VCLEZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004471 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004472 case ARMISD::VCGT:
Tim Northover45aa89c2015-02-08 00:50:47 +00004473 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004474 case ARMISD::VCLTZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004475 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004476 default:
Tim Northover45aa89c2015-02-08 00:50:47 +00004477 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004478 }
4479 } else {
Tim Northover45aa89c2015-02-08 00:50:47 +00004480 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004481 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004482
Tim Northover45aa89c2015-02-08 00:50:47 +00004483 Result = DAG.getSExtOrTrunc(Result, dl, VT);
4484
Bob Wilson2e076c42009-06-22 23:27:02 +00004485 if (Invert)
4486 Result = DAG.getNOT(dl, Result, VT);
4487
4488 return Result;
4489}
4490
Bob Wilson5b2b5042010-06-14 22:19:57 +00004491/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4492/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004493/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004494static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4495 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Andersona4076922010-11-05 21:57:54 +00004496 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004497 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004498
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004499 // SplatBitSize is set to the smallest size that splats the vector, so a
4500 // zero vector will always have SplatBitSize == 8. However, NEON modified
4501 // immediate instructions others than VMOV do not support the 8-bit encoding
4502 // of a zero vector, and the default encoding of zero is supposed to be the
4503 // 32-bit version.
4504 if (SplatBits == 0)
4505 SplatBitSize = 32;
4506
Bob Wilson2e076c42009-06-22 23:27:02 +00004507 switch (SplatBitSize) {
4508 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004509 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004510 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004511 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004512 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004513 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004514 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004515 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004516 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004517
4518 case 16:
4519 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004520 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004521 if ((SplatBits & ~0xff) == 0) {
4522 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004523 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004524 Imm = SplatBits;
4525 break;
4526 }
4527 if ((SplatBits & ~0xff00) == 0) {
4528 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004529 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004530 Imm = SplatBits >> 8;
4531 break;
4532 }
4533 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004534
4535 case 32:
4536 // NEON's 32-bit VMOV supports splat values where:
4537 // * only one byte is nonzero, or
4538 // * the least significant byte is 0xff and the second byte is nonzero, or
4539 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004540 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004541 if ((SplatBits & ~0xff) == 0) {
4542 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004543 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004544 Imm = SplatBits;
4545 break;
4546 }
4547 if ((SplatBits & ~0xff00) == 0) {
4548 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004549 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004550 Imm = SplatBits >> 8;
4551 break;
4552 }
4553 if ((SplatBits & ~0xff0000) == 0) {
4554 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004555 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004556 Imm = SplatBits >> 16;
4557 break;
4558 }
4559 if ((SplatBits & ~0xff000000) == 0) {
4560 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004561 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004562 Imm = SplatBits >> 24;
4563 break;
4564 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004565
Owen Andersona4076922010-11-05 21:57:54 +00004566 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4567 if (type == OtherModImm) return SDValue();
4568
Bob Wilson2e076c42009-06-22 23:27:02 +00004569 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004570 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4571 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004572 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004573 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004574 break;
4575 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004576
4577 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004578 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4579 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004580 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004581 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004582 break;
4583 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004584
4585 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4586 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4587 // VMOV.I32. A (very) minor optimization would be to replicate the value
4588 // and fall through here to test for a valid 64-bit splat. But, then the
4589 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004590 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004591
4592 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004593 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004594 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004595 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004596 uint64_t BitMask = 0xff;
4597 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004598 unsigned ImmMask = 1;
4599 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004600 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004601 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004602 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004603 Imm |= ImmMask;
4604 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004605 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004606 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004607 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004608 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004609 }
Christian Pirker6f81e752014-06-23 18:05:53 +00004610
4611 if (DAG.getTargetLoweringInfo().isBigEndian())
4612 // swap higher and lower 32 bit word
4613 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
4614
Bob Wilson6eae5202010-06-11 21:34:50 +00004615 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004616 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004617 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004618 break;
4619 }
4620
Bob Wilson6eae5202010-06-11 21:34:50 +00004621 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004622 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004623 }
4624
Bob Wilsona3f19012010-07-13 21:16:48 +00004625 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4626 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004627}
4628
Lang Hames591cdaf2012-03-29 21:56:11 +00004629SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4630 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004631 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004632 return SDValue();
4633
Tim Northoverf79c3a52013-08-20 08:57:11 +00004634 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004635 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004636
Oliver Stannard51b1d462014-08-21 12:50:31 +00004637 // Use the default (constant pool) lowering for double constants when we have
4638 // an SP-only FPU
4639 if (IsDouble && Subtarget->isFPOnlySP())
4640 return SDValue();
4641
Lang Hames591cdaf2012-03-29 21:56:11 +00004642 // Try splatting with a VMOV.f32...
4643 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004644 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4645
Lang Hames591cdaf2012-03-29 21:56:11 +00004646 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004647 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4648 // We have code in place to select a valid ConstantFP already, no need to
4649 // do any mangling.
4650 return Op;
4651 }
4652
4653 // It's a float and we are trying to use NEON operations where
4654 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004655 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004656 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4657 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4658 NewVal);
4659 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4660 DAG.getConstant(0, MVT::i32));
4661 }
4662
Tim Northoverf79c3a52013-08-20 08:57:11 +00004663 // The rest of our options are NEON only, make sure that's allowed before
4664 // proceeding..
4665 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4666 return SDValue();
4667
Lang Hames591cdaf2012-03-29 21:56:11 +00004668 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004669 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4670
4671 // It wouldn't really be worth bothering for doubles except for one very
4672 // important value, which does happen to match: 0.0. So make sure we don't do
4673 // anything stupid.
4674 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4675 return SDValue();
4676
4677 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
4678 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4679 false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004680 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004681 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004682 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4683 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004684 if (IsDouble)
4685 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4686
4687 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004688 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4689 VecConstant);
4690 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4691 DAG.getConstant(0, MVT::i32));
4692 }
4693
4694 // Finally, try a VMVN.i32
Tim Northoverf79c3a52013-08-20 08:57:11 +00004695 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4696 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004697 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004698 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004699 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004700
4701 if (IsDouble)
4702 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4703
4704 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004705 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4706 VecConstant);
4707 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4708 DAG.getConstant(0, MVT::i32));
4709 }
4710
4711 return SDValue();
4712}
4713
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004714// check if an VEXT instruction can handle the shuffle mask when the
4715// vector sources of the shuffle are the same.
4716static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4717 unsigned NumElts = VT.getVectorNumElements();
4718
4719 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4720 if (M[0] < 0)
4721 return false;
4722
4723 Imm = M[0];
4724
4725 // If this is a VEXT shuffle, the immediate value is the index of the first
4726 // element. The other shuffle indices must be the successive elements after
4727 // the first one.
4728 unsigned ExpectedElt = Imm;
4729 for (unsigned i = 1; i < NumElts; ++i) {
4730 // Increment the expected index. If it wraps around, just follow it
4731 // back to index zero and keep going.
4732 ++ExpectedElt;
4733 if (ExpectedElt == NumElts)
4734 ExpectedElt = 0;
4735
4736 if (M[i] < 0) continue; // ignore UNDEF indices
4737 if (ExpectedElt != static_cast<unsigned>(M[i]))
4738 return false;
4739 }
4740
4741 return true;
4742}
4743
Lang Hames591cdaf2012-03-29 21:56:11 +00004744
Benjamin Kramer339ced42012-01-15 13:16:05 +00004745static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004746 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004747 unsigned NumElts = VT.getVectorNumElements();
4748 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004749
4750 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4751 if (M[0] < 0)
4752 return false;
4753
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004754 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004755
4756 // If this is a VEXT shuffle, the immediate value is the index of the first
4757 // element. The other shuffle indices must be the successive elements after
4758 // the first one.
4759 unsigned ExpectedElt = Imm;
4760 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004761 // Increment the expected index. If it wraps around, it may still be
4762 // a VEXT but the source vectors must be swapped.
4763 ExpectedElt += 1;
4764 if (ExpectedElt == NumElts * 2) {
4765 ExpectedElt = 0;
4766 ReverseVEXT = true;
4767 }
4768
Bob Wilson411dfad2010-08-17 05:54:34 +00004769 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004770 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004771 return false;
4772 }
4773
4774 // Adjust the index value if the source operands will be swapped.
4775 if (ReverseVEXT)
4776 Imm -= NumElts;
4777
Bob Wilson32cd8552009-08-19 17:03:43 +00004778 return true;
4779}
4780
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004781/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4782/// instruction with the specified blocksize. (The order of the elements
4783/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00004784static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004785 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4786 "Only possible block sizes for VREV are: 16, 32, 64");
4787
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004788 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00004789 if (EltSz == 64)
4790 return false;
4791
4792 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004793 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00004794 // If the first shuffle index is UNDEF, be optimistic.
4795 if (M[0] < 0)
4796 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004797
4798 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4799 return false;
4800
4801 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004802 if (M[i] < 0) continue; // ignore UNDEF indices
4803 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004804 return false;
4805 }
4806
4807 return true;
4808}
4809
Benjamin Kramer339ced42012-01-15 13:16:05 +00004810static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00004811 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4812 // range, then 0 is placed into the resulting vector. So pretty much any mask
4813 // of 8 elements can work here.
4814 return VT == MVT::v8i8 && M.size() == 8;
4815}
4816
Benjamin Kramer339ced42012-01-15 13:16:05 +00004817static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004818 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4819 if (EltSz == 64)
4820 return false;
4821
Bob Wilsona7062312009-08-21 20:54:19 +00004822 unsigned NumElts = VT.getVectorNumElements();
4823 WhichResult = (M[0] == 0 ? 0 : 1);
4824 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004825 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4826 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsona7062312009-08-21 20:54:19 +00004827 return false;
4828 }
4829 return true;
4830}
4831
Bob Wilson0bbd3072009-12-03 06:40:55 +00004832/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4833/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4834/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004835static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004836 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4837 if (EltSz == 64)
4838 return false;
4839
4840 unsigned NumElts = VT.getVectorNumElements();
4841 WhichResult = (M[0] == 0 ? 0 : 1);
4842 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004843 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4844 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004845 return false;
4846 }
4847 return true;
4848}
4849
Benjamin Kramer339ced42012-01-15 13:16:05 +00004850static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004851 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4852 if (EltSz == 64)
4853 return false;
4854
Bob Wilsona7062312009-08-21 20:54:19 +00004855 unsigned NumElts = VT.getVectorNumElements();
4856 WhichResult = (M[0] == 0 ? 0 : 1);
4857 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004858 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsona7062312009-08-21 20:54:19 +00004859 if ((unsigned) M[i] != 2 * i + WhichResult)
4860 return false;
4861 }
4862
4863 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004864 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004865 return false;
4866
4867 return true;
4868}
4869
Bob Wilson0bbd3072009-12-03 06:40:55 +00004870/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4871/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4872/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00004873static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004874 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4875 if (EltSz == 64)
4876 return false;
4877
4878 unsigned Half = VT.getVectorNumElements() / 2;
4879 WhichResult = (M[0] == 0 ? 0 : 1);
4880 for (unsigned j = 0; j != 2; ++j) {
4881 unsigned Idx = WhichResult;
4882 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004883 int MIdx = M[i + j * Half];
4884 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson0bbd3072009-12-03 06:40:55 +00004885 return false;
4886 Idx += 2;
4887 }
4888 }
4889
4890 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4891 if (VT.is64BitVector() && EltSz == 32)
4892 return false;
4893
4894 return true;
4895}
4896
Benjamin Kramer339ced42012-01-15 13:16:05 +00004897static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004898 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4899 if (EltSz == 64)
4900 return false;
4901
Bob Wilsona7062312009-08-21 20:54:19 +00004902 unsigned NumElts = VT.getVectorNumElements();
4903 WhichResult = (M[0] == 0 ? 0 : 1);
4904 unsigned Idx = WhichResult * NumElts / 2;
4905 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004906 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4907 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsona7062312009-08-21 20:54:19 +00004908 return false;
4909 Idx += 1;
4910 }
4911
4912 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004913 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004914 return false;
4915
4916 return true;
4917}
4918
Bob Wilson0bbd3072009-12-03 06:40:55 +00004919/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4920/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4921/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004922static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004923 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4924 if (EltSz == 64)
4925 return false;
4926
4927 unsigned NumElts = VT.getVectorNumElements();
4928 WhichResult = (M[0] == 0 ? 0 : 1);
4929 unsigned Idx = WhichResult * NumElts / 2;
4930 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004931 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4932 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004933 return false;
4934 Idx += 1;
4935 }
4936
4937 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4938 if (VT.is64BitVector() && EltSz == 32)
4939 return false;
4940
4941 return true;
4942}
4943
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00004944/// \return true if this is a reverse operation on an vector.
4945static bool isReverseMask(ArrayRef<int> M, EVT VT) {
4946 unsigned NumElts = VT.getVectorNumElements();
4947 // Make sure the mask has the right size.
4948 if (NumElts != M.size())
4949 return false;
4950
4951 // Look for <15, ..., 3, -1, 1, 0>.
4952 for (unsigned i = 0; i != NumElts; ++i)
4953 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
4954 return false;
4955
4956 return true;
4957}
4958
Dale Johannesen2bff5052010-07-29 20:10:08 +00004959// If N is an integer constant that can be moved into a register in one
4960// instruction, return an SDValue of such a constant (will become a MOV
4961// instruction). Otherwise return null.
4962static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004963 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00004964 uint64_t Val;
4965 if (!isa<ConstantSDNode>(N))
4966 return SDValue();
4967 Val = cast<ConstantSDNode>(N)->getZExtValue();
4968
4969 if (ST->isThumb1Only()) {
4970 if (Val <= 255 || ~Val <= 255)
4971 return DAG.getConstant(Val, MVT::i32);
4972 } else {
4973 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4974 return DAG.getConstant(Val, MVT::i32);
4975 }
4976 return SDValue();
4977}
4978
Bob Wilson2e076c42009-06-22 23:27:02 +00004979// If this is a case we can't handle, return null and let the default
4980// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00004981SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4982 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00004983 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004984 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004985 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004986
4987 APInt SplatBits, SplatUndef;
4988 unsigned SplatBitSize;
4989 bool HasAnyUndefs;
4990 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00004991 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00004992 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00004993 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00004994 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00004995 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00004996 DAG, VmovVT, VT.is128BitVector(),
4997 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00004998 if (Val.getNode()) {
4999 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005000 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00005001 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00005002
5003 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00005004 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005005 Val = isNEONModifiedImm(NegatedImm,
5006 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00005007 DAG, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005008 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005009 if (Val.getNode()) {
5010 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005011 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005012 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005013
5014 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00005015 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00005016 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005017 if (ImmVal != -1) {
5018 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
5019 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
5020 }
5021 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005022 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00005023 }
5024
Bob Wilson91fdf682010-05-22 00:23:12 +00005025 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00005026 //
5027 // As an optimisation, even if more than one value is used it may be more
5028 // profitable to splat with one value then change some lanes.
5029 //
5030 // Heuristically we decide to do this if the vector has a "dominant" value,
5031 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00005032 unsigned NumElts = VT.getVectorNumElements();
5033 bool isOnlyLowElement = true;
5034 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005035 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00005036 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005037
5038 // Map of the number of times a particular SDValue appears in the
5039 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00005040 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00005041 SDValue Value;
5042 for (unsigned i = 0; i < NumElts; ++i) {
5043 SDValue V = Op.getOperand(i);
5044 if (V.getOpcode() == ISD::UNDEF)
5045 continue;
5046 if (i > 0)
5047 isOnlyLowElement = false;
5048 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5049 isConstant = false;
5050
James Molloy49bdbce2012-09-06 09:55:02 +00005051 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005052 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005053
James Molloy49bdbce2012-09-06 09:55:02 +00005054 // Is this value dominant? (takes up more than half of the lanes)
5055 if (++Count > (NumElts / 2)) {
5056 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005057 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005058 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005059 }
James Molloy49bdbce2012-09-06 09:55:02 +00005060 if (ValueCounts.size() != 1)
5061 usesOnlyOneValue = false;
5062 if (!Value.getNode() && ValueCounts.size() > 0)
5063 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005064
James Molloy49bdbce2012-09-06 09:55:02 +00005065 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005066 return DAG.getUNDEF(VT);
5067
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005068 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5069 // Keep going if we are hitting this case.
5070 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005071 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5072
Dale Johannesen2bff5052010-07-29 20:10:08 +00005073 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5074
Dale Johannesen710a2d92010-10-19 20:00:17 +00005075 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5076 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005077 if (hasDominantValue && EltSize <= 32) {
5078 if (!isConstant) {
5079 SDValue N;
5080
5081 // If we are VDUPing a value that comes directly from a vector, that will
5082 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005083 // just use VDUPLANE. We can only do this if the lane being extracted
5084 // is at a constant index, as the VDUP from lane instructions only have
5085 // constant-index forms.
5086 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5087 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005088 // We need to create a new undef vector to use for the VDUPLANE if the
5089 // size of the vector from which we get the value is different than the
5090 // size of the vector that we need to create. We will insert the element
5091 // such that the register coalescer will remove unnecessary copies.
5092 if (VT != Value->getOperand(0).getValueType()) {
5093 ConstantSDNode *constIndex;
5094 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
5095 assert(constIndex && "The index is not a constant!");
5096 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5097 VT.getVectorNumElements();
5098 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5099 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
5100 Value, DAG.getConstant(index, MVT::i32)),
5101 DAG.getConstant(index, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005102 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005103 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005104 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005105 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005106 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5107
5108 if (!usesOnlyOneValue) {
5109 // The dominant value was splatted as 'N', but we now have to insert
5110 // all differing elements.
5111 for (unsigned I = 0; I < NumElts; ++I) {
5112 if (Op.getOperand(I) == Value)
5113 continue;
5114 SmallVector<SDValue, 3> Ops;
5115 Ops.push_back(N);
5116 Ops.push_back(Op.getOperand(I));
5117 Ops.push_back(DAG.getConstant(I, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005118 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005119 }
5120 }
5121 return N;
5122 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005123 if (VT.getVectorElementType().isFloatingPoint()) {
5124 SmallVector<SDValue, 8> Ops;
5125 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005126 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005127 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005128 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005129 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005130 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5131 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005132 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005133 }
James Molloy49bdbce2012-09-06 09:55:02 +00005134 if (usesOnlyOneValue) {
5135 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5136 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005137 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005138 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005139 }
5140
5141 // If all elements are constants and the case above didn't get hit, fall back
5142 // to the default expansion, which will generate a load from the constant
5143 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005144 if (isConstant)
5145 return SDValue();
5146
Bob Wilson6f2b8962011-01-07 21:37:30 +00005147 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5148 if (NumElts >= 4) {
5149 SDValue shuffle = ReconstructShuffle(Op, DAG);
5150 if (shuffle != SDValue())
5151 return shuffle;
5152 }
5153
Bob Wilson91fdf682010-05-22 00:23:12 +00005154 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005155 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5156 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005157 if (EltSize >= 32) {
5158 // Do the expansion with floating-point types, since that is what the VFP
5159 // registers are defined to use, and since i64 is not legal.
5160 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5161 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005162 SmallVector<SDValue, 8> Ops;
5163 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005164 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005165 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005166 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005167 }
5168
Jim Grosbach24e102a2013-07-08 18:18:52 +00005169 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5170 // know the default expansion would otherwise fall back on something even
5171 // worse. For a vector with one or two non-undef values, that's
5172 // scalar_to_vector for the elements followed by a shuffle (provided the
5173 // shuffle is valid for the target) and materialization element by element
5174 // on the stack followed by a load for everything else.
5175 if (!isConstant && !usesOnlyOneValue) {
5176 SDValue Vec = DAG.getUNDEF(VT);
5177 for (unsigned i = 0 ; i < NumElts; ++i) {
5178 SDValue V = Op.getOperand(i);
5179 if (V.getOpcode() == ISD::UNDEF)
5180 continue;
5181 SDValue LaneIdx = DAG.getConstant(i, MVT::i32);
5182 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5183 }
5184 return Vec;
5185 }
5186
Bob Wilson2e076c42009-06-22 23:27:02 +00005187 return SDValue();
5188}
5189
Bob Wilson6f2b8962011-01-07 21:37:30 +00005190// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005191// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005192SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5193 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005194 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005195 EVT VT = Op.getValueType();
5196 unsigned NumElts = VT.getVectorNumElements();
5197
5198 SmallVector<SDValue, 2> SourceVecs;
5199 SmallVector<unsigned, 2> MinElts;
5200 SmallVector<unsigned, 2> MaxElts;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005201
Bob Wilson6f2b8962011-01-07 21:37:30 +00005202 for (unsigned i = 0; i < NumElts; ++i) {
5203 SDValue V = Op.getOperand(i);
5204 if (V.getOpcode() == ISD::UNDEF)
5205 continue;
5206 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5207 // A shuffle can only come from building a vector from various
5208 // elements of other vectors.
5209 return SDValue();
Eli Friedman74d1da52011-10-14 23:58:49 +00005210 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
5211 VT.getVectorElementType()) {
5212 // This code doesn't know how to handle shuffles where the vector
5213 // element types do not match (this happens because type legalization
5214 // promotes the return type of EXTRACT_VECTOR_ELT).
5215 // FIXME: It might be appropriate to extend this code to handle
5216 // mismatched types.
5217 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005218 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005219
Bob Wilson6f2b8962011-01-07 21:37:30 +00005220 // Record this extraction against the appropriate vector if possible...
5221 SDValue SourceVec = V.getOperand(0);
Jim Grosbach6df755c2012-07-25 17:02:47 +00005222 // If the element number isn't a constant, we can't effectively
5223 // analyze what's going on.
5224 if (!isa<ConstantSDNode>(V.getOperand(1)))
5225 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005226 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5227 bool FoundSource = false;
5228 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
5229 if (SourceVecs[j] == SourceVec) {
5230 if (MinElts[j] > EltNo)
5231 MinElts[j] = EltNo;
5232 if (MaxElts[j] < EltNo)
5233 MaxElts[j] = EltNo;
5234 FoundSource = true;
5235 break;
5236 }
5237 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005238
Bob Wilson6f2b8962011-01-07 21:37:30 +00005239 // Or record a new source if not...
5240 if (!FoundSource) {
5241 SourceVecs.push_back(SourceVec);
5242 MinElts.push_back(EltNo);
5243 MaxElts.push_back(EltNo);
5244 }
5245 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005246
Bob Wilson6f2b8962011-01-07 21:37:30 +00005247 // Currently only do something sane when at most two source vectors
5248 // involved.
5249 if (SourceVecs.size() > 2)
5250 return SDValue();
5251
5252 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
5253 int VEXTOffsets[2] = {0, 0};
Andrew Trick5eb0a302011-01-19 02:26:13 +00005254
Bob Wilson6f2b8962011-01-07 21:37:30 +00005255 // This loop extracts the usage patterns of the source vectors
5256 // and prepares appropriate SDValues for a shuffle if possible.
5257 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
5258 if (SourceVecs[i].getValueType() == VT) {
5259 // No VEXT necessary
5260 ShuffleSrcs[i] = SourceVecs[i];
5261 VEXTOffsets[i] = 0;
5262 continue;
5263 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
5264 // It probably isn't worth padding out a smaller vector just to
5265 // break it down again in a shuffle.
5266 return SDValue();
5267 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005268
Bob Wilson6f2b8962011-01-07 21:37:30 +00005269 // Since only 64-bit and 128-bit vectors are legal on ARM and
5270 // we've eliminated the other cases...
Bob Wilson3fa9c062011-01-07 23:40:46 +00005271 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
5272 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick5eb0a302011-01-19 02:26:13 +00005273
Bob Wilson6f2b8962011-01-07 21:37:30 +00005274 if (MaxElts[i] - MinElts[i] >= NumElts) {
5275 // Span too large for a VEXT to cope
5276 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005277 }
5278
Bob Wilson6f2b8962011-01-07 21:37:30 +00005279 if (MinElts[i] >= NumElts) {
5280 // The extraction can just take the second half
5281 VEXTOffsets[i] = NumElts;
Eric Christopher2af95512011-01-14 23:50:53 +00005282 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5283 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005284 DAG.getIntPtrConstant(NumElts));
5285 } else if (MaxElts[i] < NumElts) {
5286 // The extraction can just take the first half
5287 VEXTOffsets[i] = 0;
Eric Christopher2af95512011-01-14 23:50:53 +00005288 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5289 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005290 DAG.getIntPtrConstant(0));
5291 } else {
5292 // An actual VEXT is needed
5293 VEXTOffsets[i] = MinElts[i];
Eric Christopher2af95512011-01-14 23:50:53 +00005294 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5295 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005296 DAG.getIntPtrConstant(0));
Eric Christopher2af95512011-01-14 23:50:53 +00005297 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5298 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005299 DAG.getIntPtrConstant(NumElts));
5300 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
5301 DAG.getConstant(VEXTOffsets[i], MVT::i32));
5302 }
5303 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005304
Bob Wilson6f2b8962011-01-07 21:37:30 +00005305 SmallVector<int, 8> Mask;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005306
Bob Wilson6f2b8962011-01-07 21:37:30 +00005307 for (unsigned i = 0; i < NumElts; ++i) {
5308 SDValue Entry = Op.getOperand(i);
5309 if (Entry.getOpcode() == ISD::UNDEF) {
5310 Mask.push_back(-1);
5311 continue;
5312 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005313
Bob Wilson6f2b8962011-01-07 21:37:30 +00005314 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher2af95512011-01-14 23:50:53 +00005315 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
5316 .getOperand(1))->getSExtValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005317 if (ExtractVec == SourceVecs[0]) {
5318 Mask.push_back(ExtractElt - VEXTOffsets[0]);
5319 } else {
5320 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
5321 }
5322 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005323
Bob Wilson6f2b8962011-01-07 21:37:30 +00005324 // Final check before we try to produce nonsense...
5325 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher2af95512011-01-14 23:50:53 +00005326 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
5327 &Mask[0]);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005328
Bob Wilson6f2b8962011-01-07 21:37:30 +00005329 return SDValue();
5330}
5331
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005332/// isShuffleMaskLegal - Targets can use this to indicate that they only
5333/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5334/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5335/// are assumed to be legal.
5336bool
5337ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5338 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005339 if (VT.getVectorNumElements() == 4 &&
5340 (VT.is128BitVector() || VT.is64BitVector())) {
5341 unsigned PFIndexes[4];
5342 for (unsigned i = 0; i != 4; ++i) {
5343 if (M[i] < 0)
5344 PFIndexes[i] = 8;
5345 else
5346 PFIndexes[i] = M[i];
5347 }
5348
5349 // Compute the index in the perfect shuffle table.
5350 unsigned PFTableIndex =
5351 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5352 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5353 unsigned Cost = (PFEntry >> 30);
5354
5355 if (Cost <= 4)
5356 return true;
5357 }
5358
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005359 bool ReverseVEXT;
Bob Wilsona7062312009-08-21 20:54:19 +00005360 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005361
Bob Wilson846bd792010-06-07 23:53:38 +00005362 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5363 return (EltSize >= 32 ||
5364 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005365 isVREVMask(M, VT, 64) ||
5366 isVREVMask(M, VT, 32) ||
5367 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005368 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005369 isVTBLMask(M, VT) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005370 isVTRNMask(M, VT, WhichResult) ||
5371 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson0bbd3072009-12-03 06:40:55 +00005372 isVZIPMask(M, VT, WhichResult) ||
5373 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
5374 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005375 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
5376 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005377}
5378
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005379/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5380/// the specified operations to build the shuffle.
5381static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5382 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005383 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005384 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5385 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5386 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5387
5388 enum {
5389 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5390 OP_VREV,
5391 OP_VDUP0,
5392 OP_VDUP1,
5393 OP_VDUP2,
5394 OP_VDUP3,
5395 OP_VEXT1,
5396 OP_VEXT2,
5397 OP_VEXT3,
5398 OP_VUZPL, // VUZP, left result
5399 OP_VUZPR, // VUZP, right result
5400 OP_VZIPL, // VZIP, left result
5401 OP_VZIPR, // VZIP, right result
5402 OP_VTRNL, // VTRN, left result
5403 OP_VTRNR // VTRN, right result
5404 };
5405
5406 if (OpNum == OP_COPY) {
5407 if (LHSID == (1*9+2)*9+3) return LHS;
5408 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5409 return RHS;
5410 }
5411
5412 SDValue OpLHS, OpRHS;
5413 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5414 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5415 EVT VT = OpLHS.getValueType();
5416
5417 switch (OpNum) {
5418 default: llvm_unreachable("Unknown shuffle opcode!");
5419 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005420 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005421 if (VT.getVectorElementType() == MVT::i32 ||
5422 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005423 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5424 // vrev <4 x i16> -> VREV32
5425 if (VT.getVectorElementType() == MVT::i16)
5426 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5427 // vrev <4 x i8> -> VREV16
5428 assert(VT.getVectorElementType() == MVT::i8);
5429 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005430 case OP_VDUP0:
5431 case OP_VDUP1:
5432 case OP_VDUP2:
5433 case OP_VDUP3:
5434 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005435 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005436 case OP_VEXT1:
5437 case OP_VEXT2:
5438 case OP_VEXT3:
5439 return DAG.getNode(ARMISD::VEXT, dl, VT,
5440 OpLHS, OpRHS,
5441 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
5442 case OP_VUZPL:
5443 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005444 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005445 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5446 case OP_VZIPL:
5447 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005448 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005449 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5450 case OP_VTRNL:
5451 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005452 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5453 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005454 }
5455}
5456
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005457static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005458 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005459 SelectionDAG &DAG) {
5460 // Check to see if we can use the VTBL instruction.
5461 SDValue V1 = Op.getOperand(0);
5462 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005463 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005464
5465 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005466 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005467 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
5468 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
5469
5470 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5471 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005472 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005473
Owen Anderson77aa2662011-04-05 21:48:57 +00005474 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005475 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005476}
5477
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005478static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5479 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005480 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005481 SDValue OpLHS = Op.getOperand(0);
5482 EVT VT = OpLHS.getValueType();
5483
5484 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5485 "Expect an v8i16/v16i8 type");
5486 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5487 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5488 // extract the first 8 bytes into the top double word and the last 8 bytes
5489 // into the bottom double word. The v8i16 case is similar.
5490 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5491 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
5492 DAG.getConstant(ExtractNum, MVT::i32));
5493}
5494
Bob Wilson2e076c42009-06-22 23:27:02 +00005495static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005496 SDValue V1 = Op.getOperand(0);
5497 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005498 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005499 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005500 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005501
Bob Wilsonc6800b52009-08-13 02:13:04 +00005502 // Convert shuffles that are directly supported on NEON to target-specific
5503 // DAG nodes, instead of keeping them as shuffles and matching them again
5504 // during code selection. This is more efficient and avoids the possibility
5505 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005506 // FIXME: floating-point vectors should be canonicalized to integer vectors
5507 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005508 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005509
Bob Wilson846bd792010-06-07 23:53:38 +00005510 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5511 if (EltSize <= 32) {
5512 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5513 int Lane = SVN->getSplatIndex();
5514 // If this is undef splat, generate it via "just" vdup, if possible.
5515 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005516
Dan Gohman198b7ff2011-11-03 21:49:52 +00005517 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005518 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5519 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5520 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005521 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5522 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5523 // reaches it).
5524 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5525 !isa<ConstantSDNode>(V1.getOperand(0))) {
5526 bool IsScalarToVector = true;
5527 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5528 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5529 IsScalarToVector = false;
5530 break;
5531 }
5532 if (IsScalarToVector)
5533 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5534 }
Bob Wilson846bd792010-06-07 23:53:38 +00005535 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
5536 DAG.getConstant(Lane, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005537 }
Bob Wilson846bd792010-06-07 23:53:38 +00005538
5539 bool ReverseVEXT;
5540 unsigned Imm;
5541 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5542 if (ReverseVEXT)
5543 std::swap(V1, V2);
5544 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
5545 DAG.getConstant(Imm, MVT::i32));
5546 }
5547
5548 if (isVREVMask(ShuffleMask, VT, 64))
5549 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5550 if (isVREVMask(ShuffleMask, VT, 32))
5551 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5552 if (isVREVMask(ShuffleMask, VT, 16))
5553 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5554
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005555 if (V2->getOpcode() == ISD::UNDEF &&
5556 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5557 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
5558 DAG.getConstant(Imm, MVT::i32));
5559 }
5560
Bob Wilson846bd792010-06-07 23:53:38 +00005561 // Check for Neon shuffles that modify both input vectors in place.
5562 // If both results are used, i.e., if there are two shuffles with the same
5563 // source operands and with masks corresponding to both results of one of
5564 // these operations, DAG memoization will ensure that a single node is
5565 // used for both shuffles.
5566 unsigned WhichResult;
5567 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5568 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5569 V1, V2).getValue(WhichResult);
5570 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5571 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5572 V1, V2).getValue(WhichResult);
5573 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5574 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5575 V1, V2).getValue(WhichResult);
5576
5577 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5578 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5579 V1, V1).getValue(WhichResult);
5580 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5581 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5582 V1, V1).getValue(WhichResult);
5583 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5584 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5585 V1, V1).getValue(WhichResult);
Bob Wilsoncce31f62009-08-14 05:08:32 +00005586 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005587
Bob Wilsona7062312009-08-21 20:54:19 +00005588 // If the shuffle is not directly supported and it has 4 elements, use
5589 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005590 unsigned NumElts = VT.getVectorNumElements();
5591 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005592 unsigned PFIndexes[4];
5593 for (unsigned i = 0; i != 4; ++i) {
5594 if (ShuffleMask[i] < 0)
5595 PFIndexes[i] = 8;
5596 else
5597 PFIndexes[i] = ShuffleMask[i];
5598 }
5599
5600 // Compute the index in the perfect shuffle table.
5601 unsigned PFTableIndex =
5602 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005603 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5604 unsigned Cost = (PFEntry >> 30);
5605
5606 if (Cost <= 4)
5607 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5608 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00005609
Bob Wilsond8a9a042010-06-04 00:04:02 +00005610 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00005611 if (EltSize >= 32) {
5612 // Do the expansion with floating-point types, since that is what the VFP
5613 // registers are defined to use, and since i64 is not legal.
5614 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5615 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005616 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5617 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005618 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00005619 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00005620 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00005621 Ops.push_back(DAG.getUNDEF(EltVT));
5622 else
5623 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5624 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5625 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
5626 MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00005627 }
Craig Topper48d114b2014-04-26 18:35:24 +00005628 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005629 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00005630 }
5631
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005632 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5633 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
5634
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005635 if (VT == MVT::v8i8) {
5636 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
5637 if (NewOp.getNode())
5638 return NewOp;
5639 }
5640
Bob Wilson6f34e272009-08-14 05:16:33 +00005641 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005642}
5643
Eli Friedmana5e244c2011-10-24 23:08:52 +00005644static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5645 // INSERT_VECTOR_ELT is legal only for immediate indexes.
5646 SDValue Lane = Op.getOperand(2);
5647 if (!isa<ConstantSDNode>(Lane))
5648 return SDValue();
5649
5650 return Op;
5651}
5652
Bob Wilson2e076c42009-06-22 23:27:02 +00005653static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00005654 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00005655 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00005656 if (!isa<ConstantSDNode>(Lane))
5657 return SDValue();
5658
5659 SDValue Vec = Op.getOperand(0);
5660 if (Op.getValueType() == MVT::i32 &&
5661 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005662 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00005663 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5664 }
5665
5666 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00005667}
5668
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005669static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5670 // The only time a CONCAT_VECTORS operation can have legal types is when
5671 // two 64-bit vectors are concatenated to a 128-bit vector.
5672 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5673 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005674 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005675 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005676 SDValue Op0 = Op.getOperand(0);
5677 SDValue Op1 = Op.getOperand(1);
5678 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005679 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005680 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005681 DAG.getIntPtrConstant(0));
5682 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005683 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005684 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005685 DAG.getIntPtrConstant(1));
Wesley Peck527da1b2010-11-23 03:31:01 +00005686 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005687}
5688
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005689/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5690/// element has been zero/sign-extended, depending on the isSigned parameter,
5691/// from an integer type half its size.
5692static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5693 bool isSigned) {
5694 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5695 EVT VT = N->getValueType(0);
5696 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5697 SDNode *BVN = N->getOperand(0).getNode();
5698 if (BVN->getValueType(0) != MVT::v4i32 ||
5699 BVN->getOpcode() != ISD::BUILD_VECTOR)
5700 return false;
5701 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5702 unsigned HiElt = 1 - LoElt;
5703 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5704 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5705 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5706 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5707 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5708 return false;
5709 if (isSigned) {
5710 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5711 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5712 return true;
5713 } else {
5714 if (Hi0->isNullValue() && Hi1->isNullValue())
5715 return true;
5716 }
5717 return false;
5718 }
5719
5720 if (N->getOpcode() != ISD::BUILD_VECTOR)
5721 return false;
5722
5723 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5724 SDNode *Elt = N->getOperand(i).getNode();
5725 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5726 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5727 unsigned HalfSize = EltSize / 2;
5728 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005729 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005730 return false;
5731 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005732 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005733 return false;
5734 }
5735 continue;
5736 }
5737 return false;
5738 }
5739
5740 return true;
5741}
5742
5743/// isSignExtended - Check if a node is a vector value that is sign-extended
5744/// or a constant BUILD_VECTOR with sign-extended elements.
5745static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5746 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5747 return true;
5748 if (isExtendedBUILD_VECTOR(N, DAG, true))
5749 return true;
5750 return false;
5751}
5752
5753/// isZeroExtended - Check if a node is a vector value that is zero-extended
5754/// or a constant BUILD_VECTOR with zero-extended elements.
5755static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5756 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5757 return true;
5758 if (isExtendedBUILD_VECTOR(N, DAG, false))
5759 return true;
5760 return false;
5761}
5762
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005763static EVT getExtensionTo64Bits(const EVT &OrigVT) {
5764 if (OrigVT.getSizeInBits() >= 64)
5765 return OrigVT;
5766
5767 assert(OrigVT.isSimple() && "Expecting a simple value type");
5768
5769 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
5770 switch (OrigSimpleTy) {
5771 default: llvm_unreachable("Unexpected Vector Type");
5772 case MVT::v2i8:
5773 case MVT::v2i16:
5774 return MVT::v2i32;
5775 case MVT::v4i8:
5776 return MVT::v4i16;
5777 }
5778}
5779
Sebastian Popa204f722012-11-30 19:08:04 +00005780/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5781/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5782/// We insert the required extension here to get the vector to fill a D register.
5783static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5784 const EVT &OrigTy,
5785 const EVT &ExtTy,
5786 unsigned ExtOpcode) {
5787 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5788 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5789 // 64-bits we need to insert a new extension so that it will be 64-bits.
5790 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5791 if (OrigTy.getSizeInBits() >= 64)
5792 return N;
5793
5794 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005795 EVT NewVT = getExtensionTo64Bits(OrigTy);
5796
Andrew Trickef9de2a2013-05-25 02:42:55 +00005797 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00005798}
5799
5800/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5801/// does not do any sign/zero extension. If the original vector is less
5802/// than 64 bits, an appropriate extension will be added after the load to
5803/// reach a total size of 64 bits. We have to add the extension separately
5804/// because ARM does not have a sign/zero extending load for vectors.
5805static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005806 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
5807
5808 // The load already has the right type.
5809 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005810 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00005811 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5812 LD->isNonTemporal(), LD->isInvariant(),
5813 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005814
5815 // We need to create a zextload/sextload. We cannot just create a load
5816 // followed by a zext/zext node because LowerMUL is also run during normal
5817 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005818 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005819 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00005820 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(),
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005821 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00005822}
5823
5824/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5825/// extending load, or BUILD_VECTOR with extended elements, return the
5826/// unextended value. The unextended vector should be 64 bits so that it can
5827/// be used as an operand to a VMULL instruction. If the original vector size
5828/// before extension is less than 64 bits we add a an extension to resize
5829/// the vector to 64 bits.
5830static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00005831 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00005832 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5833 N->getOperand(0)->getValueType(0),
5834 N->getValueType(0),
5835 N->getOpcode());
5836
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005837 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00005838 return SkipLoadExtensionForVMULL(LD, DAG);
5839
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005840 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5841 // have been legalized as a BITCAST from v4i32.
5842 if (N->getOpcode() == ISD::BITCAST) {
5843 SDNode *BVN = N->getOperand(0).getNode();
5844 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5845 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5846 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00005847 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005848 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5849 }
5850 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5851 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5852 EVT VT = N->getValueType(0);
5853 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5854 unsigned NumElts = VT.getVectorNumElements();
5855 MVT TruncVT = MVT::getIntegerVT(EltSize);
5856 SmallVector<SDValue, 8> Ops;
5857 for (unsigned i = 0; i != NumElts; ++i) {
5858 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5859 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00005860 // Element types smaller than 32 bits are not legal, so use i32 elements.
5861 // The values are implicitly truncated so sext vs. zext doesn't matter.
5862 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005863 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005864 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topper48d114b2014-04-26 18:35:24 +00005865 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00005866}
5867
Evan Chenge2086e72011-03-29 01:56:09 +00005868static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5869 unsigned Opcode = N->getOpcode();
5870 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5871 SDNode *N0 = N->getOperand(0).getNode();
5872 SDNode *N1 = N->getOperand(1).getNode();
5873 return N0->hasOneUse() && N1->hasOneUse() &&
5874 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5875 }
5876 return false;
5877}
5878
5879static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5880 unsigned Opcode = N->getOpcode();
5881 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5882 SDNode *N0 = N->getOperand(0).getNode();
5883 SDNode *N1 = N->getOperand(1).getNode();
5884 return N0->hasOneUse() && N1->hasOneUse() &&
5885 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
5886 }
5887 return false;
5888}
5889
Bob Wilson38ab35a2010-09-01 23:50:19 +00005890static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
5891 // Multiplications are only custom-lowered for 128-bit vectors so that
5892 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
5893 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00005894 assert(VT.is128BitVector() && VT.isInteger() &&
5895 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00005896 SDNode *N0 = Op.getOperand(0).getNode();
5897 SDNode *N1 = Op.getOperand(1).getNode();
5898 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00005899 bool isMLA = false;
5900 bool isN0SExt = isSignExtended(N0, DAG);
5901 bool isN1SExt = isSignExtended(N1, DAG);
5902 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00005903 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00005904 else {
5905 bool isN0ZExt = isZeroExtended(N0, DAG);
5906 bool isN1ZExt = isZeroExtended(N1, DAG);
5907 if (isN0ZExt && isN1ZExt)
5908 NewOpc = ARMISD::VMULLu;
5909 else if (isN1SExt || isN1ZExt) {
5910 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5911 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5912 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5913 NewOpc = ARMISD::VMULLs;
5914 isMLA = true;
5915 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5916 NewOpc = ARMISD::VMULLu;
5917 isMLA = true;
5918 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5919 std::swap(N0, N1);
5920 NewOpc = ARMISD::VMULLu;
5921 isMLA = true;
5922 }
5923 }
5924
5925 if (!NewOpc) {
5926 if (VT == MVT::v2i64)
5927 // Fall through to expand this. It is not legal.
5928 return SDValue();
5929 else
5930 // Other vector multiplications are legal.
5931 return Op;
5932 }
5933 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005934
5935 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005936 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00005937 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00005938 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005939 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00005940 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005941 assert(Op0.getValueType().is64BitVector() &&
5942 Op1.getValueType().is64BitVector() &&
5943 "unexpected types for extended operands to VMULL");
5944 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
5945 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005946
Evan Chenge2086e72011-03-29 01:56:09 +00005947 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
5948 // isel lowering to take advantage of no-stall back to back vmul + vmla.
5949 // vmull q0, d4, d6
5950 // vmlal q0, d5, d6
5951 // is faster than
5952 // vaddl q0, d4, d5
5953 // vmovl q1, d6
5954 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00005955 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
5956 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005957 EVT Op1VT = Op1.getValueType();
5958 return DAG.getNode(N0->getOpcode(), DL, VT,
5959 DAG.getNode(NewOpc, DL, VT,
5960 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
5961 DAG.getNode(NewOpc, DL, VT,
5962 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00005963}
5964
Owen Anderson77aa2662011-04-05 21:48:57 +00005965static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005966LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005967 // Convert to float
5968 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
5969 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
5970 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
5971 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
5972 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
5973 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
5974 // Get reciprocal estimate.
5975 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00005976 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005977 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
5978 // Because char has a smaller range than uchar, we can actually get away
5979 // without any newton steps. This requires that we use a weird bias
5980 // of 0xb000, however (again, this has been exhaustively tested).
5981 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
5982 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
5983 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
5984 Y = DAG.getConstant(0xb000, MVT::i32);
5985 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
5986 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
5987 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
5988 // Convert back to short.
5989 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
5990 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
5991 return X;
5992}
5993
Owen Anderson77aa2662011-04-05 21:48:57 +00005994static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005995LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005996 SDValue N2;
5997 // Convert to float.
5998 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5999 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
6000 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
6001 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
6002 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
6003 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006004
Nate Begemanfa62d502011-02-11 20:53:29 +00006005 // Use reciprocal estimate and one refinement step.
6006 // float4 recip = vrecpeq_f32(yf);
6007 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006008 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006009 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006010 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006011 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
6012 N1, N2);
6013 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6014 // Because short has a smaller range than ushort, we can actually get away
6015 // with only a single newton step. This requires that we use a weird bias
6016 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006017 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00006018 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6019 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006020 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006021 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6022 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6023 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6024 // Convert back to integer and return.
6025 // return vmovn_s32(vcvt_s32_f32(result));
6026 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6027 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6028 return N0;
6029}
6030
6031static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
6032 EVT VT = Op.getValueType();
6033 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6034 "unexpected type for custom-lowering ISD::SDIV");
6035
Andrew Trickef9de2a2013-05-25 02:42:55 +00006036 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006037 SDValue N0 = Op.getOperand(0);
6038 SDValue N1 = Op.getOperand(1);
6039 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006040
Nate Begemanfa62d502011-02-11 20:53:29 +00006041 if (VT == MVT::v8i8) {
6042 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6043 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006044
Nate Begemanfa62d502011-02-11 20:53:29 +00006045 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6046 DAG.getIntPtrConstant(4));
6047 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00006048 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00006049 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6050 DAG.getIntPtrConstant(0));
6051 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
6052 DAG.getIntPtrConstant(0));
6053
6054 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6055 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6056
6057 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6058 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006059
Nate Begemanfa62d502011-02-11 20:53:29 +00006060 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6061 return N0;
6062 }
6063 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6064}
6065
6066static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
6067 EVT VT = Op.getValueType();
6068 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6069 "unexpected type for custom-lowering ISD::UDIV");
6070
Andrew Trickef9de2a2013-05-25 02:42:55 +00006071 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006072 SDValue N0 = Op.getOperand(0);
6073 SDValue N1 = Op.getOperand(1);
6074 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006075
Nate Begemanfa62d502011-02-11 20:53:29 +00006076 if (VT == MVT::v8i8) {
6077 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6078 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006079
Nate Begemanfa62d502011-02-11 20:53:29 +00006080 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6081 DAG.getIntPtrConstant(4));
6082 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00006083 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00006084 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6085 DAG.getIntPtrConstant(0));
6086 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
6087 DAG.getIntPtrConstant(0));
Owen Anderson77aa2662011-04-05 21:48:57 +00006088
Nate Begemanfa62d502011-02-11 20:53:29 +00006089 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6090 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006091
Nate Begemanfa62d502011-02-11 20:53:29 +00006092 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6093 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006094
6095 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begemanfa62d502011-02-11 20:53:29 +00006096 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
6097 N0);
6098 return N0;
6099 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006100
Nate Begemanfa62d502011-02-11 20:53:29 +00006101 // v4i16 sdiv ... Convert to float.
6102 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6103 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6104 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6105 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6106 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006107 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006108
6109 // Use reciprocal estimate and two refinement steps.
6110 // float4 recip = vrecpeq_f32(yf);
6111 // recip *= vrecpsq_f32(yf, recip);
6112 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006113 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006114 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006115 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006116 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006117 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006118 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006119 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006120 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006121 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006122 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6123 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6124 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6125 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006126 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006127 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6128 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
6129 N1 = DAG.getConstant(2, MVT::i32);
6130 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6131 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6132 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6133 // Convert back to integer and return.
6134 // return vmovn_u32(vcvt_s32_f32(result));
6135 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6136 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6137 return N0;
6138}
6139
Evan Chenge8916542011-08-30 01:34:54 +00006140static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6141 EVT VT = Op.getNode()->getValueType(0);
6142 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6143
6144 unsigned Opc;
6145 bool ExtraOp = false;
6146 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006147 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006148 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6149 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6150 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6151 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6152 }
6153
6154 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006155 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006156 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006157 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006158 Op.getOperand(1), Op.getOperand(2));
6159}
6160
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006161SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6162 assert(Subtarget->isTargetDarwin());
6163
6164 // For iOS, we want to call an alternative entry point: __sincos_stret,
6165 // return values are passed via sret.
6166 SDLoc dl(Op);
6167 SDValue Arg = Op.getOperand(0);
6168 EVT ArgVT = Arg.getValueType();
6169 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
6170
6171 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
6172 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6173
6174 // Pair of floats / doubles used to pass the result.
Reid Kleckner343c3952014-11-20 23:51:47 +00006175 StructType *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006176
6177 // Create stack object for sret.
6178 const uint64_t ByteSize = TLI.getDataLayout()->getTypeAllocSize(RetTy);
6179 const unsigned StackAlign = TLI.getDataLayout()->getPrefTypeAlignment(RetTy);
6180 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6181 SDValue SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
6182
6183 ArgListTy Args;
6184 ArgListEntry Entry;
6185
6186 Entry.Node = SRet;
6187 Entry.Ty = RetTy->getPointerTo();
6188 Entry.isSExt = false;
6189 Entry.isZExt = false;
6190 Entry.isSRet = true;
6191 Args.push_back(Entry);
6192
6193 Entry.Node = Arg;
6194 Entry.Ty = ArgTy;
6195 Entry.isSExt = false;
6196 Entry.isZExt = false;
6197 Args.push_back(Entry);
6198
6199 const char *LibcallName = (ArgVT == MVT::f64)
6200 ? "__sincos_stret" : "__sincosf_stret";
6201 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
6202
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006203 TargetLowering::CallLoweringInfo CLI(DAG);
6204 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
6205 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), Callee,
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00006206 std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006207 .setDiscardResult();
6208
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006209 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6210
6211 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6212 MachinePointerInfo(), false, false, false, 0);
6213
6214 // Address of cos field.
6215 SDValue Add = DAG.getNode(ISD::ADD, dl, getPointerTy(), SRet,
6216 DAG.getIntPtrConstant(ArgVT.getStoreSize()));
6217 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6218 MachinePointerInfo(), false, false, false, 0);
6219
6220 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6221 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6222 LoadSin.getValue(0), LoadCos.getValue(0));
6223}
6224
Eli Friedman10f9ce22011-09-15 22:26:18 +00006225static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006226 // Monotonic load/store is legal for all targets
6227 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6228 return Op;
6229
Alp Tokercb402912014-01-24 17:20:08 +00006230 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006231 // dmb or equivalent available.
6232 return SDValue();
6233}
6234
Tim Northoverbc933082013-05-23 19:11:20 +00006235static void ReplaceREADCYCLECOUNTER(SDNode *N,
6236 SmallVectorImpl<SDValue> &Results,
6237 SelectionDAG &DAG,
6238 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006239 SDLoc DL(N);
Tim Northoverbc933082013-05-23 19:11:20 +00006240 SDValue Cycles32, OutChain;
6241
6242 if (Subtarget->hasPerfMon()) {
6243 // Under Power Management extensions, the cycle-count is:
6244 // mrc p15, #0, <Rt>, c9, c13, #0
6245 SDValue Ops[] = { N->getOperand(0), // Chain
6246 DAG.getConstant(Intrinsic::arm_mrc, MVT::i32),
6247 DAG.getConstant(15, MVT::i32),
6248 DAG.getConstant(0, MVT::i32),
6249 DAG.getConstant(9, MVT::i32),
6250 DAG.getConstant(13, MVT::i32),
6251 DAG.getConstant(0, MVT::i32)
6252 };
6253
6254 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00006255 DAG.getVTList(MVT::i32, MVT::Other), Ops);
Tim Northoverbc933082013-05-23 19:11:20 +00006256 OutChain = Cycles32.getValue(1);
6257 } else {
6258 // Intrinsic is defined to return 0 on unsupported platforms. Technically
6259 // there are older ARM CPUs that have implementation-specific ways of
6260 // obtaining this information (FIXME!).
6261 Cycles32 = DAG.getConstant(0, MVT::i32);
6262 OutChain = DAG.getEntryNode();
6263 }
6264
6265
6266 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64,
6267 Cycles32, DAG.getConstant(0, MVT::i32));
6268 Results.push_back(Cycles64);
6269 Results.push_back(OutChain);
6270}
6271
Dan Gohman21cea8a2010-04-17 15:26:15 +00006272SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006273 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006274 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Cheng10043e22007-01-19 07:51:42 +00006275 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006276 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006277 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006278 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6279 default: llvm_unreachable("unknown object format");
6280 case Triple::COFF:
6281 return LowerGlobalAddressWindows(Op, DAG);
6282 case Triple::ELF:
6283 return LowerGlobalAddressELF(Op, DAG);
6284 case Triple::MachO:
6285 return LowerGlobalAddressDarwin(Op, DAG);
6286 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006287 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006288 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006289 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6290 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006291 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006292 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006293 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006294 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006295 case ISD::SINT_TO_FP:
6296 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6297 case ISD::FP_TO_SINT:
6298 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006299 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006300 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006301 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006302 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006303 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006304 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006305 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6306 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006307 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006308 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006309 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006310 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng15b80e42009-11-12 07:13:11 +00006311 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006312 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006313 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach8546ec92010-01-18 19:58:49 +00006314 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006315 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006316 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006317 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006318 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006319 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006320 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006321 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006322 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006323 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006324 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanfa62d502011-02-11 20:53:29 +00006325 case ISD::SDIV: return LowerSDIV(Op, DAG);
6326 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006327 case ISD::ADDC:
6328 case ISD::ADDE:
6329 case ISD::SUBC:
6330 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006331 case ISD::SADDO:
6332 case ISD::UADDO:
6333 case ISD::SSUBO:
6334 case ISD::USUBO:
6335 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006336 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006337 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006338 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006339 case ISD::SDIVREM:
6340 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00006341 case ISD::DYNAMIC_STACKALLOC:
6342 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
6343 return LowerDYNAMIC_STACKALLOC(Op, DAG);
6344 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00006345 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
6346 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006347 }
Evan Cheng10043e22007-01-19 07:51:42 +00006348}
6349
Duncan Sands6ed40142008-12-01 11:39:25 +00006350/// ReplaceNodeResults - Replace the results of node with an illegal result
6351/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006352void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
6353 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006354 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006355 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006356 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006357 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006358 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peck527da1b2010-11-23 03:31:01 +00006359 case ISD::BITCAST:
6360 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006361 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006362 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006363 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006364 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006365 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006366 case ISD::READCYCLECOUNTER:
6367 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6368 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00006369 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006370 if (Res.getNode())
6371 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006372}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006373
Evan Cheng10043e22007-01-19 07:51:42 +00006374//===----------------------------------------------------------------------===//
6375// ARM Scheduler Hooks
6376//===----------------------------------------------------------------------===//
6377
Bill Wendling030b58e2011-10-06 22:18:16 +00006378/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6379/// registers the function context.
6380void ARMTargetLowering::
6381SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6382 MachineBasicBlock *DispatchBB, int FI) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006383 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling374ee192011-10-03 21:25:38 +00006384 DebugLoc dl = MI->getDebugLoc();
6385 MachineFunction *MF = MBB->getParent();
6386 MachineRegisterInfo *MRI = &MF->getRegInfo();
6387 MachineConstantPool *MCP = MF->getConstantPool();
6388 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6389 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006390
Bill Wendling374ee192011-10-03 21:25:38 +00006391 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006392 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006393
Bill Wendling374ee192011-10-03 21:25:38 +00006394 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006395 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006396 ARMConstantPoolValue *CPV =
6397 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6398 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6399
Craig Topper61e88f42014-11-21 05:58:21 +00006400 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
6401 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006402
Bill Wendling030b58e2011-10-06 22:18:16 +00006403 // Grab constant pool and fixed stack memory operands.
6404 MachineMemOperand *CPMMO =
6405 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6406 MachineMemOperand::MOLoad, 4, 4);
6407
6408 MachineMemOperand *FIMMOSt =
6409 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6410 MachineMemOperand::MOStore, 4, 4);
6411
6412 // Load the address of the dispatch MBB into the jump buffer.
6413 if (isThumb2) {
6414 // Incoming value: jbuf
6415 // ldr.n r5, LCPI1_1
6416 // orr r5, r5, #1
6417 // add r5, pc
6418 // str r5, [$jbuf, #+4] ; &jbuf[1]
6419 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6420 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6421 .addConstantPoolIndex(CPI)
6422 .addMemOperand(CPMMO));
6423 // Set the low bit because of thumb mode.
6424 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6425 AddDefaultCC(
6426 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6427 .addReg(NewVReg1, RegState::Kill)
6428 .addImm(0x01)));
6429 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6430 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6431 .addReg(NewVReg2, RegState::Kill)
6432 .addImm(PCLabelId);
6433 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6434 .addReg(NewVReg3, RegState::Kill)
6435 .addFrameIndex(FI)
6436 .addImm(36) // &jbuf[1] :: pc
6437 .addMemOperand(FIMMOSt));
6438 } else if (isThumb) {
6439 // Incoming value: jbuf
6440 // ldr.n r1, LCPI1_4
6441 // add r1, pc
6442 // mov r2, #1
6443 // orrs r1, r2
6444 // add r2, $jbuf, #+4 ; &jbuf[1]
6445 // str r1, [r2]
6446 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6447 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6448 .addConstantPoolIndex(CPI)
6449 .addMemOperand(CPMMO));
6450 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6451 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6452 .addReg(NewVReg1, RegState::Kill)
6453 .addImm(PCLabelId);
6454 // Set the low bit because of thumb mode.
6455 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6456 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6457 .addReg(ARM::CPSR, RegState::Define)
6458 .addImm(1));
6459 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6460 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6461 .addReg(ARM::CPSR, RegState::Define)
6462 .addReg(NewVReg2, RegState::Kill)
6463 .addReg(NewVReg3, RegState::Kill));
6464 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00006465 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
6466 .addFrameIndex(FI)
6467 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00006468 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6469 .addReg(NewVReg4, RegState::Kill)
6470 .addReg(NewVReg5, RegState::Kill)
6471 .addImm(0)
6472 .addMemOperand(FIMMOSt));
6473 } else {
6474 // Incoming value: jbuf
6475 // ldr r1, LCPI1_1
6476 // add r1, pc, r1
6477 // str r1, [$jbuf, #+4] ; &jbuf[1]
6478 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6479 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6480 .addConstantPoolIndex(CPI)
6481 .addImm(0)
6482 .addMemOperand(CPMMO));
6483 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6484 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6485 .addReg(NewVReg1, RegState::Kill)
6486 .addImm(PCLabelId));
6487 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6488 .addReg(NewVReg2, RegState::Kill)
6489 .addFrameIndex(FI)
6490 .addImm(36) // &jbuf[1] :: pc
6491 .addMemOperand(FIMMOSt));
6492 }
6493}
6494
6495MachineBasicBlock *ARMTargetLowering::
6496EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006497 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00006498 DebugLoc dl = MI->getDebugLoc();
6499 MachineFunction *MF = MBB->getParent();
6500 MachineRegisterInfo *MRI = &MF->getRegInfo();
6501 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6502 MachineFrameInfo *MFI = MF->getFrameInfo();
6503 int FI = MFI->getFunctionContextIndex();
6504
Craig Topper61e88f42014-11-21 05:58:21 +00006505 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
6506 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00006507
Bill Wendling362c1b02011-10-06 21:29:56 +00006508 // Get a mapping of the call site numbers to all of the landing pads they're
6509 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00006510 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6511 unsigned MaxCSNum = 0;
6512 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00006513 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6514 ++BB) {
Bill Wendling202803e2011-10-05 00:02:33 +00006515 if (!BB->isLandingPad()) continue;
6516
6517 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6518 // pad.
6519 for (MachineBasicBlock::iterator
6520 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6521 if (!II->isEHLabel()) continue;
6522
6523 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006524 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006525
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006526 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6527 for (SmallVectorImpl<unsigned>::iterator
6528 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6529 CSI != CSE; ++CSI) {
6530 CallSiteNumToLPad[*CSI].push_back(BB);
6531 MaxCSNum = std::max(MaxCSNum, *CSI);
6532 }
Bill Wendling202803e2011-10-05 00:02:33 +00006533 break;
6534 }
6535 }
6536
6537 // Get an ordered list of the machine basic blocks for the jump table.
6538 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00006539 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00006540 LPadList.reserve(CallSiteNumToLPad.size());
6541 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6542 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6543 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006544 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00006545 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00006546 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6547 }
Bill Wendling202803e2011-10-05 00:02:33 +00006548 }
6549
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006550 assert(!LPadList.empty() &&
6551 "No landing pad destinations for the dispatch jump table!");
6552
Bill Wendling362c1b02011-10-06 21:29:56 +00006553 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00006554 MachineJumpTableInfo *JTI =
6555 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6556 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6557 unsigned UId = AFI->createJumpTableUId();
Chad Rosier96603432013-03-01 18:30:38 +00006558 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00006559
Bill Wendling362c1b02011-10-06 21:29:56 +00006560 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00006561
6562 // Shove the dispatch's address into the return slot in the function context.
6563 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6564 DispatchBB->setIsLandingPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00006565
Bill Wendling324be982011-10-05 00:39:32 +00006566 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006567 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00006568 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006569 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00006570 else
6571 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6572
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006573 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00006574 DispatchBB->addSuccessor(TrapBB);
6575
6576 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6577 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00006578
Bill Wendling510fbcd2011-10-17 21:32:56 +00006579 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00006580 MF->insert(MF->end(), DispatchBB);
6581 MF->insert(MF->end(), DispContBB);
6582 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00006583
Bill Wendling030b58e2011-10-06 22:18:16 +00006584 // Insert code into the entry block that creates and registers the function
6585 // context.
6586 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6587
Bill Wendling030b58e2011-10-06 22:18:16 +00006588 MachineMemOperand *FIMMOLd =
Bill Wendling362c1b02011-10-06 21:29:56 +00006589 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendlingb3d46782011-10-06 23:37:36 +00006590 MachineMemOperand::MOLoad |
6591 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00006592
Chad Rosier1ec8e402012-11-06 23:05:24 +00006593 MachineInstrBuilder MIB;
6594 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6595
6596 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6597 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6598
6599 // Add a register mask with no preserved registers. This results in all
6600 // registers being marked as clobbered.
6601 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00006602
Bill Wendling85833f72011-10-18 22:49:07 +00006603 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00006604 if (Subtarget->isThumb2()) {
6605 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6606 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6607 .addFrameIndex(FI)
6608 .addImm(4)
6609 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006610
Bill Wendling85833f72011-10-18 22:49:07 +00006611 if (NumLPads < 256) {
6612 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6613 .addReg(NewVReg1)
6614 .addImm(LPadList.size()));
6615 } else {
6616 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6617 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006618 .addImm(NumLPads & 0xFFFF));
6619
6620 unsigned VReg2 = VReg1;
6621 if ((NumLPads & 0xFFFF0000) != 0) {
6622 VReg2 = MRI->createVirtualRegister(TRC);
6623 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6624 .addReg(VReg1)
6625 .addImm(NumLPads >> 16));
6626 }
6627
Bill Wendling85833f72011-10-18 22:49:07 +00006628 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6629 .addReg(NewVReg1)
6630 .addReg(VReg2));
6631 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006632
Bill Wendling5626c662011-10-06 22:53:00 +00006633 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6634 .addMBB(TrapBB)
6635 .addImm(ARMCC::HI)
6636 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00006637
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006638 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6639 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006640 .addJumpTableIndex(MJTI)
6641 .addImm(UId));
Bill Wendling202803e2011-10-05 00:02:33 +00006642
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006643 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006644 AddDefaultCC(
6645 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006646 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6647 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00006648 .addReg(NewVReg1)
6649 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6650
6651 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006652 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00006653 .addReg(NewVReg1)
Bill Wendling5626c662011-10-06 22:53:00 +00006654 .addJumpTableIndex(MJTI)
6655 .addImm(UId);
6656 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00006657 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6658 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6659 .addFrameIndex(FI)
6660 .addImm(1)
6661 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00006662
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006663 if (NumLPads < 256) {
6664 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6665 .addReg(NewVReg1)
6666 .addImm(NumLPads));
6667 } else {
6668 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00006669 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6670 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6671
6672 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006673 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006674 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006675 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006676 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006677
6678 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6679 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6680 .addReg(VReg1, RegState::Define)
6681 .addConstantPoolIndex(Idx));
6682 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6683 .addReg(NewVReg1)
6684 .addReg(VReg1));
6685 }
6686
Bill Wendlingb3d46782011-10-06 23:37:36 +00006687 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6688 .addMBB(TrapBB)
6689 .addImm(ARMCC::HI)
6690 .addReg(ARM::CPSR);
6691
6692 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6693 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6694 .addReg(ARM::CPSR, RegState::Define)
6695 .addReg(NewVReg1)
6696 .addImm(2));
6697
6698 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00006699 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendlingb3d46782011-10-06 23:37:36 +00006700 .addJumpTableIndex(MJTI)
6701 .addImm(UId));
6702
6703 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6704 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6705 .addReg(ARM::CPSR, RegState::Define)
6706 .addReg(NewVReg2, RegState::Kill)
6707 .addReg(NewVReg3));
6708
6709 MachineMemOperand *JTMMOLd =
6710 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6711 MachineMemOperand::MOLoad, 4, 4);
6712
6713 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6714 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6715 .addReg(NewVReg4, RegState::Kill)
6716 .addImm(0)
6717 .addMemOperand(JTMMOLd));
6718
Chad Rosier96603432013-03-01 18:30:38 +00006719 unsigned NewVReg6 = NewVReg5;
6720 if (RelocM == Reloc::PIC_) {
6721 NewVReg6 = MRI->createVirtualRegister(TRC);
6722 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6723 .addReg(ARM::CPSR, RegState::Define)
6724 .addReg(NewVReg5, RegState::Kill)
6725 .addReg(NewVReg3));
6726 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00006727
6728 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6729 .addReg(NewVReg6, RegState::Kill)
6730 .addJumpTableIndex(MJTI)
6731 .addImm(UId);
Bill Wendling5626c662011-10-06 22:53:00 +00006732 } else {
6733 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6734 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6735 .addFrameIndex(FI)
6736 .addImm(4)
6737 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00006738
Bill Wendling4969dcd2011-10-18 22:52:20 +00006739 if (NumLPads < 256) {
6740 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6741 .addReg(NewVReg1)
6742 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00006743 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00006744 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6745 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006746 .addImm(NumLPads & 0xFFFF));
6747
6748 unsigned VReg2 = VReg1;
6749 if ((NumLPads & 0xFFFF0000) != 0) {
6750 VReg2 = MRI->createVirtualRegister(TRC);
6751 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6752 .addReg(VReg1)
6753 .addImm(NumLPads >> 16));
6754 }
6755
Bill Wendling4969dcd2011-10-18 22:52:20 +00006756 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6757 .addReg(NewVReg1)
6758 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00006759 } else {
6760 MachineConstantPool *ConstantPool = MF->getConstantPool();
6761 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6762 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6763
6764 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006765 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006766 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006767 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006768 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6769
6770 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6771 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6772 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00006773 .addConstantPoolIndex(Idx)
6774 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00006775 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6776 .addReg(NewVReg1)
6777 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00006778 }
6779
Bill Wendling5626c662011-10-06 22:53:00 +00006780 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6781 .addMBB(TrapBB)
6782 .addImm(ARMCC::HI)
6783 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00006784
Bill Wendling973c8172011-10-18 22:11:18 +00006785 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006786 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00006787 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006788 .addReg(NewVReg1)
6789 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00006790 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6791 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006792 .addJumpTableIndex(MJTI)
6793 .addImm(UId));
6794
6795 MachineMemOperand *JTMMOLd =
6796 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6797 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00006798 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006799 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00006800 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6801 .addReg(NewVReg3, RegState::Kill)
6802 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006803 .addImm(0)
6804 .addMemOperand(JTMMOLd));
6805
Chad Rosier96603432013-03-01 18:30:38 +00006806 if (RelocM == Reloc::PIC_) {
6807 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
6808 .addReg(NewVReg5, RegState::Kill)
6809 .addReg(NewVReg4)
6810 .addJumpTableIndex(MJTI)
6811 .addImm(UId);
6812 } else {
6813 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
6814 .addReg(NewVReg5, RegState::Kill)
6815 .addJumpTableIndex(MJTI)
6816 .addImm(UId);
6817 }
Bill Wendling5626c662011-10-06 22:53:00 +00006818 }
Bill Wendling202803e2011-10-05 00:02:33 +00006819
Bill Wendling324be982011-10-05 00:39:32 +00006820 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006821 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00006822 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006823 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6824 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00006825 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00006826 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006827 }
6828
Bill Wendling26d27802011-10-17 05:25:09 +00006829 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00006830 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00006831 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00006832 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006833
6834 // Remove the landing pad successor from the invoke block and replace it
6835 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00006836 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6837 BB->succ_end());
6838 while (!Successors.empty()) {
6839 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling883ec972011-10-07 23:18:02 +00006840 if (SMBB->isLandingPad()) {
6841 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00006842 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006843 }
6844 }
6845
6846 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006847
6848 // Find the invoke call and mark all of the callee-saved registers as
6849 // 'implicit defined' so that they're spilled. This prevents code from
6850 // moving instructions to before the EH block, where they will never be
6851 // executed.
6852 for (MachineBasicBlock::reverse_iterator
6853 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00006854 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006855
6856 DenseMap<unsigned, bool> DefRegs;
6857 for (MachineInstr::mop_iterator
6858 OI = II->operands_begin(), OE = II->operands_end();
6859 OI != OE; ++OI) {
6860 if (!OI->isReg()) continue;
6861 DefRegs[OI->getReg()] = true;
6862 }
6863
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00006864 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006865
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006866 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00006867 unsigned Reg = SavedRegs[i];
6868 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00006869 !ARM::tGPRRegClass.contains(Reg) &&
6870 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006871 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006872 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006873 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006874 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006875 continue;
6876 if (!DefRegs[Reg])
6877 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006878 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006879
6880 break;
6881 }
Bill Wendling883ec972011-10-07 23:18:02 +00006882 }
Bill Wendling324be982011-10-05 00:39:32 +00006883
Bill Wendling617075f2011-10-18 18:30:49 +00006884 // Mark all former landing pads as non-landing pads. The dispatch is the only
6885 // landing pad now.
6886 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6887 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6888 (*I)->setIsLandingPad(false);
6889
Bill Wendling324be982011-10-05 00:39:32 +00006890 // The instruction is gone now.
6891 MI->eraseFromParent();
6892
Bill Wendling374ee192011-10-03 21:25:38 +00006893 return MBB;
6894}
6895
Evan Cheng0cc4ad92010-07-13 19:27:42 +00006896static
6897MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6898 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6899 E = MBB->succ_end(); I != E; ++I)
6900 if (*I != Succ)
6901 return *I;
6902 llvm_unreachable("Expecting a BB with two successors!");
6903}
6904
Manman Renb504f492013-10-29 22:27:32 +00006905/// Return the load opcode for a given load size. If load size >= 8,
6906/// neon opcode will be returned.
6907static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
6908 if (LdSize >= 8)
6909 return LdSize == 16 ? ARM::VLD1q32wb_fixed
6910 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
6911 if (IsThumb1)
6912 return LdSize == 4 ? ARM::tLDRi
6913 : LdSize == 2 ? ARM::tLDRHi
6914 : LdSize == 1 ? ARM::tLDRBi : 0;
6915 if (IsThumb2)
6916 return LdSize == 4 ? ARM::t2LDR_POST
6917 : LdSize == 2 ? ARM::t2LDRH_POST
6918 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
6919 return LdSize == 4 ? ARM::LDR_POST_IMM
6920 : LdSize == 2 ? ARM::LDRH_POST
6921 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
6922}
6923
6924/// Return the store opcode for a given store size. If store size >= 8,
6925/// neon opcode will be returned.
6926static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
6927 if (StSize >= 8)
6928 return StSize == 16 ? ARM::VST1q32wb_fixed
6929 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
6930 if (IsThumb1)
6931 return StSize == 4 ? ARM::tSTRi
6932 : StSize == 2 ? ARM::tSTRHi
6933 : StSize == 1 ? ARM::tSTRBi : 0;
6934 if (IsThumb2)
6935 return StSize == 4 ? ARM::t2STR_POST
6936 : StSize == 2 ? ARM::t2STRH_POST
6937 : StSize == 1 ? ARM::t2STRB_POST : 0;
6938 return StSize == 4 ? ARM::STR_POST_IMM
6939 : StSize == 2 ? ARM::STRH_POST
6940 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
6941}
6942
6943/// Emit a post-increment load operation with given size. The instructions
6944/// will be added to BB at Pos.
6945static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
6946 const TargetInstrInfo *TII, DebugLoc dl,
6947 unsigned LdSize, unsigned Data, unsigned AddrIn,
6948 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6949 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
6950 assert(LdOpc != 0 && "Should have a load opcode");
6951 if (LdSize >= 8) {
6952 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6953 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6954 .addImm(0));
6955 } else if (IsThumb1) {
6956 // load + update AddrIn
6957 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6958 .addReg(AddrIn).addImm(0));
6959 MachineInstrBuilder MIB =
6960 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6961 MIB = AddDefaultT1CC(MIB);
6962 MIB.addReg(AddrIn).addImm(LdSize);
6963 AddDefaultPred(MIB);
6964 } else if (IsThumb2) {
6965 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6966 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6967 .addImm(LdSize));
6968 } else { // arm
6969 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6970 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6971 .addReg(0).addImm(LdSize));
6972 }
6973}
6974
6975/// Emit a post-increment store operation with given size. The instructions
6976/// will be added to BB at Pos.
6977static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
6978 const TargetInstrInfo *TII, DebugLoc dl,
6979 unsigned StSize, unsigned Data, unsigned AddrIn,
6980 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6981 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
6982 assert(StOpc != 0 && "Should have a store opcode");
6983 if (StSize >= 8) {
6984 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6985 .addReg(AddrIn).addImm(0).addReg(Data));
6986 } else if (IsThumb1) {
6987 // store + update AddrIn
6988 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
6989 .addReg(AddrIn).addImm(0));
6990 MachineInstrBuilder MIB =
6991 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6992 MIB = AddDefaultT1CC(MIB);
6993 MIB.addReg(AddrIn).addImm(StSize);
6994 AddDefaultPred(MIB);
6995 } else if (IsThumb2) {
6996 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6997 .addReg(Data).addReg(AddrIn).addImm(StSize));
6998 } else { // arm
6999 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7000 .addReg(Data).addReg(AddrIn).addReg(0)
7001 .addImm(StSize));
7002 }
7003}
7004
David Peixottoc32e24a2013-10-17 19:49:22 +00007005MachineBasicBlock *
7006ARMTargetLowering::EmitStructByval(MachineInstr *MI,
7007 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00007008 // This pseudo instruction has 3 operands: dst, src, size
7009 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7010 // Otherwise, we will generate unrolled scalar copies.
Eric Christopher1889fdc2015-01-29 00:19:39 +00007011 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00007012 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7013 MachineFunction::iterator It = BB;
7014 ++It;
7015
7016 unsigned dest = MI->getOperand(0).getReg();
7017 unsigned src = MI->getOperand(1).getReg();
7018 unsigned SizeVal = MI->getOperand(2).getImm();
7019 unsigned Align = MI->getOperand(3).getImm();
7020 DebugLoc dl = MI->getDebugLoc();
7021
Manman Rene8735522012-06-01 19:33:18 +00007022 MachineFunction *MF = BB->getParent();
7023 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00007024 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00007025 const TargetRegisterClass *TRC = nullptr;
7026 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007027
7028 bool IsThumb1 = Subtarget->isThumb1Only();
7029 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00007030
7031 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00007032 UnitSize = 1;
7033 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00007034 UnitSize = 2;
7035 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00007036 // Check whether we can use NEON instructions.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00007037 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007038 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007039 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007040 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007041 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007042 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007043 }
7044 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007045 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007046 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007047 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007048
David Peixottob0653e532013-10-24 16:39:36 +00007049 // Select the correct opcode and register class for unit size load/store
7050 bool IsNeon = UnitSize >= 8;
Craig Topper61e88f42014-11-21 05:58:21 +00007051 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007052 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00007053 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
7054 : UnitSize == 8 ? &ARM::DPRRegClass
7055 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007056
Manman Rene8735522012-06-01 19:33:18 +00007057 unsigned BytesLeft = SizeVal % UnitSize;
7058 unsigned LoopSize = SizeVal - BytesLeft;
7059
7060 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7061 // Use LDR and STR to copy.
7062 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7063 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7064 unsigned srcIn = src;
7065 unsigned destIn = dest;
7066 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007067 unsigned srcOut = MRI.createVirtualRegister(TRC);
7068 unsigned destOut = MRI.createVirtualRegister(TRC);
7069 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007070 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7071 IsThumb1, IsThumb2);
7072 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7073 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007074 srcIn = srcOut;
7075 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007076 }
7077
7078 // Handle the leftover bytes with LDRB and STRB.
7079 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7080 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007081 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007082 unsigned srcOut = MRI.createVirtualRegister(TRC);
7083 unsigned destOut = MRI.createVirtualRegister(TRC);
7084 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007085 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7086 IsThumb1, IsThumb2);
7087 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7088 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007089 srcIn = srcOut;
7090 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007091 }
7092 MI->eraseFromParent(); // The instruction is gone now.
7093 return BB;
7094 }
7095
7096 // Expand the pseudo op to a loop.
7097 // thisMBB:
7098 // ...
7099 // movw varEnd, # --> with thumb2
7100 // movt varEnd, #
7101 // ldrcp varEnd, idx --> without thumb2
7102 // fallthrough --> loopMBB
7103 // loopMBB:
7104 // PHI varPhi, varEnd, varLoop
7105 // PHI srcPhi, src, srcLoop
7106 // PHI destPhi, dst, destLoop
7107 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7108 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7109 // subs varLoop, varPhi, #UnitSize
7110 // bne loopMBB
7111 // fallthrough --> exitMBB
7112 // exitMBB:
7113 // epilogue to handle left-over bytes
7114 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7115 // [destOut] = STRB_POST(scratch, destLoop, 1)
7116 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7117 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7118 MF->insert(It, loopMBB);
7119 MF->insert(It, exitMBB);
7120
7121 // Transfer the remainder of BB and its successor edges to exitMBB.
7122 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007123 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007124 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7125
7126 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007127 unsigned varEnd = MRI.createVirtualRegister(TRC);
7128 if (IsThumb2) {
7129 unsigned Vtmp = varEnd;
7130 if ((LoopSize & 0xFFFF0000) != 0)
7131 Vtmp = MRI.createVirtualRegister(TRC);
7132 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), Vtmp)
7133 .addImm(LoopSize & 0xFFFF));
7134
7135 if ((LoopSize & 0xFFFF0000) != 0)
7136 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
7137 .addReg(Vtmp).addImm(LoopSize >> 16));
7138 } else {
7139 MachineConstantPool *ConstantPool = MF->getConstantPool();
7140 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7141 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7142
7143 // MachineConstantPool wants an explicit alignment.
7144 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
7145 if (Align == 0)
7146 Align = getDataLayout()->getTypeAllocSize(C->getType());
7147 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7148
7149 if (IsThumb1)
7150 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7151 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7152 else
7153 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7154 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7155 }
Manman Rene8735522012-06-01 19:33:18 +00007156 BB->addSuccessor(loopMBB);
7157
7158 // Generate the loop body:
7159 // varPhi = PHI(varLoop, varEnd)
7160 // srcPhi = PHI(srcLoop, src)
7161 // destPhi = PHI(destLoop, dst)
7162 MachineBasicBlock *entryBB = BB;
7163 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007164 unsigned varLoop = MRI.createVirtualRegister(TRC);
7165 unsigned varPhi = MRI.createVirtualRegister(TRC);
7166 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7167 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7168 unsigned destLoop = MRI.createVirtualRegister(TRC);
7169 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007170
7171 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7172 .addReg(varLoop).addMBB(loopMBB)
7173 .addReg(varEnd).addMBB(entryBB);
7174 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7175 .addReg(srcLoop).addMBB(loopMBB)
7176 .addReg(src).addMBB(entryBB);
7177 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7178 .addReg(destLoop).addMBB(loopMBB)
7179 .addReg(dest).addMBB(entryBB);
7180
7181 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7182 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007183 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007184 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7185 IsThumb1, IsThumb2);
7186 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7187 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007188
7189 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007190 if (IsThumb1) {
7191 MachineInstrBuilder MIB =
7192 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7193 MIB = AddDefaultT1CC(MIB);
7194 MIB.addReg(varPhi).addImm(UnitSize);
7195 AddDefaultPred(MIB);
7196 } else {
7197 MachineInstrBuilder MIB =
7198 BuildMI(*BB, BB->end(), dl,
7199 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7200 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7201 MIB->getOperand(5).setReg(ARM::CPSR);
7202 MIB->getOperand(5).setIsDef(true);
7203 }
7204 BuildMI(*BB, BB->end(), dl,
7205 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7206 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007207
7208 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7209 BB->addSuccessor(loopMBB);
7210 BB->addSuccessor(exitMBB);
7211
7212 // Add epilogue to handle BytesLeft.
7213 BB = exitMBB;
7214 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007215
7216 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7217 // [destOut] = STRB_POST(scratch, destLoop, 1)
7218 unsigned srcIn = srcLoop;
7219 unsigned destIn = destLoop;
7220 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007221 unsigned srcOut = MRI.createVirtualRegister(TRC);
7222 unsigned destOut = MRI.createVirtualRegister(TRC);
7223 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007224 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7225 IsThumb1, IsThumb2);
7226 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7227 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007228 srcIn = srcOut;
7229 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007230 }
7231
7232 MI->eraseFromParent(); // The instruction is gone now.
7233 return BB;
7234}
7235
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007236MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007237ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
7238 MachineBasicBlock *MBB) const {
7239 const TargetMachine &TM = getTargetMachine();
Eric Christopher1889fdc2015-01-29 00:19:39 +00007240 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007241 DebugLoc DL = MI->getDebugLoc();
7242
7243 assert(Subtarget->isTargetWindows() &&
7244 "__chkstk is only supported on Windows");
7245 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
7246
7247 // __chkstk takes the number of words to allocate on the stack in R4, and
7248 // returns the stack adjustment in number of bytes in R4. This will not
7249 // clober any other registers (other than the obvious lr).
7250 //
7251 // Although, technically, IP should be considered a register which may be
7252 // clobbered, the call itself will not touch it. Windows on ARM is a pure
7253 // thumb-2 environment, so there is no interworking required. As a result, we
7254 // do not expect a veneer to be emitted by the linker, clobbering IP.
7255 //
Alp Toker1d099d92014-06-19 19:41:26 +00007256 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007257 // required, again, ensuring that IP is not clobbered.
7258 //
7259 // Finally, although some linkers may theoretically provide a trampoline for
7260 // out of range calls (which is quite common due to a 32M range limitation of
7261 // branches for Thumb), we can generate the long-call version via
7262 // -mcmodel=large, alleviating the need for the trampoline which may clobber
7263 // IP.
7264
7265 switch (TM.getCodeModel()) {
7266 case CodeModel::Small:
7267 case CodeModel::Medium:
7268 case CodeModel::Default:
7269 case CodeModel::Kernel:
7270 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
7271 .addImm((unsigned)ARMCC::AL).addReg(0)
7272 .addExternalSymbol("__chkstk")
7273 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7274 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7275 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7276 break;
7277 case CodeModel::Large:
7278 case CodeModel::JITDefault: {
7279 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7280 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
7281
7282 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
7283 .addExternalSymbol("__chkstk");
7284 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
7285 .addImm((unsigned)ARMCC::AL).addReg(0)
7286 .addReg(Reg, RegState::Kill)
7287 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7288 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7289 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7290 break;
7291 }
7292 }
7293
7294 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
7295 ARM::SP)
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +00007296 .addReg(ARM::SP).addReg(ARM::R4)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007297
7298 MI->eraseFromParent();
7299 return MBB;
7300}
7301
7302MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007303ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007304 MachineBasicBlock *BB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007305 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007306 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007307 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007308 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007309 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007310 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007311 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007312 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007313 // The Thumb2 pre-indexed stores have the same MI operands, they just
7314 // define them differently in the .td files from the isel patterns, so
7315 // they need pseudos.
7316 case ARM::t2STR_preidx:
7317 MI->setDesc(TII->get(ARM::t2STR_PRE));
7318 return BB;
7319 case ARM::t2STRB_preidx:
7320 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7321 return BB;
7322 case ARM::t2STRH_preidx:
7323 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7324 return BB;
7325
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007326 case ARM::STRi_preidx:
7327 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007328 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007329 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7330 // Decode the offset.
7331 unsigned Offset = MI->getOperand(4).getImm();
7332 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7333 Offset = ARM_AM::getAM2Offset(Offset);
7334 if (isSub)
7335 Offset = -Offset;
7336
Jim Grosbachf402f692011-08-12 21:02:34 +00007337 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007338 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007339 .addOperand(MI->getOperand(0)) // Rn_wb
7340 .addOperand(MI->getOperand(1)) // Rt
7341 .addOperand(MI->getOperand(2)) // Rn
7342 .addImm(Offset) // offset (skip GPR==zero_reg)
7343 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007344 .addOperand(MI->getOperand(6))
7345 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007346 MI->eraseFromParent();
7347 return BB;
7348 }
7349 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007350 case ARM::STRBr_preidx:
7351 case ARM::STRH_preidx: {
7352 unsigned NewOpc;
7353 switch (MI->getOpcode()) {
7354 default: llvm_unreachable("unexpected opcode!");
7355 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7356 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7357 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7358 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007359 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7360 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7361 MIB.addOperand(MI->getOperand(i));
7362 MI->eraseFromParent();
7363 return BB;
7364 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007365
Evan Chengbb2af352009-08-12 05:17:19 +00007366 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007367 // To "insert" a SELECT_CC instruction, we actually have to insert the
7368 // diamond control-flow pattern. The incoming instruction knows the
7369 // destination vreg to set, the condition code register to branch on, the
7370 // true/false values to select between, and a branch opcode to use.
7371 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007372 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007373 ++It;
7374
7375 // thisMBB:
7376 // ...
7377 // TrueVal = ...
7378 // cmpTY ccX, r1, r2
7379 // bCC copy1MBB
7380 // fallthrough --> copy0MBB
7381 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007382 MachineFunction *F = BB->getParent();
7383 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7384 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007385 F->insert(It, copy0MBB);
7386 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007387
7388 // Transfer the remainder of BB and its successor edges to sinkMBB.
7389 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007390 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007391 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7392
Dan Gohmanf4f04102010-07-06 15:49:48 +00007393 BB->addSuccessor(copy0MBB);
7394 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007395
Dan Gohman34396292010-07-06 20:24:04 +00007396 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7397 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7398
Evan Cheng10043e22007-01-19 07:51:42 +00007399 // copy0MBB:
7400 // %FalseValue = ...
7401 // # fallthrough to sinkMBB
7402 BB = copy0MBB;
7403
7404 // Update machine-CFG edges
7405 BB->addSuccessor(sinkMBB);
7406
7407 // sinkMBB:
7408 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7409 // ...
7410 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007411 BuildMI(*BB, BB->begin(), dl,
7412 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007413 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7414 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7415
Dan Gohman34396292010-07-06 20:24:04 +00007416 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007417 return BB;
7418 }
Evan Chengb972e562009-08-07 00:34:42 +00007419
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007420 case ARM::BCCi64:
7421 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007422 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007423 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007424
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007425 // Compare both parts that make up the double comparison separately for
7426 // equality.
7427 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7428
7429 unsigned LHS1 = MI->getOperand(1).getReg();
7430 unsigned LHS2 = MI->getOperand(2).getReg();
7431 if (RHSisZero) {
7432 AddDefaultPred(BuildMI(BB, dl,
7433 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7434 .addReg(LHS1).addImm(0));
7435 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7436 .addReg(LHS2).addImm(0)
7437 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7438 } else {
7439 unsigned RHS1 = MI->getOperand(3).getReg();
7440 unsigned RHS2 = MI->getOperand(4).getReg();
7441 AddDefaultPred(BuildMI(BB, dl,
7442 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7443 .addReg(LHS1).addReg(RHS1));
7444 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7445 .addReg(LHS2).addReg(RHS2)
7446 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7447 }
7448
7449 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7450 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7451 if (MI->getOperand(0).getImm() == ARMCC::NE)
7452 std::swap(destMBB, exitMBB);
7453
7454 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7455 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007456 if (isThumb2)
7457 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7458 else
7459 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007460
7461 MI->eraseFromParent(); // The pseudo instruction is gone now.
7462 return BB;
7463 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007464
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007465 case ARM::Int_eh_sjlj_setjmp:
7466 case ARM::Int_eh_sjlj_setjmp_nofp:
7467 case ARM::tInt_eh_sjlj_setjmp:
7468 case ARM::t2Int_eh_sjlj_setjmp:
7469 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7470 EmitSjLjDispatchBlock(MI, BB);
7471 return BB;
7472
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007473 case ARM::ABS:
7474 case ARM::t2ABS: {
7475 // To insert an ABS instruction, we have to insert the
7476 // diamond control-flow pattern. The incoming instruction knows the
7477 // source vreg to test against 0, the destination vreg to set,
7478 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007479 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007480 // It transforms
7481 // V1 = ABS V0
7482 // into
7483 // V2 = MOVS V0
7484 // BCC (branch to SinkBB if V0 >= 0)
7485 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007486 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007487 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7488 MachineFunction::iterator BBI = BB;
7489 ++BBI;
7490 MachineFunction *Fn = BB->getParent();
7491 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7492 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7493 Fn->insert(BBI, RSBBB);
7494 Fn->insert(BBI, SinkBB);
7495
7496 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7497 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7498 bool isThumb2 = Subtarget->isThumb2();
7499 MachineRegisterInfo &MRI = Fn->getRegInfo();
7500 // In Thumb mode S must not be specified if source register is the SP or
7501 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00007502 unsigned NewRsbDstReg =
7503 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007504
7505 // Transfer the remainder of BB and its successor edges to sinkMBB.
7506 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007507 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007508 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7509
7510 BB->addSuccessor(RSBBB);
7511 BB->addSuccessor(SinkBB);
7512
7513 // fall through to SinkMBB
7514 RSBBB->addSuccessor(SinkBB);
7515
Manman Rene0763c72012-06-15 21:32:12 +00007516 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00007517 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00007518 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7519 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007520
7521 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00007522 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007523 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7524 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7525
7526 // insert rsbri in RSBBB
7527 // Note: BCC and rsbri will be converted into predicated rsbmi
7528 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00007529 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007530 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Rene0763c72012-06-15 21:32:12 +00007531 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007532 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7533
Andrew Trick3f07c422011-10-18 18:40:53 +00007534 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007535 // reuse ABSDstReg to not change uses of ABS instruction
7536 BuildMI(*SinkBB, SinkBB->begin(), dl,
7537 TII->get(ARM::PHI), ABSDstReg)
7538 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00007539 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007540
7541 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00007542 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007543
7544 // return last added BB
7545 return SinkBB;
7546 }
Manman Rene8735522012-06-01 19:33:18 +00007547 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00007548 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00007549 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007550 case ARM::WIN__CHKSTK:
7551 return EmitLowered__chkstk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00007552 }
7553}
7554
Evan Chenge6fba772011-08-30 19:09:48 +00007555void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7556 SDNode *Node) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007557 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00007558 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7559 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7560 // operand is still set to noreg. If needed, set the optional operand's
7561 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00007562 //
Andrew Trick88b24502011-10-18 19:18:52 +00007563 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00007564
Andrew Trick924123a2011-09-21 02:20:46 +00007565 // Rename pseudo opcodes.
7566 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7567 if (NewOpc) {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007568 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
Andrew Trick88b24502011-10-18 19:18:52 +00007569 MCID = &TII->get(NewOpc);
7570
7571 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7572 "converted opcode should be the same except for cc_out");
7573
7574 MI->setDesc(*MCID);
7575
7576 // Add the optional cc_out operand
7577 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00007578 }
Andrew Trick88b24502011-10-18 19:18:52 +00007579 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00007580
7581 // Any ARM instruction that sets the 's' bit should specify an optional
7582 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00007583 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007584 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007585 return;
7586 }
Andrew Trick924123a2011-09-21 02:20:46 +00007587 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7588 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007589 bool definesCPSR = false;
7590 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00007591 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00007592 i != e; ++i) {
7593 const MachineOperand &MO = MI->getOperand(i);
7594 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7595 definesCPSR = true;
7596 if (MO.isDead())
7597 deadCPSR = true;
7598 MI->RemoveOperand(i);
7599 break;
Evan Chenge6fba772011-08-30 19:09:48 +00007600 }
7601 }
Andrew Trick8586e622011-09-20 03:17:40 +00007602 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00007603 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007604 return;
7605 }
7606 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00007607 if (deadCPSR) {
7608 assert(!MI->getOperand(ccOutIdx).getReg() &&
7609 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00007610 return;
Andrew Trick924123a2011-09-21 02:20:46 +00007611 }
Andrew Trick8586e622011-09-20 03:17:40 +00007612
Andrew Trick924123a2011-09-21 02:20:46 +00007613 // If this instruction was defined with an optional CPSR def and its dag node
7614 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007615 MachineOperand &MO = MI->getOperand(ccOutIdx);
7616 MO.setReg(ARM::CPSR);
7617 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00007618}
7619
Evan Cheng10043e22007-01-19 07:51:42 +00007620//===----------------------------------------------------------------------===//
7621// ARM Optimization Hooks
7622//===----------------------------------------------------------------------===//
7623
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007624// Helper function that checks if N is a null or all ones constant.
7625static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7626 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7627 if (!C)
7628 return false;
7629 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7630}
7631
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007632// Return true if N is conditionally 0 or all ones.
7633// Detects these expressions where cc is an i1 value:
7634//
7635// (select cc 0, y) [AllOnes=0]
7636// (select cc y, 0) [AllOnes=0]
7637// (zext cc) [AllOnes=0]
7638// (sext cc) [AllOnes=0/1]
7639// (select cc -1, y) [AllOnes=1]
7640// (select cc y, -1) [AllOnes=1]
7641//
7642// Invert is set when N is the null/all ones constant when CC is false.
7643// OtherOp is set to the alternative value of N.
7644static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7645 SDValue &CC, bool &Invert,
7646 SDValue &OtherOp,
7647 SelectionDAG &DAG) {
7648 switch (N->getOpcode()) {
7649 default: return false;
7650 case ISD::SELECT: {
7651 CC = N->getOperand(0);
7652 SDValue N1 = N->getOperand(1);
7653 SDValue N2 = N->getOperand(2);
7654 if (isZeroOrAllOnes(N1, AllOnes)) {
7655 Invert = false;
7656 OtherOp = N2;
7657 return true;
7658 }
7659 if (isZeroOrAllOnes(N2, AllOnes)) {
7660 Invert = true;
7661 OtherOp = N1;
7662 return true;
7663 }
7664 return false;
7665 }
7666 case ISD::ZERO_EXTEND:
7667 // (zext cc) can never be the all ones value.
7668 if (AllOnes)
7669 return false;
7670 // Fall through.
7671 case ISD::SIGN_EXTEND: {
7672 EVT VT = N->getValueType(0);
7673 CC = N->getOperand(0);
7674 if (CC.getValueType() != MVT::i1)
7675 return false;
7676 Invert = !AllOnes;
7677 if (AllOnes)
7678 // When looking for an AllOnes constant, N is an sext, and the 'other'
7679 // value is 0.
7680 OtherOp = DAG.getConstant(0, VT);
7681 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7682 // When looking for a 0 constant, N can be zext or sext.
7683 OtherOp = DAG.getConstant(1, VT);
7684 else
7685 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7686 return true;
7687 }
7688 }
7689}
7690
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007691// Combine a constant select operand into its use:
7692//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007693// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7694// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7695// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7696// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7697// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007698//
7699// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007700// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007701//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007702// Also recognize sext/zext from i1:
7703//
7704// (add (zext cc), x) -> (select cc (add x, 1), x)
7705// (add (sext cc), x) -> (select cc (add x, -1), x)
7706//
7707// These transformations eventually create predicated instructions.
7708//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007709// @param N The node to transform.
7710// @param Slct The N operand that is a select.
7711// @param OtherOp The other N operand (x above).
7712// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007713// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007714// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00007715static
7716SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007717 TargetLowering::DAGCombinerInfo &DCI,
7718 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00007719 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007720 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007721 SDValue NonConstantVal;
7722 SDValue CCOp;
7723 bool SwapSelectOps;
7724 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7725 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007726 return SDValue();
7727
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007728 // Slct is now know to be the desired identity constant when CC is true.
7729 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007730 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007731 OtherOp, NonConstantVal);
7732 // Unless SwapSelectOps says CC should be false.
7733 if (SwapSelectOps)
7734 std::swap(TrueVal, FalseVal);
7735
Andrew Trickef9de2a2013-05-25 02:42:55 +00007736 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007737 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00007738}
7739
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007740// Attempt combineSelectAndUse on each operand of a commutative operator N.
7741static
7742SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7743 TargetLowering::DAGCombinerInfo &DCI) {
7744 SDValue N0 = N->getOperand(0);
7745 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007746 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007747 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7748 if (Result.getNode())
7749 return Result;
7750 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007751 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007752 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7753 if (Result.getNode())
7754 return Result;
7755 }
7756 return SDValue();
7757}
7758
Eric Christopher1b8b94192011-06-29 21:10:36 +00007759// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00007760// (only after legalization).
7761static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7762 TargetLowering::DAGCombinerInfo &DCI,
7763 const ARMSubtarget *Subtarget) {
7764
7765 // Only perform optimization if after legalize, and if NEON is available. We
7766 // also expected both operands to be BUILD_VECTORs.
7767 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7768 || N0.getOpcode() != ISD::BUILD_VECTOR
7769 || N1.getOpcode() != ISD::BUILD_VECTOR)
7770 return SDValue();
7771
7772 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7773 EVT VT = N->getValueType(0);
7774 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7775 return SDValue();
7776
7777 // Check that the vector operands are of the right form.
7778 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7779 // operands, where N is the size of the formed vector.
7780 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7781 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007782
7783 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00007784 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00007785 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00007786 SDValue Vec = N0->getOperand(0)->getOperand(0);
7787 SDNode *V = Vec.getNode();
7788 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00007789
Eric Christopher1b8b94192011-06-29 21:10:36 +00007790 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007791 // check to see if each of their operands are an EXTRACT_VECTOR with
7792 // the same vector and appropriate index.
7793 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7794 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7795 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00007796
Tanya Lattnere9e67052011-06-14 23:48:48 +00007797 SDValue ExtVec0 = N0->getOperand(i);
7798 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007799
Tanya Lattnere9e67052011-06-14 23:48:48 +00007800 // First operand is the vector, verify its the same.
7801 if (V != ExtVec0->getOperand(0).getNode() ||
7802 V != ExtVec1->getOperand(0).getNode())
7803 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00007804
Tanya Lattnere9e67052011-06-14 23:48:48 +00007805 // Second is the constant, verify its correct.
7806 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7807 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00007808
Tanya Lattnere9e67052011-06-14 23:48:48 +00007809 // For the constant, we want to see all the even or all the odd.
7810 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7811 || C1->getZExtValue() != nextIndex+1)
7812 return SDValue();
7813
7814 // Increment index.
7815 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007816 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00007817 return SDValue();
7818 }
7819
7820 // Create VPADDL node.
7821 SelectionDAG &DAG = DCI.DAG;
7822 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00007823
7824 // Build operand list.
7825 SmallVector<SDValue, 8> Ops;
7826 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7827 TLI.getPointerTy()));
7828
7829 // Input is the vector.
7830 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007831
Tanya Lattnere9e67052011-06-14 23:48:48 +00007832 // Get widened type and narrowed type.
7833 MVT widenType;
7834 unsigned numElem = VT.getVectorNumElements();
Silviu Barangaa3106e62014-04-03 10:44:27 +00007835
7836 EVT inputLaneType = Vec.getValueType().getVectorElementType();
7837 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00007838 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7839 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7840 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7841 default:
Craig Toppere55c5562012-02-07 02:50:20 +00007842 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00007843 }
7844
Craig Topper48d114b2014-04-26 18:35:24 +00007845 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00007846 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
7847 return DAG.getNode(ExtOp, SDLoc(N), VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00007848}
7849
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007850static SDValue findMUL_LOHI(SDValue V) {
7851 if (V->getOpcode() == ISD::UMUL_LOHI ||
7852 V->getOpcode() == ISD::SMUL_LOHI)
7853 return V;
7854 return SDValue();
7855}
7856
7857static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7858 TargetLowering::DAGCombinerInfo &DCI,
7859 const ARMSubtarget *Subtarget) {
7860
7861 if (Subtarget->isThumb1Only()) return SDValue();
7862
7863 // Only perform the checks after legalize when the pattern is available.
7864 if (DCI.isBeforeLegalize()) return SDValue();
7865
7866 // Look for multiply add opportunities.
7867 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7868 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7869 // a glue link from the first add to the second add.
7870 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7871 // a S/UMLAL instruction.
7872 // loAdd UMUL_LOHI
7873 // \ / :lo \ :hi
7874 // \ / \ [no multiline comment]
7875 // ADDC | hiAdd
7876 // \ :glue / /
7877 // \ / /
7878 // ADDE
7879 //
7880 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7881 SDValue AddcOp0 = AddcNode->getOperand(0);
7882 SDValue AddcOp1 = AddcNode->getOperand(1);
7883
7884 // Check if the two operands are from the same mul_lohi node.
7885 if (AddcOp0.getNode() == AddcOp1.getNode())
7886 return SDValue();
7887
7888 assert(AddcNode->getNumValues() == 2 &&
7889 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00007890 "Expect ADDC with two result values. First: i32");
7891
7892 // Check that we have a glued ADDC node.
7893 if (AddcNode->getValueType(1) != MVT::Glue)
7894 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007895
7896 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7897 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7898 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7899 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7900 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7901 return SDValue();
7902
7903 // Look for the glued ADDE.
7904 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00007905 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007906 return SDValue();
7907
7908 // Make sure it is really an ADDE.
7909 if (AddeNode->getOpcode() != ISD::ADDE)
7910 return SDValue();
7911
7912 assert(AddeNode->getNumOperands() == 3 &&
7913 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7914 "ADDE node has the wrong inputs");
7915
7916 // Check for the triangle shape.
7917 SDValue AddeOp0 = AddeNode->getOperand(0);
7918 SDValue AddeOp1 = AddeNode->getOperand(1);
7919
7920 // Make sure that the ADDE operands are not coming from the same node.
7921 if (AddeOp0.getNode() == AddeOp1.getNode())
7922 return SDValue();
7923
7924 // Find the MUL_LOHI node walking up ADDE's operands.
7925 bool IsLeftOperandMUL = false;
7926 SDValue MULOp = findMUL_LOHI(AddeOp0);
7927 if (MULOp == SDValue())
7928 MULOp = findMUL_LOHI(AddeOp1);
7929 else
7930 IsLeftOperandMUL = true;
7931 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00007932 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007933
7934 // Figure out the right opcode.
7935 unsigned Opc = MULOp->getOpcode();
7936 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7937
7938 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00007939 SDValue* HiAdd = nullptr;
7940 SDValue* LoMul = nullptr;
7941 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007942
Jyoti Allurf1d70502015-01-23 09:10:03 +00007943 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
7944 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
7945 return SDValue();
7946
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007947 if (IsLeftOperandMUL)
7948 HiAdd = &AddeOp1;
7949 else
7950 HiAdd = &AddeOp0;
7951
7952
Jyoti Allurf1d70502015-01-23 09:10:03 +00007953 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
7954 // whose low result is fed to the ADDC we are checking.
7955
7956 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007957 LoMul = &AddcOp0;
7958 LowAdd = &AddcOp1;
7959 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00007960 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007961 LoMul = &AddcOp1;
7962 LowAdd = &AddcOp0;
7963 }
7964
Craig Topper062a2ba2014-04-25 05:30:21 +00007965 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007966 return SDValue();
7967
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007968 // Create the merged node.
7969 SelectionDAG &DAG = DCI.DAG;
7970
7971 // Build operand list.
7972 SmallVector<SDValue, 8> Ops;
7973 Ops.push_back(LoMul->getOperand(0));
7974 Ops.push_back(LoMul->getOperand(1));
7975 Ops.push_back(*LowAdd);
7976 Ops.push_back(*HiAdd);
7977
Andrew Trickef9de2a2013-05-25 02:42:55 +00007978 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00007979 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007980
7981 // Replace the ADDs' nodes uses by the MLA node's values.
7982 SDValue HiMLALResult(MLALNode.getNode(), 1);
7983 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7984
7985 SDValue LoMLALResult(MLALNode.getNode(), 0);
7986 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7987
7988 // Return original node to notify the driver to stop replacing.
7989 SDValue resNode(AddcNode, 0);
7990 return resNode;
7991}
7992
7993/// PerformADDCCombine - Target-specific dag combine transform from
7994/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7995static SDValue PerformADDCCombine(SDNode *N,
7996 TargetLowering::DAGCombinerInfo &DCI,
7997 const ARMSubtarget *Subtarget) {
7998
7999 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
8000
8001}
8002
Bob Wilson728eb292010-07-29 20:34:14 +00008003/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
8004/// operands N0 and N1. This is a helper for PerformADDCombine that is
8005/// called with the default operands, and if that fails, with commuted
8006/// operands.
8007static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008008 TargetLowering::DAGCombinerInfo &DCI,
8009 const ARMSubtarget *Subtarget){
8010
8011 // Attempt to create vpaddl for this add.
8012 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
8013 if (Result.getNode())
8014 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008015
Chris Lattner4147f082009-03-12 06:52:53 +00008016 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008017 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008018 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
8019 if (Result.getNode()) return Result;
8020 }
Chris Lattner4147f082009-03-12 06:52:53 +00008021 return SDValue();
8022}
8023
Bob Wilson728eb292010-07-29 20:34:14 +00008024/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
8025///
8026static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008027 TargetLowering::DAGCombinerInfo &DCI,
8028 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00008029 SDValue N0 = N->getOperand(0);
8030 SDValue N1 = N->getOperand(1);
8031
8032 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008033 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008034 if (Result.getNode())
8035 return Result;
8036
8037 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008038 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008039}
8040
Chris Lattner4147f082009-03-12 06:52:53 +00008041/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00008042///
Chris Lattner4147f082009-03-12 06:52:53 +00008043static SDValue PerformSUBCombine(SDNode *N,
8044 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00008045 SDValue N0 = N->getOperand(0);
8046 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00008047
Chris Lattner4147f082009-03-12 06:52:53 +00008048 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008049 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008050 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
8051 if (Result.getNode()) return Result;
8052 }
Bob Wilson7117a912009-03-20 22:42:55 +00008053
Chris Lattner4147f082009-03-12 06:52:53 +00008054 return SDValue();
8055}
8056
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008057/// PerformVMULCombine
8058/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8059/// special multiplier accumulator forwarding.
8060/// vmul d3, d0, d2
8061/// vmla d3, d1, d2
8062/// is faster than
8063/// vadd d3, d0, d1
8064/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00008065// However, for (A + B) * (A + B),
8066// vadd d2, d0, d1
8067// vmul d3, d0, d2
8068// vmla d3, d1, d2
8069// is slower than
8070// vadd d2, d0, d1
8071// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008072static SDValue PerformVMULCombine(SDNode *N,
8073 TargetLowering::DAGCombinerInfo &DCI,
8074 const ARMSubtarget *Subtarget) {
8075 if (!Subtarget->hasVMLxForwarding())
8076 return SDValue();
8077
8078 SelectionDAG &DAG = DCI.DAG;
8079 SDValue N0 = N->getOperand(0);
8080 SDValue N1 = N->getOperand(1);
8081 unsigned Opcode = N0.getOpcode();
8082 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8083 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00008084 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008085 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8086 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8087 return SDValue();
8088 std::swap(N0, N1);
8089 }
8090
Weiming Zhao2052f482013-09-25 23:12:06 +00008091 if (N0 == N1)
8092 return SDValue();
8093
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008094 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008095 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008096 SDValue N00 = N0->getOperand(0);
8097 SDValue N01 = N0->getOperand(1);
8098 return DAG.getNode(Opcode, DL, VT,
8099 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8100 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8101}
8102
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008103static SDValue PerformMULCombine(SDNode *N,
8104 TargetLowering::DAGCombinerInfo &DCI,
8105 const ARMSubtarget *Subtarget) {
8106 SelectionDAG &DAG = DCI.DAG;
8107
8108 if (Subtarget->isThumb1Only())
8109 return SDValue();
8110
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008111 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8112 return SDValue();
8113
8114 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008115 if (VT.is64BitVector() || VT.is128BitVector())
8116 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008117 if (VT != MVT::i32)
8118 return SDValue();
8119
8120 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8121 if (!C)
8122 return SDValue();
8123
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008124 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008125 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008126
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008127 ShiftAmt = ShiftAmt & (32 - 1);
8128 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008129 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008130
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008131 SDValue Res;
8132 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008133
8134 if (MulAmt >= 0) {
8135 if (isPowerOf2_32(MulAmt - 1)) {
8136 // (mul x, 2^N + 1) => (add (shl x, N), x)
8137 Res = DAG.getNode(ISD::ADD, DL, VT,
8138 V,
8139 DAG.getNode(ISD::SHL, DL, VT,
8140 V,
8141 DAG.getConstant(Log2_32(MulAmt - 1),
8142 MVT::i32)));
8143 } else if (isPowerOf2_32(MulAmt + 1)) {
8144 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8145 Res = DAG.getNode(ISD::SUB, DL, VT,
8146 DAG.getNode(ISD::SHL, DL, VT,
8147 V,
8148 DAG.getConstant(Log2_32(MulAmt + 1),
8149 MVT::i32)),
8150 V);
8151 } else
8152 return SDValue();
8153 } else {
8154 uint64_t MulAmtAbs = -MulAmt;
8155 if (isPowerOf2_32(MulAmtAbs + 1)) {
8156 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8157 Res = DAG.getNode(ISD::SUB, DL, VT,
8158 V,
8159 DAG.getNode(ISD::SHL, DL, VT,
8160 V,
8161 DAG.getConstant(Log2_32(MulAmtAbs + 1),
8162 MVT::i32)));
8163 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8164 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8165 Res = DAG.getNode(ISD::ADD, DL, VT,
8166 V,
8167 DAG.getNode(ISD::SHL, DL, VT,
8168 V,
8169 DAG.getConstant(Log2_32(MulAmtAbs-1),
8170 MVT::i32)));
8171 Res = DAG.getNode(ISD::SUB, DL, VT,
8172 DAG.getConstant(0, MVT::i32),Res);
8173
8174 } else
8175 return SDValue();
8176 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008177
8178 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008179 Res = DAG.getNode(ISD::SHL, DL, VT,
8180 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008181
8182 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008183 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008184 return SDValue();
8185}
8186
Owen Anderson30c48922010-11-05 19:27:46 +00008187static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008188 TargetLowering::DAGCombinerInfo &DCI,
8189 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008190
Owen Anderson30c48922010-11-05 19:27:46 +00008191 // Attempt to use immediate-form VBIC
8192 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008193 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008194 EVT VT = N->getValueType(0);
8195 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008196
Tanya Lattner266792a2011-04-07 15:24:20 +00008197 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8198 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008199
Owen Anderson30c48922010-11-05 19:27:46 +00008200 APInt SplatBits, SplatUndef;
8201 unsigned SplatBitSize;
8202 bool HasAnyUndefs;
8203 if (BVN &&
8204 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8205 if (SplatBitSize <= 64) {
8206 EVT VbicVT;
8207 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8208 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00008209 DAG, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008210 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008211 if (Val.getNode()) {
8212 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008213 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008214 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008215 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008216 }
8217 }
8218 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008219
Evan Chenge87681c2012-02-23 01:19:06 +00008220 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008221 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8222 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8223 if (Result.getNode())
8224 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008225 }
8226
Owen Anderson30c48922010-11-05 19:27:46 +00008227 return SDValue();
8228}
8229
Jim Grosbach11013ed2010-07-16 23:05:05 +00008230/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8231static SDValue PerformORCombine(SDNode *N,
8232 TargetLowering::DAGCombinerInfo &DCI,
8233 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008234 // Attempt to use immediate-form VORR
8235 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008236 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008237 EVT VT = N->getValueType(0);
8238 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008239
Tanya Lattner266792a2011-04-07 15:24:20 +00008240 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8241 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008242
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008243 APInt SplatBits, SplatUndef;
8244 unsigned SplatBitSize;
8245 bool HasAnyUndefs;
8246 if (BVN && Subtarget->hasNEON() &&
8247 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8248 if (SplatBitSize <= 64) {
8249 EVT VorrVT;
8250 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8251 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00008252 DAG, VorrVT, VT.is128BitVector(),
8253 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008254 if (Val.getNode()) {
8255 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008256 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008257 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008258 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008259 }
8260 }
8261 }
8262
Evan Chenge87681c2012-02-23 01:19:06 +00008263 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008264 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8265 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8266 if (Result.getNode())
8267 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008268 }
8269
Nadav Rotem3a94c542012-08-13 18:52:44 +00008270 // The code below optimizes (or (and X, Y), Z).
8271 // The AND operand needs to have a single user to make these optimizations
8272 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008273 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008274 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008275 return SDValue();
8276 SDValue N1 = N->getOperand(1);
8277
8278 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8279 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8280 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8281 APInt SplatUndef;
8282 unsigned SplatBitSize;
8283 bool HasAnyUndefs;
8284
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008285 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008286 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008287 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8288 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008289 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008290 HasAnyUndefs) && !HasAnyUndefs) {
8291 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8292 HasAnyUndefs) && !HasAnyUndefs) {
8293 // Ensure that the bit width of the constants are the same and that
8294 // the splat arguments are logical inverses as per the pattern we
8295 // are trying to simplify.
8296 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8297 SplatBits0 == ~SplatBits1) {
8298 // Canonicalize the vector type to make instruction selection
8299 // simpler.
8300 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8301 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8302 N0->getOperand(1),
8303 N0->getOperand(0),
8304 N1->getOperand(0));
8305 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8306 }
8307 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008308 }
8309 }
8310
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008311 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8312 // reasonable.
8313
Jim Grosbach11013ed2010-07-16 23:05:05 +00008314 // BFI is only available on V6T2+
8315 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8316 return SDValue();
8317
Andrew Trickef9de2a2013-05-25 02:42:55 +00008318 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008319 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008320 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008321 //
8322 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008323 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008324 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008325 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008326 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008327 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008328
Jim Grosbach11013ed2010-07-16 23:05:05 +00008329 if (VT != MVT::i32)
8330 return SDValue();
8331
Evan Cheng2e51bb42010-12-13 20:32:54 +00008332 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008333
Jim Grosbach11013ed2010-07-16 23:05:05 +00008334 // The value and the mask need to be constants so we can verify this is
8335 // actually a bitfield set. If the mask is 0xffff, we can do better
8336 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008337 SDValue MaskOp = N0.getOperand(1);
8338 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8339 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008340 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008341 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008342 if (Mask == 0xffff)
8343 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008344 SDValue Res;
8345 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008346 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8347 if (N1C) {
8348 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008349 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008350 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008351
Evan Cheng34345752010-12-11 04:11:38 +00008352 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008353 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008354
Evan Cheng2e51bb42010-12-13 20:32:54 +00008355 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Cheng34345752010-12-11 04:11:38 +00008356 DAG.getConstant(Val, MVT::i32),
8357 DAG.getConstant(Mask, MVT::i32));
8358
8359 // Do not add new nodes to DAG combiner worklist.
8360 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008361 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008362 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008363 } else if (N1.getOpcode() == ISD::AND) {
8364 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008365 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8366 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008367 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008368 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008369
Eric Christopherd5530962011-03-26 01:21:03 +00008370 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8371 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008372 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008373 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008374 // The pack halfword instruction works better for masks that fit it,
8375 // so use that when it's available.
8376 if (Subtarget->hasT2ExtractPack() &&
8377 (Mask == 0xffff || Mask == 0xffff0000))
8378 return SDValue();
8379 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008380 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008381 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopherd5530962011-03-26 01:21:03 +00008382 DAG.getConstant(amt, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008383 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008384 DAG.getConstant(Mask, MVT::i32));
8385 // Do not add new nodes to DAG combiner worklist.
8386 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008387 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008388 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008389 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008390 // The pack halfword instruction works better for masks that fit it,
8391 // so use that when it's available.
8392 if (Subtarget->hasT2ExtractPack() &&
8393 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8394 return SDValue();
8395 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008396 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008397 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008398 DAG.getConstant(lsb, MVT::i32));
8399 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopherd5530962011-03-26 01:21:03 +00008400 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008401 // Do not add new nodes to DAG combiner worklist.
8402 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008403 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008404 }
8405 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008406
Evan Cheng2e51bb42010-12-13 20:32:54 +00008407 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8408 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8409 ARM::isBitFieldInvertedMask(~Mask)) {
8410 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8411 // where lsb(mask) == #shamt and masked bits of B are known zero.
8412 SDValue ShAmt = N00.getOperand(1);
8413 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008414 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008415 if (ShAmtC != LSB)
8416 return SDValue();
8417
8418 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8419 DAG.getConstant(~Mask, MVT::i32));
8420
8421 // Do not add new nodes to DAG combiner worklist.
8422 DCI.CombineTo(N, Res, false);
8423 }
8424
Jim Grosbach11013ed2010-07-16 23:05:05 +00008425 return SDValue();
8426}
8427
Evan Chenge87681c2012-02-23 01:19:06 +00008428static SDValue PerformXORCombine(SDNode *N,
8429 TargetLowering::DAGCombinerInfo &DCI,
8430 const ARMSubtarget *Subtarget) {
8431 EVT VT = N->getValueType(0);
8432 SelectionDAG &DAG = DCI.DAG;
8433
8434 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8435 return SDValue();
8436
8437 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008438 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8439 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8440 if (Result.getNode())
8441 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008442 }
8443
8444 return SDValue();
8445}
8446
Evan Cheng6d02d902011-06-15 01:12:31 +00008447/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8448/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00008449static SDValue PerformBFICombine(SDNode *N,
8450 TargetLowering::DAGCombinerInfo &DCI) {
8451 SDValue N1 = N->getOperand(1);
8452 if (N1.getOpcode() == ISD::AND) {
8453 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8454 if (!N11C)
8455 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008456 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008457 unsigned LSB = countTrailingZeros(~InvMask);
8458 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +00008459 assert(Width <
8460 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00008461 "undefined behavior");
8462 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +00008463 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008464 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008465 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00008466 N->getOperand(0), N1.getOperand(0),
8467 N->getOperand(2));
8468 }
8469 return SDValue();
8470}
8471
Bob Wilson22806742010-09-22 22:09:21 +00008472/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8473/// ARMISD::VMOVRRD.
8474static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008475 TargetLowering::DAGCombinerInfo &DCI,
8476 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +00008477 // vmovrrd(vmovdrr x, y) -> x,y
8478 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00008479 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +00008480 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008481
8482 // vmovrrd(load f64) -> (load i32), (load i32)
8483 SDNode *InNode = InDouble.getNode();
8484 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8485 InNode->getValueType(0) == MVT::f64 &&
8486 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8487 !cast<LoadSDNode>(InNode)->isVolatile()) {
8488 // TODO: Should this be done for non-FrameIndex operands?
8489 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8490
8491 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008492 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008493 SDValue BasePtr = LD->getBasePtr();
8494 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8495 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008496 LD->isNonTemporal(), LD->isInvariant(),
8497 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008498
8499 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8500 DAG.getConstant(4, MVT::i32));
8501 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8502 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008503 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008504 std::min(4U, LD->getAlignment() / 2));
8505
8506 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Christian Pirker762b2c62014-06-01 09:30:52 +00008507 if (DCI.DAG.getTargetLoweringInfo().isBigEndian())
8508 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008509 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008510 return Result;
8511 }
8512
Bob Wilson22806742010-09-22 22:09:21 +00008513 return SDValue();
8514}
8515
8516/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8517/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8518static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8519 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8520 SDValue Op0 = N->getOperand(0);
8521 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00008522 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008523 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008524 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008525 Op1 = Op1.getOperand(0);
8526 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8527 Op0.getNode() == Op1.getNode() &&
8528 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008529 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00008530 N->getValueType(0), Op0.getOperand(0));
8531 return SDValue();
8532}
8533
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008534/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8535/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8536/// i64 vector to have f64 elements, since the value can then be loaded
8537/// directly into a VFP register.
8538static bool hasNormalLoadOperand(SDNode *N) {
8539 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8540 for (unsigned i = 0; i < NumElts; ++i) {
8541 SDNode *Elt = N->getOperand(i).getNode();
8542 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8543 return true;
8544 }
8545 return false;
8546}
8547
Bob Wilsoncb6db982010-09-17 22:59:05 +00008548/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8549/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008550static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008551 TargetLowering::DAGCombinerInfo &DCI,
8552 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +00008553 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8554 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8555 // into a pair of GPRs, which is fine when the value is used as a scalar,
8556 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008557 SelectionDAG &DAG = DCI.DAG;
8558 if (N->getNumOperands() == 2) {
8559 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8560 if (RV.getNode())
8561 return RV;
8562 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00008563
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008564 // Load i64 elements as f64 values so that type legalization does not split
8565 // them up into i32 values.
8566 EVT VT = N->getValueType(0);
8567 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8568 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008569 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008570 SmallVector<SDValue, 8> Ops;
8571 unsigned NumElts = VT.getVectorNumElements();
8572 for (unsigned i = 0; i < NumElts; ++i) {
8573 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8574 Ops.push_back(V);
8575 // Make the DAGCombiner fold the bitcast.
8576 DCI.AddToWorklist(V.getNode());
8577 }
8578 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00008579 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008580 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8581}
8582
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008583/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
8584static SDValue
8585PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8586 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
8587 // At that time, we may have inserted bitcasts from integer to float.
8588 // If these bitcasts have survived DAGCombine, change the lowering of this
8589 // BUILD_VECTOR in something more vector friendly, i.e., that does not
8590 // force to use floating point types.
8591
8592 // Make sure we can change the type of the vector.
8593 // This is possible iff:
8594 // 1. The vector is only used in a bitcast to a integer type. I.e.,
8595 // 1.1. Vector is used only once.
8596 // 1.2. Use is a bit convert to an integer type.
8597 // 2. The size of its operands are 32-bits (64-bits are not legal).
8598 EVT VT = N->getValueType(0);
8599 EVT EltVT = VT.getVectorElementType();
8600
8601 // Check 1.1. and 2.
8602 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
8603 return SDValue();
8604
8605 // By construction, the input type must be float.
8606 assert(EltVT == MVT::f32 && "Unexpected type!");
8607
8608 // Check 1.2.
8609 SDNode *Use = *N->use_begin();
8610 if (Use->getOpcode() != ISD::BITCAST ||
8611 Use->getValueType(0).isFloatingPoint())
8612 return SDValue();
8613
8614 // Check profitability.
8615 // Model is, if more than half of the relevant operands are bitcast from
8616 // i32, turn the build_vector into a sequence of insert_vector_elt.
8617 // Relevant operands are everything that is not statically
8618 // (i.e., at compile time) bitcasted.
8619 unsigned NumOfBitCastedElts = 0;
8620 unsigned NumElts = VT.getVectorNumElements();
8621 unsigned NumOfRelevantElts = NumElts;
8622 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
8623 SDValue Elt = N->getOperand(Idx);
8624 if (Elt->getOpcode() == ISD::BITCAST) {
8625 // Assume only bit cast to i32 will go away.
8626 if (Elt->getOperand(0).getValueType() == MVT::i32)
8627 ++NumOfBitCastedElts;
8628 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
8629 // Constants are statically casted, thus do not count them as
8630 // relevant operands.
8631 --NumOfRelevantElts;
8632 }
8633
8634 // Check if more than half of the elements require a non-free bitcast.
8635 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
8636 return SDValue();
8637
8638 SelectionDAG &DAG = DCI.DAG;
8639 // Create the new vector type.
8640 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
8641 // Check if the type is legal.
8642 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8643 if (!TLI.isTypeLegal(VecVT))
8644 return SDValue();
8645
8646 // Combine:
8647 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
8648 // => BITCAST INSERT_VECTOR_ELT
8649 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
8650 // (BITCAST EN), N.
8651 SDValue Vec = DAG.getUNDEF(VecVT);
8652 SDLoc dl(N);
8653 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
8654 SDValue V = N->getOperand(Idx);
8655 if (V.getOpcode() == ISD::UNDEF)
8656 continue;
8657 if (V.getOpcode() == ISD::BITCAST &&
8658 V->getOperand(0).getValueType() == MVT::i32)
8659 // Fold obvious case.
8660 V = V.getOperand(0);
8661 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00008662 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008663 // Make the DAGCombiner fold the bitcasts.
8664 DCI.AddToWorklist(V.getNode());
8665 }
8666 SDValue LaneIdx = DAG.getConstant(Idx, MVT::i32);
8667 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
8668 }
8669 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
8670 // Make the DAGCombiner fold the bitcasts.
8671 DCI.AddToWorklist(Vec.getNode());
8672 return Vec;
8673}
8674
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008675/// PerformInsertEltCombine - Target-specific dag combine xforms for
8676/// ISD::INSERT_VECTOR_ELT.
8677static SDValue PerformInsertEltCombine(SDNode *N,
8678 TargetLowering::DAGCombinerInfo &DCI) {
8679 // Bitcast an i64 load inserted into a vector to f64.
8680 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8681 EVT VT = N->getValueType(0);
8682 SDNode *Elt = N->getOperand(1).getNode();
8683 if (VT.getVectorElementType() != MVT::i64 ||
8684 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8685 return SDValue();
8686
8687 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008688 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008689 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8690 VT.getVectorNumElements());
8691 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8692 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8693 // Make the DAGCombiner fold the bitcasts.
8694 DCI.AddToWorklist(Vec.getNode());
8695 DCI.AddToWorklist(V.getNode());
8696 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8697 Vec, V, N->getOperand(2));
8698 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00008699}
8700
Bob Wilsonc7334a12010-10-27 20:38:28 +00008701/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8702/// ISD::VECTOR_SHUFFLE.
8703static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8704 // The LLVM shufflevector instruction does not require the shuffle mask
8705 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8706 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8707 // operands do not match the mask length, they are extended by concatenating
8708 // them with undef vectors. That is probably the right thing for other
8709 // targets, but for NEON it is better to concatenate two double-register
8710 // size vector operands into a single quad-register size vector. Do that
8711 // transformation here:
8712 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8713 // shuffle(concat(v1, v2), undef)
8714 SDValue Op0 = N->getOperand(0);
8715 SDValue Op1 = N->getOperand(1);
8716 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8717 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8718 Op0.getNumOperands() != 2 ||
8719 Op1.getNumOperands() != 2)
8720 return SDValue();
8721 SDValue Concat0Op1 = Op0.getOperand(1);
8722 SDValue Concat1Op1 = Op1.getOperand(1);
8723 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8724 Concat1Op1.getOpcode() != ISD::UNDEF)
8725 return SDValue();
8726 // Skip the transformation if any of the types are illegal.
8727 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8728 EVT VT = N->getValueType(0);
8729 if (!TLI.isTypeLegal(VT) ||
8730 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8731 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8732 return SDValue();
8733
Andrew Trickef9de2a2013-05-25 02:42:55 +00008734 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008735 Op0.getOperand(0), Op1.getOperand(0));
8736 // Translate the shuffle mask.
8737 SmallVector<int, 16> NewMask;
8738 unsigned NumElts = VT.getVectorNumElements();
8739 unsigned HalfElts = NumElts/2;
8740 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8741 for (unsigned n = 0; n < NumElts; ++n) {
8742 int MaskElt = SVN->getMaskElt(n);
8743 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00008744 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00008745 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00008746 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00008747 NewElt = HalfElts + MaskElt - NumElts;
8748 NewMask.push_back(NewElt);
8749 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00008750 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008751 DAG.getUNDEF(VT), NewMask.data());
8752}
8753
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008754/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
8755/// NEON load/store intrinsics, and generic vector load/stores, to merge
8756/// base address updates.
8757/// For generic load/stores, the memory type is assumed to be a vector.
8758/// The caller is assumed to have checked legality.
Bob Wilson06fce872011-02-07 17:43:21 +00008759static SDValue CombineBaseUpdate(SDNode *N,
8760 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson06fce872011-02-07 17:43:21 +00008761 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008762 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8763 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008764 const bool isStore = N->getOpcode() == ISD::STORE;
8765 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +00008766 SDValue Addr = N->getOperand(AddrOpIdx);
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008767 MemSDNode *MemN = cast<MemSDNode>(N);
Bob Wilson06fce872011-02-07 17:43:21 +00008768
8769 // Search for a use of the address operand that is an increment.
8770 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8771 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8772 SDNode *User = *UI;
8773 if (User->getOpcode() != ISD::ADD ||
8774 UI.getUse().getResNo() != Addr.getResNo())
8775 continue;
8776
8777 // Check that the add is independent of the load/store. Otherwise, folding
8778 // it would create a cycle.
8779 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8780 continue;
8781
8782 // Find the new opcode for the updating load/store.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008783 bool isLoadOp = true;
Bob Wilson06fce872011-02-07 17:43:21 +00008784 bool isLaneOp = false;
8785 unsigned NewOpc = 0;
8786 unsigned NumVecs = 0;
8787 if (isIntrinsic) {
8788 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8789 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00008790 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008791 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8792 NumVecs = 1; break;
8793 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8794 NumVecs = 2; break;
8795 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8796 NumVecs = 3; break;
8797 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8798 NumVecs = 4; break;
8799 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8800 NumVecs = 2; isLaneOp = true; break;
8801 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8802 NumVecs = 3; isLaneOp = true; break;
8803 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8804 NumVecs = 4; isLaneOp = true; break;
8805 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008806 NumVecs = 1; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008807 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008808 NumVecs = 2; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008809 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008810 NumVecs = 3; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008811 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008812 NumVecs = 4; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008813 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008814 NumVecs = 2; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008815 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008816 NumVecs = 3; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008817 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008818 NumVecs = 4; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008819 }
8820 } else {
8821 isLaneOp = true;
8822 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00008823 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008824 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8825 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8826 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008827 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD;
8828 NumVecs = 1; isLaneOp = false; break;
8829 case ISD::STORE: NewOpc = ARMISD::VST1_UPD;
8830 NumVecs = 1; isLaneOp = false; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008831 }
8832 }
8833
8834 // Find the size of memory referenced by the load/store.
8835 EVT VecTy;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008836 if (isLoadOp) {
Bob Wilson06fce872011-02-07 17:43:21 +00008837 VecTy = N->getValueType(0);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008838 } else if (isIntrinsic) {
Renato Golin2a5c0a52015-02-04 10:11:59 +00008839 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008840 } else {
8841 assert(isStore && "Node has to be a load, a store, or an intrinsic!");
8842 VecTy = N->getOperand(1).getValueType();
8843 }
8844
Bob Wilson06fce872011-02-07 17:43:21 +00008845 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8846 if (isLaneOp)
8847 NumBytes /= VecTy.getVectorNumElements();
8848
8849 // If the increment is a constant, it must match the memory ref size.
8850 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8851 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8852 uint64_t IncVal = CInc->getZExtValue();
8853 if (IncVal != NumBytes)
8854 continue;
8855 } else if (NumBytes >= 3 * 16) {
8856 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8857 // separate instructions that make it harder to use a non-constant update.
8858 continue;
8859 }
8860
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008861 // OK, we found an ADD we can fold into the base update.
8862 // Now, create a _UPD node, taking care of not breaking alignment.
8863
8864 EVT AlignedVecTy = VecTy;
8865 unsigned Alignment = MemN->getAlignment();
8866
8867 // If this is a less-than-standard-aligned load/store, change the type to
8868 // match the standard alignment.
8869 // The alignment is overlooked when selecting _UPD variants; and it's
8870 // easier to introduce bitcasts here than fix that.
8871 // There are 3 ways to get to this base-update combine:
8872 // - intrinsics: they are assumed to be properly aligned (to the standard
8873 // alignment of the memory type), so we don't need to do anything.
8874 // - ARMISD::VLDx nodes: they are only generated from the aforementioned
8875 // intrinsics, so, likewise, there's nothing to do.
8876 // - generic load/store instructions: the alignment is specified as an
8877 // explicit operand, rather than implicitly as the standard alignment
8878 // of the memory type (like the intrisics). We need to change the
8879 // memory type to match the explicit alignment. That way, we don't
8880 // generate non-standard-aligned ARMISD::VLDx nodes.
8881 if (isa<LSBaseSDNode>(N)) {
8882 if (Alignment == 0)
8883 Alignment = 1;
8884 if (Alignment < VecTy.getScalarSizeInBits() / 8) {
8885 MVT EltTy = MVT::getIntegerVT(Alignment * 8);
8886 assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
8887 assert(!isLaneOp && "Unexpected generic load/store lane.");
8888 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
8889 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
8890 }
8891 // Don't set an explicit alignment on regular load/stores that we want
8892 // to transform to VLD/VST 1_UPD nodes.
8893 // This matches the behavior of regular load/stores, which only get an
8894 // explicit alignment if the MMO alignment is larger than the standard
8895 // alignment of the memory type.
8896 // Intrinsics, however, always get an explicit alignment, set to the
8897 // alignment of the MMO.
8898 Alignment = 1;
8899 }
8900
Bob Wilson06fce872011-02-07 17:43:21 +00008901 // Create the new updating load/store node.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008902 // First, create an SDVTList for the new updating node's results.
Bob Wilson06fce872011-02-07 17:43:21 +00008903 EVT Tys[6];
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008904 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0);
Bob Wilson06fce872011-02-07 17:43:21 +00008905 unsigned n;
8906 for (n = 0; n < NumResultVecs; ++n)
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008907 Tys[n] = AlignedVecTy;
Bob Wilson06fce872011-02-07 17:43:21 +00008908 Tys[n++] = MVT::i32;
8909 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00008910 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008911
8912 // Then, gather the new node's operands.
Bob Wilson06fce872011-02-07 17:43:21 +00008913 SmallVector<SDValue, 8> Ops;
8914 Ops.push_back(N->getOperand(0)); // incoming chain
8915 Ops.push_back(N->getOperand(AddrOpIdx));
8916 Ops.push_back(Inc);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008917
8918 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
8919 // Try to match the intrinsic's signature
8920 Ops.push_back(StN->getValue());
8921 } else {
8922 // Loads (and of course intrinsics) match the intrinsics' signature,
8923 // so just add all but the alignment operand.
8924 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i)
8925 Ops.push_back(N->getOperand(i));
8926 }
8927
8928 // For all node types, the alignment operand is always the last one.
8929 Ops.push_back(DAG.getConstant(Alignment, MVT::i32));
8930
8931 // If this is a non-standard-aligned STORE, the penultimate operand is the
8932 // stored value. Bitcast it to the aligned type.
8933 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
8934 SDValue &StVal = Ops[Ops.size()-2];
8935 StVal = DAG.getNode(ISD::BITCAST, SDLoc(N), AlignedVecTy, StVal);
8936 }
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008937
Andrew Trickef9de2a2013-05-25 02:42:55 +00008938 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys,
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008939 Ops, AlignedVecTy,
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008940 MemN->getMemOperand());
Bob Wilson06fce872011-02-07 17:43:21 +00008941
8942 // Update the uses.
Ahmed Bougacha4c2b0782015-02-19 23:13:10 +00008943 SmallVector<SDValue, 5> NewResults;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008944 for (unsigned i = 0; i < NumResultVecs; ++i)
Bob Wilson06fce872011-02-07 17:43:21 +00008945 NewResults.push_back(SDValue(UpdN.getNode(), i));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008946
8947 // If this is an non-standard-aligned LOAD, the first result is the loaded
8948 // value. Bitcast it to the expected result type.
8949 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
8950 SDValue &LdVal = NewResults[0];
8951 LdVal = DAG.getNode(ISD::BITCAST, SDLoc(N), VecTy, LdVal);
8952 }
8953
Bob Wilson06fce872011-02-07 17:43:21 +00008954 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8955 DCI.CombineTo(N, NewResults);
8956 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8957
8958 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00008959 }
Bob Wilson06fce872011-02-07 17:43:21 +00008960 return SDValue();
8961}
8962
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008963static SDValue PerformVLDCombine(SDNode *N,
8964 TargetLowering::DAGCombinerInfo &DCI) {
8965 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8966 return SDValue();
8967
8968 return CombineBaseUpdate(N, DCI);
8969}
8970
Bob Wilson2d790df2010-11-28 06:51:26 +00008971/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8972/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8973/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8974/// return true.
8975static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8976 SelectionDAG &DAG = DCI.DAG;
8977 EVT VT = N->getValueType(0);
8978 // vldN-dup instructions only support 64-bit vectors for N > 1.
8979 if (!VT.is64BitVector())
8980 return false;
8981
8982 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8983 SDNode *VLD = N->getOperand(0).getNode();
8984 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8985 return false;
8986 unsigned NumVecs = 0;
8987 unsigned NewOpc = 0;
8988 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8989 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8990 NumVecs = 2;
8991 NewOpc = ARMISD::VLD2DUP;
8992 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8993 NumVecs = 3;
8994 NewOpc = ARMISD::VLD3DUP;
8995 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8996 NumVecs = 4;
8997 NewOpc = ARMISD::VLD4DUP;
8998 } else {
8999 return false;
9000 }
9001
9002 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9003 // numbers match the load.
9004 unsigned VLDLaneNo =
9005 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9006 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9007 UI != UE; ++UI) {
9008 // Ignore uses of the chain result.
9009 if (UI.getUse().getResNo() == NumVecs)
9010 continue;
9011 SDNode *User = *UI;
9012 if (User->getOpcode() != ARMISD::VDUPLANE ||
9013 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9014 return false;
9015 }
9016
9017 // Create the vldN-dup node.
9018 EVT Tys[5];
9019 unsigned n;
9020 for (n = 0; n < NumVecs; ++n)
9021 Tys[n] = VT;
9022 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009023 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00009024 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9025 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009026 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009027 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00009028 VLDMemInt->getMemOperand());
9029
9030 // Update the uses.
9031 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9032 UI != UE; ++UI) {
9033 unsigned ResNo = UI.getUse().getResNo();
9034 // Ignore uses of the chain result.
9035 if (ResNo == NumVecs)
9036 continue;
9037 SDNode *User = *UI;
9038 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9039 }
9040
9041 // Now the vldN-lane intrinsic is dead except for its chain result.
9042 // Update uses of the chain.
9043 std::vector<SDValue> VLDDupResults;
9044 for (unsigned n = 0; n < NumVecs; ++n)
9045 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9046 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9047 DCI.CombineTo(VLD, VLDDupResults);
9048
9049 return true;
9050}
9051
Bob Wilson103a0dc2010-07-14 01:22:12 +00009052/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9053/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009054static SDValue PerformVDUPLANECombine(SDNode *N,
9055 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009056 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009057
Bob Wilson2d790df2010-11-28 06:51:26 +00009058 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9059 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9060 if (CombineVLDDUP(N, DCI))
9061 return SDValue(N, 0);
9062
9063 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9064 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009065 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009066 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009067 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009068 return SDValue();
9069
9070 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9071 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9072 // The canonical VMOV for a zero vector uses a 32-bit element size.
9073 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9074 unsigned EltBits;
9075 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9076 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00009077 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009078 if (EltSize > VT.getVectorElementType().getSizeInBits())
9079 return SDValue();
9080
Andrew Trickef9de2a2013-05-25 02:42:55 +00009081 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009082}
9083
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009084static SDValue PerformLOADCombine(SDNode *N,
9085 TargetLowering::DAGCombinerInfo &DCI) {
9086 EVT VT = N->getValueType(0);
9087
9088 // If this is a legal vector load, try to combine it into a VLD1_UPD.
9089 if (ISD::isNormalLoad(N) && VT.isVector() &&
9090 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9091 return CombineBaseUpdate(N, DCI);
9092
9093 return SDValue();
9094}
9095
Ahmed Bougacha23167462014-12-09 21:26:53 +00009096/// PerformSTORECombine - Target-specific dag combine xforms for
9097/// ISD::STORE.
9098static SDValue PerformSTORECombine(SDNode *N,
9099 TargetLowering::DAGCombinerInfo &DCI) {
9100 StoreSDNode *St = cast<StoreSDNode>(N);
9101 if (St->isVolatile())
9102 return SDValue();
9103
9104 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
9105 // pack all of the elements in one place. Next, store to memory in fewer
9106 // chunks.
9107 SDValue StVal = St->getValue();
9108 EVT VT = StVal.getValueType();
9109 if (St->isTruncatingStore() && VT.isVector()) {
9110 SelectionDAG &DAG = DCI.DAG;
9111 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9112 EVT StVT = St->getMemoryVT();
9113 unsigned NumElems = VT.getVectorNumElements();
9114 assert(StVT != VT && "Cannot truncate to the same type");
9115 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
9116 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
9117
9118 // From, To sizes and ElemCount must be pow of two
9119 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
9120
9121 // We are going to use the original vector elt for storing.
9122 // Accumulated smaller vector elements must be a multiple of the store size.
9123 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
9124
9125 unsigned SizeRatio = FromEltSz / ToEltSz;
9126 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
9127
9128 // Create a type on which we perform the shuffle.
9129 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
9130 NumElems*SizeRatio);
9131 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
9132
9133 SDLoc DL(St);
9134 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
9135 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
9136 for (unsigned i = 0; i < NumElems; ++i)
9137 ShuffleVec[i] = TLI.isBigEndian() ? (i+1) * SizeRatio - 1 : i * SizeRatio;
9138
9139 // Can't shuffle using an illegal type.
9140 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
9141
9142 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
9143 DAG.getUNDEF(WideVec.getValueType()),
9144 ShuffleVec.data());
9145 // At this point all of the data is stored at the bottom of the
9146 // register. We now need to save it to mem.
9147
9148 // Find the largest store unit
9149 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +00009150 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +00009151 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
9152 StoreType = Tp;
9153 }
9154 // Didn't find a legal store type.
9155 if (!TLI.isTypeLegal(StoreType))
9156 return SDValue();
9157
9158 // Bitcast the original vector into a vector of store-size units
9159 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
9160 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
9161 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
9162 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
9163 SmallVector<SDValue, 8> Chains;
9164 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
9165 TLI.getPointerTy());
9166 SDValue BasePtr = St->getBasePtr();
9167
9168 // Perform one or more big stores into memory.
9169 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
9170 for (unsigned I = 0; I < E; I++) {
9171 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
9172 StoreType, ShuffWide,
9173 DAG.getIntPtrConstant(I));
9174 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
9175 St->getPointerInfo(), St->isVolatile(),
9176 St->isNonTemporal(), St->getAlignment());
9177 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
9178 Increment);
9179 Chains.push_back(Ch);
9180 }
9181 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
9182 }
9183
9184 if (!ISD::isNormalStore(St))
9185 return SDValue();
9186
9187 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
9188 // ARM stores of arguments in the same cache line.
9189 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
9190 StVal.getNode()->hasOneUse()) {
9191 SelectionDAG &DAG = DCI.DAG;
9192 bool isBigEndian = DAG.getTargetLoweringInfo().isBigEndian();
9193 SDLoc DL(St);
9194 SDValue BasePtr = St->getBasePtr();
9195 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
9196 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
9197 BasePtr, St->getPointerInfo(), St->isVolatile(),
9198 St->isNonTemporal(), St->getAlignment());
9199
9200 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
9201 DAG.getConstant(4, MVT::i32));
9202 return DAG.getStore(NewST1.getValue(0), DL,
9203 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
9204 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
9205 St->isNonTemporal(),
9206 std::min(4U, St->getAlignment() / 2));
9207 }
9208
9209 if (StVal.getValueType() == MVT::i64 &&
9210 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9211
9212 // Bitcast an i64 store extracted from a vector to f64.
9213 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9214 SelectionDAG &DAG = DCI.DAG;
9215 SDLoc dl(StVal);
9216 SDValue IntVec = StVal.getOperand(0);
9217 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9218 IntVec.getValueType().getVectorNumElements());
9219 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
9220 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
9221 Vec, StVal.getOperand(1));
9222 dl = SDLoc(N);
9223 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
9224 // Make the DAGCombiner fold the bitcasts.
9225 DCI.AddToWorklist(Vec.getNode());
9226 DCI.AddToWorklist(ExtElt.getNode());
9227 DCI.AddToWorklist(V.getNode());
9228 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
9229 St->getPointerInfo(), St->isVolatile(),
9230 St->isNonTemporal(), St->getAlignment(),
9231 St->getAAInfo());
9232 }
9233
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009234 // If this is a legal vector store, try to combine it into a VST1_UPD.
9235 if (ISD::isNormalStore(N) && VT.isVector() &&
9236 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9237 return CombineBaseUpdate(N, DCI);
9238
Ahmed Bougacha23167462014-12-09 21:26:53 +00009239 return SDValue();
9240}
9241
Eric Christopher1b8b94192011-06-29 21:10:36 +00009242// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00009243// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
9244static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
9245{
Chad Rosier6b610b32011-06-28 17:26:57 +00009246 integerPart cN;
9247 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009248 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
9249 I != E; I++) {
9250 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
9251 if (!C)
9252 return false;
9253
Eric Christopher1b8b94192011-06-29 21:10:36 +00009254 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009255 APFloat APF = C->getValueAPF();
9256 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
9257 != APFloat::opOK || !isExact)
9258 return false;
9259
9260 c0 = (I == 0) ? cN : c0;
9261 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
9262 return false;
9263 }
9264 C = c0;
9265 return true;
9266}
9267
9268/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
9269/// can replace combinations of VMUL and VCVT (floating-point to integer)
9270/// when the VMUL has a constant operand that is a power of 2.
9271///
9272/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9273/// vmul.f32 d16, d17, d16
9274/// vcvt.s32.f32 d16, d16
9275/// becomes:
9276/// vcvt.s32.f32 d16, d16, #3
9277static SDValue PerformVCVTCombine(SDNode *N,
9278 TargetLowering::DAGCombinerInfo &DCI,
9279 const ARMSubtarget *Subtarget) {
9280 SelectionDAG &DAG = DCI.DAG;
9281 SDValue Op = N->getOperand(0);
9282
9283 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
9284 Op.getOpcode() != ISD::FMUL)
9285 return SDValue();
9286
9287 uint64_t C;
9288 SDValue N0 = Op->getOperand(0);
9289 SDValue ConstVec = Op->getOperand(1);
9290 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
9291
Eric Christopher1b8b94192011-06-29 21:10:36 +00009292 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00009293 !isConstVecPow2(ConstVec, isSigned, C))
9294 return SDValue();
9295
Tim Northover7cbc2152013-06-28 15:29:25 +00009296 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
9297 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Bradley Smithececb7f2014-12-16 10:59:27 +00009298 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9299 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32 ||
9300 NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +00009301 // These instructions only exist converting from f32 to i32. We can handle
9302 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +00009303 // be lossy. We also can't handle more then 4 lanes, since these intructions
9304 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +00009305 return SDValue();
9306 }
9307
Chad Rosierfa8d8932011-06-24 19:23:04 +00009308 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9309 Intrinsic::arm_neon_vcvtfp2fxu;
Tim Northover7cbc2152013-06-28 15:29:25 +00009310 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
9311 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9312 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
9313 DAG.getConstant(Log2_64(C), MVT::i32));
9314
9315 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9316 FixConv = DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), FixConv);
9317
9318 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009319}
9320
9321/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9322/// can replace combinations of VCVT (integer to floating-point) and VDIV
9323/// when the VDIV has a constant operand that is a power of 2.
9324///
9325/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9326/// vcvt.f32.s32 d16, d16
9327/// vdiv.f32 d16, d17, d16
9328/// becomes:
9329/// vcvt.f32.s32 d16, d16, #3
9330static SDValue PerformVDIVCombine(SDNode *N,
9331 TargetLowering::DAGCombinerInfo &DCI,
9332 const ARMSubtarget *Subtarget) {
9333 SelectionDAG &DAG = DCI.DAG;
9334 SDValue Op = N->getOperand(0);
9335 unsigned OpOpcode = Op.getNode()->getOpcode();
9336
9337 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
9338 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9339 return SDValue();
9340
9341 uint64_t C;
9342 SDValue ConstVec = N->getOperand(1);
9343 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
9344
9345 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9346 !isConstVecPow2(ConstVec, isSigned, C))
9347 return SDValue();
9348
Tim Northover7cbc2152013-06-28 15:29:25 +00009349 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
9350 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
9351 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9352 // These instructions only exist converting from i32 to f32. We can handle
9353 // smaller integers by generating an extra extend, but larger ones would
9354 // be lossy.
9355 return SDValue();
9356 }
9357
9358 SDValue ConvInput = Op.getOperand(0);
9359 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9360 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9361 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
9362 SDLoc(N), NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9363 ConvInput);
9364
Eric Christopher1b8b94192011-06-29 21:10:36 +00009365 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00009366 Intrinsic::arm_neon_vcvtfxu2fp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009367 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
Chad Rosierfa8d8932011-06-24 19:23:04 +00009368 Op.getValueType(),
Eric Christopher1b8b94192011-06-29 21:10:36 +00009369 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Tim Northover7cbc2152013-06-28 15:29:25 +00009370 ConvInput, DAG.getConstant(Log2_64(C), MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00009371}
9372
9373/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00009374/// operand of a vector shift operation, where all the elements of the
9375/// build_vector must have the same constant integer value.
9376static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9377 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00009378 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00009379 Op = Op.getOperand(0);
9380 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9381 APInt SplatBits, SplatUndef;
9382 unsigned SplatBitSize;
9383 bool HasAnyUndefs;
9384 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9385 HasAnyUndefs, ElementBits) ||
9386 SplatBitSize > ElementBits)
9387 return false;
9388 Cnt = SplatBits.getSExtValue();
9389 return true;
9390}
9391
9392/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9393/// operand of a vector shift left operation. That value must be in the range:
9394/// 0 <= Value < ElementBits for a left shift; or
9395/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009396static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009397 assert(VT.isVector() && "vector shift count is not a vector type");
9398 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9399 if (! getVShiftImm(Op, ElementBits, Cnt))
9400 return false;
9401 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9402}
9403
9404/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9405/// operand of a vector shift right operation. For a shift opcode, the value
9406/// is positive, but for an intrinsic the value count must be negative. The
9407/// absolute value must be in the range:
9408/// 1 <= |Value| <= ElementBits for a right shift; or
9409/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009410static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00009411 int64_t &Cnt) {
9412 assert(VT.isVector() && "vector shift count is not a vector type");
9413 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9414 if (! getVShiftImm(Op, ElementBits, Cnt))
9415 return false;
9416 if (isIntrinsic)
9417 Cnt = -Cnt;
9418 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9419}
9420
9421/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9422static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9423 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9424 switch (IntNo) {
9425 default:
9426 // Don't do anything for most intrinsics.
9427 break;
9428
9429 // Vector shifts: check for immediate versions and lower them.
9430 // Note: This is done during DAG combining instead of DAG legalizing because
9431 // the build_vectors for 64-bit vector element shift counts are generally
9432 // not legal, and it is hard to see their values after they get legalized to
9433 // loads from a constant pool.
9434 case Intrinsic::arm_neon_vshifts:
9435 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009436 case Intrinsic::arm_neon_vrshifts:
9437 case Intrinsic::arm_neon_vrshiftu:
9438 case Intrinsic::arm_neon_vrshiftn:
9439 case Intrinsic::arm_neon_vqshifts:
9440 case Intrinsic::arm_neon_vqshiftu:
9441 case Intrinsic::arm_neon_vqshiftsu:
9442 case Intrinsic::arm_neon_vqshiftns:
9443 case Intrinsic::arm_neon_vqshiftnu:
9444 case Intrinsic::arm_neon_vqshiftnsu:
9445 case Intrinsic::arm_neon_vqrshiftns:
9446 case Intrinsic::arm_neon_vqrshiftnu:
9447 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009448 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009449 int64_t Cnt;
9450 unsigned VShiftOpc = 0;
9451
9452 switch (IntNo) {
9453 case Intrinsic::arm_neon_vshifts:
9454 case Intrinsic::arm_neon_vshiftu:
9455 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9456 VShiftOpc = ARMISD::VSHL;
9457 break;
9458 }
9459 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9460 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9461 ARMISD::VSHRs : ARMISD::VSHRu);
9462 break;
9463 }
9464 return SDValue();
9465
Bob Wilson2e076c42009-06-22 23:27:02 +00009466 case Intrinsic::arm_neon_vrshifts:
9467 case Intrinsic::arm_neon_vrshiftu:
9468 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9469 break;
9470 return SDValue();
9471
9472 case Intrinsic::arm_neon_vqshifts:
9473 case Intrinsic::arm_neon_vqshiftu:
9474 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9475 break;
9476 return SDValue();
9477
9478 case Intrinsic::arm_neon_vqshiftsu:
9479 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9480 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +00009481 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009482
Bob Wilson2e076c42009-06-22 23:27:02 +00009483 case Intrinsic::arm_neon_vrshiftn:
9484 case Intrinsic::arm_neon_vqshiftns:
9485 case Intrinsic::arm_neon_vqshiftnu:
9486 case Intrinsic::arm_neon_vqshiftnsu:
9487 case Intrinsic::arm_neon_vqrshiftns:
9488 case Intrinsic::arm_neon_vqrshiftnu:
9489 case Intrinsic::arm_neon_vqrshiftnsu:
9490 // Narrowing shifts require an immediate right shift.
9491 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9492 break;
Jim Grosbach84511e12010-06-02 21:53:11 +00009493 llvm_unreachable("invalid shift count for narrowing vector shift "
9494 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009495
9496 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00009497 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00009498 }
9499
9500 switch (IntNo) {
9501 case Intrinsic::arm_neon_vshifts:
9502 case Intrinsic::arm_neon_vshiftu:
9503 // Opcode already set above.
9504 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00009505 case Intrinsic::arm_neon_vrshifts:
9506 VShiftOpc = ARMISD::VRSHRs; break;
9507 case Intrinsic::arm_neon_vrshiftu:
9508 VShiftOpc = ARMISD::VRSHRu; break;
9509 case Intrinsic::arm_neon_vrshiftn:
9510 VShiftOpc = ARMISD::VRSHRN; break;
9511 case Intrinsic::arm_neon_vqshifts:
9512 VShiftOpc = ARMISD::VQSHLs; break;
9513 case Intrinsic::arm_neon_vqshiftu:
9514 VShiftOpc = ARMISD::VQSHLu; break;
9515 case Intrinsic::arm_neon_vqshiftsu:
9516 VShiftOpc = ARMISD::VQSHLsu; break;
9517 case Intrinsic::arm_neon_vqshiftns:
9518 VShiftOpc = ARMISD::VQSHRNs; break;
9519 case Intrinsic::arm_neon_vqshiftnu:
9520 VShiftOpc = ARMISD::VQSHRNu; break;
9521 case Intrinsic::arm_neon_vqshiftnsu:
9522 VShiftOpc = ARMISD::VQSHRNsu; break;
9523 case Intrinsic::arm_neon_vqrshiftns:
9524 VShiftOpc = ARMISD::VQRSHRNs; break;
9525 case Intrinsic::arm_neon_vqrshiftnu:
9526 VShiftOpc = ARMISD::VQRSHRNu; break;
9527 case Intrinsic::arm_neon_vqrshiftnsu:
9528 VShiftOpc = ARMISD::VQRSHRNsu; break;
9529 }
9530
Andrew Trickef9de2a2013-05-25 02:42:55 +00009531 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009532 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009533 }
9534
9535 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009536 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009537 int64_t Cnt;
9538 unsigned VShiftOpc = 0;
9539
9540 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9541 VShiftOpc = ARMISD::VSLI;
9542 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9543 VShiftOpc = ARMISD::VSRI;
9544 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009545 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009546 }
9547
Andrew Trickef9de2a2013-05-25 02:42:55 +00009548 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +00009549 N->getOperand(1), N->getOperand(2),
Owen Anderson9f944592009-08-11 20:47:22 +00009550 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009551 }
9552
9553 case Intrinsic::arm_neon_vqrshifts:
9554 case Intrinsic::arm_neon_vqrshiftu:
9555 // No immediate versions of these to check for.
9556 break;
9557 }
9558
9559 return SDValue();
9560}
9561
9562/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9563/// lowers them. As with the vector shift intrinsics, this is done during DAG
9564/// combining instead of DAG legalizing because the build_vectors for 64-bit
9565/// vector element shift counts are generally not legal, and it is hard to see
9566/// their values after they get legalized to loads from a constant pool.
9567static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9568 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009569 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +00009570 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9571 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9572 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9573 SDValue N1 = N->getOperand(1);
9574 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9575 SDValue N0 = N->getOperand(0);
9576 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9577 DAG.MaskedValueIsZero(N0.getOperand(0),
9578 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009579 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +00009580 }
9581 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009582
9583 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +00009584 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9585 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +00009586 return SDValue();
9587
9588 assert(ST->hasNEON() && "unexpected vector shift");
9589 int64_t Cnt;
9590
9591 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009592 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009593
9594 case ISD::SHL:
9595 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009596 return DAG.getNode(ARMISD::VSHL, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009597 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009598 break;
9599
9600 case ISD::SRA:
9601 case ISD::SRL:
9602 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9603 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9604 ARMISD::VSHRs : ARMISD::VSHRu);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009605 return DAG.getNode(VShiftOpc, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009606 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009607 }
9608 }
9609 return SDValue();
9610}
9611
9612/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9613/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9614static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9615 const ARMSubtarget *ST) {
9616 SDValue N0 = N->getOperand(0);
9617
9618 // Check for sign- and zero-extensions of vector extract operations of 8-
9619 // and 16-bit vector elements. NEON supports these directly. They are
9620 // handled during DAG combining because type legalization will promote them
9621 // to 32-bit types and it is messy to recognize the operations after that.
9622 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9623 SDValue Vec = N0.getOperand(0);
9624 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009625 EVT VT = N->getValueType(0);
9626 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009627 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9628
Owen Anderson9f944592009-08-11 20:47:22 +00009629 if (VT == MVT::i32 &&
9630 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +00009631 TLI.isTypeLegal(Vec.getValueType()) &&
9632 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009633
9634 unsigned Opc = 0;
9635 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009636 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009637 case ISD::SIGN_EXTEND:
9638 Opc = ARMISD::VGETLANEs;
9639 break;
9640 case ISD::ZERO_EXTEND:
9641 case ISD::ANY_EXTEND:
9642 Opc = ARMISD::VGETLANEu;
9643 break;
9644 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009645 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +00009646 }
9647 }
9648
9649 return SDValue();
9650}
9651
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009652/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9653/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9654static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9655 const ARMSubtarget *ST) {
9656 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng55f0c6b2010-07-15 22:07:12 +00009657 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009658 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9659 // a NaN; only do the transformation when it matches that behavior.
9660
9661 // For now only do this when using NEON for FP operations; if using VFP, it
9662 // is not obvious that the benefit outweighs the cost of switching to the
9663 // NEON pipeline.
9664 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9665 N->getValueType(0) != MVT::f32)
9666 return SDValue();
9667
9668 SDValue CondLHS = N->getOperand(0);
9669 SDValue CondRHS = N->getOperand(1);
9670 SDValue LHS = N->getOperand(2);
9671 SDValue RHS = N->getOperand(3);
9672 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9673
9674 unsigned Opcode = 0;
9675 bool IsReversed;
Bob Wilsonba8ac742010-02-24 22:15:53 +00009676 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009677 IsReversed = false; // x CC y ? x : y
Bob Wilsonba8ac742010-02-24 22:15:53 +00009678 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009679 IsReversed = true ; // x CC y ? y : x
9680 } else {
9681 return SDValue();
9682 }
9683
Bob Wilsonba8ac742010-02-24 22:15:53 +00009684 bool IsUnordered;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009685 switch (CC) {
9686 default: break;
9687 case ISD::SETOLT:
9688 case ISD::SETOLE:
9689 case ISD::SETLT:
9690 case ISD::SETLE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009691 case ISD::SETULT:
9692 case ISD::SETULE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009693 // If LHS is NaN, an ordered comparison will be false and the result will
9694 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9695 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9696 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9697 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9698 break;
9699 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9700 // will return -0, so vmin can only be used for unsafe math or if one of
9701 // the operands is known to be nonzero.
9702 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009703 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009704 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9705 break;
9706 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009707 break;
9708
9709 case ISD::SETOGT:
9710 case ISD::SETOGE:
9711 case ISD::SETGT:
9712 case ISD::SETGE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009713 case ISD::SETUGT:
9714 case ISD::SETUGE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009715 // If LHS is NaN, an ordered comparison will be false and the result will
9716 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9717 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9718 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9719 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9720 break;
9721 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9722 // will return +0, so vmax can only be used for unsafe math or if one of
9723 // the operands is known to be nonzero.
9724 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009725 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009726 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9727 break;
9728 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009729 break;
9730 }
9731
9732 if (!Opcode)
9733 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009734 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009735}
9736
Evan Chengf863e3f2011-07-13 00:42:17 +00009737/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9738SDValue
9739ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9740 SDValue Cmp = N->getOperand(4);
9741 if (Cmp.getOpcode() != ARMISD::CMPZ)
9742 // Only looking at EQ and NE cases.
9743 return SDValue();
9744
9745 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009746 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +00009747 SDValue LHS = Cmp.getOperand(0);
9748 SDValue RHS = Cmp.getOperand(1);
9749 SDValue FalseVal = N->getOperand(0);
9750 SDValue TrueVal = N->getOperand(1);
9751 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +00009752 ARMCC::CondCodes CC =
9753 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +00009754
9755 // Simplify
9756 // mov r1, r0
9757 // cmp r1, x
9758 // mov r0, y
9759 // moveq r0, x
9760 // to
9761 // cmp r0, x
9762 // movne r0, y
9763 //
9764 // mov r1, r0
9765 // cmp r1, x
9766 // mov r0, x
9767 // movne r0, y
9768 // to
9769 // cmp r0, x
9770 // movne r0, y
9771 /// FIXME: Turn this into a target neutral optimization?
9772 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +00009773 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +00009774 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9775 N->getOperand(3), Cmp);
9776 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9777 SDValue ARMcc;
9778 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9779 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9780 N->getOperand(3), NewCmp);
9781 }
9782
9783 if (Res.getNode()) {
9784 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00009785 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +00009786 // Capture demanded bits information that would be otherwise lost.
9787 if (KnownZero == 0xfffffffe)
9788 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9789 DAG.getValueType(MVT::i1));
9790 else if (KnownZero == 0xffffff00)
9791 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9792 DAG.getValueType(MVT::i8));
9793 else if (KnownZero == 0xffff0000)
9794 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9795 DAG.getValueType(MVT::i16));
9796 }
9797
9798 return Res;
9799}
9800
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009801SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +00009802 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009803 switch (N->getOpcode()) {
9804 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009805 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +00009806 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009807 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009808 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009809 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +00009810 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9811 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +00009812 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009813 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +00009814 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009815 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009816 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009817 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +00009818 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +00009819 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009820 case ISD::FP_TO_SINT:
9821 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9822 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009823 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00009824 case ISD::SHL:
9825 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009826 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00009827 case ISD::SIGN_EXTEND:
9828 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009829 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9830 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +00009831 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009832 case ISD::LOAD: return PerformLOADCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009833 case ARMISD::VLD2DUP:
9834 case ARMISD::VLD3DUP:
9835 case ARMISD::VLD4DUP:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009836 return PerformVLDCombine(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009837 case ARMISD::BUILD_VECTOR:
9838 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009839 case ISD::INTRINSIC_VOID:
9840 case ISD::INTRINSIC_W_CHAIN:
9841 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9842 case Intrinsic::arm_neon_vld1:
9843 case Intrinsic::arm_neon_vld2:
9844 case Intrinsic::arm_neon_vld3:
9845 case Intrinsic::arm_neon_vld4:
9846 case Intrinsic::arm_neon_vld2lane:
9847 case Intrinsic::arm_neon_vld3lane:
9848 case Intrinsic::arm_neon_vld4lane:
9849 case Intrinsic::arm_neon_vst1:
9850 case Intrinsic::arm_neon_vst2:
9851 case Intrinsic::arm_neon_vst3:
9852 case Intrinsic::arm_neon_vst4:
9853 case Intrinsic::arm_neon_vst2lane:
9854 case Intrinsic::arm_neon_vst3lane:
9855 case Intrinsic::arm_neon_vst4lane:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009856 return PerformVLDCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009857 default: break;
9858 }
9859 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009860 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009861 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009862}
9863
Evan Chengd42641c2011-02-02 01:06:55 +00009864bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9865 EVT VT) const {
9866 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9867}
9868
Matt Arsenault6f2a5262014-07-27 17:46:40 +00009869bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
9870 unsigned,
9871 unsigned,
9872 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009873 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +00009874 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009875
9876 switch (VT.getSimpleVT().SimpleTy) {
9877 default:
9878 return false;
9879 case MVT::i8:
9880 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009881 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009882 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +00009883 if (AllowsUnaligned) {
9884 if (Fast)
9885 *Fast = Subtarget->hasV7Ops();
9886 return true;
9887 }
9888 return false;
9889 }
Evan Chengeec6bc62012-08-15 17:44:53 +00009890 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009891 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009892 // For any little-endian targets with neon, we can support unaligned ld/st
9893 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +00009894 // A big-endian target may also explicitly support unaligned accesses
Evan Cheng79e2ca92012-12-10 23:21:26 +00009895 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
9896 if (Fast)
9897 *Fast = true;
9898 return true;
9899 }
9900 return false;
9901 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009902 }
9903}
9904
Lang Hames9929c422011-11-02 22:52:45 +00009905static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9906 unsigned AlignCheck) {
9907 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9908 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9909}
9910
9911EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9912 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00009913 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +00009914 bool MemcpyStrSrc,
9915 MachineFunction &MF) const {
9916 const Function *F = MF.getFunction();
9917
9918 // See if we can use NEON instructions for this...
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00009919 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
9920 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009921 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +00009922 if (Size >= 16 &&
9923 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +00009924 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009925 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +00009926 } else if (Size >= 8 &&
9927 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +00009928 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
9929 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009930 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +00009931 }
9932 }
9933
Lang Hamesb85fcd02011-11-08 18:56:23 +00009934 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +00009935 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009936 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +00009937 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009938 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +00009939
Lang Hames9929c422011-11-02 22:52:45 +00009940 // Let the target-independent logic figure it out.
9941 return MVT::Other;
9942}
9943
Evan Cheng9ec512d2012-12-06 19:13:27 +00009944bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
9945 if (Val.getOpcode() != ISD::LOAD)
9946 return false;
9947
9948 EVT VT1 = Val.getValueType();
9949 if (!VT1.isSimple() || !VT1.isInteger() ||
9950 !VT2.isSimple() || !VT2.isInteger())
9951 return false;
9952
9953 switch (VT1.getSimpleVT().SimpleTy) {
9954 default: break;
9955 case MVT::i1:
9956 case MVT::i8:
9957 case MVT::i16:
9958 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
9959 return true;
9960 }
9961
9962 return false;
9963}
9964
Ahmed Bougacha4200cc92015-03-05 19:37:53 +00009965bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
9966 EVT VT = ExtVal.getValueType();
9967
9968 if (!isTypeLegal(VT))
9969 return false;
9970
9971 // Don't create a loadext if we can fold the extension into a wide/long
9972 // instruction.
9973 // If there's more than one user instruction, the loadext is desirable no
9974 // matter what. There can be two uses by the same instruction.
9975 if (ExtVal->use_empty() ||
9976 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode()))
9977 return true;
9978
9979 SDNode *U = *ExtVal->use_begin();
9980 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB ||
9981 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL))
9982 return false;
9983
9984 return true;
9985}
9986
Tim Northovercc2e9032013-08-06 13:58:03 +00009987bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
9988 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
9989 return false;
9990
9991 if (!isTypeLegal(EVT::getEVT(Ty1)))
9992 return false;
9993
9994 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
9995
9996 // Assuming the caller doesn't have a zeroext or signext return parameter,
9997 // truncation all the way down to i1 is valid.
9998 return true;
9999}
10000
10001
Evan Chengdc49a8d2009-08-14 20:09:37 +000010002static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
10003 if (V < 0)
10004 return false;
10005
10006 unsigned Scale = 1;
10007 switch (VT.getSimpleVT().SimpleTy) {
10008 default: return false;
10009 case MVT::i1:
10010 case MVT::i8:
10011 // Scale == 1;
10012 break;
10013 case MVT::i16:
10014 // Scale == 2;
10015 Scale = 2;
10016 break;
10017 case MVT::i32:
10018 // Scale == 4;
10019 Scale = 4;
10020 break;
10021 }
10022
10023 if ((V & (Scale - 1)) != 0)
10024 return false;
10025 V /= Scale;
10026 return V == (V & ((1LL << 5) - 1));
10027}
10028
10029static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
10030 const ARMSubtarget *Subtarget) {
10031 bool isNeg = false;
10032 if (V < 0) {
10033 isNeg = true;
10034 V = - V;
10035 }
10036
10037 switch (VT.getSimpleVT().SimpleTy) {
10038 default: return false;
10039 case MVT::i1:
10040 case MVT::i8:
10041 case MVT::i16:
10042 case MVT::i32:
10043 // + imm12 or - imm8
10044 if (isNeg)
10045 return V == (V & ((1LL << 8) - 1));
10046 return V == (V & ((1LL << 12) - 1));
10047 case MVT::f32:
10048 case MVT::f64:
10049 // Same as ARM mode. FIXME: NEON?
10050 if (!Subtarget->hasVFP2())
10051 return false;
10052 if ((V & 3) != 0)
10053 return false;
10054 V >>= 2;
10055 return V == (V & ((1LL << 8) - 1));
10056 }
10057}
10058
Evan Cheng2150b922007-03-12 23:30:29 +000010059/// isLegalAddressImmediate - Return true if the integer value can be used
10060/// as the offset of the target addressing mode for load / store of the
10061/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010062static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010063 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000010064 if (V == 0)
10065 return true;
10066
Evan Chengce5dfb62009-03-09 19:15:00 +000010067 if (!VT.isSimple())
10068 return false;
10069
Evan Chengdc49a8d2009-08-14 20:09:37 +000010070 if (Subtarget->isThumb1Only())
10071 return isLegalT1AddressImmediate(V, VT);
10072 else if (Subtarget->isThumb2())
10073 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000010074
Evan Chengdc49a8d2009-08-14 20:09:37 +000010075 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000010076 if (V < 0)
10077 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000010078 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000010079 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010080 case MVT::i1:
10081 case MVT::i8:
10082 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000010083 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010084 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010085 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000010086 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010087 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010088 case MVT::f32:
10089 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010090 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000010091 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000010092 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000010093 return false;
10094 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010095 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000010096 }
Evan Cheng10043e22007-01-19 07:51:42 +000010097}
10098
Evan Chengdc49a8d2009-08-14 20:09:37 +000010099bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
10100 EVT VT) const {
10101 int Scale = AM.Scale;
10102 if (Scale < 0)
10103 return false;
10104
10105 switch (VT.getSimpleVT().SimpleTy) {
10106 default: return false;
10107 case MVT::i1:
10108 case MVT::i8:
10109 case MVT::i16:
10110 case MVT::i32:
10111 if (Scale == 1)
10112 return true;
10113 // r + r << imm
10114 Scale = Scale & ~1;
10115 return Scale == 2 || Scale == 4 || Scale == 8;
10116 case MVT::i64:
10117 // r + r
10118 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
10119 return true;
10120 return false;
10121 case MVT::isVoid:
10122 // Note, we allow "void" uses (basically, uses that aren't loads or
10123 // stores), because arm allows folding a scale into many arithmetic
10124 // operations. This should be made more precise and revisited later.
10125
10126 // Allow r << imm, but the imm has to be a multiple of two.
10127 if (Scale & 1) return false;
10128 return isPowerOf2_32(Scale);
10129 }
10130}
10131
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010132/// isLegalAddressingMode - Return true if the addressing mode represented
10133/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson7117a912009-03-20 22:42:55 +000010134bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +000010135 Type *Ty) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010136 EVT VT = getValueType(Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000010137 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000010138 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010139
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010140 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000010141 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010142 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010143
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010144 switch (AM.Scale) {
10145 case 0: // no scale reg, must be "r+i" or "r", or "i".
10146 break;
10147 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010148 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010149 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +000010150 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010151 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000010152 // ARM doesn't support any R+R*scale+imm addr modes.
10153 if (AM.BaseOffs)
10154 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010155
Bob Wilson866c1742009-04-08 17:55:28 +000010156 if (!VT.isSimple())
10157 return false;
10158
Evan Chengdc49a8d2009-08-14 20:09:37 +000010159 if (Subtarget->isThumb2())
10160 return isLegalT2ScaledAddressingMode(AM, VT);
10161
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010162 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000010163 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010164 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010165 case MVT::i1:
10166 case MVT::i8:
10167 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010168 if (Scale < 0) Scale = -Scale;
10169 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010170 return true;
10171 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000010172 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000010173 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010174 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010175 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010176 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010177 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000010178 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010179
Owen Anderson9f944592009-08-11 20:47:22 +000010180 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010181 // Note, we allow "void" uses (basically, uses that aren't loads or
10182 // stores), because arm allows folding a scale into many arithmetic
10183 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000010184
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010185 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000010186 if (Scale & 1) return false;
10187 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010188 }
Evan Cheng2150b922007-03-12 23:30:29 +000010189 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010190 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000010191}
10192
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010193/// isLegalICmpImmediate - Return true if the specified immediate is legal
10194/// icmp immediate, that is the target has icmp instructions which can compare
10195/// a register against the immediate without having to materialize the
10196/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000010197bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010198 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010199 if (!Subtarget->isThumb())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010200 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010201 if (Subtarget->isThumb2())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010202 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010203 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000010204 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010205}
10206
Andrew Tricka22cdb72012-07-18 18:34:27 +000010207/// isLegalAddImmediate - Return true if the specified immediate is a legal add
10208/// *or sub* immediate, that is the target has add or sub instructions which can
10209/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000010210/// immediate into a register.
10211bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000010212 // Same encoding for add/sub, just flip the sign.
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010213 int64_t AbsImm = std::abs(Imm);
Andrew Tricka22cdb72012-07-18 18:34:27 +000010214 if (!Subtarget->isThumb())
10215 return ARM_AM::getSOImmVal(AbsImm) != -1;
10216 if (Subtarget->isThumb2())
10217 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
10218 // Thumb1 only has 8-bit unsigned immediate.
10219 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000010220}
10221
Owen Anderson53aa7a92009-08-10 22:56:29 +000010222static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010223 bool isSEXTLoad, SDValue &Base,
10224 SDValue &Offset, bool &isInc,
10225 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000010226 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10227 return false;
10228
Owen Anderson9f944592009-08-11 20:47:22 +000010229 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000010230 // AddressingMode 3
10231 Base = Ptr->getOperand(0);
10232 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010233 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010234 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010235 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010236 isInc = false;
10237 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10238 return true;
10239 }
10240 }
10241 isInc = (Ptr->getOpcode() == ISD::ADD);
10242 Offset = Ptr->getOperand(1);
10243 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000010244 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000010245 // AddressingMode 2
10246 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010247 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010248 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010249 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010250 isInc = false;
10251 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10252 Base = Ptr->getOperand(0);
10253 return true;
10254 }
10255 }
10256
10257 if (Ptr->getOpcode() == ISD::ADD) {
10258 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000010259 ARM_AM::ShiftOpc ShOpcVal=
10260 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000010261 if (ShOpcVal != ARM_AM::no_shift) {
10262 Base = Ptr->getOperand(1);
10263 Offset = Ptr->getOperand(0);
10264 } else {
10265 Base = Ptr->getOperand(0);
10266 Offset = Ptr->getOperand(1);
10267 }
10268 return true;
10269 }
10270
10271 isInc = (Ptr->getOpcode() == ISD::ADD);
10272 Base = Ptr->getOperand(0);
10273 Offset = Ptr->getOperand(1);
10274 return true;
10275 }
10276
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000010277 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000010278 return false;
10279}
10280
Owen Anderson53aa7a92009-08-10 22:56:29 +000010281static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010282 bool isSEXTLoad, SDValue &Base,
10283 SDValue &Offset, bool &isInc,
10284 SelectionDAG &DAG) {
10285 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10286 return false;
10287
10288 Base = Ptr->getOperand(0);
10289 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
10290 int RHSC = (int)RHS->getZExtValue();
10291 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
10292 assert(Ptr->getOpcode() == ISD::ADD);
10293 isInc = false;
10294 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10295 return true;
10296 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
10297 isInc = Ptr->getOpcode() == ISD::ADD;
10298 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
10299 return true;
10300 }
10301 }
10302
10303 return false;
10304}
10305
Evan Cheng10043e22007-01-19 07:51:42 +000010306/// getPreIndexedAddressParts - returns true by value, base pointer and
10307/// offset pointer and addressing mode by reference if the node's address
10308/// can be legally represented as pre-indexed load / store address.
10309bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010310ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
10311 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010312 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010313 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010314 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010315 return false;
10316
Owen Anderson53aa7a92009-08-10 22:56:29 +000010317 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010318 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010319 bool isSEXTLoad = false;
10320 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10321 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010322 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010323 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10324 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10325 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010326 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010327 } else
10328 return false;
10329
10330 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010331 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010332 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010333 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10334 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010335 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010336 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000010337 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000010338 if (!isLegal)
10339 return false;
10340
10341 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10342 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010343}
10344
10345/// getPostIndexedAddressParts - returns true by value, base pointer and
10346/// offset pointer and addressing mode by reference if this node can be
10347/// combined with a load / store to form a post-indexed load / store.
10348bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010349 SDValue &Base,
10350 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010351 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010352 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010353 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010354 return false;
10355
Owen Anderson53aa7a92009-08-10 22:56:29 +000010356 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010357 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010358 bool isSEXTLoad = false;
10359 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010360 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010361 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010362 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10363 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010364 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010365 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010366 } else
10367 return false;
10368
10369 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010370 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010371 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010372 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000010373 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010374 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010375 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10376 isInc, DAG);
10377 if (!isLegal)
10378 return false;
10379
Evan Chengf19384d2010-05-18 21:31:17 +000010380 if (Ptr != Base) {
10381 // Swap base ptr and offset to catch more post-index load / store when
10382 // it's legal. In Thumb2 mode, offset must be an immediate.
10383 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10384 !Subtarget->isThumb2())
10385 std::swap(Base, Offset);
10386
10387 // Post-indexed load / store update the base pointer.
10388 if (Ptr != Base)
10389 return false;
10390 }
10391
Evan Cheng84c6cda2009-07-02 07:28:31 +000010392 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10393 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010394}
10395
Jay Foada0653a32014-05-14 21:14:37 +000010396void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10397 APInt &KnownZero,
10398 APInt &KnownOne,
10399 const SelectionDAG &DAG,
10400 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000010401 unsigned BitWidth = KnownOne.getBitWidth();
10402 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000010403 switch (Op.getOpcode()) {
10404 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000010405 case ARMISD::ADDC:
10406 case ARMISD::ADDE:
10407 case ARMISD::SUBC:
10408 case ARMISD::SUBE:
10409 // These nodes' second result is a boolean
10410 if (Op.getResNo() == 0)
10411 break;
10412 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10413 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010414 case ARMISD::CMOV: {
10415 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000010416 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010417 if (KnownZero == 0 && KnownOne == 0) return;
10418
Dan Gohmanf990faf2008-02-13 00:35:47 +000010419 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000010420 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010421 KnownZero &= KnownZeroRHS;
10422 KnownOne &= KnownOneRHS;
10423 return;
10424 }
Tim Northover01b4aa92014-04-03 15:10:35 +000010425 case ISD::INTRINSIC_W_CHAIN: {
10426 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
10427 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
10428 switch (IntID) {
10429 default: return;
10430 case Intrinsic::arm_ldaex:
10431 case Intrinsic::arm_ldrex: {
10432 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
10433 unsigned MemBits = VT.getScalarType().getSizeInBits();
10434 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
10435 return;
10436 }
10437 }
10438 }
Evan Cheng10043e22007-01-19 07:51:42 +000010439 }
10440}
10441
10442//===----------------------------------------------------------------------===//
10443// ARM Inline Assembly Support
10444//===----------------------------------------------------------------------===//
10445
Evan Cheng078b0b02011-01-08 01:24:27 +000010446bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10447 // Looking for "rev" which is V6+.
10448 if (!Subtarget->hasV6Ops())
10449 return false;
10450
10451 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10452 std::string AsmStr = IA->getAsmString();
10453 SmallVector<StringRef, 4> AsmPieces;
10454 SplitString(AsmStr, AsmPieces, ";\n");
10455
10456 switch (AsmPieces.size()) {
10457 default: return false;
10458 case 1:
10459 AsmStr = AsmPieces[0];
10460 AsmPieces.clear();
10461 SplitString(AsmStr, AsmPieces, " \t,");
10462
10463 // rev $0, $1
10464 if (AsmPieces.size() == 3 &&
10465 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10466 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010467 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010468 if (Ty && Ty->getBitWidth() == 32)
10469 return IntrinsicLowering::LowerToByteSwap(CI);
10470 }
10471 break;
10472 }
10473
10474 return false;
10475}
10476
Evan Cheng10043e22007-01-19 07:51:42 +000010477/// getConstraintType - Given a constraint letter, return the type of
10478/// constraint it is for this target.
10479ARMTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +000010480ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
10481 if (Constraint.size() == 1) {
10482 switch (Constraint[0]) {
10483 default: break;
10484 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010485 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010486 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010487 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010488 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010489 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010490 // An address with a single base register. Due to the way we
10491 // currently handle addresses it is the same as an 'r' memory constraint.
10492 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010493 }
Eric Christophere256cd02011-06-21 22:10:57 +000010494 } else if (Constraint.size() == 2) {
10495 switch (Constraint[0]) {
10496 default: break;
10497 // All 'U+' constraints are addresses.
10498 case 'U': return C_Memory;
10499 }
Evan Cheng10043e22007-01-19 07:51:42 +000010500 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010501 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010502}
10503
John Thompsone8360b72010-10-29 17:29:13 +000010504/// Examine constraint type and operand type and determine a weight value.
10505/// This object must already have been set up with the operand type
10506/// and the current alternative constraint selected.
10507TargetLowering::ConstraintWeight
10508ARMTargetLowering::getSingleConstraintMatchWeight(
10509 AsmOperandInfo &info, const char *constraint) const {
10510 ConstraintWeight weight = CW_Invalid;
10511 Value *CallOperandVal = info.CallOperandVal;
10512 // If we don't have a value, we can't do a match,
10513 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010514 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010515 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010516 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010517 // Look at the constraint type.
10518 switch (*constraint) {
10519 default:
10520 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10521 break;
10522 case 'l':
10523 if (type->isIntegerTy()) {
10524 if (Subtarget->isThumb())
10525 weight = CW_SpecificReg;
10526 else
10527 weight = CW_Register;
10528 }
10529 break;
10530 case 'w':
10531 if (type->isFloatingPointTy())
10532 weight = CW_Register;
10533 break;
10534 }
10535 return weight;
10536}
10537
Eric Christophercf2007c2011-06-30 23:50:52 +000010538typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10539RCPair
Eric Christopher11e4df72015-02-26 22:38:43 +000010540ARMTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
10541 const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000010542 MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000010543 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010544 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000010545 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000010546 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010547 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010548 return RCPair(0U, &ARM::tGPRRegClass);
10549 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000010550 case 'h': // High regs or no regs.
10551 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010552 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000010553 break;
Chris Lattner6223e832007-04-02 17:24:08 +000010554 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000010555 if (Subtarget->isThumb1Only())
10556 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000010557 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010558 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000010559 if (VT == MVT::Other)
10560 break;
Owen Anderson9f944592009-08-11 20:47:22 +000010561 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010562 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000010563 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010564 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000010565 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010566 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010567 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010568 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000010569 if (VT == MVT::Other)
10570 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010571 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010572 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010573 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010574 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010575 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010576 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010577 break;
Eric Christopherc011d312011-07-01 00:30:46 +000010578 case 't':
10579 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010580 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000010581 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010582 }
10583 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000010584 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000010585 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000010586
Eric Christopher11e4df72015-02-26 22:38:43 +000010587 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Evan Cheng10043e22007-01-19 07:51:42 +000010588}
10589
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010590/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10591/// vector. If it is invalid, don't add anything to Ops.
10592void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010593 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010594 std::vector<SDValue>&Ops,
10595 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000010596 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010597
Eric Christopherde9399b2011-06-02 23:16:42 +000010598 // Currently only support length 1 constraints.
10599 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010600
Eric Christopherde9399b2011-06-02 23:16:42 +000010601 char ConstraintLetter = Constraint[0];
10602 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010603 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000010604 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010605 case 'I': case 'J': case 'K': case 'L':
10606 case 'M': case 'N': case 'O':
10607 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10608 if (!C)
10609 return;
10610
10611 int64_t CVal64 = C->getSExtValue();
10612 int CVal = (int) CVal64;
10613 // None of these constraints allow values larger than 32 bits. Check
10614 // that the value fits in an int.
10615 if (CVal != CVal64)
10616 return;
10617
Eric Christopherde9399b2011-06-02 23:16:42 +000010618 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000010619 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000010620 // Constant suitable for movw, must be between 0 and
10621 // 65535.
10622 if (Subtarget->hasV6T2Ops())
10623 if (CVal >= 0 && CVal <= 65535)
10624 break;
10625 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010626 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000010627 if (Subtarget->isThumb1Only()) {
10628 // This must be a constant between 0 and 255, for ADD
10629 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010630 if (CVal >= 0 && CVal <= 255)
10631 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010632 } else if (Subtarget->isThumb2()) {
10633 // A constant that can be used as an immediate value in a
10634 // data-processing instruction.
10635 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10636 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010637 } else {
10638 // A constant that can be used as an immediate value in a
10639 // data-processing instruction.
10640 if (ARM_AM::getSOImmVal(CVal) != -1)
10641 break;
10642 }
10643 return;
10644
10645 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000010646 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010647 // This must be a constant between -255 and -1, for negated ADD
10648 // immediates. This can be used in GCC with an "n" modifier that
10649 // prints the negated value, for use with SUB instructions. It is
10650 // not useful otherwise but is implemented for compatibility.
10651 if (CVal >= -255 && CVal <= -1)
10652 break;
10653 } else {
10654 // This must be a constant between -4095 and 4095. It is not clear
10655 // what this constraint is intended for. Implemented for
10656 // compatibility with GCC.
10657 if (CVal >= -4095 && CVal <= 4095)
10658 break;
10659 }
10660 return;
10661
10662 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000010663 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010664 // A 32-bit value where only one byte has a nonzero value. Exclude
10665 // zero to match GCC. This constraint is used by GCC internally for
10666 // constants that can be loaded with a move/shift combination.
10667 // It is not useful otherwise but is implemented for compatibility.
10668 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10669 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010670 } else if (Subtarget->isThumb2()) {
10671 // A constant whose bitwise inverse can be used as an immediate
10672 // value in a data-processing instruction. This can be used in GCC
10673 // with a "B" modifier that prints the inverted value, for use with
10674 // BIC and MVN instructions. It is not useful otherwise but is
10675 // implemented for compatibility.
10676 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10677 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010678 } else {
10679 // A constant whose bitwise inverse can be used as an immediate
10680 // value in a data-processing instruction. This can be used in GCC
10681 // with a "B" modifier that prints the inverted value, for use with
10682 // BIC and MVN instructions. It is not useful otherwise but is
10683 // implemented for compatibility.
10684 if (ARM_AM::getSOImmVal(~CVal) != -1)
10685 break;
10686 }
10687 return;
10688
10689 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000010690 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010691 // This must be a constant between -7 and 7,
10692 // for 3-operand ADD/SUB immediate instructions.
10693 if (CVal >= -7 && CVal < 7)
10694 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010695 } else if (Subtarget->isThumb2()) {
10696 // A constant whose negation can be used as an immediate value in a
10697 // data-processing instruction. This can be used in GCC with an "n"
10698 // modifier that prints the negated value, for use with SUB
10699 // instructions. It is not useful otherwise but is implemented for
10700 // compatibility.
10701 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10702 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010703 } else {
10704 // A constant whose negation can be used as an immediate value in a
10705 // data-processing instruction. This can be used in GCC with an "n"
10706 // modifier that prints the negated value, for use with SUB
10707 // instructions. It is not useful otherwise but is implemented for
10708 // compatibility.
10709 if (ARM_AM::getSOImmVal(-CVal) != -1)
10710 break;
10711 }
10712 return;
10713
10714 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000010715 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010716 // This must be a multiple of 4 between 0 and 1020, for
10717 // ADD sp + immediate.
10718 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10719 break;
10720 } else {
10721 // A power of two or a constant between 0 and 32. This is used in
10722 // GCC for the shift amount on shifted register operands, but it is
10723 // useful in general for any shift amounts.
10724 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10725 break;
10726 }
10727 return;
10728
10729 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000010730 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010731 // This must be a constant between 0 and 31, for shift amounts.
10732 if (CVal >= 0 && CVal <= 31)
10733 break;
10734 }
10735 return;
10736
10737 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000010738 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010739 // This must be a multiple of 4 between -508 and 508, for
10740 // ADD/SUB sp = sp + immediate.
10741 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10742 break;
10743 }
10744 return;
10745 }
10746 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10747 break;
10748 }
10749
10750 if (Result.getNode()) {
10751 Ops.push_back(Result);
10752 return;
10753 }
Dale Johannesence97d552010-06-25 21:55:36 +000010754 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010755}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010756
Renato Golin87610692013-07-16 09:32:17 +000010757SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
10758 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only");
10759 unsigned Opcode = Op->getOpcode();
10760 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000010761 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000010762 bool isSigned = (Opcode == ISD::SDIVREM);
10763 EVT VT = Op->getValueType(0);
10764 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
10765
10766 RTLIB::Libcall LC;
10767 switch (VT.getSimpleVT().SimpleTy) {
10768 default: llvm_unreachable("Unexpected request for libcall!");
Saleem Abdulrasool740be892014-08-17 22:50:59 +000010769 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
10770 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
10771 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
10772 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
Renato Golin87610692013-07-16 09:32:17 +000010773 }
10774
10775 SDValue InChain = DAG.getEntryNode();
10776
10777 TargetLowering::ArgListTy Args;
10778 TargetLowering::ArgListEntry Entry;
10779 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
10780 EVT ArgVT = Op->getOperand(i).getValueType();
10781 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
10782 Entry.Node = Op->getOperand(i);
10783 Entry.Ty = ArgTy;
10784 Entry.isSExt = isSigned;
10785 Entry.isZExt = !isSigned;
10786 Args.push_back(Entry);
10787 }
10788
10789 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
10790 getPointerTy());
10791
Reid Kleckner343c3952014-11-20 23:51:47 +000010792 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000010793
10794 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010795 TargetLowering::CallLoweringInfo CLI(DAG);
10796 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000010797 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010798 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000010799
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010800 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000010801 return CallInfo.first;
10802}
10803
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000010804SDValue
10805ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
10806 assert(Subtarget->isTargetWindows() && "unsupported target platform");
10807 SDLoc DL(Op);
10808
10809 // Get the inputs.
10810 SDValue Chain = Op.getOperand(0);
10811 SDValue Size = Op.getOperand(1);
10812
10813 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
10814 DAG.getConstant(2, MVT::i32));
10815
10816 SDValue Flag;
10817 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
10818 Flag = Chain.getValue(1);
10819
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000010820 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000010821 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
10822
10823 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
10824 Chain = NewSP.getValue(1);
10825
10826 SDValue Ops[2] = { NewSP, Chain };
10827 return DAG.getMergeValues(Ops, DL);
10828}
10829
Oliver Stannard51b1d462014-08-21 12:50:31 +000010830SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
10831 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
10832 "Unexpected type for custom-lowering FP_EXTEND");
10833
10834 RTLIB::Libcall LC;
10835 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
10836
10837 SDValue SrcVal = Op.getOperand(0);
10838 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
10839 /*isSigned*/ false, SDLoc(Op)).first;
10840}
10841
10842SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
10843 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
10844 Subtarget->isFPOnlySP() &&
10845 "Unexpected type for custom-lowering FP_ROUND");
10846
10847 RTLIB::Libcall LC;
10848 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
10849
10850 SDValue SrcVal = Op.getOperand(0);
10851 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
10852 /*isSigned*/ false, SDLoc(Op)).first;
10853}
10854
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010855bool
10856ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10857 // The ARM target isn't yet aware of offsets.
10858 return false;
10859}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010860
Jim Grosbach11013ed2010-07-16 23:05:05 +000010861bool ARM::isBitFieldInvertedMask(unsigned v) {
10862 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010863 return false;
10864
Jim Grosbach11013ed2010-07-16 23:05:05 +000010865 // there can be 1's on either or both "outsides", all the "inside"
10866 // bits must be 0's
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000010867 return isShiftedMask_32(~v);
Jim Grosbach11013ed2010-07-16 23:05:05 +000010868}
10869
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010870/// isFPImmLegal - Returns true if the target can instruction select the
10871/// specified FP immediate natively. If false, the legalizer will
10872/// materialize the FP immediate as a load from a constant pool.
10873bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10874 if (!Subtarget->hasVFP3())
10875 return false;
10876 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010877 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000010878 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000010879 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010880 return false;
10881}
Bob Wilson5549d492010-09-21 17:56:22 +000010882
Wesley Peck527da1b2010-11-23 03:31:01 +000010883/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000010884/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10885/// specified in the intrinsic calls.
10886bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10887 const CallInst &I,
10888 unsigned Intrinsic) const {
10889 switch (Intrinsic) {
10890 case Intrinsic::arm_neon_vld1:
10891 case Intrinsic::arm_neon_vld2:
10892 case Intrinsic::arm_neon_vld3:
10893 case Intrinsic::arm_neon_vld4:
10894 case Intrinsic::arm_neon_vld2lane:
10895 case Intrinsic::arm_neon_vld3lane:
10896 case Intrinsic::arm_neon_vld4lane: {
10897 Info.opc = ISD::INTRINSIC_W_CHAIN;
10898 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010899 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010900 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10901 Info.ptrVal = I.getArgOperand(0);
10902 Info.offset = 0;
10903 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10904 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10905 Info.vol = false; // volatile loads with NEON intrinsics not supported
10906 Info.readMem = true;
10907 Info.writeMem = false;
10908 return true;
10909 }
10910 case Intrinsic::arm_neon_vst1:
10911 case Intrinsic::arm_neon_vst2:
10912 case Intrinsic::arm_neon_vst3:
10913 case Intrinsic::arm_neon_vst4:
10914 case Intrinsic::arm_neon_vst2lane:
10915 case Intrinsic::arm_neon_vst3lane:
10916 case Intrinsic::arm_neon_vst4lane: {
10917 Info.opc = ISD::INTRINSIC_VOID;
10918 // Conservatively set memVT to the entire set of vectors stored.
10919 unsigned NumElts = 0;
10920 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000010921 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000010922 if (!ArgTy->isVectorTy())
10923 break;
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010924 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010925 }
10926 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10927 Info.ptrVal = I.getArgOperand(0);
10928 Info.offset = 0;
10929 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10930 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10931 Info.vol = false; // volatile stores with NEON intrinsics not supported
10932 Info.readMem = false;
10933 Info.writeMem = true;
10934 return true;
10935 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010936 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010937 case Intrinsic::arm_ldrex: {
10938 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
10939 Info.opc = ISD::INTRINSIC_W_CHAIN;
10940 Info.memVT = MVT::getVT(PtrTy->getElementType());
10941 Info.ptrVal = I.getArgOperand(0);
10942 Info.offset = 0;
10943 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10944 Info.vol = true;
10945 Info.readMem = true;
10946 Info.writeMem = false;
10947 return true;
10948 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010949 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010950 case Intrinsic::arm_strex: {
10951 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
10952 Info.opc = ISD::INTRINSIC_W_CHAIN;
10953 Info.memVT = MVT::getVT(PtrTy->getElementType());
10954 Info.ptrVal = I.getArgOperand(1);
10955 Info.offset = 0;
10956 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10957 Info.vol = true;
10958 Info.readMem = false;
10959 Info.writeMem = true;
10960 return true;
10961 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010962 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010963 case Intrinsic::arm_strexd: {
10964 Info.opc = ISD::INTRINSIC_W_CHAIN;
10965 Info.memVT = MVT::i64;
10966 Info.ptrVal = I.getArgOperand(2);
10967 Info.offset = 0;
10968 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010969 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010970 Info.readMem = false;
10971 Info.writeMem = true;
10972 return true;
10973 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010974 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010975 case Intrinsic::arm_ldrexd: {
10976 Info.opc = ISD::INTRINSIC_W_CHAIN;
10977 Info.memVT = MVT::i64;
10978 Info.ptrVal = I.getArgOperand(0);
10979 Info.offset = 0;
10980 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010981 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010982 Info.readMem = true;
10983 Info.writeMem = false;
10984 return true;
10985 }
Bob Wilson5549d492010-09-21 17:56:22 +000010986 default:
10987 break;
10988 }
10989
10990 return false;
10991}
Juergen Ributzka659ce002014-01-28 01:20:14 +000010992
10993/// \brief Returns true if it is beneficial to convert a load of a constant
10994/// to just the constant itself.
10995bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
10996 Type *Ty) const {
10997 assert(Ty->isIntegerTy());
10998
10999 unsigned Bits = Ty->getPrimitiveSizeInBits();
11000 if (Bits == 0 || Bits > 32)
11001 return false;
11002 return true;
11003}
Tim Northover037f26f22014-04-17 18:22:47 +000011004
Robin Morisset25c8e312014-09-17 00:06:58 +000011005bool ARMTargetLowering::hasLoadLinkedStoreConditional() const { return true; }
11006
Robin Morisset5349e8e2014-09-18 18:56:04 +000011007Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
11008 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000011009 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000011010
11011 // First, if the target has no DMB, see what fallback we can use.
11012 if (!Subtarget->hasDataBarrier()) {
11013 // Some ARMv6 cpus can support data barriers with an mcr instruction.
11014 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
11015 // here.
11016 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
11017 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
11018 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
11019 Builder.getInt32(0), Builder.getInt32(7),
11020 Builder.getInt32(10), Builder.getInt32(5)};
11021 return Builder.CreateCall(MCR, args);
11022 } else {
11023 // Instead of using barriers, atomic accesses on these subtargets use
11024 // libcalls.
11025 llvm_unreachable("makeDMB on a target so old that it has no barriers");
11026 }
11027 } else {
11028 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
11029 // Only a full system barrier exists in the M-class architectures.
11030 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
11031 Constant *CDomain = Builder.getInt32(Domain);
11032 return Builder.CreateCall(DMB, CDomain);
11033 }
Robin Morisseta47cb412014-09-03 21:01:03 +000011034}
11035
11036// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000011037Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011038 AtomicOrdering Ord, bool IsStore,
11039 bool IsLoad) const {
11040 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011041 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011042
11043 switch (Ord) {
11044 case NotAtomic:
11045 case Unordered:
11046 llvm_unreachable("Invalid fence: unordered/non-atomic");
11047 case Monotonic:
11048 case Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000011049 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011050 case SequentiallyConsistent:
11051 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000011052 return nullptr; // Nothing to do
11053 /*FALLTHROUGH*/
Robin Morisseta47cb412014-09-03 21:01:03 +000011054 case Release:
11055 case AcquireRelease:
11056 if (Subtarget->isSwift())
Robin Morissetdedef332014-09-23 20:31:14 +000011057 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000011058 // FIXME: add a comment with a link to documentation justifying this.
11059 else
Robin Morissetdedef332014-09-23 20:31:14 +000011060 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011061 }
Robin Morissetdedef332014-09-23 20:31:14 +000011062 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011063}
11064
Robin Morissetdedef332014-09-23 20:31:14 +000011065Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011066 AtomicOrdering Ord, bool IsStore,
11067 bool IsLoad) const {
11068 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011069 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011070
11071 switch (Ord) {
11072 case NotAtomic:
11073 case Unordered:
11074 llvm_unreachable("Invalid fence: unordered/not-atomic");
11075 case Monotonic:
11076 case Release:
Robin Morissetdedef332014-09-23 20:31:14 +000011077 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011078 case Acquire:
11079 case AcquireRelease:
Robin Morissetdedef332014-09-23 20:31:14 +000011080 case SequentiallyConsistent:
11081 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011082 }
Robin Morissetdedef332014-09-23 20:31:14 +000011083 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011084}
11085
Robin Morisseted3d48f2014-09-03 21:29:59 +000011086// Loads and stores less than 64-bits are already atomic; ones above that
11087// are doomed anyway, so defer to the default libcall and blame the OS when
11088// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11089// anything for those.
11090bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
11091 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
11092 return (Size == 64) && !Subtarget->isMClass();
11093}
Tim Northover037f26f22014-04-17 18:22:47 +000011094
Robin Morisseted3d48f2014-09-03 21:29:59 +000011095// Loads and stores less than 64-bits are already atomic; ones above that
11096// are doomed anyway, so defer to the default libcall and blame the OS when
11097// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11098// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000011099// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
11100// guarantee, see DDI0406C ARM architecture reference manual,
11101// sections A8.8.72-74 LDRD)
Robin Morisseted3d48f2014-09-03 21:29:59 +000011102bool ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
11103 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
11104 return (Size == 64) && !Subtarget->isMClass();
11105}
11106
11107// For the real atomic operations, we have ldrex/strex up to 32 bits,
11108// and up to 64 bits on the non-M profiles
JF Bastienf14889e2015-03-04 15:47:57 +000011109TargetLoweringBase::AtomicRMWExpansionKind
11110ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000011111 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
JF Bastienf14889e2015-03-04 15:47:57 +000011112 return (Size <= (Subtarget->isMClass() ? 32U : 64U))
11113 ? AtomicRMWExpansionKind::LLSC
11114 : AtomicRMWExpansionKind::None;
Tim Northover037f26f22014-04-17 18:22:47 +000011115}
11116
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011117// This has so far only been implemented for MachO.
11118bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000011119 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011120}
11121
Quentin Colombetc32615d2014-10-31 17:52:53 +000011122bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
11123 unsigned &Cost) const {
11124 // If we do not have NEON, vector types are not natively supported.
11125 if (!Subtarget->hasNEON())
11126 return false;
11127
11128 // Floating point values and vector values map to the same register file.
11129 // Therefore, althought we could do a store extract of a vector type, this is
11130 // better to leave at float as we have more freedom in the addressing mode for
11131 // those.
11132 if (VectorTy->isFPOrFPVectorTy())
11133 return false;
11134
11135 // If the index is unknown at compile time, this is very expensive to lower
11136 // and it is not possible to combine the store with the extract.
11137 if (!isa<ConstantInt>(Idx))
11138 return false;
11139
11140 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
11141 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
11142 // We can do a store + vector extract on any vector that fits perfectly in a D
11143 // or Q register.
11144 if (BitWidth == 64 || BitWidth == 128) {
11145 Cost = 0;
11146 return true;
11147 }
11148 return false;
11149}
11150
Tim Northover037f26f22014-04-17 18:22:47 +000011151Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
11152 AtomicOrdering Ord) const {
11153 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
11154 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
Robin Morissetb155f522014-08-18 16:48:58 +000011155 bool IsAcquire = isAtLeastAcquire(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011156
11157 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
11158 // intrinsic must return {i32, i32} and we have to recombine them into a
11159 // single i64 here.
11160 if (ValTy->getPrimitiveSizeInBits() == 64) {
11161 Intrinsic::ID Int =
11162 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
11163 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
11164
11165 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11166 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
11167
11168 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
11169 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011170 if (!Subtarget->isLittle())
11171 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011172 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
11173 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
11174 return Builder.CreateOr(
11175 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
11176 }
11177
11178 Type *Tys[] = { Addr->getType() };
11179 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
11180 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
11181
11182 return Builder.CreateTruncOrBitCast(
11183 Builder.CreateCall(Ldrex, Addr),
11184 cast<PointerType>(Addr->getType())->getElementType());
11185}
11186
11187Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
11188 Value *Addr,
11189 AtomicOrdering Ord) const {
11190 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morissetb155f522014-08-18 16:48:58 +000011191 bool IsRelease = isAtLeastRelease(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011192
11193 // Since the intrinsics must have legal type, the i64 intrinsics take two
11194 // parameters: "i32, i32". We must marshal Val into the appropriate form
11195 // before the call.
11196 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
11197 Intrinsic::ID Int =
11198 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
11199 Function *Strex = Intrinsic::getDeclaration(M, Int);
11200 Type *Int32Ty = Type::getInt32Ty(M->getContext());
11201
11202 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
11203 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011204 if (!Subtarget->isLittle())
11205 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011206 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11207 return Builder.CreateCall3(Strex, Lo, Hi, Addr);
11208 }
11209
11210 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
11211 Type *Tys[] = { Addr->getType() };
11212 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
11213
11214 return Builder.CreateCall2(
11215 Strex, Builder.CreateZExtOrBitCast(
11216 Val, Strex->getFunctionType()->getParamType(0)),
11217 Addr);
11218}
Oliver Stannardc24f2172014-05-09 14:01:47 +000011219
11220enum HABaseType {
11221 HA_UNKNOWN = 0,
11222 HA_FLOAT,
11223 HA_DOUBLE,
11224 HA_VECT64,
11225 HA_VECT128
11226};
11227
11228static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
11229 uint64_t &Members) {
11230 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
11231 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
11232 uint64_t SubMembers = 0;
11233 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
11234 return false;
11235 Members += SubMembers;
11236 }
11237 } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
11238 uint64_t SubMembers = 0;
11239 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
11240 return false;
11241 Members += SubMembers * AT->getNumElements();
11242 } else if (Ty->isFloatTy()) {
11243 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
11244 return false;
11245 Members = 1;
11246 Base = HA_FLOAT;
11247 } else if (Ty->isDoubleTy()) {
11248 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
11249 return false;
11250 Members = 1;
11251 Base = HA_DOUBLE;
11252 } else if (const VectorType *VT = dyn_cast<VectorType>(Ty)) {
11253 Members = 1;
11254 switch (Base) {
11255 case HA_FLOAT:
11256 case HA_DOUBLE:
11257 return false;
11258 case HA_VECT64:
11259 return VT->getBitWidth() == 64;
11260 case HA_VECT128:
11261 return VT->getBitWidth() == 128;
11262 case HA_UNKNOWN:
11263 switch (VT->getBitWidth()) {
11264 case 64:
11265 Base = HA_VECT64;
11266 return true;
11267 case 128:
11268 Base = HA_VECT128;
11269 return true;
11270 default:
11271 return false;
11272 }
11273 }
11274 }
11275
11276 return (Members > 0 && Members <= 4);
11277}
11278
Tim Northovere95c5b32015-02-24 17:22:34 +000011279/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of
11280/// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when
11281/// passing according to AAPCS rules.
Oliver Stannardc24f2172014-05-09 14:01:47 +000011282bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
11283 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000011284 if (getEffectiveCallingConv(CallConv, isVarArg) !=
11285 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000011286 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000011287
11288 HABaseType Base = HA_UNKNOWN;
11289 uint64_t Members = 0;
Tim Northovere95c5b32015-02-24 17:22:34 +000011290 bool IsHA = isHomogeneousAggregate(Ty, Base, Members);
11291 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump());
11292
11293 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy();
11294 return IsHA || IsIntArray;
Oliver Stannardc24f2172014-05-09 14:01:47 +000011295}