blob: 63214885b58677e6ef463003793b8809de6c02aa [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"
John Brawn0dbcd652015-03-18 12:01:59 +000044#include "llvm/IR/IntrinsicInst.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000045#include "llvm/IR/Intrinsics.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000046#include "llvm/IR/Type.h"
Bill Wendling46ffefc2010-03-09 02:46:12 +000047#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach32bb3622010-04-14 22:28:31 +000048#include "llvm/Support/CommandLine.h"
Oliver Stannardc24f2172014-05-09 14:01:47 +000049#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000050#include "llvm/Support/ErrorHandling.h"
Evan Cheng2150b922007-03-12 23:30:29 +000051#include "llvm/Support/MathExtras.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000052#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000053#include "llvm/Target/TargetOptions.h"
David Peixottoc32e24a2013-10-17 19:49:22 +000054#include <utility>
Evan Cheng10043e22007-01-19 07:51:42 +000055using namespace llvm;
56
Chandler Carruth84e68b22014-04-22 02:41:26 +000057#define DEBUG_TYPE "arm-isel"
58
Dale Johannesend679ff72010-06-03 21:09:53 +000059STATISTIC(NumTailCalls, "Number of tail calls");
Evan Cheng68aec142011-01-19 02:16:49 +000060STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren9f911162012-06-01 02:44:42 +000061STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesend679ff72010-06-03 21:09:53 +000062
Eric Christopher347f4c32010-12-15 23:47:29 +000063cl::opt<bool>
Jim Grosbach32bb3622010-04-14 22:28:31 +000064EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng25f93642010-07-08 02:08:50 +000065 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbach32bb3622010-04-14 22:28:31 +000066 cl::init(false));
67
Evan Chengf128bdc2010-06-16 07:35:02 +000068static cl::opt<bool>
69ARMInterworking("arm-interworking", cl::Hidden,
70 cl::desc("Enable / disable ARM interworking (for debugging only)"),
71 cl::init(true));
72
Benjamin Kramer7ba71be2011-11-26 23:01:57 +000073namespace {
Cameron Zwarich89019782011-06-10 20:59:24 +000074 class ARMCCState : public CCState {
75 public:
76 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Eric Christopherb5217502014-08-06 18:45:26 +000077 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
78 ParmContext PC)
79 : CCState(CC, isVarArg, MF, locs, C) {
Cameron Zwarich89019782011-06-10 20:59:24 +000080 assert(((PC == Call) || (PC == Prologue)) &&
81 "ARMCCState users must specify whether their context is call"
82 "or prologue generation.");
83 CallOrPrologue = PC;
84 }
85 };
86}
87
Stuart Hastings45fe3c32011-04-20 16:47:52 +000088// The APCS parameter registers.
Craig Topper840beec2014-04-04 05:16:06 +000089static const MCPhysReg GPRArgRegs[] = {
Stuart Hastings45fe3c32011-04-20 16:47:52 +000090 ARM::R0, ARM::R1, ARM::R2, ARM::R3
91};
92
Craig Topper4fa625f2012-08-12 03:16:37 +000093void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
94 MVT PromotedBitwiseVT) {
Bob Wilson2e076c42009-06-22 23:27:02 +000095 if (VT != PromotedLdStVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +000096 setOperationAction(ISD::LOAD, VT, Promote);
97 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000098
Craig Topper4fa625f2012-08-12 03:16:37 +000099 setOperationAction(ISD::STORE, VT, Promote);
100 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000101 }
102
Craig Topper4fa625f2012-08-12 03:16:37 +0000103 MVT ElemTy = VT.getVectorElementType();
Owen Anderson9f944592009-08-11 20:47:22 +0000104 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Craig Topper4fa625f2012-08-12 03:16:37 +0000105 setOperationAction(ISD::SETCC, VT, Custom);
106 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
107 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000108 if (ElemTy == MVT::i32) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000109 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
110 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
111 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
112 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000113 } else {
Craig Topper4fa625f2012-08-12 03:16:37 +0000114 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
115 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
116 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
117 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson5d8cfb22009-09-16 20:20:44 +0000118 }
Craig Topper4fa625f2012-08-12 03:16:37 +0000119 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
120 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
121 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
122 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
123 setOperationAction(ISD::SELECT, VT, Expand);
124 setOperationAction(ISD::SELECT_CC, VT, Expand);
Jim Grosbach30af4422012-10-12 22:59:21 +0000125 setOperationAction(ISD::VSELECT, VT, Expand);
Craig Topper4fa625f2012-08-12 03:16:37 +0000126 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000127 if (VT.isInteger()) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000128 setOperationAction(ISD::SHL, VT, Custom);
129 setOperationAction(ISD::SRA, VT, Custom);
130 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson2e076c42009-06-22 23:27:02 +0000131 }
132
133 // Promote all bit-wise operations.
134 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000135 setOperationAction(ISD::AND, VT, Promote);
136 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
137 setOperationAction(ISD::OR, VT, Promote);
138 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
139 setOperationAction(ISD::XOR, VT, Promote);
140 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000141 }
Bob Wilson4ed397c2009-09-16 00:17:28 +0000142
143 // Neon does not support vector divide/remainder operations.
Craig Topper4fa625f2012-08-12 03:16:37 +0000144 setOperationAction(ISD::SDIV, VT, Expand);
145 setOperationAction(ISD::UDIV, VT, Expand);
146 setOperationAction(ISD::FDIV, VT, Expand);
147 setOperationAction(ISD::SREM, VT, Expand);
148 setOperationAction(ISD::UREM, VT, Expand);
149 setOperationAction(ISD::FREM, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000150}
151
Craig Topper4fa625f2012-08-12 03:16:37 +0000152void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000153 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000154 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000155}
156
Craig Topper4fa625f2012-08-12 03:16:37 +0000157void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000158 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000159 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000160}
161
Eric Christopher1889fdc2015-01-29 00:19:39 +0000162ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM,
163 const ARMSubtarget &STI)
164 : TargetLowering(TM), Subtarget(&STI) {
165 RegInfo = Subtarget->getRegisterInfo();
166 Itins = Subtarget->getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000167
Duncan Sandsf2641e12011-09-06 19:07:46 +0000168 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
169
Tim Northoverd6a729b2014-01-06 14:28:05 +0000170 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000171 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000172 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
Eric Christopher824f42f2015-05-12 01:26:05 +0000173 Subtarget->hasARMOps() && !Subtarget->useSoftFloat()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000174 // Single-precision floating-point arithmetic.
175 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
176 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
177 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
178 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000179
Evan Chengc9f22fd12007-04-27 08:15:43 +0000180 // Double-precision floating-point arithmetic.
181 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
182 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
183 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
184 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng143576d2007-01-31 09:30:58 +0000185
Evan Chengc9f22fd12007-04-27 08:15:43 +0000186 // Single-precision comparisons.
187 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
188 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
189 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
190 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
191 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
192 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
193 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
194 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000195
Evan Chengc9f22fd12007-04-27 08:15:43 +0000196 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
197 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
198 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
199 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
200 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
201 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng143576d2007-01-31 09:30:58 +0000204
Evan Chengc9f22fd12007-04-27 08:15:43 +0000205 // Double-precision comparisons.
206 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
207 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
208 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
209 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
210 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
211 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
212 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
213 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000214
Evan Chengc9f22fd12007-04-27 08:15:43 +0000215 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
216 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
217 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
218 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
219 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
220 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
221 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
222 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Cheng10043e22007-01-19 07:51:42 +0000223
Evan Chengc9f22fd12007-04-27 08:15:43 +0000224 // Floating-point to integer conversions.
225 // i64 conversions are done via library routines even when generating VFP
226 // instructions, so use the same ones.
227 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
228 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
229 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
230 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000231
Evan Chengc9f22fd12007-04-27 08:15:43 +0000232 // Conversions between floating types.
233 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
234 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
235
236 // Integer to floating-point conversions.
237 // i64 conversions are done via library routines even when generating VFP
238 // instructions, so use the same ones.
Bob Wilsondc40d5a2009-03-20 23:16:43 +0000239 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
240 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengc9f22fd12007-04-27 08:15:43 +0000241 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
242 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
243 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
244 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
245 }
Evan Cheng10043e22007-01-19 07:51:42 +0000246 }
247
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000248 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000249 setLibcallName(RTLIB::SHL_I128, nullptr);
250 setLibcallName(RTLIB::SRL_I128, nullptr);
251 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000252
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000253 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetMachO() &&
254 !Subtarget->isTargetWindows()) {
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000255 static const struct {
256 const RTLIB::Libcall Op;
257 const char * const Name;
258 const CallingConv::ID CC;
259 const ISD::CondCode Cond;
260 } LibraryCalls[] = {
261 // Double-precision floating-point arithmetic helper functions
262 // RTABI chapter 4.1.2, Table 2
263 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
264 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
265 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
266 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000267
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000268 // Double-precision floating-point comparison helper functions
269 // RTABI chapter 4.1.2, Table 3
270 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
271 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
272 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
273 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
274 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
275 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
276 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
277 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000278
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000279 // Single-precision floating-point arithmetic helper functions
280 // RTABI chapter 4.1.2, Table 4
281 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
282 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
283 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
284 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000285
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000286 // Single-precision floating-point comparison helper functions
287 // RTABI chapter 4.1.2, Table 5
288 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
289 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
290 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
291 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
292 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
293 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
294 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
295 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000296
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000297 // Floating-point to integer conversions.
298 // RTABI chapter 4.1.2, Table 6
299 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
300 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
301 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
302 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
303 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
304 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
305 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
306 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000307
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000308 // Conversions between floating types.
309 // RTABI chapter 4.1.2, Table 7
310 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000311 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Chad Rosierad7c9102014-08-23 18:29:43 +0000312 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000313
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000314 // Integer to floating-point conversions.
315 // RTABI chapter 4.1.2, Table 8
316 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
317 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
318 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
319 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
320 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
321 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
322 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
323 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000324
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000325 // Long long helper functions
326 // RTABI chapter 4.2, Table 9
Chad Rosierad7c9102014-08-23 18:29:43 +0000327 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
328 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
329 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
330 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000331
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000332 // Integer division functions
333 // RTABI chapter 4.3.1
Chad Rosierad7c9102014-08-23 18:29:43 +0000334 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
335 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
336 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
337 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
338 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
339 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
340 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
341 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Renato Golin4cd51872011-05-22 21:41:23 +0000342
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000343 // Memory operations
344 // RTABI chapter 4.3.4
345 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
346 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
347 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
348 };
349
350 for (const auto &LC : LibraryCalls) {
351 setLibcallName(LC.Op, LC.Name);
352 setLibcallCallingConv(LC.Op, LC.CC);
353 if (LC.Cond != ISD::SETCC_INVALID)
354 setCmpLibcallCC(LC.Op, LC.Cond);
355 }
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000356 }
357
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000358 if (Subtarget->isTargetWindows()) {
359 static const struct {
360 const RTLIB::Libcall Op;
361 const char * const Name;
362 const CallingConv::ID CC;
363 } LibraryCalls[] = {
364 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
365 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
366 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
367 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
368 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
369 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
370 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
371 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
372 };
373
374 for (const auto &LC : LibraryCalls) {
375 setLibcallName(LC.Op, LC.Name);
376 setLibcallCallingConv(LC.Op, LC.CC);
377 }
378 }
379
Bob Wilsonbc158992011-10-07 16:59:21 +0000380 // Use divmod compiler-rt calls for iOS 5.0 and later.
Cameron Esfahani943908b2013-08-29 20:23:14 +0000381 if (Subtarget->getTargetTriple().isiOS() &&
Bob Wilsonbc158992011-10-07 16:59:21 +0000382 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
383 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
384 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
385 }
386
Oliver Stannard11790b22014-08-11 09:12:32 +0000387 // The half <-> float conversion functions are always soft-float, but are
388 // needed for some targets which use a hard-float calling convention by
389 // default.
390 if (Subtarget->isAAPCS_ABI()) {
391 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
392 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
393 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
394 } else {
395 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
396 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
397 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
398 }
399
David Goodwin22c2fba2009-07-08 23:10:31 +0000400 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000401 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000402 else
Craig Topperc7242e02012-04-20 07:30:17 +0000403 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Eric Christopher824f42f2015-05-12 01:26:05 +0000404 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000405 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000406 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000407 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Evan Cheng10043e22007-01-19 07:51:42 +0000408 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000409
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000410 for (MVT VT : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000411 for (MVT InnerVT : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000412 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000413 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
414 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
415 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
416 }
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000417
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000418 setOperationAction(ISD::MULHS, VT, Expand);
419 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
420 setOperationAction(ISD::MULHU, VT, Expand);
421 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000422
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000423 setOperationAction(ISD::BSWAP, VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000424 }
425
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000426 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000427 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000428
Bob Wilson2e076c42009-06-22 23:27:02 +0000429 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000430 addDRTypeForNEON(MVT::v2f32);
431 addDRTypeForNEON(MVT::v8i8);
432 addDRTypeForNEON(MVT::v4i16);
433 addDRTypeForNEON(MVT::v2i32);
434 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000435
Owen Anderson9f944592009-08-11 20:47:22 +0000436 addQRTypeForNEON(MVT::v4f32);
437 addQRTypeForNEON(MVT::v2f64);
438 addQRTypeForNEON(MVT::v16i8);
439 addQRTypeForNEON(MVT::v8i16);
440 addQRTypeForNEON(MVT::v4i32);
441 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000442
Bob Wilson194a2512009-09-15 23:55:57 +0000443 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
444 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000445 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
446 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000447 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
448 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
449 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000450 // FIXME: Code duplication: FDIV and FREM are expanded always, see
451 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000452 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
453 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000454 // FIXME: Create unittest.
455 // In another words, find a way when "copysign" appears in DAG with vector
456 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000457 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000458 // FIXME: Code duplication: SETCC has custom operation action, see
459 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000460 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000461 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000462 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
463 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
464 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
465 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
466 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
467 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
468 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
469 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
470 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
471 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
472 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
473 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000474 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000475 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
476 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
477 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
478 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
479 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000480 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000481
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000482 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
483 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
484 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
485 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
486 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
487 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
488 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
489 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
490 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
491 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000492 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
493 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
494 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
495 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000496 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000497
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000498 // Mark v2f32 intrinsics.
499 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
500 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
501 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
502 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
503 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
504 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
505 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
506 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
507 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
508 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
509 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
510 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
511 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
512 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
513 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
514
Bob Wilson6cc46572009-09-16 00:32:15 +0000515 // Neon does not support some operations on v1i64 and v2i64 types.
516 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000517 // Custom handling for some quad-vector types to detect VMULL.
518 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
519 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
520 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000521 // Custom handling for some vector types to avoid expensive expansions
522 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
523 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
524 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
525 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000526 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
527 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000528 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000529 // a destination type that is wider than the source, and nor does
530 // it have a FP_TO_[SU]INT instruction with a narrower destination than
531 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000532 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
533 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000534 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
535 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000536
Eli Friedmane6385e62012-11-15 22:44:27 +0000537 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000538 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000539
Evan Chengb4eae132012-12-04 22:41:50 +0000540 // NEON does not have single instruction CTPOP for vectors with element
541 // types wider than 8-bits. However, custom lowering can leverage the
542 // v8i8/v16i8 vcnt instruction.
543 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
544 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
545 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
546 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
547
Jim Grosbach5f215872013-02-27 21:31:12 +0000548 // NEON only has FMA instructions as of VFP4.
549 if (!Subtarget->hasVFP4()) {
550 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
551 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
552 }
553
Bob Wilson06fce872011-02-07 17:43:21 +0000554 setTargetDAGCombine(ISD::INTRINSIC_VOID);
555 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000556 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
557 setTargetDAGCombine(ISD::SHL);
558 setTargetDAGCombine(ISD::SRL);
559 setTargetDAGCombine(ISD::SRA);
560 setTargetDAGCombine(ISD::SIGN_EXTEND);
561 setTargetDAGCombine(ISD::ZERO_EXTEND);
562 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsonc6c13a32010-02-18 06:05:53 +0000563 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000564 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000565 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000566 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
567 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000568 setTargetDAGCombine(ISD::FP_TO_SINT);
569 setTargetDAGCombine(ISD::FP_TO_UINT);
570 setTargetDAGCombine(ISD::FDIV);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +0000571 setTargetDAGCombine(ISD::LOAD);
Nadav Rotem097106b2011-10-15 20:03:12 +0000572
James Molloy547d4c02012-02-20 09:24:05 +0000573 // It is legal to extload from v4i8 to v4i16 or v4i32.
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000574 for (MVT Ty : {MVT::v8i8, MVT::v4i8, MVT::v2i8, MVT::v4i16, MVT::v2i16,
575 MVT::v2i32}) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000576 for (MVT VT : MVT::integer_vector_valuetypes()) {
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000577 setLoadExtAction(ISD::EXTLOAD, VT, Ty, Legal);
578 setLoadExtAction(ISD::ZEXTLOAD, VT, Ty, Legal);
579 setLoadExtAction(ISD::SEXTLOAD, VT, Ty, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000580 }
James Molloy547d4c02012-02-20 09:24:05 +0000581 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000582 }
583
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000584 // ARM and Thumb2 support UMLAL/SMLAL.
585 if (!Subtarget->isThumb1Only())
586 setTargetDAGCombine(ISD::ADDC);
587
Oliver Stannard51b1d462014-08-21 12:50:31 +0000588 if (Subtarget->isFPOnlySP()) {
589 // When targetting a floating-point unit with only single-precision
590 // operations, f64 is legal for the few double-precision instructions which
591 // are present However, no double-precision operations other than moves,
592 // loads and stores are provided by the hardware.
593 setOperationAction(ISD::FADD, MVT::f64, Expand);
594 setOperationAction(ISD::FSUB, MVT::f64, Expand);
595 setOperationAction(ISD::FMUL, MVT::f64, Expand);
596 setOperationAction(ISD::FMA, MVT::f64, Expand);
597 setOperationAction(ISD::FDIV, MVT::f64, Expand);
598 setOperationAction(ISD::FREM, MVT::f64, Expand);
599 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
600 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
601 setOperationAction(ISD::FNEG, MVT::f64, Expand);
602 setOperationAction(ISD::FABS, MVT::f64, Expand);
603 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
604 setOperationAction(ISD::FSIN, MVT::f64, Expand);
605 setOperationAction(ISD::FCOS, MVT::f64, Expand);
606 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
607 setOperationAction(ISD::FPOW, MVT::f64, Expand);
608 setOperationAction(ISD::FLOG, MVT::f64, Expand);
609 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
610 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
611 setOperationAction(ISD::FEXP, MVT::f64, Expand);
612 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
613 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
614 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
615 setOperationAction(ISD::FRINT, MVT::f64, Expand);
616 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
617 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
James Molloyfa041152015-03-23 16:15:16 +0000618 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
619 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
620 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
621 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
622 setOperationAction(ISD::FP_TO_SINT, MVT::f64, Custom);
623 setOperationAction(ISD::FP_TO_UINT, MVT::f64, Custom);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000624 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
625 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
626 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000627
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000628 computeRegisterProperties(Subtarget->getRegisterInfo());
Evan Cheng10043e22007-01-19 07:51:42 +0000629
Tim Northover4e80b582014-07-18 13:01:19 +0000630 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000631 for (MVT VT : MVT::fp_valuetypes()) {
632 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
633 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
634 }
Tim Northover4e80b582014-07-18 13:01:19 +0000635
636 // ... or truncating stores
637 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
638 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
639 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000640
Duncan Sands95d46ef2008-01-23 20:39:46 +0000641 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000642 for (MVT VT : MVT::integer_valuetypes())
643 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000644
Evan Cheng10043e22007-01-19 07:51:42 +0000645 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000646 if (!Subtarget->isThumb1Only()) {
647 for (unsigned im = (unsigned)ISD::PRE_INC;
648 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000649 setIndexedLoadAction(im, MVT::i1, Legal);
650 setIndexedLoadAction(im, MVT::i8, Legal);
651 setIndexedLoadAction(im, MVT::i16, Legal);
652 setIndexedLoadAction(im, MVT::i32, Legal);
653 setIndexedStoreAction(im, MVT::i1, Legal);
654 setIndexedStoreAction(im, MVT::i8, Legal);
655 setIndexedStoreAction(im, MVT::i16, Legal);
656 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000657 }
Evan Cheng10043e22007-01-19 07:51:42 +0000658 }
659
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000660 setOperationAction(ISD::SADDO, MVT::i32, Custom);
661 setOperationAction(ISD::UADDO, MVT::i32, Custom);
662 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
663 setOperationAction(ISD::USUBO, MVT::i32, Custom);
664
Evan Cheng10043e22007-01-19 07:51:42 +0000665 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000666 setOperationAction(ISD::MUL, MVT::i64, Expand);
667 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000668 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000669 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
670 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000671 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000672 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
673 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000674 setOperationAction(ISD::MULHS, MVT::i32, Expand);
675
Jim Grosbach5d994042009-10-31 19:38:01 +0000676 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000677 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000678 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000679 setOperationAction(ISD::SRL, MVT::i64, Custom);
680 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000681
Evan Chenge8916542011-08-30 01:34:54 +0000682 if (!Subtarget->isThumb1Only()) {
683 // FIXME: We should do this for Thumb1 as well.
684 setOperationAction(ISD::ADDC, MVT::i32, Custom);
685 setOperationAction(ISD::ADDE, MVT::i32, Custom);
686 setOperationAction(ISD::SUBC, MVT::i32, Custom);
687 setOperationAction(ISD::SUBE, MVT::i32, Custom);
688 }
689
Evan Cheng10043e22007-01-19 07:51:42 +0000690 // ARM does not have ROTL.
Owen Anderson9f944592009-08-11 20:47:22 +0000691 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach8546ec92010-01-18 19:58:49 +0000692 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000693 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000694 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000695 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000696
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000697 // These just redirect to CTTZ and CTLZ on ARM.
698 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
699 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
700
Tim Northoverbc933082013-05-23 19:11:20 +0000701 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
702
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000703 // Only ARMv6 has BSWAP.
704 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000705 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000706
Bob Wilsone8a549c2012-09-29 21:43:49 +0000707 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
708 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
709 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbach92d999002010-05-05 20:44:35 +0000710 setOperationAction(ISD::SDIV, MVT::i32, Expand);
711 setOperationAction(ISD::UDIV, MVT::i32, Expand);
712 }
Renato Golin87610692013-07-16 09:32:17 +0000713
714 // FIXME: Also set divmod for SREM on EABI
Chad Rosierad7c9102014-08-23 18:29:43 +0000715 setOperationAction(ISD::SREM, MVT::i32, Expand);
716 setOperationAction(ISD::UREM, MVT::i32, Expand);
717 // Register based DivRem for AEABI (RTABI 4.2)
718 if (Subtarget->isTargetAEABI()) {
719 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
720 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
721 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
722 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
723 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
724 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
725 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
726 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
727
728 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
729 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
730 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
731 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
732 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
733 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
734 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
735 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
736
737 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
738 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
739 } else {
Renato Golin87610692013-07-16 09:32:17 +0000740 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
741 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
742 }
Bob Wilson7117a912009-03-20 22:42:55 +0000743
Owen Anderson9f944592009-08-11 20:47:22 +0000744 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
745 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
746 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
747 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000748 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000749
Evan Cheng74d92c12011-04-08 21:37:21 +0000750 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000751
Evan Cheng10043e22007-01-19 07:51:42 +0000752 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000753 setOperationAction(ISD::VASTART, MVT::Other, Custom);
754 setOperationAction(ISD::VAARG, MVT::Other, Expand);
755 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
756 setOperationAction(ISD::VAEND, MVT::Other, Expand);
757 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
758 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000759
Tim Northoverd6a729b2014-01-06 14:28:05 +0000760 if (!Subtarget->isTargetMachO()) {
761 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000762 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000763 setExceptionPointerRegister(ARM::R0);
764 setExceptionSelectorRegister(ARM::R1);
765 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000766
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000767 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
768 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
769 else
770 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
771
Evan Cheng6e809de2010-08-11 06:22:01 +0000772 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000773 // the default expansion. If we are targeting a single threaded system,
774 // then set them all for expand so we can lower them later into their
775 // non-atomic form.
776 if (TM.Options.ThreadModel == ThreadModel::Single)
777 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
778 else if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000779 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
780 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000781 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000782
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000783 // On v8, we have particularly efficient implementations of atomic fences
784 // if they can be combined with nearby atomic loads and stores.
785 if (!Subtarget->hasV8Ops()) {
Robin Morissetd18cda62014-08-15 22:17:28 +0000786 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000787 setInsertFencesForAtomic(true);
788 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000789 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000790 // If there's anything we can use as a barrier, go through custom lowering
791 // for ATOMIC_FENCE.
792 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
793 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
794
Jim Grosbach6860bb72010-06-18 22:35:32 +0000795 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000796 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000797 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000798 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000799 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000800 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000801 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000802 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000803 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000804 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000805 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000806 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000807 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000808 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
809 // Unordered/Monotonic case.
810 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
811 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000812 }
Evan Cheng10043e22007-01-19 07:51:42 +0000813
Evan Cheng21acf9f2010-11-04 05:19:35 +0000814 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000815
Eli Friedman8cfa7712010-06-26 04:36:50 +0000816 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
817 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000818 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
819 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000820 }
Owen Anderson9f944592009-08-11 20:47:22 +0000821 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000822
Eric Christopher824f42f2015-05-12 01:26:05 +0000823 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000824 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000825 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000826 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000827 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000828 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
829 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000830
831 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000832 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbach31984832010-07-07 00:07:57 +0000833 if (Subtarget->isTargetDarwin()) {
834 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
835 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall7d84ece2011-05-29 19:50:32 +0000836 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbach31984832010-07-07 00:07:57 +0000837 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000838
Owen Anderson9f944592009-08-11 20:47:22 +0000839 setOperationAction(ISD::SETCC, MVT::i32, Expand);
840 setOperationAction(ISD::SETCC, MVT::f32, Expand);
841 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000842 setOperationAction(ISD::SELECT, MVT::i32, Custom);
843 setOperationAction(ISD::SELECT, MVT::f32, Custom);
844 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000845 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
846 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
847 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000848
Owen Anderson9f944592009-08-11 20:47:22 +0000849 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
850 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
851 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
852 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
853 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000854
Dan Gohman482732a2007-10-11 23:21:31 +0000855 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000856 setOperationAction(ISD::FSIN, MVT::f64, Expand);
857 setOperationAction(ISD::FSIN, MVT::f32, Expand);
858 setOperationAction(ISD::FCOS, MVT::f32, Expand);
859 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000860 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
861 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000862 setOperationAction(ISD::FREM, MVT::f64, Expand);
863 setOperationAction(ISD::FREM, MVT::f32, Expand);
Eric Christopher824f42f2015-05-12 01:26:05 +0000864 if (!Subtarget->useSoftFloat() && Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000865 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000866 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
867 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000868 }
Owen Anderson9f944592009-08-11 20:47:22 +0000869 setOperationAction(ISD::FPOW, MVT::f64, Expand);
870 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000871
Evan Chengd0007f32012-04-10 21:40:28 +0000872 if (!Subtarget->hasVFP4()) {
873 setOperationAction(ISD::FMA, MVT::f64, Expand);
874 setOperationAction(ISD::FMA, MVT::f32, Expand);
875 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000876
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000877 // Various VFP goodness
Eric Christopher824f42f2015-05-12 01:26:05 +0000878 if (!Subtarget->useSoftFloat() && !Subtarget->isThumb1Only()) {
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
Eric Christopher824f42f2015-05-12 01:26:05 +0000935 if (Subtarget->useSoftFloat() || Subtarget->isThumb1Only() ||
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000936 !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
Eric Christopher824f42f2015-05-12 01:26:05 +0000959bool ARMTargetLowering::useSoftFloat() const {
960 return Subtarget->useSoftFloat();
961}
962
Andrew Trick43f25632011-01-19 02:35:27 +0000963// FIXME: It might make sense to define the representative register class as the
964// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
965// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
966// SPR's representative would be DPR_VFP2. This should work well if register
967// pressure tracking were modified such that a register use would increment the
968// pressure of the register class's representative and all of it's super
969// classes' representatives transitively. We have not implemented this because
970// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000971// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +0000972// and extractions.
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000973std::pair<const TargetRegisterClass *, uint8_t>
974ARMTargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
975 MVT VT) const {
Craig Topper062a2ba2014-04-25 05:30:21 +0000976 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +0000977 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000978 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +0000979 default:
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000980 return TargetLowering::findRepresentativeClass(TRI, VT);
Evan Cheng28590382010-07-21 23:53:58 +0000981 // Use DPR as representative register class for all floating point
982 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
983 // the cost is 1 for both f32 and f64.
984 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +0000985 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +0000986 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +0000987 // When NEON is used for SP, only half of the register file is available
988 // because operations that define both SP and DP results will be constrained
989 // to the VFP2 class (D0-D15). We currently model this constraint prior to
990 // coalescing by double-counting the SP regs. See the FIXME above.
991 if (Subtarget->useNEONForSinglePrecisionFP())
992 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000993 break;
994 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
995 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +0000996 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000997 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000998 break;
999 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001000 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001001 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +00001002 break;
1003 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001004 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001005 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001006 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001007 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001008 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001009}
1010
Evan Cheng10043e22007-01-19 07:51:42 +00001011const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00001012 switch ((ARMISD::NodeType)Opcode) {
1013 case ARMISD::FIRST_NUMBER: break;
Evan Cheng10043e22007-01-19 07:51:42 +00001014 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001015 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001016 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
Matthias Braunf45afee2015-05-07 22:16:10 +00001017 case ARMISD::COPY_STRUCT_BYVAL: return "ARMISD::COPY_STRUCT_BYVAL";
Evan Cheng10043e22007-01-19 07:51:42 +00001018 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001019 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001020 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
1021 case ARMISD::tCALL: return "ARMISD::tCALL";
1022 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1023 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001024 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001025 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001026 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001027 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1028 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001029 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001030 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001031 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1032 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001033 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001034 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001035
Evan Cheng10043e22007-01-19 07:51:42 +00001036 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001037
Jim Grosbach8546ec92010-01-18 19:58:49 +00001038 case ARMISD::RBIT: return "ARMISD::RBIT";
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";
Matthias Braund04893f2015-05-07 21:33:59 +00001094 case ARMISD::VSLI: return "ARMISD::VSLI";
1095 case ARMISD::VSRI: return "ARMISD::VSRI";
Bob Wilson2e076c42009-06-22 23:27:02 +00001096 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1097 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001098 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001099 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001100 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001101 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001102 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001103 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001104 case ARMISD::VREV64: return "ARMISD::VREV64";
1105 case ARMISD::VREV32: return "ARMISD::VREV32";
1106 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001107 case ARMISD::VZIP: return "ARMISD::VZIP";
1108 case ARMISD::VUZP: return "ARMISD::VUZP";
1109 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001110 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1111 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001112 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1113 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001114 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1115 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001116 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilsonc6c13a32010-02-18 06:05:53 +00001117 case ARMISD::FMAX: return "ARMISD::FMAX";
1118 case ARMISD::FMIN: return "ARMISD::FMIN";
Joey Goulye3dd6842013-08-23 12:01:13 +00001119 case ARMISD::VMAXNM: return "ARMISD::VMAX";
1120 case ARMISD::VMINNM: return "ARMISD::VMIN";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001121 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001122 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1123 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001124 case ARMISD::VBSL: return "ARMISD::VBSL";
Peter Collingbourne450fbee2015-05-28 20:02:45 +00001125 case ARMISD::MCOPY: return "ARMISD::MCOPY";
Bob Wilson2d790df2010-11-28 06:51:26 +00001126 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1127 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1128 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001129 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1130 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1131 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1132 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1133 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1134 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1135 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1136 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1137 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1138 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1139 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1140 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1141 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1142 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1143 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1144 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1145 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001146 }
Matthias Braund04893f2015-05-07 21:33:59 +00001147 return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001148}
1149
Matt Arsenault758659232013-05-18 00:21:46 +00001150EVT ARMTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Duncan Sandsf2641e12011-09-06 19:07:46 +00001151 if (!VT.isVector()) return getPointerTy();
1152 return VT.changeVectorElementTypeToInteger();
1153}
1154
Evan Cheng4cad68e2010-05-15 02:18:07 +00001155/// getRegClassFor - Return the register class that should be used for the
1156/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001157const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001158 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1159 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1160 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001161 if (Subtarget->hasNEON()) {
1162 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001163 return &ARM::QQPRRegClass;
1164 if (VT == MVT::v8i64)
1165 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001166 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001167 return TargetLowering::getRegClassFor(VT);
1168}
1169
John Brawn0dbcd652015-03-18 12:01:59 +00001170// memcpy, and other memory intrinsics, typically tries to use LDM/STM if the
1171// source/dest is aligned and the copy size is large enough. We therefore want
1172// to align such objects passed to memory intrinsics.
1173bool ARMTargetLowering::shouldAlignPointerArgs(CallInst *CI, unsigned &MinSize,
1174 unsigned &PrefAlign) const {
1175 if (!isa<MemIntrinsic>(CI))
1176 return false;
1177 MinSize = 8;
1178 // On ARM11 onwards (excluding M class) 8-byte aligned LDM is typically 1
1179 // cycle faster than 4-byte aligned LDM.
1180 PrefAlign = (Subtarget->hasV6Ops() && !Subtarget->isMClass() ? 8 : 4);
1181 return true;
1182}
1183
Eric Christopher84bdfd82010-07-21 22:26:11 +00001184// Create a fast isel object.
1185FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001186ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1187 const TargetLibraryInfo *libInfo) const {
1188 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001189}
1190
Evan Cheng4401f882010-05-20 23:26:43 +00001191Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001192 unsigned NumVals = N->getNumValues();
1193 if (!NumVals)
1194 return Sched::RegPressure;
1195
1196 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001197 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001198 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001199 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001200 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001201 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001202 }
Evan Chengbf914992010-05-28 23:25:23 +00001203
1204 if (!N->isMachineOpcode())
1205 return Sched::RegPressure;
1206
1207 // Load are scheduled for latency even if there instruction itinerary
1208 // is not available.
Eric Christopher1889fdc2015-01-29 00:19:39 +00001209 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001210 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001211
Evan Cheng6cc775f2011-06-28 19:10:37 +00001212 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001213 return Sched::RegPressure;
1214 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001215 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001216 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001217
Evan Cheng4401f882010-05-20 23:26:43 +00001218 return Sched::RegPressure;
1219}
1220
Evan Cheng10043e22007-01-19 07:51:42 +00001221//===----------------------------------------------------------------------===//
1222// Lowering Code
1223//===----------------------------------------------------------------------===//
1224
Evan Cheng10043e22007-01-19 07:51:42 +00001225/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1226static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1227 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001228 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001229 case ISD::SETNE: return ARMCC::NE;
1230 case ISD::SETEQ: return ARMCC::EQ;
1231 case ISD::SETGT: return ARMCC::GT;
1232 case ISD::SETGE: return ARMCC::GE;
1233 case ISD::SETLT: return ARMCC::LT;
1234 case ISD::SETLE: return ARMCC::LE;
1235 case ISD::SETUGT: return ARMCC::HI;
1236 case ISD::SETUGE: return ARMCC::HS;
1237 case ISD::SETULT: return ARMCC::LO;
1238 case ISD::SETULE: return ARMCC::LS;
1239 }
1240}
1241
Bob Wilsona2e83332009-09-09 23:14:54 +00001242/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1243static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001244 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001245 CondCode2 = ARMCC::AL;
1246 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001247 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001248 case ISD::SETEQ:
1249 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1250 case ISD::SETGT:
1251 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1252 case ISD::SETGE:
1253 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1254 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001255 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001256 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1257 case ISD::SETO: CondCode = ARMCC::VC; break;
1258 case ISD::SETUO: CondCode = ARMCC::VS; break;
1259 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1260 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1261 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1262 case ISD::SETLT:
1263 case ISD::SETULT: CondCode = ARMCC::LT; break;
1264 case ISD::SETLE:
1265 case ISD::SETULE: CondCode = ARMCC::LE; break;
1266 case ISD::SETNE:
1267 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1268 }
Evan Cheng10043e22007-01-19 07:51:42 +00001269}
1270
Bob Wilsona4c22902009-04-17 19:07:39 +00001271//===----------------------------------------------------------------------===//
1272// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001273//===----------------------------------------------------------------------===//
1274
1275#include "ARMGenCallingConv.inc"
1276
Oliver Stannardc24f2172014-05-09 14:01:47 +00001277/// getEffectiveCallingConv - Get the effective calling convention, taking into
1278/// account presence of floating point hardware and calling convention
1279/// limitations, such as support for variadic functions.
1280CallingConv::ID
1281ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1282 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001283 switch (CC) {
1284 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001285 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001286 case CallingConv::ARM_AAPCS:
1287 case CallingConv::ARM_APCS:
1288 case CallingConv::GHC:
1289 return CC;
1290 case CallingConv::ARM_AAPCS_VFP:
1291 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1292 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001293 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001294 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001295 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001296 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1297 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001298 return CallingConv::ARM_AAPCS_VFP;
1299 else
1300 return CallingConv::ARM_AAPCS;
1301 case CallingConv::Fast:
1302 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001303 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001304 return CallingConv::Fast;
1305 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001306 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001307 return CallingConv::ARM_AAPCS_VFP;
1308 else
1309 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001310 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001311}
1312
1313/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1314/// CallingConvention.
1315CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1316 bool Return,
1317 bool isVarArg) const {
1318 switch (getEffectiveCallingConv(CC, isVarArg)) {
1319 default:
1320 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001321 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001322 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001323 case CallingConv::ARM_AAPCS:
1324 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1325 case CallingConv::ARM_AAPCS_VFP:
1326 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1327 case CallingConv::Fast:
1328 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001329 case CallingConv::GHC:
1330 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001331 }
1332}
1333
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001334/// LowerCallResult - Lower the result values of a call into the
1335/// appropriate copies out of appropriate physical registers.
1336SDValue
1337ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001338 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001339 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001340 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001341 SmallVectorImpl<SDValue> &InVals,
1342 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001343
Bob Wilsona4c22902009-04-17 19:07:39 +00001344 // Assign locations to each value returned by this call.
1345 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001346 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1347 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001348 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001349 CCAssignFnForNode(CallConv, /* Return*/ true,
1350 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001351
1352 // Copy all of the result registers out of their specified physreg.
1353 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1354 CCValAssign VA = RVLocs[i];
1355
Stephen Linb8bd2322013-04-20 05:14:40 +00001356 // Pass 'this' value directly from the argument to return value, to avoid
1357 // reg unit interference
1358 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001359 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1360 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001361 InVals.push_back(ThisVal);
1362 continue;
1363 }
1364
Bob Wilson0041bd32009-04-25 00:33:20 +00001365 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001366 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001367 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001368 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001369 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001370 Chain = Lo.getValue(1);
1371 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001372 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001373 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001374 InFlag);
1375 Chain = Hi.getValue(1);
1376 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001377 if (!Subtarget->isLittle())
1378 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001379 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001380
Owen Anderson9f944592009-08-11 20:47:22 +00001381 if (VA.getLocVT() == MVT::v2f64) {
1382 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1383 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001384 DAG.getConstant(0, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001385
1386 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001387 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001388 Chain = Lo.getValue(1);
1389 InFlag = Lo.getValue(2);
1390 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001391 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001392 Chain = Hi.getValue(1);
1393 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001394 if (!Subtarget->isLittle())
1395 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001396 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001397 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001398 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001399 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001400 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001401 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1402 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001403 Chain = Val.getValue(1);
1404 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001405 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001406
1407 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001408 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001409 case CCValAssign::Full: break;
1410 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001411 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001412 break;
1413 }
1414
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001415 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001416 }
1417
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001418 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001419}
1420
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001421/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001422SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001423ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1424 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001425 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001426 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001427 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001428 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001429 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00001430 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilsona4c22902009-04-17 19:07:39 +00001431 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner886250c2010-09-21 18:51:21 +00001432 MachinePointerInfo::getStack(LocMemOffset),
David Greene0d0149f2010-02-15 16:55:24 +00001433 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001434}
1435
Andrew Trickef9de2a2013-05-25 02:42:55 +00001436void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001437 SDValue Chain, SDValue &Arg,
1438 RegsToPassVector &RegsToPass,
1439 CCValAssign &VA, CCValAssign &NextVA,
1440 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001441 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001442 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001443
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001444 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001445 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001446 unsigned id = Subtarget->isLittle() ? 0 : 1;
1447 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001448
1449 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001450 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001451 else {
1452 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001453 if (!StackPtr.getNode())
Bob Wilson2e076c42009-06-22 23:27:02 +00001454 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1455
Christian Pirkerb5728192014-05-08 14:06:24 +00001456 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001457 dl, DAG, NextVA,
1458 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001459 }
1460}
1461
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001462/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001463/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1464/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001465SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001466ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001467 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001468 SelectionDAG &DAG = CLI.DAG;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001469 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001470 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1471 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1472 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001473 SDValue Chain = CLI.Chain;
1474 SDValue Callee = CLI.Callee;
1475 bool &isTailCall = CLI.IsTailCall;
1476 CallingConv::ID CallConv = CLI.CallConv;
1477 bool doesNotRet = CLI.DoesNotReturn;
1478 bool isVarArg = CLI.IsVarArg;
1479
Dale Johannesend679ff72010-06-03 21:09:53 +00001480 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001481 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1482 bool isThisReturn = false;
1483 bool isSibCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001484
Bob Wilson8decdc42011-10-07 17:17:49 +00001485 // Disable tail calls if they're not supported.
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001486 if (!Subtarget->supportsTailCall() || MF.getTarget().Options.DisableTailCalls)
Bob Wilson3c9ed762010-08-13 22:43:33 +00001487 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001488
Dale Johannesend679ff72010-06-03 21:09:53 +00001489 if (isTailCall) {
1490 // Check if it's really possible to do a tail call.
1491 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001492 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001493 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001494 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1495 report_fatal_error("failed to perform tail call elimination on a call "
1496 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001497 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1498 // detected sibcalls.
1499 if (isTailCall) {
1500 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001501 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001502 }
1503 }
Evan Cheng10043e22007-01-19 07:51:42 +00001504
Bob Wilsona4c22902009-04-17 19:07:39 +00001505 // Analyze operands of the call, assigning locations to each operand.
1506 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001507 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1508 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001509 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001510 CCAssignFnForNode(CallConv, /* Return*/ false,
1511 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001512
Bob Wilsona4c22902009-04-17 19:07:39 +00001513 // Get a count of how many bytes are to be pushed on the stack.
1514 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001515
Dale Johannesend679ff72010-06-03 21:09:53 +00001516 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001517 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001518 NumBytes = 0;
1519
Evan Cheng10043e22007-01-19 07:51:42 +00001520 // Adjust the stack pointer for the new arguments...
1521 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001522 if (!isSibCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001523 Chain = DAG.getCALLSEQ_START(Chain,
1524 DAG.getIntPtrConstant(NumBytes, dl, true), dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001525
Jim Grosbach6ad4bcb2010-02-24 01:43:03 +00001526 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +00001527
Bob Wilson2e076c42009-06-22 23:27:02 +00001528 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001529 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001530
Bob Wilsona4c22902009-04-17 19:07:39 +00001531 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001532 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001533 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1534 i != e;
1535 ++i, ++realArgIdx) {
1536 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001537 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001538 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001539 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001540
Bob Wilsona4c22902009-04-17 19:07:39 +00001541 // Promote the value if needed.
1542 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001543 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001544 case CCValAssign::Full: break;
1545 case CCValAssign::SExt:
1546 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1547 break;
1548 case CCValAssign::ZExt:
1549 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1550 break;
1551 case CCValAssign::AExt:
1552 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1553 break;
1554 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001555 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001556 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001557 }
1558
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001559 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001560 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001561 if (VA.getLocVT() == MVT::v2f64) {
1562 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001563 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00001564 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001565 DAG.getConstant(1, dl, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001566
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001567 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001568 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1569
1570 VA = ArgLocs[++i]; // skip ahead to next loc
1571 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001572 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001573 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1574 } else {
1575 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001576
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001577 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1578 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001579 }
1580 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001581 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001582 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001583 }
1584 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001585 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1586 assert(VA.getLocVT() == MVT::i32 &&
1587 "unexpected calling convention register assignment");
1588 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001589 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001590 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001591 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001592 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001593 } else if (isByVal) {
1594 assert(VA.isMemLoc());
1595 unsigned offset = 0;
1596
1597 // True if this byval aggregate will be split between registers
1598 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001599 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001600 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001601
1602 if (CurByValIdx < ByValArgsCount) {
1603
1604 unsigned RegBegin, RegEnd;
1605 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1606
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001607 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1608 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001609 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001610 SDValue Const = DAG.getConstant(4*i, dl, MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001611 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1612 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1613 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001614 false, false, false,
1615 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001616 MemOpChains.push_back(Load.getValue(1));
1617 RegsToPass.push_back(std::make_pair(j, Load));
1618 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001619
1620 // If parameter size outsides register area, "offset" value
1621 // helps us to calculate stack slot for remained part properly.
1622 offset = RegEnd - RegBegin;
1623
1624 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001625 }
1626
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001627 if (Flags.getByValSize() > 4*offset) {
Manman Ren9f911162012-06-01 02:44:42 +00001628 unsigned LocMemOffset = VA.getLocMemOffset();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001629 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
Manman Ren9f911162012-06-01 02:44:42 +00001630 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1631 StkPtrOff);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001632 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset, dl);
Manman Ren9f911162012-06-01 02:44:42 +00001633 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001634 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset, dl,
Manman Ren9f911162012-06-01 02:44:42 +00001635 MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001636 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), dl,
1637 MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001638
Manman Ren9f911162012-06-01 02:44:42 +00001639 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001640 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001641 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001642 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001643 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001644 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001645 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001646
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001647 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1648 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001649 }
Evan Cheng10043e22007-01-19 07:51:42 +00001650 }
1651
1652 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001653 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001654
1655 // Build a sequence of copy-to-reg nodes chained together with token chain
1656 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001657 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001658 // Tail call byval lowering might overwrite argument registers so in case of
1659 // tail call optimization the copies to registers are lowered later.
1660 if (!isTailCall)
1661 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1662 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1663 RegsToPass[i].second, InFlag);
1664 InFlag = Chain.getValue(1);
1665 }
Evan Cheng10043e22007-01-19 07:51:42 +00001666
Dale Johannesend679ff72010-06-03 21:09:53 +00001667 // For tail calls lower the arguments to the 'real' stack slot.
1668 if (isTailCall) {
1669 // Force all the incoming stack arguments to be loaded from the stack
1670 // before any new outgoing arguments are stored to the stack, because the
1671 // outgoing stack slots may alias the incoming argument stack slots, and
1672 // the alias isn't otherwise explicit. This is slightly more conservative
1673 // than necessary, because it means that each store effectively depends
1674 // on every argument instead of just those arguments it would clobber.
1675
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001676 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001677 InFlag = SDValue();
1678 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1679 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1680 RegsToPass[i].second, InFlag);
1681 InFlag = Chain.getValue(1);
1682 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001683 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001684 }
1685
Bill Wendling24c79f22008-09-16 21:48:12 +00001686 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1687 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1688 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001689 bool isDirect = false;
1690 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001691 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001692 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001693
1694 if (EnableARMLongCalls) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001695 assert((Subtarget->isTargetWindows() ||
1696 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1697 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001698 // Handle a global address or an external symbol. If it's not one of
1699 // those, the target's already in a register, so we don't need to do
1700 // anything extra.
1701 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001702 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001703 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001704 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001705 ARMConstantPoolValue *CPV =
1706 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1707
Jim Grosbach32bb3622010-04-14 22:28:31 +00001708 // Get the address of the callee into a register
1709 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1710 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1711 Callee = DAG.getLoad(getPointerTy(), dl,
1712 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001713 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001714 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001715 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1716 const char *Sym = S->getSymbol();
1717
1718 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001719 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001720 ARMConstantPoolValue *CPV =
1721 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1722 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001723 // Get the address of the callee into a register
1724 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1725 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1726 Callee = DAG.getLoad(getPointerTy(), dl,
1727 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001728 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001729 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001730 }
1731 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001732 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001733 isDirect = true;
Chris Lattner55452c22009-07-15 04:12:33 +00001734 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Tim Northoverd6a729b2014-01-06 14:28:05 +00001735 bool isStub = (isExt && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001736 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001737 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00001738 // ARM call to a local ARM function is predicable.
Evan Chengf128bdc2010-06-16 07:35:02 +00001739 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001740 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001741 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001742 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Tim Northover72360d22013-12-02 10:35:41 +00001743 Callee = DAG.getNode(ARMISD::WrapperPIC, dl, getPointerTy(),
Tim Northoverd4d294d2014-08-06 11:13:06 +00001744 DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
1745 0, ARMII::MO_NONLAZY));
1746 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
1747 MachinePointerInfo::getGOT(), false, false, true, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001748 } else if (Subtarget->isTargetCOFF()) {
1749 assert(Subtarget->isTargetWindows() &&
1750 "Windows is the only supported COFF target");
1751 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1752 ? ARMII::MO_DLLIMPORT
1753 : ARMII::MO_NO_FLAG;
1754 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), /*Offset=*/0,
1755 TargetFlags);
1756 if (GV->hasDLLImportStorageClass())
1757 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
1758 DAG.getNode(ARMISD::Wrapper, dl, getPointerTy(),
1759 Callee), MachinePointerInfo::getGOT(),
1760 false, false, false, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001761 } else {
1762 // On ELF targets for PIC code, direct calls should go through the PLT
1763 unsigned OpFlags = 0;
1764 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001765 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001766 OpFlags = ARMII::MO_PLT;
1767 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1768 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001769 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001770 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001771 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001772 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001773 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Cheng83f35172007-01-30 20:37:08 +00001774 // tBX takes a register source operand.
1775 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001776 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001777 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001778 ARMConstantPoolValue *CPV =
1779 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1780 ARMPCLabelIndex, 4);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001781 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001782 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen021052a2009-02-04 20:06:27 +00001783 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Chengcdbb70c2009-10-31 03:39:36 +00001784 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001785 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001786 false, false, false, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001787 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Bob Wilson7117a912009-03-20 22:42:55 +00001788 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen021052a2009-02-04 20:06:27 +00001789 getPointerTy(), Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001790 } else {
1791 unsigned OpFlags = 0;
1792 // On ELF targets for PIC code, direct calls should go through the PLT
1793 if (Subtarget->isTargetELF() &&
1794 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1795 OpFlags = ARMII::MO_PLT;
1796 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1797 }
Evan Cheng10043e22007-01-19 07:51:42 +00001798 }
1799
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001800 // FIXME: handle tail calls differently.
1801 unsigned CallOpc;
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00001802 bool HasMinSizeAttr = MF.getFunction()->hasFnAttribute(Attribute::MinSize);
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001803 if (Subtarget->isThumb()) {
1804 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001805 CallOpc = ARMISD::CALL_NOLINK;
1806 else
1807 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1808 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001809 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001810 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001811 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet8e1fe842012-11-02 21:32:17 +00001812 // Emit regular call when code size is the priority
1813 !HasMinSizeAttr)
Evan Cheng65f9d192012-02-28 18:51:51 +00001814 // "mov lr, pc; b _foo" to avoid confusing the RSP
1815 CallOpc = ARMISD::CALL_NOLINK;
1816 else
1817 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001818 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001819
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001820 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001821 Ops.push_back(Chain);
1822 Ops.push_back(Callee);
1823
1824 // Add argument registers to the end of the list so that they are known live
1825 // into the call.
1826 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1827 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1828 RegsToPass[i].second.getValueType()));
1829
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001830 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001831 if (!isTailCall) {
1832 const uint32_t *Mask;
Eric Christopher1889fdc2015-01-29 00:19:39 +00001833 const ARMBaseRegisterInfo *ARI = Subtarget->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001834 if (isThisReturn) {
1835 // For 'this' returns, use the R0-preserving mask if applicable
Eric Christopher9deb75d2015-03-11 22:42:13 +00001836 Mask = ARI->getThisReturnPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001837 if (!Mask) {
1838 // Set isThisReturn to false if the calling convention is not one that
1839 // allows 'returned' to be modeled in this way, so LowerCallResult does
1840 // not try to pass 'this' straight through
1841 isThisReturn = false;
Eric Christopher9deb75d2015-03-11 22:42:13 +00001842 Mask = ARI->getCallPreservedMask(MF, CallConv);
Matthias Braunc22630e2013-10-04 16:52:54 +00001843 }
1844 } else
Eric Christopher9deb75d2015-03-11 22:42:13 +00001845 Mask = ARI->getCallPreservedMask(MF, CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001846
Matthias Braunc22630e2013-10-04 16:52:54 +00001847 assert(Mask && "Missing call preserved mask for calling convention");
1848 Ops.push_back(DAG.getRegisterMask(Mask));
1849 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001850
Gabor Greiff304a7a2008-08-28 21:40:38 +00001851 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001852 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001853
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001854 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001855 if (isTailCall) {
1856 MF.getFrameInfo()->setHasTailCall();
Craig Topper48d114b2014-04-26 18:35:24 +00001857 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Arnold Schwaighoferf54b73d2015-05-08 23:52:00 +00001858 }
Dale Johannesend679ff72010-06-03 21:09:53 +00001859
Duncan Sands739a0542008-07-02 17:40:58 +00001860 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001861 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001862 InFlag = Chain.getValue(1);
1863
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001864 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, dl, true),
1865 DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001866 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001867 InFlag = Chain.getValue(1);
1868
Bob Wilsona4c22902009-04-17 19:07:39 +00001869 // Handle result values, copying them out of physregs into vregs that we
1870 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001871 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001872 InVals, isThisReturn,
1873 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001874}
1875
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001876/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001877/// on the stack. Remember the next parameter register to allocate,
1878/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001879/// this.
Tim Northover8cda34f2015-03-11 18:54:22 +00001880void ARMTargetLowering::HandleByVal(CCState *State, unsigned &Size,
1881 unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001882 assert((State->getCallOrPrologue() == Prologue ||
1883 State->getCallOrPrologue() == Call) &&
1884 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001885
Tim Northover8cda34f2015-03-11 18:54:22 +00001886 // Byval (as with any stack) slots are always at least 4 byte aligned.
1887 Align = std::max(Align, 4U);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001888
Tim Northover8cda34f2015-03-11 18:54:22 +00001889 unsigned Reg = State->AllocateReg(GPRArgRegs);
1890 if (!Reg)
1891 return;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001892
Tim Northover8cda34f2015-03-11 18:54:22 +00001893 unsigned AlignInRegs = Align / 4;
1894 unsigned Waste = (ARM::R4 - Reg) % AlignInRegs;
1895 for (unsigned i = 0; i < Waste; ++i)
1896 Reg = State->AllocateReg(GPRArgRegs);
1897
1898 if (!Reg)
1899 return;
1900
1901 unsigned Excess = 4 * (ARM::R4 - Reg);
1902
1903 // Special case when NSAA != SP and parameter size greater than size of
1904 // all remained GPR regs. In that case we can't split parameter, we must
1905 // send it to stack. We also must set NCRN to R4, so waste all
1906 // remained registers.
1907 const unsigned NSAAOffset = State->getNextStackOffset();
1908 if (NSAAOffset != 0 && Size > Excess) {
1909 while (State->AllocateReg(GPRArgRegs))
1910 ;
1911 return;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001912 }
Tim Northover8cda34f2015-03-11 18:54:22 +00001913
1914 // First register for byval parameter is the first register that wasn't
1915 // allocated before this method call, so it would be "reg".
1916 // If parameter is small enough to be saved in range [reg, r4), then
1917 // the end (first after last) register would be reg + param-size-in-regs,
1918 // else parameter would be splitted between registers and stack,
1919 // end register would be r4 in this case.
1920 unsigned ByValRegBegin = Reg;
1921 unsigned ByValRegEnd = std::min<unsigned>(Reg + Size / 4, ARM::R4);
1922 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1923 // Note, first register is allocated in the beginning of function already,
1924 // allocate remained amount of registers we need.
1925 for (unsigned i = Reg + 1; i != ByValRegEnd; ++i)
1926 State->AllocateReg(GPRArgRegs);
1927 // A byval parameter that is split between registers and memory needs its
1928 // size truncated here.
1929 // In the case where the entire structure fits in registers, we set the
1930 // size in memory to zero.
1931 Size = std::max<int>(Size - Excess, 0);
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001932}
1933
Dale Johannesend679ff72010-06-03 21:09:53 +00001934/// MatchingStackOffset - Return true if the given stack call argument is
1935/// already available in the same position (relatively) of the caller's
1936/// incoming argument stack.
1937static
1938bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1939 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00001940 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001941 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1942 int FI = INT_MAX;
1943 if (Arg.getOpcode() == ISD::CopyFromReg) {
1944 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001945 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00001946 return false;
1947 MachineInstr *Def = MRI->getVRegDef(VR);
1948 if (!Def)
1949 return false;
1950 if (!Flags.isByVal()) {
1951 if (!TII->isLoadFromStackSlot(Def, FI))
1952 return false;
1953 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00001954 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00001955 }
1956 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1957 if (Flags.isByVal())
1958 // ByVal argument is passed in as a pointer but it's now being
1959 // dereferenced. e.g.
1960 // define @foo(%struct.X* %A) {
1961 // tail call @bar(%struct.X* byval %A)
1962 // }
1963 return false;
1964 SDValue Ptr = Ld->getBasePtr();
1965 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1966 if (!FINode)
1967 return false;
1968 FI = FINode->getIndex();
1969 } else
1970 return false;
1971
1972 assert(FI != INT_MAX);
1973 if (!MFI->isFixedObjectIndex(FI))
1974 return false;
1975 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1976}
1977
1978/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1979/// for tail call optimization. Targets which want to do tail call
1980/// optimization should implement this function.
1981bool
1982ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1983 CallingConv::ID CalleeCC,
1984 bool isVarArg,
1985 bool isCalleeStructRet,
1986 bool isCallerStructRet,
1987 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001988 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00001989 const SmallVectorImpl<ISD::InputArg> &Ins,
1990 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00001991 const Function *CallerF = DAG.getMachineFunction().getFunction();
1992 CallingConv::ID CallerCC = CallerF->getCallingConv();
1993 bool CCMatch = CallerCC == CalleeCC;
1994
1995 // Look for obvious safe cases to perform tail call optimization that do not
1996 // require ABI changes. This is what gcc calls sibcall.
1997
Jim Grosbache3864cc2010-06-16 23:45:49 +00001998 // Do not sibcall optimize vararg calls unless the call site is not passing
1999 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00002000 if (isVarArg && !Outs.empty())
2001 return false;
2002
Tim Northoverd8407452013-10-01 14:33:28 +00002003 // Exception-handling functions need a special set of instructions to indicate
2004 // a return to the hardware. Tail-calling another function would probably
2005 // break this.
2006 if (CallerF->hasFnAttribute("interrupt"))
2007 return false;
2008
Dale Johannesend679ff72010-06-03 21:09:53 +00002009 // Also avoid sibcall optimization if either caller or callee uses struct
2010 // return semantics.
2011 if (isCalleeStructRet || isCallerStructRet)
2012 return false;
2013
Eric Christopherae326492015-03-12 22:48:50 +00002014 // FIXME: Completely disable sibcall for Thumb1 since ThumbRegisterInfo::
Jim Grosbach3840c902011-07-08 20:18:11 +00002015 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
2016 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
2017 // support in the assembler and linker to be used. This would need to be
2018 // fixed to fully support tail calls in Thumb1.
2019 //
Dale Johannesene2289282010-07-08 01:18:23 +00002020 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
2021 // LR. This means if we need to reload LR, it takes an extra instructions,
2022 // which outweighs the value of the tail call; but here we don't know yet
2023 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach535d3b42010-09-08 03:54:02 +00002024 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesene2289282010-07-08 01:18:23 +00002025 // emitEpilogue if LR is used.
Dale Johannesene2289282010-07-08 01:18:23 +00002026
2027 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
2028 // but we need to make sure there are enough registers; the only valid
2029 // registers are the 4 used for parameters. We don't currently do this
2030 // case.
Evan Chengd4b08732010-11-30 23:55:39 +00002031 if (Subtarget->isThumb1Only())
2032 return false;
Dale Johannesen3ac52b32010-06-18 18:13:11 +00002033
Oliver Stannard12993dd2014-08-18 12:42:15 +00002034 // Externally-defined functions with weak linkage should not be
2035 // tail-called on ARM when the OS does not support dynamic
2036 // pre-emption of symbols, as the AAELF spec requires normal calls
2037 // to undefined weak functions to be replaced with a NOP or jump to the
2038 // next instruction. The behaviour of branch instructions in this
2039 // situation (as used for tail calls) is implementation-defined, so we
2040 // cannot rely on the linker replacing the tail call with a return.
2041 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2042 const GlobalValue *GV = G->getGlobal();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002043 const Triple TT(getTargetMachine().getTargetTriple());
2044 if (GV->hasExternalWeakLinkage() &&
2045 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002046 return false;
2047 }
2048
Dale Johannesend679ff72010-06-03 21:09:53 +00002049 // If the calling conventions do not match, then we'd better make sure the
2050 // results are returned in the same way as what the caller expects.
2051 if (!CCMatch) {
2052 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopherb5217502014-08-06 18:45:26 +00002053 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
2054 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002055 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
2056
2057 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopherb5217502014-08-06 18:45:26 +00002058 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
2059 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002060 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
2061
2062 if (RVLocs1.size() != RVLocs2.size())
2063 return false;
2064 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2065 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2066 return false;
2067 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2068 return false;
2069 if (RVLocs1[i].isRegLoc()) {
2070 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2071 return false;
2072 } else {
2073 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2074 return false;
2075 }
2076 }
2077 }
2078
Manman Ren7e48b252012-10-12 23:39:43 +00002079 // If Caller's vararg or byval argument has been split between registers and
2080 // stack, do not perform tail call, since part of the argument is in caller's
2081 // local frame.
2082 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
2083 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002084 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002085 return false;
2086
Dale Johannesend679ff72010-06-03 21:09:53 +00002087 // If the callee takes no arguments then go on to check the results of the
2088 // call.
2089 if (!Outs.empty()) {
2090 // Check if stack adjustment is needed. For now, do not do this if any
2091 // argument is passed on the stack.
2092 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002093 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2094 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002095 CCInfo.AnalyzeCallOperands(Outs,
2096 CCAssignFnForNode(CalleeCC, false, isVarArg));
2097 if (CCInfo.getNextStackOffset()) {
2098 MachineFunction &MF = DAG.getMachineFunction();
2099
2100 // Check if the arguments are already laid out in the right way as
2101 // the caller's fixed stack objects.
2102 MachineFrameInfo *MFI = MF.getFrameInfo();
2103 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopher1889fdc2015-01-29 00:19:39 +00002104 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002105 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2106 i != e;
2107 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002108 CCValAssign &VA = ArgLocs[i];
2109 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002110 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002111 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002112 if (VA.getLocInfo() == CCValAssign::Indirect)
2113 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002114 if (VA.needsCustom()) {
2115 // f64 and vector types are split into multiple registers or
2116 // register/stack-slot combinations. The types will not match
2117 // the registers; give up on memory f64 refs until we figure
2118 // out what to do about this.
2119 if (!VA.isRegLoc())
2120 return false;
2121 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002122 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002123 if (RegVT == MVT::v2f64) {
2124 if (!ArgLocs[++i].isRegLoc())
2125 return false;
2126 if (!ArgLocs[++i].isRegLoc())
2127 return false;
2128 }
2129 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002130 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2131 MFI, MRI, TII))
2132 return false;
2133 }
2134 }
2135 }
2136 }
2137
2138 return true;
2139}
2140
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002141bool
2142ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2143 MachineFunction &MF, bool isVarArg,
2144 const SmallVectorImpl<ISD::OutputArg> &Outs,
2145 LLVMContext &Context) const {
2146 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002147 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002148 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2149 isVarArg));
2150}
2151
Tim Northoverd8407452013-10-01 14:33:28 +00002152static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2153 SDLoc DL, SelectionDAG &DAG) {
2154 const MachineFunction &MF = DAG.getMachineFunction();
2155 const Function *F = MF.getFunction();
2156
2157 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2158
2159 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2160 // version of the "preferred return address". These offsets affect the return
2161 // instruction if this is a return from PL1 without hypervisor extensions.
2162 // IRQ/FIQ: +4 "subs pc, lr, #4"
2163 // SWI: 0 "subs pc, lr, #0"
2164 // ABORT: +4 "subs pc, lr, #4"
2165 // UNDEF: +4/+2 "subs pc, lr, #0"
2166 // UNDEF varies depending on where the exception came from ARM or Thumb
2167 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2168
2169 int64_t LROffset;
2170 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2171 IntKind == "ABORT")
2172 LROffset = 4;
2173 else if (IntKind == "SWI" || IntKind == "UNDEF")
2174 LROffset = 0;
2175 else
2176 report_fatal_error("Unsupported interrupt attribute. If present, value "
2177 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2178
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002179 RetOps.insert(RetOps.begin() + 1,
2180 DAG.getConstant(LROffset, DL, MVT::i32, false));
Tim Northoverd8407452013-10-01 14:33:28 +00002181
Craig Topper48d114b2014-04-26 18:35:24 +00002182 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002183}
2184
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002185SDValue
2186ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002187 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002188 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002189 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002190 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002191
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002192 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002193 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002194
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002195 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002196 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2197 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002198
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002199 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002200 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2201 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002202
Bob Wilsona4c22902009-04-17 19:07:39 +00002203 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002204 SmallVector<SDValue, 4> RetOps;
2205 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002206 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002207
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002208 MachineFunction &MF = DAG.getMachineFunction();
2209 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2210 AFI->setReturnRegsCount(RVLocs.size());
2211
Bob Wilsona4c22902009-04-17 19:07:39 +00002212 // Copy the result values into the output registers.
2213 for (unsigned i = 0, realRVLocIdx = 0;
2214 i != RVLocs.size();
2215 ++i, ++realRVLocIdx) {
2216 CCValAssign &VA = RVLocs[i];
2217 assert(VA.isRegLoc() && "Can only return in registers!");
2218
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002219 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002220
2221 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002222 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002223 case CCValAssign::Full: break;
2224 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002225 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002226 break;
2227 }
2228
Bob Wilsona4c22902009-04-17 19:07:39 +00002229 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002230 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002231 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002232 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002233 DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002234 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002235 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002236
Christian Pirkerb5728192014-05-08 14:06:24 +00002237 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2238 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2239 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002240 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002241 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002242 VA = RVLocs[++i]; // skip ahead to next loc
2243 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002244 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2245 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002246 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002247 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002248 VA = RVLocs[++i]; // skip ahead to next loc
2249
2250 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002251 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002252 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002253 }
2254 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2255 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002256 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002257 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002258 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2259 fmrrd.getValue(isLittleEndian ? 0 : 1),
2260 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002261 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002262 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002263 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002264 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2265 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002266 Flag);
2267 } else
2268 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2269
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002270 // Guarantee that all emitted copies are
2271 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002272 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002273 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002274 }
2275
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002276 // Update chain and glue.
2277 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002278 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002279 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002280
Tim Northoverd8407452013-10-01 14:33:28 +00002281 // CPUs which aren't M-class use a special sequence to return from
2282 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2283 // though we use "subs pc, lr, #N").
2284 //
2285 // M-class CPUs actually use a normal return sequence with a special
2286 // (hardware-provided) value in LR, so the normal code path works.
2287 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2288 !Subtarget->isMClass()) {
2289 if (Subtarget->isThumb1Only())
2290 report_fatal_error("interrupt attribute is not supported in Thumb1");
2291 return LowerInterruptReturn(RetOps, dl, DAG);
2292 }
2293
Craig Topper48d114b2014-04-26 18:35:24 +00002294 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002295}
2296
Evan Chengf8bad082012-04-10 01:51:00 +00002297bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002298 if (N->getNumValues() != 1)
2299 return false;
2300 if (!N->hasNUsesOfValue(1, 0))
2301 return false;
2302
Evan Chengf8bad082012-04-10 01:51:00 +00002303 SDValue TCChain = Chain;
2304 SDNode *Copy = *N->use_begin();
2305 if (Copy->getOpcode() == ISD::CopyToReg) {
2306 // If the copy has a glue operand, we conservatively assume it isn't safe to
2307 // perform a tail call.
2308 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2309 return false;
2310 TCChain = Copy->getOperand(0);
2311 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2312 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002313 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002314 SmallPtrSet<SDNode*, 2> Copies;
2315 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002316 UI != UE; ++UI) {
2317 if (UI->getOpcode() != ISD::CopyToReg)
2318 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002319 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002320 }
Evan Chengf8bad082012-04-10 01:51:00 +00002321 if (Copies.size() > 2)
2322 return false;
2323
2324 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2325 UI != UE; ++UI) {
2326 SDValue UseChain = UI->getOperand(0);
2327 if (Copies.count(UseChain.getNode()))
2328 // Second CopyToReg
2329 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002330 else {
2331 // We are at the top of this chain.
2332 // If the copy has a glue operand, we conservatively assume it
2333 // isn't safe to perform a tail call.
2334 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2335 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002336 // First CopyToReg
2337 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002338 }
Evan Chengf8bad082012-04-10 01:51:00 +00002339 }
2340 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002341 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002342 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002343 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002344 Copy = *Copy->use_begin();
2345 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002346 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002347 // If the copy has a glue operand, we conservatively assume it isn't safe to
2348 // perform a tail call.
2349 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2350 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002351 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002352 } else {
2353 return false;
2354 }
2355
Evan Cheng419ea282010-12-01 22:59:46 +00002356 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002357 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2358 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002359 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2360 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002361 return false;
2362 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002363 }
2364
Evan Chengf8bad082012-04-10 01:51:00 +00002365 if (!HasRet)
2366 return false;
2367
2368 Chain = TCChain;
2369 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002370}
2371
Evan Cheng0663f232011-03-21 01:19:09 +00002372bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002373 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002374 return false;
2375
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00002376 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng0663f232011-03-21 01:19:09 +00002377 return false;
2378
2379 return !Subtarget->isThumb1Only();
2380}
2381
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002382// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2383// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2384// one of the above mentioned nodes. It has to be wrapped because otherwise
2385// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2386// be used to form addressing mode. These wrapped nodes will be selected
2387// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002388static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002389 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002390 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002391 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002392 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002393 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002394 if (CP->isMachineConstantPoolEntry())
2395 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2396 CP->getAlignment());
2397 else
2398 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2399 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002400 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002401}
2402
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002403unsigned ARMTargetLowering::getJumpTableEncoding() const {
2404 return MachineJumpTableInfo::EK_Inline;
2405}
2406
Dan Gohman21cea8a2010-04-17 15:26:15 +00002407SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2408 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002409 MachineFunction &MF = DAG.getMachineFunction();
2410 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2411 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002412 SDLoc DL(Op);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002413 EVT PtrVT = getPointerTy();
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002414 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002415 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2416 SDValue CPAddr;
2417 if (RelocM == Reloc::Static) {
2418 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2419 } else {
2420 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002421 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002422 ARMConstantPoolValue *CPV =
2423 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2424 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002425 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2426 }
2427 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2428 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002429 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002430 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002431 if (RelocM == Reloc::Static)
2432 return Result;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002433 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, DL, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002434 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002435}
2436
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002437// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002438SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002439ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002440 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002441 SDLoc dl(GA);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002442 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002443 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002444 MachineFunction &MF = DAG.getMachineFunction();
2445 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002446 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002447 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002448 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2449 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002450 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002451 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002452 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattner7727d052010-09-21 06:44:06 +00002453 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002454 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002455 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002456
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002457 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002458 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002459
2460 // call __tls_get_addr.
2461 ArgListTy Args;
2462 ArgListEntry Entry;
2463 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002464 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002465 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002466
Dale Johannesen555a3752009-01-30 23:10:59 +00002467 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002468 TargetLowering::CallLoweringInfo CLI(DAG);
2469 CLI.setDebugLoc(dl).setChain(Chain)
2470 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002471 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args),
2472 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002473
Justin Holewinskiaa583972012-05-25 16:35:28 +00002474 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002475 return CallResult.first;
2476}
2477
2478// Lower ISD::GlobalTLSAddress using the "initial exec" or
2479// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002480SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002481ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002482 SelectionDAG &DAG,
2483 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002484 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002485 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002486 SDValue Offset;
2487 SDValue Chain = DAG.getEntryNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002488 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002489 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002490 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002491
Hans Wennborgaea41202012-05-04 09:40:39 +00002492 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002493 MachineFunction &MF = DAG.getMachineFunction();
2494 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002495 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002496 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002497 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2498 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002499 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2500 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2501 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002502 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002503 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002504 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002505 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002506 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002507 Chain = Offset.getValue(1);
2508
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002509 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002510 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002511
Evan Chengcdbb70c2009-10-31 03:39:36 +00002512 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002513 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002514 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002515 } else {
2516 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002517 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002518 ARMConstantPoolValue *CPV =
2519 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002520 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002521 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002522 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002523 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002524 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002525 }
2526
2527 // The address of the thread local variable is the add of the thread
2528 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002529 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002530}
2531
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002532SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002533ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002534 // TODO: implement the "local dynamic" model
2535 assert(Subtarget->isTargetELF() &&
2536 "TLS not implemented for non-ELF targets");
2537 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgaea41202012-05-04 09:40:39 +00002538
2539 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2540
2541 switch (model) {
2542 case TLSModel::GeneralDynamic:
2543 case TLSModel::LocalDynamic:
2544 return LowerToTLSGeneralDynamicModel(GA, DAG);
2545 case TLSModel::InitialExec:
2546 case TLSModel::LocalExec:
2547 return LowerToTLSExecModels(GA, DAG, model);
2548 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002549 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002550}
2551
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002552SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002553 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002554 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002555 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002556 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002557 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002558 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002559 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002560 ARMConstantPoolConstant::Create(GV,
2561 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002562 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002563 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson7117a912009-03-20 22:42:55 +00002564 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002565 CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002566 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002567 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002568 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002569 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002570 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002571 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002572 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002573 MachinePointerInfo::getGOT(),
2574 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002575 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002576 }
2577
2578 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002579 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002580 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002581 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002582 // FIXME: Once remat is capable of dealing with instructions with register
2583 // operands, expand this into two nodes.
2584 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2585 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002586 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002587 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2588 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2589 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2590 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002591 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002592 }
2593}
2594
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002595SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002596 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002597 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002598 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002599 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002600 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002601
Eric Christopherc1058df2014-07-04 01:55:26 +00002602 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002603 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002604
Tim Northover72360d22013-12-02 10:35:41 +00002605 // FIXME: Once remat is capable of dealing with instructions with register
2606 // operands, expand this into multiple nodes
2607 unsigned Wrapper =
2608 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002609
Tim Northover72360d22013-12-02 10:35:41 +00002610 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2611 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002612
Evan Cheng1b389522009-09-03 07:04:02 +00002613 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002614 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2615 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002616 return Result;
2617}
2618
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002619SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2620 SelectionDAG &DAG) const {
2621 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002622 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2623 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002624
2625 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002626 const ARMII::TOF TargetFlags =
2627 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002628 EVT PtrVT = getPointerTy();
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002629 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002630 SDLoc DL(Op);
2631
2632 ++NumMovwMovt;
2633
2634 // FIXME: Once remat is capable of dealing with instructions with register
2635 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002636 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2637 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
2638 TargetFlags));
2639 if (GV->hasDLLImportStorageClass())
2640 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
2641 MachinePointerInfo::getGOT(), false, false, false, 0);
2642 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002643}
2644
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002645SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002646 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002647 assert(Subtarget->isTargetELF() &&
2648 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002649 MachineFunction &MF = DAG.getMachineFunction();
2650 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002651 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002652 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002653 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002654 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002655 ARMConstantPoolValue *CPV =
2656 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2657 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002658 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002659 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002660 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002661 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002662 false, false, false, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002663 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002664 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002665}
2666
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002667SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002668ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002669 SDLoc dl(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002670 SDValue Val = DAG.getConstant(0, dl, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002671 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2672 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002673 Op.getOperand(1), Val);
2674}
2675
2676SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002677ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002678 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002679 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002680 Op.getOperand(1), DAG.getConstant(0, dl, MVT::i32));
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002681}
2682
2683SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002684ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002685 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002686 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002687 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002688 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002689 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002690 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00002691 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00002692 "RBIT intrinsic must have i32 type!");
Yi Kongc655f0c2014-08-20 10:40:20 +00002693 return DAG.getNode(ARMISD::RBIT, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00002694 }
Bob Wilson17f88782009-08-04 00:25:01 +00002695 case Intrinsic::arm_thread_pointer: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002696 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson17f88782009-08-04 00:25:01 +00002697 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2698 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002699 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002700 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002701 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002702 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002703 EVT PtrVT = getPointerTy();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002704 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2705 SDValue CPAddr;
2706 unsigned PCAdj = (RelocM != Reloc::PIC_)
2707 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002708 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002709 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2710 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002711 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002712 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002713 SDValue Result =
Evan Chengcdbb70c2009-10-31 03:39:36 +00002714 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002715 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002716 false, false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002717
2718 if (RelocM == Reloc::PIC_) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002719 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, dl, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002720 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2721 }
2722 return Result;
2723 }
Evan Cheng18381b42011-03-29 23:06:19 +00002724 case Intrinsic::arm_neon_vmulls:
2725 case Intrinsic::arm_neon_vmullu: {
2726 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2727 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002728 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002729 Op.getOperand(1), Op.getOperand(2));
2730 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002731 }
2732}
2733
Eli Friedman30a49e92011-08-03 21:06:02 +00002734static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2735 const ARMSubtarget *Subtarget) {
2736 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002737 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002738 if (!Subtarget->hasDataBarrier()) {
2739 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2740 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2741 // here.
2742 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002743 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002744 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002745 DAG.getConstant(0, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002746 }
2747
Tim Northover36b24172013-07-03 09:20:36 +00002748 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2749 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00002750 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002751 if (Subtarget->isMClass()) {
2752 // Only a full system barrier exists in the M-class architectures.
2753 Domain = ARM_MB::SY;
2754 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002755 // Swift happens to implement ISHST barriers in a way that's compatible with
2756 // Release semantics but weaker than ISH so we'd be fools not to use
2757 // it. Beware: other processors probably don't!
2758 Domain = ARM_MB::ISHST;
2759 }
2760
Joey Gouly926d3f52013-09-05 15:35:24 +00002761 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002762 DAG.getConstant(Intrinsic::arm_dmb, dl, MVT::i32),
2763 DAG.getConstant(Domain, dl, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002764}
2765
Evan Cheng8740ee32010-11-03 06:34:55 +00002766static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2767 const ARMSubtarget *Subtarget) {
2768 // ARM pre v5TE and Thumb1 does not have preload instructions.
2769 if (!(Subtarget->isThumb2() ||
2770 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2771 // Just preserve the chain.
2772 return Op.getOperand(0);
2773
Andrew Trickef9de2a2013-05-25 02:42:55 +00002774 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002775 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2776 if (!isRead &&
2777 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2778 // ARMv7 with MP extension has PLDW.
2779 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002780
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002781 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2782 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002783 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002784 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002785 isData = ~isData & 1;
2786 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002787
2788 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002789 Op.getOperand(1), DAG.getConstant(isRead, dl, MVT::i32),
2790 DAG.getConstant(isData, dl, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002791}
2792
Dan Gohman31ae5862010-04-17 14:41:14 +00002793static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2794 MachineFunction &MF = DAG.getMachineFunction();
2795 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2796
Evan Cheng10043e22007-01-19 07:51:42 +00002797 // vastart just stores the address of the VarArgsFrameIndex slot into the
2798 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002799 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002800 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002801 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002802 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002803 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2804 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002805}
2806
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002807SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002808ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2809 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002810 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002811 MachineFunction &MF = DAG.getMachineFunction();
2812 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2813
Craig Topper760b1342012-02-22 05:59:10 +00002814 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002815 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002816 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002817 else
Craig Topperc7242e02012-04-20 07:30:17 +00002818 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002819
2820 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002821 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002822 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002823
2824 SDValue ArgValue2;
2825 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002826 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002827 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002828
2829 // Create load node to retrieve arguments from the stack.
2830 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Chengcdbb70c2009-10-31 03:39:36 +00002831 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002832 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002833 false, false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002834 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002835 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002836 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002837 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002838 if (!Subtarget->isLittle())
2839 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002840 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002841}
2842
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002843// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002844// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002845// byval). Either way, we allocate stack slots adjacent to the data
2846// provided by our caller, and store the unallocated registers there.
2847// If this is a variadic function, the va_list pointer will begin with
2848// these values; otherwise, this reassembles a (byval) structure that
2849// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002850// Return: The frame index registers were stored into.
2851int
2852ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002853 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002854 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002855 unsigned InRegsParamRecordIdx,
Tim Northover8cda34f2015-03-11 18:54:22 +00002856 int ArgOffset,
2857 unsigned ArgSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002858 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002859 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002860 // Setup first unallocated register as first byval register;
2861 // eat all remained registers
2862 // (these two actions are performed by HandleByVal method).
2863 // Then, here, we initialize stack frame with
2864 // "store-reg" instructions.
2865 // Case #2. Var-args function, that doesn't contain byval parameters.
2866 // The same: eat all remained unallocated registers,
2867 // initialize stack frame.
2868
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002869 MachineFunction &MF = DAG.getMachineFunction();
2870 MachineFrameInfo *MFI = MF.getFrameInfo();
2871 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002872 unsigned RBegin, REnd;
2873 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2874 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002875 } else {
Tim Northover8cda34f2015-03-11 18:54:22 +00002876 unsigned RBeginIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
Aaron Ballmanc579d662015-03-12 13:24:06 +00002877 RBegin = RBeginIdx == 4 ? (unsigned)ARM::R4 : GPRArgRegs[RBeginIdx];
Tim Northover8cda34f2015-03-11 18:54:22 +00002878 REnd = ARM::R4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002879 }
2880
Tim Northover8cda34f2015-03-11 18:54:22 +00002881 if (REnd != RBegin)
2882 ArgOffset = -4 * (ARM::R4 - RBegin);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002883
Tim Northover8cda34f2015-03-11 18:54:22 +00002884 int FrameIndex = MFI->CreateFixedObject(ArgSize, ArgOffset, false);
2885 SDValue FIN = DAG.getFrameIndex(FrameIndex, getPointerTy());
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002886
Tim Northover8cda34f2015-03-11 18:54:22 +00002887 SmallVector<SDValue, 4> MemOps;
2888 const TargetRegisterClass *RC =
2889 AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002890
Tim Northover8cda34f2015-03-11 18:54:22 +00002891 for (unsigned Reg = RBegin, i = 0; Reg < REnd; ++Reg, ++i) {
2892 unsigned VReg = MF.addLiveIn(Reg, RC);
2893 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2894 SDValue Store =
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002895 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Tim Northover8cda34f2015-03-11 18:54:22 +00002896 MachinePointerInfo(OrigArg, 4 * i), false, false, 0);
2897 MemOps.push_back(Store);
2898 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002899 DAG.getConstant(4, dl, getPointerTy()));
Oliver Stannardd55e1152014-03-05 15:25:27 +00002900 }
Tim Northover8cda34f2015-03-11 18:54:22 +00002901
2902 if (!MemOps.empty())
2903 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
2904 return FrameIndex;
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002905}
2906
2907// Setup stack frame, the va_list pointer will start from.
2908void
2909ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002910 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002911 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002912 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002913 bool ForceMutable) const {
2914 MachineFunction &MF = DAG.getMachineFunction();
2915 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2916
2917 // Try to store any remaining integer argument regs
2918 // to their spots on the stack so that they may be loaded by deferencing
2919 // the result of va_next.
2920 // If there is no regs to be stored, just point address after last
2921 // argument passed via stack.
Tim Northover8cda34f2015-03-11 18:54:22 +00002922 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
2923 CCInfo.getInRegsParamsCount(),
2924 CCInfo.getNextStackOffset(), 4);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002925 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002926}
2927
Bob Wilson2e076c42009-06-22 23:27:02 +00002928SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002929ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002930 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002931 const SmallVectorImpl<ISD::InputArg>
2932 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002933 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002934 SmallVectorImpl<SDValue> &InVals)
2935 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00002936 MachineFunction &MF = DAG.getMachineFunction();
2937 MachineFrameInfo *MFI = MF.getFrameInfo();
2938
Bob Wilsona4c22902009-04-17 19:07:39 +00002939 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2940
2941 // Assign locations to all of the incoming arguments.
2942 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002943 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
2944 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002945 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002946 CCAssignFnForNode(CallConv, /* Return*/ false,
2947 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00002948
Bob Wilsona4c22902009-04-17 19:07:39 +00002949 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002950 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002951 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2952 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002953
2954 // Initially ArgRegsSaveSize is zero.
2955 // Then we increase this value each time we meet byval parameter.
2956 // We also increase this value in case of varargs function.
2957 AFI->setArgRegsSaveSize(0);
2958
Oliver Stannardd55e1152014-03-05 15:25:27 +00002959 // Calculate the amount of stack space that we need to allocate to store
2960 // byval and variadic arguments that are passed in registers.
2961 // We need to know this before we allocate the first byval or variadic
2962 // argument, as they will be allocated a stack slot below the CFA (Canonical
2963 // Frame Address, the stack pointer at entry to the function).
Tim Northover8cda34f2015-03-11 18:54:22 +00002964 unsigned ArgRegBegin = ARM::R4;
Oliver Stannardd55e1152014-03-05 15:25:27 +00002965 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Tim Northover8cda34f2015-03-11 18:54:22 +00002966 if (CCInfo.getInRegsParamsProcessed() >= CCInfo.getInRegsParamsCount())
2967 break;
Oliver Stannardd55e1152014-03-05 15:25:27 +00002968
Tim Northover8cda34f2015-03-11 18:54:22 +00002969 CCValAssign &VA = ArgLocs[i];
2970 unsigned Index = VA.getValNo();
2971 ISD::ArgFlagsTy Flags = Ins[Index].Flags;
2972 if (!Flags.isByVal())
2973 continue;
2974
2975 assert(VA.isMemLoc() && "unexpected byval pointer in reg");
2976 unsigned RBegin, REnd;
2977 CCInfo.getInRegsParamInfo(CCInfo.getInRegsParamsProcessed(), RBegin, REnd);
2978 ArgRegBegin = std::min(ArgRegBegin, RBegin);
2979
2980 CCInfo.nextInRegsParam();
Oliver Stannardd55e1152014-03-05 15:25:27 +00002981 }
2982 CCInfo.rewindByValRegsInfo();
Tim Northover8cda34f2015-03-11 18:54:22 +00002983
2984 int lastInsIndex = -1;
Reid Kleckner2d9bb652014-08-22 21:59:26 +00002985 if (isVarArg && MFI->hasVAStart()) {
Tim Northover8cda34f2015-03-11 18:54:22 +00002986 unsigned RegIdx = CCInfo.getFirstUnallocated(GPRArgRegs);
2987 if (RegIdx != array_lengthof(GPRArgRegs))
2988 ArgRegBegin = std::min(ArgRegBegin, (unsigned)GPRArgRegs[RegIdx]);
Oliver Stannardd55e1152014-03-05 15:25:27 +00002989 }
Tim Northover8cda34f2015-03-11 18:54:22 +00002990
2991 unsigned TotalArgRegsSaveSize = 4 * (ARM::R4 - ArgRegBegin);
2992 AFI->setArgRegsSaveSize(TotalArgRegsSaveSize);
Oliver Stannardd55e1152014-03-05 15:25:27 +00002993
Bob Wilsona4c22902009-04-17 19:07:39 +00002994 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2995 CCValAssign &VA = ArgLocs[i];
Andrew Trick05938a52015-02-16 18:10:47 +00002996 if (Ins[VA.getValNo()].isOrigArg()) {
2997 std::advance(CurOrigArg,
2998 Ins[VA.getValNo()].getOrigArgIndex() - CurArgIdx);
2999 CurArgIdx = Ins[VA.getValNo()].getOrigArgIndex();
3000 }
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003001 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003002 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003003 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003004
Bob Wilsona4c22902009-04-17 19:07:39 +00003005 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003006 // f64 and vector types are split up into multiple registers or
3007 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003008 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003009 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003010 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003011 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003012 SDValue ArgValue2;
3013 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003014 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson699bdf72010-04-13 22:03:22 +00003015 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3016 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003017 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003018 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003019 } else {
3020 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3021 Chain, DAG, dl);
3022 }
Owen Anderson9f944592009-08-11 20:47:22 +00003023 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3024 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003025 ArgValue, ArgValue1,
3026 DAG.getIntPtrConstant(0, dl));
Owen Anderson9f944592009-08-11 20:47:22 +00003027 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003028 ArgValue, ArgValue2,
3029 DAG.getIntPtrConstant(1, dl));
Bob Wilson2e076c42009-06-22 23:27:02 +00003030 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003031 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003032
Bob Wilson2e076c42009-06-22 23:27:02 +00003033 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003034 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003035
Owen Anderson9f944592009-08-11 20:47:22 +00003036 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003037 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003038 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003039 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003040 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003041 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003042 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003043 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3044 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003045 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003046 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003047
3048 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003049 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003050 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003051 }
3052
3053 // If this is an 8 or 16-bit value, it is really passed promoted
3054 // to 32 bits. Insert an assert[sz]ext to capture this, then
3055 // truncate to the right size.
3056 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003057 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003058 case CCValAssign::Full: break;
3059 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003060 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003061 break;
3062 case CCValAssign::SExt:
3063 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3064 DAG.getValueType(VA.getValVT()));
3065 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3066 break;
3067 case CCValAssign::ZExt:
3068 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3069 DAG.getValueType(VA.getValVT()));
3070 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3071 break;
3072 }
3073
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003074 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003075
3076 } else { // VA.isRegLoc()
3077
3078 // sanity check
3079 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003080 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003081
Andrew Trick05938a52015-02-16 18:10:47 +00003082 int index = VA.getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003083
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003084 // Some Ins[] entries become multiple ArgLoc[] entries.
3085 // Process them only once.
3086 if (index != lastInsIndex)
3087 {
3088 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003089 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003090 // This can be changed with more analysis.
3091 // In case of tail call optimization mark all arguments mutable.
3092 // Since they could be overwritten by lowering of arguments in case of
3093 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003094 if (Flags.isByVal()) {
Andrew Trick05938a52015-02-16 18:10:47 +00003095 assert(Ins[index].isOrigArg() &&
3096 "Byval arguments cannot be implicit");
Daniel Sanders8104b752014-11-01 19:32:23 +00003097 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003098
Tim Northover8cda34f2015-03-11 18:54:22 +00003099 int FrameIndex = StoreByValRegs(CCInfo, DAG, dl, Chain, CurOrigArg,
3100 CurByValIndex, VA.getLocMemOffset(),
3101 Flags.getByValSize());
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003102 InVals.push_back(DAG.getFrameIndex(FrameIndex, getPointerTy()));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003103 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003104 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003105 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003106 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003107 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003108
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003109 // Create load nodes to retrieve arguments from the stack.
3110 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3111 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3112 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003113 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003114 }
3115 lastInsIndex = index;
3116 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003117 }
3118 }
3119
3120 // varargs
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003121 if (isVarArg && MFI->hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003122 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003123 CCInfo.getNextStackOffset(),
3124 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003125
Oliver Stannardb14c6252014-04-02 16:10:33 +00003126 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3127
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003128 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003129}
3130
3131/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003132static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003133 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003134 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003135 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003136 // Maybe this has already been legalized into the constant pool?
3137 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003138 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003139 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003140 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003141 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003142 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003143 } else if (Op->getOpcode() == ISD::BITCAST &&
3144 Op->getValueType(0) == MVT::f64) {
3145 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3146 // created by LowerConstantFP().
3147 SDValue BitcastOp = Op->getOperand(0);
3148 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM) {
3149 SDValue MoveOp = BitcastOp->getOperand(0);
3150 if (MoveOp->getOpcode() == ISD::TargetConstant &&
3151 cast<ConstantSDNode>(MoveOp)->getZExtValue() == 0) {
3152 return true;
3153 }
3154 }
Evan Cheng10043e22007-01-19 07:51:42 +00003155 }
3156 return false;
3157}
3158
Evan Cheng10043e22007-01-19 07:51:42 +00003159/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3160/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003161SDValue
3162ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003163 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003164 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003165 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003166 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003167 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003168 // Constant does not fit, try adjusting it by one?
3169 switch (CC) {
3170 default: break;
3171 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003172 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003173 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003174 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003175 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003176 }
3177 break;
3178 case ISD::SETULT:
3179 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003180 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003181 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003182 RHS = DAG.getConstant(C - 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003183 }
3184 break;
3185 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003186 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003187 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003188 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003189 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003190 }
3191 break;
3192 case ISD::SETULE:
3193 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003194 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003195 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003196 RHS = DAG.getConstant(C + 1, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003197 }
3198 break;
3199 }
3200 }
3201 }
3202
3203 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003204 ARMISD::NodeType CompareType;
3205 switch (CondCode) {
3206 default:
3207 CompareType = ARMISD::CMP;
3208 break;
3209 case ARMCC::EQ:
3210 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003211 // Uses only Z Flag
3212 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003213 break;
3214 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003215 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003216 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003217}
3218
3219/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003220SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003221ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003222 SDLoc dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003223 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003224 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003225 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003226 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003227 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003228 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3229 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003230}
3231
Bob Wilson45acbd02011-03-08 01:17:20 +00003232/// duplicateCmp - Glue values can have only one use, so this function
3233/// duplicates a comparison node.
3234SDValue
3235ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3236 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003237 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003238 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3239 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3240
3241 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3242 Cmp = Cmp.getOperand(0);
3243 Opc = Cmp.getOpcode();
3244 if (Opc == ARMISD::CMPFP)
3245 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3246 else {
3247 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3248 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3249 }
3250 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3251}
3252
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003253std::pair<SDValue, SDValue>
3254ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3255 SDValue &ARMcc) const {
3256 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3257
3258 SDValue Value, OverflowCmp;
3259 SDValue LHS = Op.getOperand(0);
3260 SDValue RHS = Op.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003261 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003262
3263 // FIXME: We are currently always generating CMPs because we don't support
3264 // generating CMN through the backend. This is not as good as the natural
3265 // CMP case because it causes a register dependency and cannot be folded
3266 // later.
3267
3268 switch (Op.getOpcode()) {
3269 default:
3270 llvm_unreachable("Unknown overflow instruction!");
3271 case ISD::SADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003272 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3273 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3274 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003275 break;
3276 case ISD::UADDO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003277 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3278 Value = DAG.getNode(ISD::ADD, dl, Op.getValueType(), LHS, RHS);
3279 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, Value, LHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003280 break;
3281 case ISD::SSUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003282 ARMcc = DAG.getConstant(ARMCC::VC, dl, MVT::i32);
3283 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3284 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003285 break;
3286 case ISD::USUBO:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003287 ARMcc = DAG.getConstant(ARMCC::HS, dl, MVT::i32);
3288 Value = DAG.getNode(ISD::SUB, dl, Op.getValueType(), LHS, RHS);
3289 OverflowCmp = DAG.getNode(ARMISD::CMP, dl, MVT::Glue, LHS, RHS);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003290 break;
3291 } // switch (...)
3292
3293 return std::make_pair(Value, OverflowCmp);
3294}
3295
3296
3297SDValue
3298ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3299 // Let legalize expand this if it isn't a legal type yet.
3300 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3301 return SDValue();
3302
3303 SDValue Value, OverflowCmp;
3304 SDValue ARMcc;
3305 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3306 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003307 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003308 // We use 0 and 1 as false and true values.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003309 SDValue TVal = DAG.getConstant(1, dl, MVT::i32);
3310 SDValue FVal = DAG.getConstant(0, dl, MVT::i32);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003311 EVT VT = Op.getValueType();
3312
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003313 SDValue Overflow = DAG.getNode(ARMISD::CMOV, dl, VT, TVal, FVal,
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003314 ARMcc, CCR, OverflowCmp);
3315
3316 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003317 return DAG.getNode(ISD::MERGE_VALUES, dl, VTs, Value, Overflow);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003318}
3319
3320
Bill Wendling6a981312010-08-11 08:43:16 +00003321SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3322 SDValue Cond = Op.getOperand(0);
3323 SDValue SelectTrue = Op.getOperand(1);
3324 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003325 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003326 unsigned Opc = Cond.getOpcode();
3327
3328 if (Cond.getResNo() == 1 &&
3329 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3330 Opc == ISD::USUBO)) {
3331 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3332 return SDValue();
3333
3334 SDValue Value, OverflowCmp;
3335 SDValue ARMcc;
3336 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3337 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3338 EVT VT = Op.getValueType();
3339
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003340 return getCMOV(dl, VT, SelectTrue, SelectFalse, ARMcc, CCR,
Oliver Stannard51b1d462014-08-21 12:50:31 +00003341 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003342 }
Bill Wendling6a981312010-08-11 08:43:16 +00003343
3344 // Convert:
3345 //
3346 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3347 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3348 //
3349 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3350 const ConstantSDNode *CMOVTrue =
3351 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3352 const ConstantSDNode *CMOVFalse =
3353 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3354
3355 if (CMOVTrue && CMOVFalse) {
3356 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3357 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3358
3359 SDValue True;
3360 SDValue False;
3361 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3362 True = SelectTrue;
3363 False = SelectFalse;
3364 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3365 True = SelectFalse;
3366 False = SelectTrue;
3367 }
3368
3369 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003370 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003371 SDValue ARMcc = Cond.getOperand(2);
3372 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003373 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003374 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003375 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00003376 }
3377 }
3378 }
3379
Dan Gohmand4a77c42012-02-24 00:09:36 +00003380 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3381 // undefined bits before doing a full-word comparison with zero.
3382 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003383 DAG.getConstant(1, dl, Cond.getValueType()));
Dan Gohmand4a77c42012-02-24 00:09:36 +00003384
Bill Wendling6a981312010-08-11 08:43:16 +00003385 return DAG.getSelectCC(dl, Cond,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003386 DAG.getConstant(0, dl, Cond.getValueType()),
Bill Wendling6a981312010-08-11 08:43:16 +00003387 SelectTrue, SelectFalse, ISD::SETNE);
3388}
3389
Joey Gouly881eab52013-08-22 15:29:11 +00003390static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3391 bool &swpCmpOps, bool &swpVselOps) {
3392 // Start by selecting the GE condition code for opcodes that return true for
3393 // 'equality'
3394 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3395 CC == ISD::SETULE)
3396 CondCode = ARMCC::GE;
3397
3398 // and GT for opcodes that return false for 'equality'.
3399 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3400 CC == ISD::SETULT)
3401 CondCode = ARMCC::GT;
3402
3403 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3404 // to swap the compare operands.
3405 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3406 CC == ISD::SETULT)
3407 swpCmpOps = true;
3408
3409 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3410 // If we have an unordered opcode, we need to swap the operands to the VSEL
3411 // instruction (effectively negating the condition).
3412 //
3413 // This also has the effect of swapping which one of 'less' or 'greater'
3414 // returns true, so we also swap the compare operands. It also switches
3415 // whether we return true for 'equality', so we compensate by picking the
3416 // opposite condition code to our original choice.
3417 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3418 CC == ISD::SETUGT) {
3419 swpCmpOps = !swpCmpOps;
3420 swpVselOps = !swpVselOps;
3421 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3422 }
3423
3424 // 'ordered' is 'anything but unordered', so use the VS condition code and
3425 // swap the VSEL operands.
3426 if (CC == ISD::SETO) {
3427 CondCode = ARMCC::VS;
3428 swpVselOps = true;
3429 }
3430
3431 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3432 // code and swap the VSEL operands.
3433 if (CC == ISD::SETUNE) {
3434 CondCode = ARMCC::EQ;
3435 swpVselOps = true;
3436 }
3437}
3438
Oliver Stannard51b1d462014-08-21 12:50:31 +00003439SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal,
3440 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
3441 SDValue Cmp, SelectionDAG &DAG) const {
3442 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
3443 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3444 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
3445 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3446 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
3447
3448 SDValue TrueLow = TrueVal.getValue(0);
3449 SDValue TrueHigh = TrueVal.getValue(1);
3450 SDValue FalseLow = FalseVal.getValue(0);
3451 SDValue FalseHigh = FalseVal.getValue(1);
3452
3453 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
3454 ARMcc, CCR, Cmp);
3455 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
3456 ARMcc, CCR, duplicateCmp(Cmp, DAG));
3457
3458 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
3459 } else {
3460 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3461 Cmp);
3462 }
3463}
3464
Dan Gohman21cea8a2010-04-17 15:26:15 +00003465SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003466 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003467 SDValue LHS = Op.getOperand(0);
3468 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003469 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003470 SDValue TrueVal = Op.getOperand(2);
3471 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003472 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003473
Oliver Stannard51b1d462014-08-21 12:50:31 +00003474 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3475 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3476 dl);
3477
3478 // If softenSetCCOperands only returned one value, we should compare it to
3479 // zero.
3480 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003481 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003482 CC = ISD::SETNE;
3483 }
3484 }
3485
Owen Anderson9f944592009-08-11 20:47:22 +00003486 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003487 // Try to generate VSEL on ARMv8.
3488 // The VSEL instruction can't use all the usual ARM condition
3489 // codes: it only has two bits to select the condition code, so it's
3490 // constrained to use only GE, GT, VS and EQ.
3491 //
3492 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3493 // swap the operands of the previous compare instruction (effectively
3494 // inverting the compare condition, swapping 'less' and 'greater') and
3495 // sometimes need to swap the operands to the VSEL (which inverts the
3496 // condition in the sense of firing whenever the previous condition didn't)
Eric Christopher1889fdc2015-01-29 00:19:39 +00003497 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3498 TrueVal.getValueType() == MVT::f64)) {
Joey Gouly881eab52013-08-22 15:29:11 +00003499 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3500 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3501 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00003502 CC = ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003503 std::swap(TrueVal, FalseVal);
3504 }
3505 }
3506
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003507 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003508 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003509 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003510 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003511 }
3512
3513 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003514 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003515
Scott Douglass7ad77922015-04-08 17:18:28 +00003516 // Try to generate VMAXNM/VMINNM on ARMv8.
Eric Christopher1889fdc2015-01-29 00:19:39 +00003517 if (Subtarget->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
3518 TrueVal.getValueType() == MVT::f64)) {
Scott Douglass7ad77922015-04-08 17:18:28 +00003519 // We can use VMAXNM/VMINNM for a compare followed by a select with the
Joey Goulye3dd6842013-08-23 12:01:13 +00003520 // same operands, as follows:
Scott Douglass7ad77922015-04-08 17:18:28 +00003521 // c = fcmp [?gt, ?ge, ?lt, ?le] a, b
Joey Goulye3dd6842013-08-23 12:01:13 +00003522 // select c, a, b
Scott Douglass7ad77922015-04-08 17:18:28 +00003523 // In NoNaNsFPMath the CC will have been changed from, e.g., 'ogt' to 'gt'.
Artyom Skrobov3f8eae92015-05-06 11:44:10 +00003524 bool swapSides = false;
3525 if (!getTargetMachine().Options.NoNaNsFPMath) {
3526 // transformability may depend on which way around we compare
3527 switch (CC) {
3528 default:
3529 break;
3530 case ISD::SETOGT:
3531 case ISD::SETOGE:
3532 case ISD::SETOLT:
3533 case ISD::SETOLE:
3534 // the non-NaN should be RHS
3535 swapSides = DAG.isKnownNeverNaN(LHS) && !DAG.isKnownNeverNaN(RHS);
3536 break;
3537 case ISD::SETUGT:
3538 case ISD::SETUGE:
3539 case ISD::SETULT:
3540 case ISD::SETULE:
3541 // the non-NaN should be LHS
3542 swapSides = DAG.isKnownNeverNaN(RHS) && !DAG.isKnownNeverNaN(LHS);
3543 break;
3544 }
3545 }
3546 swapSides = swapSides || (LHS == FalseVal && RHS == TrueVal);
3547 if (swapSides) {
3548 CC = ISD::getSetCCSwappedOperands(CC);
3549 std::swap(LHS, RHS);
3550 }
3551 if (LHS == TrueVal && RHS == FalseVal) {
3552 bool canTransform = true;
3553 // FIXME: FastMathFlags::noSignedZeros() doesn't appear reachable from here
3554 if (!getTargetMachine().Options.UnsafeFPMath &&
3555 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
3556 const ConstantFPSDNode *Zero;
3557 switch (CC) {
3558 default:
3559 break;
3560 case ISD::SETOGT:
3561 case ISD::SETUGT:
3562 case ISD::SETGT:
3563 // RHS must not be -0
3564 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(RHS)) &&
3565 !Zero->isNegative();
3566 break;
3567 case ISD::SETOGE:
3568 case ISD::SETUGE:
3569 case ISD::SETGE:
3570 // LHS must not be -0
3571 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(LHS)) &&
3572 !Zero->isNegative();
3573 break;
3574 case ISD::SETOLT:
3575 case ISD::SETULT:
3576 case ISD::SETLT:
3577 // RHS must not be +0
3578 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(RHS)) &&
3579 Zero->isNegative();
3580 break;
3581 case ISD::SETOLE:
3582 case ISD::SETULE:
3583 case ISD::SETLE:
3584 // LHS must not be +0
3585 canTransform = (Zero = dyn_cast<ConstantFPSDNode>(LHS)) &&
3586 Zero->isNegative();
3587 break;
3588 }
3589 }
3590 if (canTransform) {
3591 // Note: If one of the elements in a pair is a number and the other
3592 // element is NaN, the corresponding result element is the number.
3593 // This is consistent with the IEEE 754-2008 standard.
3594 // Therefore, a > b ? a : b <=> vmax(a,b), if b is constant and a is NaN
3595 switch (CC) {
3596 default:
3597 break;
3598 case ISD::SETOGT:
3599 case ISD::SETOGE:
3600 if (!DAG.isKnownNeverNaN(RHS))
3601 break;
3602 return DAG.getNode(ARMISD::VMAXNM, dl, VT, LHS, RHS);
3603 case ISD::SETUGT:
3604 case ISD::SETUGE:
3605 if (!DAG.isKnownNeverNaN(LHS))
3606 break;
3607 case ISD::SETGT:
3608 case ISD::SETGE:
3609 return DAG.getNode(ARMISD::VMAXNM, dl, VT, LHS, RHS);
3610 case ISD::SETOLT:
3611 case ISD::SETOLE:
3612 if (!DAG.isKnownNeverNaN(RHS))
3613 break;
3614 return DAG.getNode(ARMISD::VMINNM, dl, VT, LHS, RHS);
3615 case ISD::SETULT:
3616 case ISD::SETULE:
3617 if (!DAG.isKnownNeverNaN(LHS))
3618 break;
3619 case ISD::SETLT:
3620 case ISD::SETLE:
3621 return DAG.getNode(ARMISD::VMINNM, dl, VT, LHS, RHS);
3622 }
Oliver Stannard79efe412014-10-27 09:23:02 +00003623 }
Joey Goulye3dd6842013-08-23 12:01:13 +00003624 }
3625
Joey Gouly881eab52013-08-22 15:29:11 +00003626 bool swpCmpOps = false;
3627 bool swpVselOps = false;
3628 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3629
3630 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3631 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3632 if (swpCmpOps)
3633 std::swap(LHS, RHS);
3634 if (swpVselOps)
3635 std::swap(TrueVal, FalseVal);
3636 }
3637 }
3638
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003639 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003640 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003641 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003642 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003643 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003644 SDValue ARMcc2 = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003645 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003646 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003647 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003648 }
3649 return Result;
3650}
3651
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003652/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3653/// to morph to an integer compare sequence.
3654static bool canChangeToInt(SDValue Op, bool &SeenZero,
3655 const ARMSubtarget *Subtarget) {
3656 SDNode *N = Op.getNode();
3657 if (!N->hasOneUse())
3658 // Otherwise it requires moving the value from fp to integer registers.
3659 return false;
3660 if (!N->getNumValues())
3661 return false;
3662 EVT VT = Op.getValueType();
3663 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3664 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3665 // vmrs are very slow, e.g. cortex-a8.
3666 return false;
3667
3668 if (isFloatingPointZero(Op)) {
3669 SeenZero = true;
3670 return true;
3671 }
3672 return ISD::isNormalLoad(N);
3673}
3674
3675static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3676 if (isFloatingPointZero(Op))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003677 return DAG.getConstant(0, SDLoc(Op), MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003678
3679 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003680 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003681 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003682 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003683 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003684
3685 llvm_unreachable("Unknown VFP cmp argument!");
3686}
3687
3688static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3689 SDValue &RetVal1, SDValue &RetVal2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003690 SDLoc dl(Op);
3691
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003692 if (isFloatingPointZero(Op)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003693 RetVal1 = DAG.getConstant(0, dl, MVT::i32);
3694 RetVal2 = DAG.getConstant(0, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003695 return;
3696 }
3697
3698 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3699 SDValue Ptr = Ld->getBasePtr();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003700 RetVal1 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003701 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003702 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003703 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003704 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003705
3706 EVT PtrType = Ptr.getValueType();
3707 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003708 SDValue NewPtr = DAG.getNode(ISD::ADD, dl,
3709 PtrType, Ptr, DAG.getConstant(4, dl, PtrType));
3710 RetVal2 = DAG.getLoad(MVT::i32, dl,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003711 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003712 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003713 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003714 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003715 return;
3716 }
3717
3718 llvm_unreachable("Unknown VFP cmp argument!");
3719}
3720
3721/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3722/// f32 and even f64 comparisons to integer ones.
3723SDValue
3724ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3725 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003726 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003727 SDValue LHS = Op.getOperand(2);
3728 SDValue RHS = Op.getOperand(3);
3729 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003730 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003731
Evan Chengd12af5d2012-03-01 23:27:13 +00003732 bool LHSSeenZero = false;
3733 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3734 bool RHSSeenZero = false;
3735 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3736 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003737 // If unsafe fp math optimization is enabled and there are no other uses of
3738 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003739 // to an integer comparison.
3740 if (CC == ISD::SETOEQ)
3741 CC = ISD::SETEQ;
3742 else if (CC == ISD::SETUNE)
3743 CC = ISD::SETNE;
3744
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003745 SDValue Mask = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003746 SDValue ARMcc;
3747 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003748 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3749 bitcastf32Toi32(LHS, DAG), Mask);
3750 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3751 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003752 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3753 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3754 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3755 Chain, Dest, ARMcc, CCR, Cmp);
3756 }
3757
3758 SDValue LHS1, LHS2;
3759 SDValue RHS1, RHS2;
3760 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3761 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003762 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3763 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003764 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003765 ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003766 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003767 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003768 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003769 }
3770
3771 return SDValue();
3772}
3773
3774SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3775 SDValue Chain = Op.getOperand(0);
3776 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3777 SDValue LHS = Op.getOperand(2);
3778 SDValue RHS = Op.getOperand(3);
3779 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003780 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003781
Oliver Stannard51b1d462014-08-21 12:50:31 +00003782 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3783 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3784 dl);
3785
3786 // If softenSetCCOperands only returned one value, we should compare it to
3787 // zero.
3788 if (!RHS.getNode()) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003789 RHS = DAG.getConstant(0, dl, LHS.getValueType());
Oliver Stannard51b1d462014-08-21 12:50:31 +00003790 CC = ISD::SETNE;
3791 }
3792 }
3793
Owen Anderson9f944592009-08-11 20:47:22 +00003794 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003795 SDValue ARMcc;
3796 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003797 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003798 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003799 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003800 }
3801
Owen Anderson9f944592009-08-11 20:47:22 +00003802 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003803
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003804 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003805 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3806 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3807 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3808 if (Result.getNode())
3809 return Result;
3810 }
3811
Evan Cheng10043e22007-01-19 07:51:42 +00003812 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003813 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003814
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003815 SDValue ARMcc = DAG.getConstant(CondCode, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003816 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003817 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003818 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003819 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003820 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003821 if (CondCode2 != ARMCC::AL) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003822 ARMcc = DAG.getConstant(CondCode2, dl, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003823 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003824 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003825 }
3826 return Res;
3827}
3828
Dan Gohman21cea8a2010-04-17 15:26:15 +00003829SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003830 SDValue Chain = Op.getOperand(0);
3831 SDValue Table = Op.getOperand(1);
3832 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003833 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003834
Owen Anderson53aa7a92009-08-10 22:56:29 +00003835 EVT PTy = getPointerTy();
Evan Cheng10043e22007-01-19 07:51:42 +00003836 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003837 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Tim Northover4998a472015-05-13 20:28:38 +00003838 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003839 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, dl, PTy));
Evan Chengc8bed032009-07-28 20:53:24 +00003840 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003841 if (Subtarget->isThumb2()) {
3842 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3843 // which does another jump to the destination. This also makes it easier
3844 // to translate it to TBB / TBH later.
3845 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003846 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Tim Northover4998a472015-05-13 20:28:38 +00003847 Addr, Op.getOperand(2), JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003848 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003849 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003850 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattner7727d052010-09-21 06:44:06 +00003851 MachinePointerInfo::getJumpTable(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003852 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003853 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003854 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Tim Northover4998a472015-05-13 20:28:38 +00003855 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003856 } else {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003857 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003858 MachinePointerInfo::getJumpTable(),
3859 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003860 Chain = Addr.getValue(1);
Tim Northover4998a472015-05-13 20:28:38 +00003861 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003862 }
Evan Cheng10043e22007-01-19 07:51:42 +00003863}
3864
Eli Friedman2d4055b2011-11-09 23:36:02 +00003865static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003866 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003867 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003868
James Molloy547d4c02012-02-20 09:24:05 +00003869 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3870 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3871 return Op;
3872 return DAG.UnrollVectorOp(Op.getNode());
3873 }
3874
3875 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3876 "Invalid type for custom lowering!");
3877 if (VT != MVT::v4i16)
3878 return DAG.UnrollVectorOp(Op.getNode());
3879
3880 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3881 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003882}
3883
Oliver Stannard51b1d462014-08-21 12:50:31 +00003884SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003885 EVT VT = Op.getValueType();
3886 if (VT.isVector())
3887 return LowerVectorFP_TO_INT(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003888 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
3889 RTLIB::Libcall LC;
3890 if (Op.getOpcode() == ISD::FP_TO_SINT)
3891 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
3892 Op.getValueType());
3893 else
3894 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
3895 Op.getValueType());
3896 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3897 /*isSigned*/ false, SDLoc(Op)).first;
3898 }
3899
James Molloyfa041152015-03-23 16:15:16 +00003900 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00003901}
3902
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003903static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3904 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003905 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003906
Eli Friedman2d4055b2011-11-09 23:36:02 +00003907 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3908 if (VT.getVectorElementType() == MVT::f32)
3909 return Op;
3910 return DAG.UnrollVectorOp(Op.getNode());
3911 }
3912
Duncan Sandsa41634e2011-08-12 14:54:45 +00003913 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3914 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003915 if (VT != MVT::v4f32)
3916 return DAG.UnrollVectorOp(Op.getNode());
3917
3918 unsigned CastOpc;
3919 unsigned Opc;
3920 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003921 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003922 case ISD::SINT_TO_FP:
3923 CastOpc = ISD::SIGN_EXTEND;
3924 Opc = ISD::SINT_TO_FP;
3925 break;
3926 case ISD::UINT_TO_FP:
3927 CastOpc = ISD::ZERO_EXTEND;
3928 Opc = ISD::UINT_TO_FP;
3929 break;
3930 }
3931
3932 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3933 return DAG.getNode(Opc, dl, VT, Op);
3934}
3935
Oliver Stannard51b1d462014-08-21 12:50:31 +00003936SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00003937 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003938 if (VT.isVector())
3939 return LowerVectorINT_TO_FP(Op, DAG);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003940 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
3941 RTLIB::Libcall LC;
3942 if (Op.getOpcode() == ISD::SINT_TO_FP)
3943 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
3944 Op.getValueType());
3945 else
3946 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
3947 Op.getValueType());
3948 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3949 /*isSigned*/ false, SDLoc(Op)).first;
3950 }
3951
James Molloyfa041152015-03-23 16:15:16 +00003952 return Op;
Bob Wilsone4191e72010-03-19 22:51:32 +00003953}
3954
Evan Cheng25f93642010-07-08 02:08:50 +00003955SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00003956 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003957 SDValue Tmp0 = Op.getOperand(0);
3958 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003959 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003960 EVT VT = Op.getValueType();
3961 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00003962 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3963 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3964 bool UseNEON = !InGPR && Subtarget->hasNEON();
3965
3966 if (UseNEON) {
3967 // Use VBSL to copy the sign bit.
3968 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3969 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003970 DAG.getTargetConstant(EncodedVal, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00003971 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3972 if (VT == MVT::f64)
3973 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3974 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003975 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00003976 else /*if (VT == MVT::f32)*/
3977 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3978 if (SrcVT == MVT::f32) {
3979 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3980 if (VT == MVT::f64)
3981 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3982 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003983 DAG.getConstant(32, dl, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00003984 } else if (VT == MVT::f32)
3985 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3986 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003987 DAG.getConstant(32, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00003988 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3989 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3990
3991 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003992 dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00003993 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3994 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3995 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00003996
Evan Chengd6b641e2011-02-23 02:24:55 +00003997 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3998 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3999 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004000 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004001 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4002 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004003 DAG.getConstant(0, dl, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004004 } else {
4005 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4006 }
4007
4008 return Res;
4009 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004010
4011 // Bitcast operand 1 to i32.
4012 if (SrcVT == MVT::f64)
4013 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004014 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004015 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4016
Evan Chengd6b641e2011-02-23 02:24:55 +00004017 // Or in the signbit with integer operations.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004018 SDValue Mask1 = DAG.getConstant(0x80000000, dl, MVT::i32);
4019 SDValue Mask2 = DAG.getConstant(0x7fffffff, dl, MVT::i32);
Evan Chengd6b641e2011-02-23 02:24:55 +00004020 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4021 if (VT == MVT::f32) {
4022 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4023 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4024 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4025 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004026 }
4027
Evan Chengd6b641e2011-02-23 02:24:55 +00004028 // f64: Or the high part with signbit and then combine two parts.
4029 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004030 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004031 SDValue Lo = Tmp0.getValue(0);
4032 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4033 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4034 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004035}
4036
Evan Cheng168ced92010-05-22 01:47:14 +00004037SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4038 MachineFunction &MF = DAG.getMachineFunction();
4039 MachineFrameInfo *MFI = MF.getFrameInfo();
4040 MFI->setReturnAddressIsTaken(true);
4041
Bill Wendling908bf812014-01-06 00:43:20 +00004042 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004043 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004044
Evan Cheng168ced92010-05-22 01:47:14 +00004045 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004046 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004047 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4048 if (Depth) {
4049 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004050 SDValue Offset = DAG.getConstant(4, dl, MVT::i32);
Evan Cheng168ced92010-05-22 01:47:14 +00004051 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4052 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004053 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00004054 }
4055
4056 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004057 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004058 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4059}
4060
Dan Gohman21cea8a2010-04-17 15:26:15 +00004061SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004062 const ARMBaseRegisterInfo &ARI =
4063 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4064 MachineFunction &MF = DAG.getMachineFunction();
4065 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004066 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004067
Owen Anderson53aa7a92009-08-10 22:56:29 +00004068 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004069 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004070 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004071 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004072 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4073 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004074 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
4075 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004076 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004077 return FrameAddr;
4078}
4079
Renato Golinc7aea402014-05-06 16:51:25 +00004080// FIXME? Maybe this could be a TableGen attribute on some registers and
4081// this table could be generated automatically from RegInfo.
Hal Finkelf0e086a2014-05-11 19:29:07 +00004082unsigned ARMTargetLowering::getRegisterByName(const char* RegName,
4083 EVT VT) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004084 unsigned Reg = StringSwitch<unsigned>(RegName)
4085 .Case("sp", ARM::SP)
4086 .Default(0);
4087 if (Reg)
4088 return Reg;
Diego Novillobfecc062015-05-26 17:45:38 +00004089 report_fatal_error("Invalid register name global variable");
Renato Golinc7aea402014-05-06 16:51:25 +00004090}
4091
Wesley Peck527da1b2010-11-23 03:31:01 +00004092/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004093/// expand a bit convert where either the source or destination type is i64 to
4094/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4095/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4096/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004097static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004098 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004099 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004100 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004101
Bob Wilson59b70ea2010-04-17 05:30:19 +00004102 // This function is only supposed to be called for i64 types, either as the
4103 // source or destination of the bit convert.
4104 EVT SrcVT = Op.getValueType();
4105 EVT DstVT = N->getValueType(0);
4106 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004107 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004108
Bob Wilson59b70ea2010-04-17 05:30:19 +00004109 // Turn i64->f64 into VMOVDRR.
4110 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00004111 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004112 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004113 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004114 DAG.getConstant(1, dl, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004115 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004116 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004117 }
Bob Wilson7117a912009-03-20 22:42:55 +00004118
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004119 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004120 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004121 SDValue Cvt;
Christian Pirker6692e7c2014-05-14 16:59:44 +00004122 if (TLI.isBigEndian() && SrcVT.isVector() &&
4123 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004124 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4125 DAG.getVTList(MVT::i32, MVT::i32),
4126 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4127 else
4128 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4129 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004130 // Merge the pieces into a single i64 value.
4131 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4132 }
Bob Wilson7117a912009-03-20 22:42:55 +00004133
Bob Wilson59b70ea2010-04-17 05:30:19 +00004134 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004135}
4136
Bob Wilson2e076c42009-06-22 23:27:02 +00004137/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004138/// Zero vectors are used to represent vector negation and in those cases
4139/// will be implemented with the NEON VNEG instruction. However, VNEG does
4140/// not support i64 elements, so sometimes the zero vectors will need to be
4141/// explicitly constructed. Regardless, use a canonical VMOV to create the
4142/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004143static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004144 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004145 // The canonical modified immediate encoding of a zero vector is....0!
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004146 SDValue EncodedVal = DAG.getTargetConstant(0, dl, MVT::i32);
Bob Wilsona3f19012010-07-13 21:16:48 +00004147 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4148 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004149 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004150}
4151
Jim Grosbach624fcb22009-10-31 21:00:56 +00004152/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4153/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004154SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4155 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004156 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4157 EVT VT = Op.getValueType();
4158 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004159 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004160 SDValue ShOpLo = Op.getOperand(0);
4161 SDValue ShOpHi = Op.getOperand(1);
4162 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004163 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004164 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004165
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004166 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4167
Jim Grosbach624fcb22009-10-31 21:00:56 +00004168 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004169 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004170 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4171 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004172 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach624fcb22009-10-31 21:00:56 +00004173 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4174 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004175 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004176
4177 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004178 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4179 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004180 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004181 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004182 CCR, Cmp);
4183
4184 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004185 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004186}
4187
Jim Grosbach5d994042009-10-31 19:38:01 +00004188/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4189/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004190SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4191 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004192 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4193 EVT VT = Op.getValueType();
4194 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004195 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004196 SDValue ShOpLo = Op.getOperand(0);
4197 SDValue ShOpHi = Op.getOperand(1);
4198 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004199 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004200
4201 assert(Op.getOpcode() == ISD::SHL_PARTS);
4202 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004203 DAG.getConstant(VTBits, dl, MVT::i32), ShAmt);
Jim Grosbach5d994042009-10-31 19:38:01 +00004204 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4205 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004206 DAG.getConstant(VTBits, dl, MVT::i32));
Jim Grosbach5d994042009-10-31 19:38:01 +00004207 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4208 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4209
4210 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4211 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004212 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, dl, MVT::i32),
4213 ISD::SETGE, ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004214 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004215 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004216 CCR, Cmp);
4217
4218 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004219 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004220}
4221
Jim Grosbach535d3b42010-09-08 03:54:02 +00004222SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004223 SelectionDAG &DAG) const {
4224 // The rounding mode is in bits 23:22 of the FPSCR.
4225 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4226 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4227 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004228 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004229 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004230 DAG.getConstant(Intrinsic::arm_get_fpscr, dl,
Nate Begemanb69b1822010-08-03 21:31:55 +00004231 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004232 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004233 DAG.getConstant(1U << 22, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004234 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004235 DAG.getConstant(22, dl, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004236 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004237 DAG.getConstant(3, dl, MVT::i32));
Nate Begemanb69b1822010-08-03 21:31:55 +00004238}
4239
Jim Grosbach8546ec92010-01-18 19:58:49 +00004240static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4241 const ARMSubtarget *ST) {
4242 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004243 SDLoc dl(N);
Jim Grosbach8546ec92010-01-18 19:58:49 +00004244
4245 if (!ST->hasV6T2Ops())
4246 return SDValue();
4247
4248 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
4249 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4250}
4251
Evan Chengb4eae132012-12-04 22:41:50 +00004252/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4253/// for each 16-bit element from operand, repeated. The basic idea is to
4254/// leverage vcnt to get the 8-bit counts, gather and add the results.
4255///
4256/// Trace for v4i16:
4257/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4258/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4259/// 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 +00004260/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004261/// [b0 b1 b2 b3 b4 b5 b6 b7]
4262/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4263/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4264/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4265static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4266 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004267 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004268
4269 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4270 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4271 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4272 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4273 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4274 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4275}
4276
4277/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4278/// bit-count for each 16-bit element from the operand. We need slightly
4279/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4280/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004281///
Evan Chengb4eae132012-12-04 22:41:50 +00004282/// Trace for v4i16:
4283/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4284/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4285/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4286/// v4i16:Extracted = [k0 k1 k2 k3 ]
4287static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4288 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004289 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004290
4291 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4292 if (VT.is64BitVector()) {
4293 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4294 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004295 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004296 } else {
4297 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004298 BitCounts, DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004299 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4300 }
4301}
4302
4303/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4304/// bit-count for each 32-bit element from the operand. The idea here is
4305/// to split the vector into 16-bit elements, leverage the 16-bit count
4306/// routine, and then combine the results.
4307///
4308/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4309/// input = [v0 v1 ] (vi: 32-bit elements)
4310/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4311/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004312/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004313/// [k0 k1 k2 k3 ]
4314/// N1 =+[k1 k0 k3 k2 ]
4315/// [k0 k2 k1 k3 ]
4316/// N2 =+[k1 k3 k0 k2 ]
4317/// [k0 k2 k1 k3 ]
4318/// Extended =+[k1 k3 k0 k2 ]
4319/// [k0 k2 ]
4320/// Extracted=+[k1 k3 ]
4321///
4322static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4323 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004324 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004325
4326 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4327
4328 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4329 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4330 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4331 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4332 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4333
4334 if (VT.is64BitVector()) {
4335 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4336 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004337 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004338 } else {
4339 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004340 DAG.getIntPtrConstant(0, DL));
Evan Chengb4eae132012-12-04 22:41:50 +00004341 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4342 }
4343}
4344
4345static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4346 const ARMSubtarget *ST) {
4347 EVT VT = N->getValueType(0);
4348
4349 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004350 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4351 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004352 "Unexpected type for custom ctpop lowering");
4353
4354 if (VT.getVectorElementType() == MVT::i32)
4355 return lowerCTPOP32BitElements(N, DAG);
4356 else
4357 return lowerCTPOP16BitElements(N, DAG);
4358}
4359
Bob Wilson2e076c42009-06-22 23:27:02 +00004360static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4361 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004362 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004363 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004364
Bob Wilson7d471332010-11-18 21:16:28 +00004365 if (!VT.isVector())
4366 return SDValue();
4367
Bob Wilson2e076c42009-06-22 23:27:02 +00004368 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004369 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004370
Bob Wilson7d471332010-11-18 21:16:28 +00004371 // Left shifts translate directly to the vshiftu intrinsic.
4372 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004373 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004374 DAG.getConstant(Intrinsic::arm_neon_vshiftu, dl,
4375 MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004376 N->getOperand(0), N->getOperand(1));
4377
4378 assert((N->getOpcode() == ISD::SRA ||
4379 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4380
4381 // NEON uses the same intrinsics for both left and right shifts. For
4382 // right shifts, the shift amounts are negative, so negate the vector of
4383 // shift amounts.
4384 EVT ShiftVT = N->getOperand(1).getValueType();
4385 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4386 getZeroVector(ShiftVT, DAG, dl),
4387 N->getOperand(1));
4388 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4389 Intrinsic::arm_neon_vshifts :
4390 Intrinsic::arm_neon_vshiftu);
4391 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004392 DAG.getConstant(vshiftInt, dl, MVT::i32),
Bob Wilson7d471332010-11-18 21:16:28 +00004393 N->getOperand(0), NegatedCount);
4394}
4395
4396static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4397 const ARMSubtarget *ST) {
4398 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004399 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004400
Eli Friedman682d8c12009-08-22 03:13:10 +00004401 // We can get here for a node like i32 = ISD::SHL i32, i64
4402 if (VT != MVT::i64)
4403 return SDValue();
4404
4405 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004406 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004407
Chris Lattnerf81d5882007-11-24 07:07:01 +00004408 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4409 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004410 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004411 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004412
Chris Lattnerf81d5882007-11-24 07:07:01 +00004413 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004414 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004415
Chris Lattnerf81d5882007-11-24 07:07:01 +00004416 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004417 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004418 DAG.getConstant(0, dl, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004419 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004420 DAG.getConstant(1, dl, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004421
Chris Lattnerf81d5882007-11-24 07:07:01 +00004422 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4423 // captures the result into a carry flag.
4424 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004425 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004426
Chris Lattnerf81d5882007-11-24 07:07:01 +00004427 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004428 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004429
Chris Lattnerf81d5882007-11-24 07:07:01 +00004430 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004431 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004432}
4433
Bob Wilson2e076c42009-06-22 23:27:02 +00004434static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4435 SDValue TmpOp0, TmpOp1;
4436 bool Invert = false;
4437 bool Swap = false;
4438 unsigned Opc = 0;
4439
4440 SDValue Op0 = Op.getOperand(0);
4441 SDValue Op1 = Op.getOperand(1);
4442 SDValue CC = Op.getOperand(2);
Tim Northover45aa89c2015-02-08 00:50:47 +00004443 EVT CmpVT = Op0.getValueType().changeVectorElementTypeToInteger();
Owen Anderson53aa7a92009-08-10 22:56:29 +00004444 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004445 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004446 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004447
Oliver Stannard51b1d462014-08-21 12:50:31 +00004448 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004449 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004450 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004451 case ISD::SETUNE:
4452 case ISD::SETNE: Invert = true; // Fallthrough
4453 case ISD::SETOEQ:
4454 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4455 case ISD::SETOLT:
4456 case ISD::SETLT: Swap = true; // Fallthrough
4457 case ISD::SETOGT:
4458 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4459 case ISD::SETOLE:
4460 case ISD::SETLE: Swap = true; // Fallthrough
4461 case ISD::SETOGE:
4462 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4463 case ISD::SETUGE: Swap = true; // Fallthrough
4464 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4465 case ISD::SETUGT: Swap = true; // Fallthrough
4466 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4467 case ISD::SETUEQ: Invert = true; // Fallthrough
4468 case ISD::SETONE:
4469 // Expand this to (OLT | OGT).
4470 TmpOp0 = Op0;
4471 TmpOp1 = Op1;
4472 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004473 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4474 Op1 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004475 break;
4476 case ISD::SETUO: Invert = true; // Fallthrough
4477 case ISD::SETO:
4478 // Expand this to (OLT | OGE).
4479 TmpOp0 = Op0;
4480 TmpOp1 = Op1;
4481 Opc = ISD::OR;
Tim Northover45aa89c2015-02-08 00:50:47 +00004482 Op0 = DAG.getNode(ARMISD::VCGT, dl, CmpVT, TmpOp1, TmpOp0);
4483 Op1 = DAG.getNode(ARMISD::VCGE, dl, CmpVT, TmpOp0, TmpOp1);
Bob Wilson2e076c42009-06-22 23:27:02 +00004484 break;
4485 }
4486 } else {
4487 // Integer comparisons.
4488 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004489 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004490 case ISD::SETNE: Invert = true;
4491 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4492 case ISD::SETLT: Swap = true;
4493 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4494 case ISD::SETLE: Swap = true;
4495 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4496 case ISD::SETULT: Swap = true;
4497 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4498 case ISD::SETULE: Swap = true;
4499 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4500 }
4501
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004502 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004503 if (Opc == ARMISD::VCEQ) {
4504
4505 SDValue AndOp;
4506 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4507 AndOp = Op0;
4508 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4509 AndOp = Op1;
4510
4511 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004512 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004513 AndOp = AndOp.getOperand(0);
4514
4515 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4516 Opc = ARMISD::VTST;
Tim Northover45aa89c2015-02-08 00:50:47 +00004517 Op0 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(0));
4518 Op1 = DAG.getNode(ISD::BITCAST, dl, CmpVT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004519 Invert = !Invert;
4520 }
4521 }
4522 }
4523
4524 if (Swap)
4525 std::swap(Op0, Op1);
4526
Owen Andersonc7baee32010-11-08 23:21:22 +00004527 // If one of the operands is a constant vector zero, attempt to fold the
4528 // comparison to a specialized compare-against-zero form.
4529 SDValue SingleOp;
4530 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4531 SingleOp = Op0;
4532 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4533 if (Opc == ARMISD::VCGE)
4534 Opc = ARMISD::VCLEZ;
4535 else if (Opc == ARMISD::VCGT)
4536 Opc = ARMISD::VCLTZ;
4537 SingleOp = Op1;
4538 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004539
Owen Andersonc7baee32010-11-08 23:21:22 +00004540 SDValue Result;
4541 if (SingleOp.getNode()) {
4542 switch (Opc) {
4543 case ARMISD::VCEQ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004544 Result = DAG.getNode(ARMISD::VCEQZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004545 case ARMISD::VCGE:
Tim Northover45aa89c2015-02-08 00:50:47 +00004546 Result = DAG.getNode(ARMISD::VCGEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004547 case ARMISD::VCLEZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004548 Result = DAG.getNode(ARMISD::VCLEZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004549 case ARMISD::VCGT:
Tim Northover45aa89c2015-02-08 00:50:47 +00004550 Result = DAG.getNode(ARMISD::VCGTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004551 case ARMISD::VCLTZ:
Tim Northover45aa89c2015-02-08 00:50:47 +00004552 Result = DAG.getNode(ARMISD::VCLTZ, dl, CmpVT, SingleOp); break;
Owen Andersonc7baee32010-11-08 23:21:22 +00004553 default:
Tim Northover45aa89c2015-02-08 00:50:47 +00004554 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004555 }
4556 } else {
Tim Northover45aa89c2015-02-08 00:50:47 +00004557 Result = DAG.getNode(Opc, dl, CmpVT, Op0, Op1);
Owen Andersonc7baee32010-11-08 23:21:22 +00004558 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004559
Tim Northover45aa89c2015-02-08 00:50:47 +00004560 Result = DAG.getSExtOrTrunc(Result, dl, VT);
4561
Bob Wilson2e076c42009-06-22 23:27:02 +00004562 if (Invert)
4563 Result = DAG.getNOT(dl, Result, VT);
4564
4565 return Result;
4566}
4567
Bob Wilson5b2b5042010-06-14 22:19:57 +00004568/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4569/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004570/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004571static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4572 unsigned SplatBitSize, SelectionDAG &DAG,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004573 SDLoc dl, EVT &VT, bool is128Bits,
4574 NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004575 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004576
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004577 // SplatBitSize is set to the smallest size that splats the vector, so a
4578 // zero vector will always have SplatBitSize == 8. However, NEON modified
4579 // immediate instructions others than VMOV do not support the 8-bit encoding
4580 // of a zero vector, and the default encoding of zero is supposed to be the
4581 // 32-bit version.
4582 if (SplatBits == 0)
4583 SplatBitSize = 32;
4584
Bob Wilson2e076c42009-06-22 23:27:02 +00004585 switch (SplatBitSize) {
4586 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004587 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004588 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004589 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004590 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004591 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004592 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004593 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004594 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004595
4596 case 16:
4597 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004598 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004599 if ((SplatBits & ~0xff) == 0) {
4600 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004601 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004602 Imm = SplatBits;
4603 break;
4604 }
4605 if ((SplatBits & ~0xff00) == 0) {
4606 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004607 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004608 Imm = SplatBits >> 8;
4609 break;
4610 }
4611 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004612
4613 case 32:
4614 // NEON's 32-bit VMOV supports splat values where:
4615 // * only one byte is nonzero, or
4616 // * the least significant byte is 0xff and the second byte is nonzero, or
4617 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004618 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004619 if ((SplatBits & ~0xff) == 0) {
4620 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004621 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004622 Imm = SplatBits;
4623 break;
4624 }
4625 if ((SplatBits & ~0xff00) == 0) {
4626 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004627 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004628 Imm = SplatBits >> 8;
4629 break;
4630 }
4631 if ((SplatBits & ~0xff0000) == 0) {
4632 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004633 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004634 Imm = SplatBits >> 16;
4635 break;
4636 }
4637 if ((SplatBits & ~0xff000000) == 0) {
4638 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004639 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004640 Imm = SplatBits >> 24;
4641 break;
4642 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004643
Owen Andersona4076922010-11-05 21:57:54 +00004644 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4645 if (type == OtherModImm) return SDValue();
4646
Bob Wilson2e076c42009-06-22 23:27:02 +00004647 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004648 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4649 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004650 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004651 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004652 break;
4653 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004654
4655 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004656 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4657 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004658 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004659 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004660 break;
4661 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004662
4663 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4664 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4665 // VMOV.I32. A (very) minor optimization would be to replicate the value
4666 // and fall through here to test for a valid 64-bit splat. But, then the
4667 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004668 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004669
4670 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004671 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004672 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004673 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004674 uint64_t BitMask = 0xff;
4675 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004676 unsigned ImmMask = 1;
4677 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004678 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004679 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004680 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004681 Imm |= ImmMask;
4682 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004683 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004684 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004685 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004686 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004687 }
Christian Pirker6f81e752014-06-23 18:05:53 +00004688
4689 if (DAG.getTargetLoweringInfo().isBigEndian())
4690 // swap higher and lower 32 bit word
4691 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
4692
Bob Wilson6eae5202010-06-11 21:34:50 +00004693 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004694 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004695 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004696 break;
4697 }
4698
Bob Wilson6eae5202010-06-11 21:34:50 +00004699 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004700 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004701 }
4702
Bob Wilsona3f19012010-07-13 21:16:48 +00004703 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004704 return DAG.getTargetConstant(EncodedVal, dl, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004705}
4706
Lang Hames591cdaf2012-03-29 21:56:11 +00004707SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4708 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004709 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004710 return SDValue();
4711
Tim Northoverf79c3a52013-08-20 08:57:11 +00004712 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004713 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004714
Oliver Stannard51b1d462014-08-21 12:50:31 +00004715 // Use the default (constant pool) lowering for double constants when we have
4716 // an SP-only FPU
4717 if (IsDouble && Subtarget->isFPOnlySP())
4718 return SDValue();
4719
Lang Hames591cdaf2012-03-29 21:56:11 +00004720 // Try splatting with a VMOV.f32...
4721 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004722 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4723
Lang Hames591cdaf2012-03-29 21:56:11 +00004724 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004725 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4726 // We have code in place to select a valid ConstantFP already, no need to
4727 // do any mangling.
4728 return Op;
4729 }
4730
4731 // It's a float and we are trying to use NEON operations where
4732 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004733 SDLoc DL(Op);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004734 SDValue NewVal = DAG.getTargetConstant(ImmVal, DL, MVT::i32);
Lang Hames591cdaf2012-03-29 21:56:11 +00004735 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4736 NewVal);
4737 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004738 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004739 }
4740
Tim Northoverf79c3a52013-08-20 08:57:11 +00004741 // The rest of our options are NEON only, make sure that's allowed before
4742 // proceeding..
4743 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4744 return SDValue();
4745
Lang Hames591cdaf2012-03-29 21:56:11 +00004746 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004747 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4748
4749 // It wouldn't really be worth bothering for doubles except for one very
4750 // important value, which does happen to match: 0.0. So make sure we don't do
4751 // anything stupid.
4752 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4753 return SDValue();
4754
4755 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004756 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op),
4757 VMovVT, false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004758 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004759 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004760 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4761 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004762 if (IsDouble)
4763 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4764
4765 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004766 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4767 VecConstant);
4768 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004769 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004770 }
4771
4772 // Finally, try a VMVN.i32
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004773 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, SDLoc(Op), VMovVT,
Tim Northoverf79c3a52013-08-20 08:57:11 +00004774 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004775 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004776 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004777 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004778
4779 if (IsDouble)
4780 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4781
4782 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004783 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4784 VecConstant);
4785 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004786 DAG.getConstant(0, DL, MVT::i32));
Lang Hames591cdaf2012-03-29 21:56:11 +00004787 }
4788
4789 return SDValue();
4790}
4791
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004792// check if an VEXT instruction can handle the shuffle mask when the
4793// vector sources of the shuffle are the same.
4794static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4795 unsigned NumElts = VT.getVectorNumElements();
4796
4797 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4798 if (M[0] < 0)
4799 return false;
4800
4801 Imm = M[0];
4802
4803 // If this is a VEXT shuffle, the immediate value is the index of the first
4804 // element. The other shuffle indices must be the successive elements after
4805 // the first one.
4806 unsigned ExpectedElt = Imm;
4807 for (unsigned i = 1; i < NumElts; ++i) {
4808 // Increment the expected index. If it wraps around, just follow it
4809 // back to index zero and keep going.
4810 ++ExpectedElt;
4811 if (ExpectedElt == NumElts)
4812 ExpectedElt = 0;
4813
4814 if (M[i] < 0) continue; // ignore UNDEF indices
4815 if (ExpectedElt != static_cast<unsigned>(M[i]))
4816 return false;
4817 }
4818
4819 return true;
4820}
4821
Lang Hames591cdaf2012-03-29 21:56:11 +00004822
Benjamin Kramer339ced42012-01-15 13:16:05 +00004823static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004824 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004825 unsigned NumElts = VT.getVectorNumElements();
4826 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004827
4828 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4829 if (M[0] < 0)
4830 return false;
4831
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004832 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004833
4834 // If this is a VEXT shuffle, the immediate value is the index of the first
4835 // element. The other shuffle indices must be the successive elements after
4836 // the first one.
4837 unsigned ExpectedElt = Imm;
4838 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004839 // Increment the expected index. If it wraps around, it may still be
4840 // a VEXT but the source vectors must be swapped.
4841 ExpectedElt += 1;
4842 if (ExpectedElt == NumElts * 2) {
4843 ExpectedElt = 0;
4844 ReverseVEXT = true;
4845 }
4846
Bob Wilson411dfad2010-08-17 05:54:34 +00004847 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004848 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004849 return false;
4850 }
4851
4852 // Adjust the index value if the source operands will be swapped.
4853 if (ReverseVEXT)
4854 Imm -= NumElts;
4855
Bob Wilson32cd8552009-08-19 17:03:43 +00004856 return true;
4857}
4858
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004859/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4860/// instruction with the specified blocksize. (The order of the elements
4861/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00004862static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004863 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4864 "Only possible block sizes for VREV are: 16, 32, 64");
4865
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004866 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00004867 if (EltSz == 64)
4868 return false;
4869
4870 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004871 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00004872 // If the first shuffle index is UNDEF, be optimistic.
4873 if (M[0] < 0)
4874 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004875
4876 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4877 return false;
4878
4879 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004880 if (M[i] < 0) continue; // ignore UNDEF indices
4881 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004882 return false;
4883 }
4884
4885 return true;
4886}
4887
Benjamin Kramer339ced42012-01-15 13:16:05 +00004888static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00004889 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4890 // range, then 0 is placed into the resulting vector. So pretty much any mask
4891 // of 8 elements can work here.
4892 return VT == MVT::v8i8 && M.size() == 8;
4893}
4894
Benjamin Kramer339ced42012-01-15 13:16:05 +00004895static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004896 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4897 if (EltSz == 64)
4898 return false;
4899
Bob Wilsona7062312009-08-21 20:54:19 +00004900 unsigned NumElts = VT.getVectorNumElements();
4901 WhichResult = (M[0] == 0 ? 0 : 1);
4902 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004903 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4904 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsona7062312009-08-21 20:54:19 +00004905 return false;
4906 }
4907 return true;
4908}
4909
Bob Wilson0bbd3072009-12-03 06:40:55 +00004910/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4911/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4912/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004913static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004914 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4915 if (EltSz == 64)
4916 return false;
4917
4918 unsigned NumElts = VT.getVectorNumElements();
4919 WhichResult = (M[0] == 0 ? 0 : 1);
4920 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004921 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4922 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004923 return false;
4924 }
4925 return true;
4926}
4927
Benjamin Kramer339ced42012-01-15 13:16:05 +00004928static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004929 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4930 if (EltSz == 64)
4931 return false;
4932
Bob Wilsona7062312009-08-21 20:54:19 +00004933 unsigned NumElts = VT.getVectorNumElements();
4934 WhichResult = (M[0] == 0 ? 0 : 1);
4935 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004936 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsona7062312009-08-21 20:54:19 +00004937 if ((unsigned) M[i] != 2 * i + WhichResult)
4938 return false;
4939 }
4940
4941 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004942 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004943 return false;
4944
4945 return true;
4946}
4947
Bob Wilson0bbd3072009-12-03 06:40:55 +00004948/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4949/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4950/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00004951static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004952 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4953 if (EltSz == 64)
4954 return false;
4955
4956 unsigned Half = VT.getVectorNumElements() / 2;
4957 WhichResult = (M[0] == 0 ? 0 : 1);
4958 for (unsigned j = 0; j != 2; ++j) {
4959 unsigned Idx = WhichResult;
4960 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004961 int MIdx = M[i + j * Half];
4962 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson0bbd3072009-12-03 06:40:55 +00004963 return false;
4964 Idx += 2;
4965 }
4966 }
4967
4968 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4969 if (VT.is64BitVector() && EltSz == 32)
4970 return false;
4971
4972 return true;
4973}
4974
Benjamin Kramer339ced42012-01-15 13:16:05 +00004975static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004976 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4977 if (EltSz == 64)
4978 return false;
4979
Bob Wilsona7062312009-08-21 20:54:19 +00004980 unsigned NumElts = VT.getVectorNumElements();
4981 WhichResult = (M[0] == 0 ? 0 : 1);
4982 unsigned Idx = WhichResult * NumElts / 2;
4983 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004984 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4985 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsona7062312009-08-21 20:54:19 +00004986 return false;
4987 Idx += 1;
4988 }
4989
4990 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004991 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004992 return false;
4993
4994 return true;
4995}
4996
Bob Wilson0bbd3072009-12-03 06:40:55 +00004997/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4998/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4999/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005000static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005001 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5002 if (EltSz == 64)
5003 return false;
5004
5005 unsigned NumElts = VT.getVectorNumElements();
5006 WhichResult = (M[0] == 0 ? 0 : 1);
5007 unsigned Idx = WhichResult * NumElts / 2;
5008 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005009 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
5010 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson0bbd3072009-12-03 06:40:55 +00005011 return false;
5012 Idx += 1;
5013 }
5014
5015 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5016 if (VT.is64BitVector() && EltSz == 32)
5017 return false;
5018
5019 return true;
5020}
5021
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005022/// \return true if this is a reverse operation on an vector.
5023static bool isReverseMask(ArrayRef<int> M, EVT VT) {
5024 unsigned NumElts = VT.getVectorNumElements();
5025 // Make sure the mask has the right size.
5026 if (NumElts != M.size())
5027 return false;
5028
5029 // Look for <15, ..., 3, -1, 1, 0>.
5030 for (unsigned i = 0; i != NumElts; ++i)
5031 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
5032 return false;
5033
5034 return true;
5035}
5036
Dale Johannesen2bff5052010-07-29 20:10:08 +00005037// If N is an integer constant that can be moved into a register in one
5038// instruction, return an SDValue of such a constant (will become a MOV
5039// instruction). Otherwise return null.
5040static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005041 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00005042 uint64_t Val;
5043 if (!isa<ConstantSDNode>(N))
5044 return SDValue();
5045 Val = cast<ConstantSDNode>(N)->getZExtValue();
5046
5047 if (ST->isThumb1Only()) {
5048 if (Val <= 255 || ~Val <= 255)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005049 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005050 } else {
5051 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005052 return DAG.getConstant(Val, dl, MVT::i32);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005053 }
5054 return SDValue();
5055}
5056
Bob Wilson2e076c42009-06-22 23:27:02 +00005057// If this is a case we can't handle, return null and let the default
5058// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005059SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
5060 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00005061 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005062 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005063 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005064
5065 APInt SplatBits, SplatUndef;
5066 unsigned SplatBitSize;
5067 bool HasAnyUndefs;
5068 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005069 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00005070 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00005071 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00005072 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00005073 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005074 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005075 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00005076 if (Val.getNode()) {
5077 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005078 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00005079 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00005080
5081 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00005082 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005083 Val = isNEONModifiedImm(NegatedImm,
5084 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005085 DAG, dl, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005086 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005087 if (Val.getNode()) {
5088 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005089 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005090 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005091
5092 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00005093 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00005094 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005095 if (ImmVal != -1) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005096 SDValue Val = DAG.getTargetConstant(ImmVal, dl, MVT::i32);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005097 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
5098 }
5099 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005100 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00005101 }
5102
Bob Wilson91fdf682010-05-22 00:23:12 +00005103 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00005104 //
5105 // As an optimisation, even if more than one value is used it may be more
5106 // profitable to splat with one value then change some lanes.
5107 //
5108 // Heuristically we decide to do this if the vector has a "dominant" value,
5109 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00005110 unsigned NumElts = VT.getVectorNumElements();
5111 bool isOnlyLowElement = true;
5112 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005113 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00005114 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005115
5116 // Map of the number of times a particular SDValue appears in the
5117 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00005118 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00005119 SDValue Value;
5120 for (unsigned i = 0; i < NumElts; ++i) {
5121 SDValue V = Op.getOperand(i);
5122 if (V.getOpcode() == ISD::UNDEF)
5123 continue;
5124 if (i > 0)
5125 isOnlyLowElement = false;
5126 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5127 isConstant = false;
5128
James Molloy49bdbce2012-09-06 09:55:02 +00005129 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005130 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005131
James Molloy49bdbce2012-09-06 09:55:02 +00005132 // Is this value dominant? (takes up more than half of the lanes)
5133 if (++Count > (NumElts / 2)) {
5134 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005135 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005136 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005137 }
James Molloy49bdbce2012-09-06 09:55:02 +00005138 if (ValueCounts.size() != 1)
5139 usesOnlyOneValue = false;
5140 if (!Value.getNode() && ValueCounts.size() > 0)
5141 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005142
James Molloy49bdbce2012-09-06 09:55:02 +00005143 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005144 return DAG.getUNDEF(VT);
5145
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005146 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5147 // Keep going if we are hitting this case.
5148 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005149 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5150
Dale Johannesen2bff5052010-07-29 20:10:08 +00005151 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5152
Dale Johannesen710a2d92010-10-19 20:00:17 +00005153 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5154 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005155 if (hasDominantValue && EltSize <= 32) {
5156 if (!isConstant) {
5157 SDValue N;
5158
5159 // If we are VDUPing a value that comes directly from a vector, that will
5160 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005161 // just use VDUPLANE. We can only do this if the lane being extracted
5162 // is at a constant index, as the VDUP from lane instructions only have
5163 // constant-index forms.
5164 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5165 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005166 // We need to create a new undef vector to use for the VDUPLANE if the
5167 // size of the vector from which we get the value is different than the
5168 // size of the vector that we need to create. We will insert the element
5169 // such that the register coalescer will remove unnecessary copies.
5170 if (VT != Value->getOperand(0).getValueType()) {
5171 ConstantSDNode *constIndex;
5172 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
5173 assert(constIndex && "The index is not a constant!");
5174 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5175 VT.getVectorNumElements();
5176 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5177 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005178 Value, DAG.getConstant(index, dl, MVT::i32)),
5179 DAG.getConstant(index, dl, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005180 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005181 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005182 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005183 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005184 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5185
5186 if (!usesOnlyOneValue) {
5187 // The dominant value was splatted as 'N', but we now have to insert
5188 // all differing elements.
5189 for (unsigned I = 0; I < NumElts; ++I) {
5190 if (Op.getOperand(I) == Value)
5191 continue;
5192 SmallVector<SDValue, 3> Ops;
5193 Ops.push_back(N);
5194 Ops.push_back(Op.getOperand(I));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005195 Ops.push_back(DAG.getConstant(I, dl, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005196 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005197 }
5198 }
5199 return N;
5200 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005201 if (VT.getVectorElementType().isFloatingPoint()) {
5202 SmallVector<SDValue, 8> Ops;
5203 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005204 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005205 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005206 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005207 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005208 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5209 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005210 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005211 }
James Molloy49bdbce2012-09-06 09:55:02 +00005212 if (usesOnlyOneValue) {
5213 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5214 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005215 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005216 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005217 }
5218
5219 // If all elements are constants and the case above didn't get hit, fall back
5220 // to the default expansion, which will generate a load from the constant
5221 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005222 if (isConstant)
5223 return SDValue();
5224
Bob Wilson6f2b8962011-01-07 21:37:30 +00005225 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5226 if (NumElts >= 4) {
5227 SDValue shuffle = ReconstructShuffle(Op, DAG);
5228 if (shuffle != SDValue())
5229 return shuffle;
5230 }
5231
Bob Wilson91fdf682010-05-22 00:23:12 +00005232 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005233 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5234 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005235 if (EltSize >= 32) {
5236 // Do the expansion with floating-point types, since that is what the VFP
5237 // registers are defined to use, and since i64 is not legal.
5238 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5239 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005240 SmallVector<SDValue, 8> Ops;
5241 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005242 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005243 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005244 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005245 }
5246
Jim Grosbach24e102a2013-07-08 18:18:52 +00005247 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5248 // know the default expansion would otherwise fall back on something even
5249 // worse. For a vector with one or two non-undef values, that's
5250 // scalar_to_vector for the elements followed by a shuffle (provided the
5251 // shuffle is valid for the target) and materialization element by element
5252 // on the stack followed by a load for everything else.
5253 if (!isConstant && !usesOnlyOneValue) {
5254 SDValue Vec = DAG.getUNDEF(VT);
5255 for (unsigned i = 0 ; i < NumElts; ++i) {
5256 SDValue V = Op.getOperand(i);
5257 if (V.getOpcode() == ISD::UNDEF)
5258 continue;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005259 SDValue LaneIdx = DAG.getConstant(i, dl, MVT::i32);
Jim Grosbach24e102a2013-07-08 18:18:52 +00005260 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5261 }
5262 return Vec;
5263 }
5264
Bob Wilson2e076c42009-06-22 23:27:02 +00005265 return SDValue();
5266}
5267
Bob Wilson6f2b8962011-01-07 21:37:30 +00005268// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005269// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005270SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5271 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005272 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005273 EVT VT = Op.getValueType();
5274 unsigned NumElts = VT.getVectorNumElements();
5275
5276 SmallVector<SDValue, 2> SourceVecs;
5277 SmallVector<unsigned, 2> MinElts;
5278 SmallVector<unsigned, 2> MaxElts;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005279
Bob Wilson6f2b8962011-01-07 21:37:30 +00005280 for (unsigned i = 0; i < NumElts; ++i) {
5281 SDValue V = Op.getOperand(i);
5282 if (V.getOpcode() == ISD::UNDEF)
5283 continue;
5284 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5285 // A shuffle can only come from building a vector from various
5286 // elements of other vectors.
5287 return SDValue();
Eli Friedman74d1da52011-10-14 23:58:49 +00005288 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
5289 VT.getVectorElementType()) {
5290 // This code doesn't know how to handle shuffles where the vector
5291 // element types do not match (this happens because type legalization
5292 // promotes the return type of EXTRACT_VECTOR_ELT).
5293 // FIXME: It might be appropriate to extend this code to handle
5294 // mismatched types.
5295 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005296 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005297
Bob Wilson6f2b8962011-01-07 21:37:30 +00005298 // Record this extraction against the appropriate vector if possible...
5299 SDValue SourceVec = V.getOperand(0);
Jim Grosbach6df755c2012-07-25 17:02:47 +00005300 // If the element number isn't a constant, we can't effectively
5301 // analyze what's going on.
5302 if (!isa<ConstantSDNode>(V.getOperand(1)))
5303 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005304 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5305 bool FoundSource = false;
5306 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
5307 if (SourceVecs[j] == SourceVec) {
5308 if (MinElts[j] > EltNo)
5309 MinElts[j] = EltNo;
5310 if (MaxElts[j] < EltNo)
5311 MaxElts[j] = EltNo;
5312 FoundSource = true;
5313 break;
5314 }
5315 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005316
Bob Wilson6f2b8962011-01-07 21:37:30 +00005317 // Or record a new source if not...
5318 if (!FoundSource) {
5319 SourceVecs.push_back(SourceVec);
5320 MinElts.push_back(EltNo);
5321 MaxElts.push_back(EltNo);
5322 }
5323 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005324
Bob Wilson6f2b8962011-01-07 21:37:30 +00005325 // Currently only do something sane when at most two source vectors
5326 // involved.
5327 if (SourceVecs.size() > 2)
5328 return SDValue();
5329
5330 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
5331 int VEXTOffsets[2] = {0, 0};
Andrew Trick5eb0a302011-01-19 02:26:13 +00005332
Bob Wilson6f2b8962011-01-07 21:37:30 +00005333 // This loop extracts the usage patterns of the source vectors
5334 // and prepares appropriate SDValues for a shuffle if possible.
5335 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
5336 if (SourceVecs[i].getValueType() == VT) {
5337 // No VEXT necessary
5338 ShuffleSrcs[i] = SourceVecs[i];
5339 VEXTOffsets[i] = 0;
5340 continue;
5341 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
5342 // It probably isn't worth padding out a smaller vector just to
5343 // break it down again in a shuffle.
5344 return SDValue();
5345 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005346
Bob Wilson6f2b8962011-01-07 21:37:30 +00005347 // Since only 64-bit and 128-bit vectors are legal on ARM and
5348 // we've eliminated the other cases...
Bob Wilson3fa9c062011-01-07 23:40:46 +00005349 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
5350 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick5eb0a302011-01-19 02:26:13 +00005351
Bob Wilson6f2b8962011-01-07 21:37:30 +00005352 if (MaxElts[i] - MinElts[i] >= NumElts) {
5353 // Span too large for a VEXT to cope
5354 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005355 }
5356
Bob Wilson6f2b8962011-01-07 21:37:30 +00005357 if (MinElts[i] >= NumElts) {
5358 // The extraction can just take the second half
5359 VEXTOffsets[i] = NumElts;
Eric Christopher2af95512011-01-14 23:50:53 +00005360 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5361 SourceVecs[i],
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005362 DAG.getIntPtrConstant(NumElts, dl));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005363 } else if (MaxElts[i] < NumElts) {
5364 // The extraction can just take the first half
5365 VEXTOffsets[i] = 0;
Eric Christopher2af95512011-01-14 23:50:53 +00005366 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5367 SourceVecs[i],
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005368 DAG.getIntPtrConstant(0, dl));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005369 } else {
5370 // An actual VEXT is needed
5371 VEXTOffsets[i] = MinElts[i];
Eric Christopher2af95512011-01-14 23:50:53 +00005372 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5373 SourceVecs[i],
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005374 DAG.getIntPtrConstant(0, dl));
Eric Christopher2af95512011-01-14 23:50:53 +00005375 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5376 SourceVecs[i],
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005377 DAG.getIntPtrConstant(NumElts, dl));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005378 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005379 DAG.getConstant(VEXTOffsets[i], dl,
5380 MVT::i32));
Bob Wilson6f2b8962011-01-07 21:37:30 +00005381 }
5382 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005383
Bob Wilson6f2b8962011-01-07 21:37:30 +00005384 SmallVector<int, 8> Mask;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005385
Bob Wilson6f2b8962011-01-07 21:37:30 +00005386 for (unsigned i = 0; i < NumElts; ++i) {
5387 SDValue Entry = Op.getOperand(i);
5388 if (Entry.getOpcode() == ISD::UNDEF) {
5389 Mask.push_back(-1);
5390 continue;
5391 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005392
Bob Wilson6f2b8962011-01-07 21:37:30 +00005393 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher2af95512011-01-14 23:50:53 +00005394 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
5395 .getOperand(1))->getSExtValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005396 if (ExtractVec == SourceVecs[0]) {
5397 Mask.push_back(ExtractElt - VEXTOffsets[0]);
5398 } else {
5399 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
5400 }
5401 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005402
Bob Wilson6f2b8962011-01-07 21:37:30 +00005403 // Final check before we try to produce nonsense...
5404 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher2af95512011-01-14 23:50:53 +00005405 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
5406 &Mask[0]);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005407
Bob Wilson6f2b8962011-01-07 21:37:30 +00005408 return SDValue();
5409}
5410
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005411/// isShuffleMaskLegal - Targets can use this to indicate that they only
5412/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5413/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5414/// are assumed to be legal.
5415bool
5416ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5417 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005418 if (VT.getVectorNumElements() == 4 &&
5419 (VT.is128BitVector() || VT.is64BitVector())) {
5420 unsigned PFIndexes[4];
5421 for (unsigned i = 0; i != 4; ++i) {
5422 if (M[i] < 0)
5423 PFIndexes[i] = 8;
5424 else
5425 PFIndexes[i] = M[i];
5426 }
5427
5428 // Compute the index in the perfect shuffle table.
5429 unsigned PFTableIndex =
5430 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5431 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5432 unsigned Cost = (PFEntry >> 30);
5433
5434 if (Cost <= 4)
5435 return true;
5436 }
5437
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005438 bool ReverseVEXT;
Bob Wilsona7062312009-08-21 20:54:19 +00005439 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005440
Bob Wilson846bd792010-06-07 23:53:38 +00005441 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5442 return (EltSize >= 32 ||
5443 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005444 isVREVMask(M, VT, 64) ||
5445 isVREVMask(M, VT, 32) ||
5446 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005447 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005448 isVTBLMask(M, VT) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005449 isVTRNMask(M, VT, WhichResult) ||
5450 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson0bbd3072009-12-03 06:40:55 +00005451 isVZIPMask(M, VT, WhichResult) ||
5452 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
5453 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005454 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
5455 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005456}
5457
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005458/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5459/// the specified operations to build the shuffle.
5460static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5461 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005462 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005463 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5464 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5465 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5466
5467 enum {
5468 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5469 OP_VREV,
5470 OP_VDUP0,
5471 OP_VDUP1,
5472 OP_VDUP2,
5473 OP_VDUP3,
5474 OP_VEXT1,
5475 OP_VEXT2,
5476 OP_VEXT3,
5477 OP_VUZPL, // VUZP, left result
5478 OP_VUZPR, // VUZP, right result
5479 OP_VZIPL, // VZIP, left result
5480 OP_VZIPR, // VZIP, right result
5481 OP_VTRNL, // VTRN, left result
5482 OP_VTRNR // VTRN, right result
5483 };
5484
5485 if (OpNum == OP_COPY) {
5486 if (LHSID == (1*9+2)*9+3) return LHS;
5487 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5488 return RHS;
5489 }
5490
5491 SDValue OpLHS, OpRHS;
5492 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5493 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5494 EVT VT = OpLHS.getValueType();
5495
5496 switch (OpNum) {
5497 default: llvm_unreachable("Unknown shuffle opcode!");
5498 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005499 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005500 if (VT.getVectorElementType() == MVT::i32 ||
5501 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005502 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5503 // vrev <4 x i16> -> VREV32
5504 if (VT.getVectorElementType() == MVT::i16)
5505 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5506 // vrev <4 x i8> -> VREV16
5507 assert(VT.getVectorElementType() == MVT::i8);
5508 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005509 case OP_VDUP0:
5510 case OP_VDUP1:
5511 case OP_VDUP2:
5512 case OP_VDUP3:
5513 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005514 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005515 case OP_VEXT1:
5516 case OP_VEXT2:
5517 case OP_VEXT3:
5518 return DAG.getNode(ARMISD::VEXT, dl, VT,
5519 OpLHS, OpRHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005520 DAG.getConstant(OpNum - OP_VEXT1 + 1, dl, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005521 case OP_VUZPL:
5522 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005523 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005524 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5525 case OP_VZIPL:
5526 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005527 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005528 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5529 case OP_VTRNL:
5530 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005531 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5532 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005533 }
5534}
5535
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005536static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005537 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005538 SelectionDAG &DAG) {
5539 // Check to see if we can use the VTBL instruction.
5540 SDValue V1 = Op.getOperand(0);
5541 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005542 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005543
5544 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005545 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005546 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005547 VTBLMask.push_back(DAG.getConstant(*I, DL, MVT::i32));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005548
5549 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5550 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005551 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005552
Owen Anderson77aa2662011-04-05 21:48:57 +00005553 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005554 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005555}
5556
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005557static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5558 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005559 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005560 SDValue OpLHS = Op.getOperand(0);
5561 EVT VT = OpLHS.getValueType();
5562
5563 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5564 "Expect an v8i16/v16i8 type");
5565 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5566 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5567 // extract the first 8 bytes into the top double word and the last 8 bytes
5568 // into the bottom double word. The v8i16 case is similar.
5569 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5570 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005571 DAG.getConstant(ExtractNum, DL, MVT::i32));
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005572}
5573
Bob Wilson2e076c42009-06-22 23:27:02 +00005574static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005575 SDValue V1 = Op.getOperand(0);
5576 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005577 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005578 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005579 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005580
Bob Wilsonc6800b52009-08-13 02:13:04 +00005581 // Convert shuffles that are directly supported on NEON to target-specific
5582 // DAG nodes, instead of keeping them as shuffles and matching them again
5583 // during code selection. This is more efficient and avoids the possibility
5584 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005585 // FIXME: floating-point vectors should be canonicalized to integer vectors
5586 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005587 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005588
Bob Wilson846bd792010-06-07 23:53:38 +00005589 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5590 if (EltSize <= 32) {
5591 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5592 int Lane = SVN->getSplatIndex();
5593 // If this is undef splat, generate it via "just" vdup, if possible.
5594 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005595
Dan Gohman198b7ff2011-11-03 21:49:52 +00005596 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005597 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5598 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5599 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005600 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5601 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5602 // reaches it).
5603 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5604 !isa<ConstantSDNode>(V1.getOperand(0))) {
5605 bool IsScalarToVector = true;
5606 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5607 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5608 IsScalarToVector = false;
5609 break;
5610 }
5611 if (IsScalarToVector)
5612 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5613 }
Bob Wilson846bd792010-06-07 23:53:38 +00005614 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005615 DAG.getConstant(Lane, dl, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005616 }
Bob Wilson846bd792010-06-07 23:53:38 +00005617
5618 bool ReverseVEXT;
5619 unsigned Imm;
5620 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5621 if (ReverseVEXT)
5622 std::swap(V1, V2);
5623 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005624 DAG.getConstant(Imm, dl, MVT::i32));
Bob Wilson846bd792010-06-07 23:53:38 +00005625 }
5626
5627 if (isVREVMask(ShuffleMask, VT, 64))
5628 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5629 if (isVREVMask(ShuffleMask, VT, 32))
5630 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5631 if (isVREVMask(ShuffleMask, VT, 16))
5632 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5633
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005634 if (V2->getOpcode() == ISD::UNDEF &&
5635 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5636 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005637 DAG.getConstant(Imm, dl, MVT::i32));
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005638 }
5639
Bob Wilson846bd792010-06-07 23:53:38 +00005640 // Check for Neon shuffles that modify both input vectors in place.
5641 // If both results are used, i.e., if there are two shuffles with the same
5642 // source operands and with masks corresponding to both results of one of
5643 // these operations, DAG memoization will ensure that a single node is
5644 // used for both shuffles.
5645 unsigned WhichResult;
5646 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5647 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5648 V1, V2).getValue(WhichResult);
5649 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5650 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5651 V1, V2).getValue(WhichResult);
5652 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5653 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5654 V1, V2).getValue(WhichResult);
5655
5656 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5657 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5658 V1, V1).getValue(WhichResult);
5659 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5660 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5661 V1, V1).getValue(WhichResult);
5662 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5663 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5664 V1, V1).getValue(WhichResult);
Bob Wilsoncce31f62009-08-14 05:08:32 +00005665 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005666
Bob Wilsona7062312009-08-21 20:54:19 +00005667 // If the shuffle is not directly supported and it has 4 elements, use
5668 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005669 unsigned NumElts = VT.getVectorNumElements();
5670 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005671 unsigned PFIndexes[4];
5672 for (unsigned i = 0; i != 4; ++i) {
5673 if (ShuffleMask[i] < 0)
5674 PFIndexes[i] = 8;
5675 else
5676 PFIndexes[i] = ShuffleMask[i];
5677 }
5678
5679 // Compute the index in the perfect shuffle table.
5680 unsigned PFTableIndex =
5681 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005682 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5683 unsigned Cost = (PFEntry >> 30);
5684
5685 if (Cost <= 4)
5686 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5687 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00005688
Bob Wilsond8a9a042010-06-04 00:04:02 +00005689 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00005690 if (EltSize >= 32) {
5691 // Do the expansion with floating-point types, since that is what the VFP
5692 // registers are defined to use, and since i64 is not legal.
5693 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5694 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005695 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5696 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005697 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00005698 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00005699 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00005700 Ops.push_back(DAG.getUNDEF(EltVT));
5701 else
5702 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5703 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5704 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005705 dl, MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00005706 }
Craig Topper48d114b2014-04-26 18:35:24 +00005707 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005708 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00005709 }
5710
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005711 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5712 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
5713
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005714 if (VT == MVT::v8i8) {
5715 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
5716 if (NewOp.getNode())
5717 return NewOp;
5718 }
5719
Bob Wilson6f34e272009-08-14 05:16:33 +00005720 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005721}
5722
Eli Friedmana5e244c2011-10-24 23:08:52 +00005723static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5724 // INSERT_VECTOR_ELT is legal only for immediate indexes.
5725 SDValue Lane = Op.getOperand(2);
5726 if (!isa<ConstantSDNode>(Lane))
5727 return SDValue();
5728
5729 return Op;
5730}
5731
Bob Wilson2e076c42009-06-22 23:27:02 +00005732static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00005733 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00005734 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00005735 if (!isa<ConstantSDNode>(Lane))
5736 return SDValue();
5737
5738 SDValue Vec = Op.getOperand(0);
5739 if (Op.getValueType() == MVT::i32 &&
5740 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005741 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00005742 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5743 }
5744
5745 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00005746}
5747
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005748static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5749 // The only time a CONCAT_VECTORS operation can have legal types is when
5750 // two 64-bit vectors are concatenated to a 128-bit vector.
5751 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5752 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005753 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005754 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005755 SDValue Op0 = Op.getOperand(0);
5756 SDValue Op1 = Op.getOperand(1);
5757 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005758 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005759 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005760 DAG.getIntPtrConstant(0, dl));
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005761 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005762 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005763 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005764 DAG.getIntPtrConstant(1, dl));
Wesley Peck527da1b2010-11-23 03:31:01 +00005765 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005766}
5767
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005768/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5769/// element has been zero/sign-extended, depending on the isSigned parameter,
5770/// from an integer type half its size.
5771static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5772 bool isSigned) {
5773 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5774 EVT VT = N->getValueType(0);
5775 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5776 SDNode *BVN = N->getOperand(0).getNode();
5777 if (BVN->getValueType(0) != MVT::v4i32 ||
5778 BVN->getOpcode() != ISD::BUILD_VECTOR)
5779 return false;
5780 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5781 unsigned HiElt = 1 - LoElt;
5782 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5783 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5784 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5785 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5786 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5787 return false;
5788 if (isSigned) {
5789 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5790 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5791 return true;
5792 } else {
5793 if (Hi0->isNullValue() && Hi1->isNullValue())
5794 return true;
5795 }
5796 return false;
5797 }
5798
5799 if (N->getOpcode() != ISD::BUILD_VECTOR)
5800 return false;
5801
5802 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5803 SDNode *Elt = N->getOperand(i).getNode();
5804 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5805 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5806 unsigned HalfSize = EltSize / 2;
5807 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005808 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005809 return false;
5810 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005811 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005812 return false;
5813 }
5814 continue;
5815 }
5816 return false;
5817 }
5818
5819 return true;
5820}
5821
5822/// isSignExtended - Check if a node is a vector value that is sign-extended
5823/// or a constant BUILD_VECTOR with sign-extended elements.
5824static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5825 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5826 return true;
5827 if (isExtendedBUILD_VECTOR(N, DAG, true))
5828 return true;
5829 return false;
5830}
5831
5832/// isZeroExtended - Check if a node is a vector value that is zero-extended
5833/// or a constant BUILD_VECTOR with zero-extended elements.
5834static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5835 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5836 return true;
5837 if (isExtendedBUILD_VECTOR(N, DAG, false))
5838 return true;
5839 return false;
5840}
5841
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005842static EVT getExtensionTo64Bits(const EVT &OrigVT) {
5843 if (OrigVT.getSizeInBits() >= 64)
5844 return OrigVT;
5845
5846 assert(OrigVT.isSimple() && "Expecting a simple value type");
5847
5848 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
5849 switch (OrigSimpleTy) {
5850 default: llvm_unreachable("Unexpected Vector Type");
5851 case MVT::v2i8:
5852 case MVT::v2i16:
5853 return MVT::v2i32;
5854 case MVT::v4i8:
5855 return MVT::v4i16;
5856 }
5857}
5858
Sebastian Popa204f722012-11-30 19:08:04 +00005859/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5860/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5861/// We insert the required extension here to get the vector to fill a D register.
5862static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5863 const EVT &OrigTy,
5864 const EVT &ExtTy,
5865 unsigned ExtOpcode) {
5866 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5867 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5868 // 64-bits we need to insert a new extension so that it will be 64-bits.
5869 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5870 if (OrigTy.getSizeInBits() >= 64)
5871 return N;
5872
5873 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005874 EVT NewVT = getExtensionTo64Bits(OrigTy);
5875
Andrew Trickef9de2a2013-05-25 02:42:55 +00005876 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00005877}
5878
5879/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5880/// does not do any sign/zero extension. If the original vector is less
5881/// than 64 bits, an appropriate extension will be added after the load to
5882/// reach a total size of 64 bits. We have to add the extension separately
5883/// because ARM does not have a sign/zero extending load for vectors.
5884static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005885 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
5886
5887 // The load already has the right type.
5888 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005889 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00005890 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5891 LD->isNonTemporal(), LD->isInvariant(),
5892 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005893
5894 // We need to create a zextload/sextload. We cannot just create a load
5895 // followed by a zext/zext node because LowerMUL is also run during normal
5896 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005897 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005898 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00005899 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(),
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005900 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00005901}
5902
5903/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5904/// extending load, or BUILD_VECTOR with extended elements, return the
5905/// unextended value. The unextended vector should be 64 bits so that it can
5906/// be used as an operand to a VMULL instruction. If the original vector size
5907/// before extension is less than 64 bits we add a an extension to resize
5908/// the vector to 64 bits.
5909static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00005910 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00005911 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5912 N->getOperand(0)->getValueType(0),
5913 N->getValueType(0),
5914 N->getOpcode());
5915
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005916 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00005917 return SkipLoadExtensionForVMULL(LD, DAG);
5918
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005919 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5920 // have been legalized as a BITCAST from v4i32.
5921 if (N->getOpcode() == ISD::BITCAST) {
5922 SDNode *BVN = N->getOperand(0).getNode();
5923 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5924 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5925 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00005926 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005927 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5928 }
5929 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5930 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5931 EVT VT = N->getValueType(0);
5932 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5933 unsigned NumElts = VT.getVectorNumElements();
5934 MVT TruncVT = MVT::getIntegerVT(EltSize);
5935 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005936 SDLoc dl(N);
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005937 for (unsigned i = 0; i != NumElts; ++i) {
5938 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5939 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00005940 // Element types smaller than 32 bits are not legal, so use i32 elements.
5941 // The values are implicitly truncated so sext vs. zext doesn't matter.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005942 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), dl, MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005943 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00005944 return DAG.getNode(ISD::BUILD_VECTOR, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00005945 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00005946}
5947
Evan Chenge2086e72011-03-29 01:56:09 +00005948static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5949 unsigned Opcode = N->getOpcode();
5950 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5951 SDNode *N0 = N->getOperand(0).getNode();
5952 SDNode *N1 = N->getOperand(1).getNode();
5953 return N0->hasOneUse() && N1->hasOneUse() &&
5954 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5955 }
5956 return false;
5957}
5958
5959static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5960 unsigned Opcode = N->getOpcode();
5961 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5962 SDNode *N0 = N->getOperand(0).getNode();
5963 SDNode *N1 = N->getOperand(1).getNode();
5964 return N0->hasOneUse() && N1->hasOneUse() &&
5965 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
5966 }
5967 return false;
5968}
5969
Bob Wilson38ab35a2010-09-01 23:50:19 +00005970static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
5971 // Multiplications are only custom-lowered for 128-bit vectors so that
5972 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
5973 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00005974 assert(VT.is128BitVector() && VT.isInteger() &&
5975 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00005976 SDNode *N0 = Op.getOperand(0).getNode();
5977 SDNode *N1 = Op.getOperand(1).getNode();
5978 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00005979 bool isMLA = false;
5980 bool isN0SExt = isSignExtended(N0, DAG);
5981 bool isN1SExt = isSignExtended(N1, DAG);
5982 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00005983 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00005984 else {
5985 bool isN0ZExt = isZeroExtended(N0, DAG);
5986 bool isN1ZExt = isZeroExtended(N1, DAG);
5987 if (isN0ZExt && isN1ZExt)
5988 NewOpc = ARMISD::VMULLu;
5989 else if (isN1SExt || isN1ZExt) {
5990 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5991 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5992 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5993 NewOpc = ARMISD::VMULLs;
5994 isMLA = true;
5995 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5996 NewOpc = ARMISD::VMULLu;
5997 isMLA = true;
5998 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5999 std::swap(N0, N1);
6000 NewOpc = ARMISD::VMULLu;
6001 isMLA = true;
6002 }
6003 }
6004
6005 if (!NewOpc) {
6006 if (VT == MVT::v2i64)
6007 // Fall through to expand this. It is not legal.
6008 return SDValue();
6009 else
6010 // Other vector multiplications are legal.
6011 return Op;
6012 }
6013 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006014
6015 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006016 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00006017 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00006018 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006019 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00006020 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006021 assert(Op0.getValueType().is64BitVector() &&
6022 Op1.getValueType().is64BitVector() &&
6023 "unexpected types for extended operands to VMULL");
6024 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
6025 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006026
Evan Chenge2086e72011-03-29 01:56:09 +00006027 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
6028 // isel lowering to take advantage of no-stall back to back vmul + vmla.
6029 // vmull q0, d4, d6
6030 // vmlal q0, d5, d6
6031 // is faster than
6032 // vaddl q0, d4, d5
6033 // vmovl q1, d6
6034 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00006035 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
6036 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006037 EVT Op1VT = Op1.getValueType();
6038 return DAG.getNode(N0->getOpcode(), DL, VT,
6039 DAG.getNode(NewOpc, DL, VT,
6040 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
6041 DAG.getNode(NewOpc, DL, VT,
6042 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00006043}
6044
Owen Anderson77aa2662011-04-05 21:48:57 +00006045static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006046LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00006047 // Convert to float
6048 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
6049 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
6050 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
6051 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
6052 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
6053 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
6054 // Get reciprocal estimate.
6055 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00006056 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006057 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6058 Y);
Nate Begemanfa62d502011-02-11 20:53:29 +00006059 // Because char has a smaller range than uchar, we can actually get away
6060 // without any newton steps. This requires that we use a weird bias
6061 // of 0xb000, however (again, this has been exhaustively tested).
6062 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
6063 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
6064 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006065 Y = DAG.getConstant(0xb000, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006066 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
6067 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
6068 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
6069 // Convert back to short.
6070 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
6071 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
6072 return X;
6073}
6074
Owen Anderson77aa2662011-04-05 21:48:57 +00006075static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006076LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00006077 SDValue N2;
6078 // Convert to float.
6079 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
6080 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
6081 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
6082 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
6083 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
6084 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006085
Nate Begemanfa62d502011-02-11 20:53:29 +00006086 // Use reciprocal estimate and one refinement step.
6087 // float4 recip = vrecpeq_f32(yf);
6088 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006089 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006090 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6091 N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006092 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006093 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006094 N1, N2);
6095 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6096 // Because short has a smaller range than ushort, we can actually get away
6097 // with only a single newton step. This requires that we use a weird bias
6098 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006099 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00006100 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6101 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006102 N1 = DAG.getConstant(0x89, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006103 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6104 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6105 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6106 // Convert back to integer and return.
6107 // return vmovn_s32(vcvt_s32_f32(result));
6108 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6109 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6110 return N0;
6111}
6112
6113static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
6114 EVT VT = Op.getValueType();
6115 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6116 "unexpected type for custom-lowering ISD::SDIV");
6117
Andrew Trickef9de2a2013-05-25 02:42:55 +00006118 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006119 SDValue N0 = Op.getOperand(0);
6120 SDValue N1 = Op.getOperand(1);
6121 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006122
Nate Begemanfa62d502011-02-11 20:53:29 +00006123 if (VT == MVT::v8i8) {
6124 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6125 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006126
Nate Begemanfa62d502011-02-11 20:53:29 +00006127 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006128 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006129 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006130 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006131 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006132 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006133 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006134 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006135
6136 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6137 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6138
6139 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6140 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006141
Nate Begemanfa62d502011-02-11 20:53:29 +00006142 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6143 return N0;
6144 }
6145 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6146}
6147
6148static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
6149 EVT VT = Op.getValueType();
6150 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6151 "unexpected type for custom-lowering ISD::UDIV");
6152
Andrew Trickef9de2a2013-05-25 02:42:55 +00006153 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006154 SDValue N0 = Op.getOperand(0);
6155 SDValue N1 = Op.getOperand(1);
6156 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006157
Nate Begemanfa62d502011-02-11 20:53:29 +00006158 if (VT == MVT::v8i8) {
6159 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6160 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006161
Nate Begemanfa62d502011-02-11 20:53:29 +00006162 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006163 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006164 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006165 DAG.getIntPtrConstant(4, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006166 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006167 DAG.getIntPtrConstant(0, dl));
Nate Begemanfa62d502011-02-11 20:53:29 +00006168 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006169 DAG.getIntPtrConstant(0, dl));
Owen Anderson77aa2662011-04-05 21:48:57 +00006170
Nate Begemanfa62d502011-02-11 20:53:29 +00006171 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6172 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006173
Nate Begemanfa62d502011-02-11 20:53:29 +00006174 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6175 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006176
6177 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006178 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, dl,
6179 MVT::i32),
Nate Begemanfa62d502011-02-11 20:53:29 +00006180 N0);
6181 return N0;
6182 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006183
Nate Begemanfa62d502011-02-11 20:53:29 +00006184 // v4i16 sdiv ... Convert to float.
6185 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6186 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6187 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6188 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6189 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006190 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006191
6192 // Use reciprocal estimate and two refinement steps.
6193 // float4 recip = vrecpeq_f32(yf);
6194 // recip *= vrecpsq_f32(yf, recip);
6195 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006196 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006197 DAG.getConstant(Intrinsic::arm_neon_vrecpe, dl, MVT::i32),
6198 BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006199 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006200 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006201 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006202 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006203 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006204 DAG.getConstant(Intrinsic::arm_neon_vrecps, dl, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006205 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006206 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6207 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6208 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6209 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006210 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006211 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6212 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006213 N1 = DAG.getConstant(2, dl, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006214 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6215 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6216 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6217 // Convert back to integer and return.
6218 // return vmovn_u32(vcvt_s32_f32(result));
6219 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6220 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6221 return N0;
6222}
6223
Evan Chenge8916542011-08-30 01:34:54 +00006224static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6225 EVT VT = Op.getNode()->getValueType(0);
6226 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6227
6228 unsigned Opc;
6229 bool ExtraOp = false;
6230 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006231 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006232 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6233 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6234 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6235 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6236 }
6237
6238 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006239 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006240 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006241 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006242 Op.getOperand(1), Op.getOperand(2));
6243}
6244
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006245SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6246 assert(Subtarget->isTargetDarwin());
6247
6248 // For iOS, we want to call an alternative entry point: __sincos_stret,
6249 // return values are passed via sret.
6250 SDLoc dl(Op);
6251 SDValue Arg = Op.getOperand(0);
6252 EVT ArgVT = Arg.getValueType();
6253 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
6254
6255 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
6256 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6257
6258 // Pair of floats / doubles used to pass the result.
Reid Kleckner343c3952014-11-20 23:51:47 +00006259 StructType *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006260
6261 // Create stack object for sret.
6262 const uint64_t ByteSize = TLI.getDataLayout()->getTypeAllocSize(RetTy);
6263 const unsigned StackAlign = TLI.getDataLayout()->getPrefTypeAlignment(RetTy);
6264 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6265 SDValue SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
6266
6267 ArgListTy Args;
6268 ArgListEntry Entry;
6269
6270 Entry.Node = SRet;
6271 Entry.Ty = RetTy->getPointerTo();
6272 Entry.isSExt = false;
6273 Entry.isZExt = false;
6274 Entry.isSRet = true;
6275 Args.push_back(Entry);
6276
6277 Entry.Node = Arg;
6278 Entry.Ty = ArgTy;
6279 Entry.isSExt = false;
6280 Entry.isZExt = false;
6281 Args.push_back(Entry);
6282
6283 const char *LibcallName = (ArgVT == MVT::f64)
6284 ? "__sincos_stret" : "__sincosf_stret";
6285 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
6286
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006287 TargetLowering::CallLoweringInfo CLI(DAG);
6288 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
6289 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), Callee,
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00006290 std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006291 .setDiscardResult();
6292
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006293 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6294
6295 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6296 MachinePointerInfo(), false, false, false, 0);
6297
6298 // Address of cos field.
6299 SDValue Add = DAG.getNode(ISD::ADD, dl, getPointerTy(), SRet,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006300 DAG.getIntPtrConstant(ArgVT.getStoreSize(), dl));
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006301 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6302 MachinePointerInfo(), false, false, false, 0);
6303
6304 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6305 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6306 LoadSin.getValue(0), LoadCos.getValue(0));
6307}
6308
Eli Friedman10f9ce22011-09-15 22:26:18 +00006309static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006310 // Monotonic load/store is legal for all targets
6311 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6312 return Op;
6313
Alp Tokercb402912014-01-24 17:20:08 +00006314 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006315 // dmb or equivalent available.
6316 return SDValue();
6317}
6318
Tim Northoverbc933082013-05-23 19:11:20 +00006319static void ReplaceREADCYCLECOUNTER(SDNode *N,
6320 SmallVectorImpl<SDValue> &Results,
6321 SelectionDAG &DAG,
6322 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006323 SDLoc DL(N);
Tim Northoverbc933082013-05-23 19:11:20 +00006324 SDValue Cycles32, OutChain;
6325
6326 if (Subtarget->hasPerfMon()) {
6327 // Under Power Management extensions, the cycle-count is:
6328 // mrc p15, #0, <Rt>, c9, c13, #0
6329 SDValue Ops[] = { N->getOperand(0), // Chain
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006330 DAG.getConstant(Intrinsic::arm_mrc, DL, MVT::i32),
6331 DAG.getConstant(15, DL, MVT::i32),
6332 DAG.getConstant(0, DL, MVT::i32),
6333 DAG.getConstant(9, DL, MVT::i32),
6334 DAG.getConstant(13, DL, MVT::i32),
6335 DAG.getConstant(0, DL, MVT::i32)
Tim Northoverbc933082013-05-23 19:11:20 +00006336 };
6337
6338 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00006339 DAG.getVTList(MVT::i32, MVT::Other), Ops);
Tim Northoverbc933082013-05-23 19:11:20 +00006340 OutChain = Cycles32.getValue(1);
6341 } else {
6342 // Intrinsic is defined to return 0 on unsupported platforms. Technically
6343 // there are older ARM CPUs that have implementation-specific ways of
6344 // obtaining this information (FIXME!).
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006345 Cycles32 = DAG.getConstant(0, DL, MVT::i32);
Tim Northoverbc933082013-05-23 19:11:20 +00006346 OutChain = DAG.getEntryNode();
6347 }
6348
6349
6350 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00006351 Cycles32, DAG.getConstant(0, DL, MVT::i32));
Tim Northoverbc933082013-05-23 19:11:20 +00006352 Results.push_back(Cycles64);
6353 Results.push_back(OutChain);
6354}
6355
Dan Gohman21cea8a2010-04-17 15:26:15 +00006356SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006357 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006358 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Cheng10043e22007-01-19 07:51:42 +00006359 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006360 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006361 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006362 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6363 default: llvm_unreachable("unknown object format");
6364 case Triple::COFF:
6365 return LowerGlobalAddressWindows(Op, DAG);
6366 case Triple::ELF:
6367 return LowerGlobalAddressELF(Op, DAG);
6368 case Triple::MachO:
6369 return LowerGlobalAddressDarwin(Op, DAG);
6370 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006371 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006372 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006373 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6374 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006375 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006376 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006377 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006378 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006379 case ISD::SINT_TO_FP:
6380 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6381 case ISD::FP_TO_SINT:
6382 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006383 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006384 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006385 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006386 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006387 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006388 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006389 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6390 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006391 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006392 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006393 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006394 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng15b80e42009-11-12 07:13:11 +00006395 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006396 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006397 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach8546ec92010-01-18 19:58:49 +00006398 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006399 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006400 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006401 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006402 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006403 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006404 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006405 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006406 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006407 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006408 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanfa62d502011-02-11 20:53:29 +00006409 case ISD::SDIV: return LowerSDIV(Op, DAG);
6410 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006411 case ISD::ADDC:
6412 case ISD::ADDE:
6413 case ISD::SUBC:
6414 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006415 case ISD::SADDO:
6416 case ISD::UADDO:
6417 case ISD::SSUBO:
6418 case ISD::USUBO:
6419 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006420 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006421 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006422 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006423 case ISD::SDIVREM:
6424 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00006425 case ISD::DYNAMIC_STACKALLOC:
6426 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
6427 return LowerDYNAMIC_STACKALLOC(Op, DAG);
6428 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00006429 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
6430 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006431 }
Evan Cheng10043e22007-01-19 07:51:42 +00006432}
6433
Duncan Sands6ed40142008-12-01 11:39:25 +00006434/// ReplaceNodeResults - Replace the results of node with an illegal result
6435/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006436void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
6437 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006438 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006439 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006440 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006441 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006442 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peck527da1b2010-11-23 03:31:01 +00006443 case ISD::BITCAST:
6444 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006445 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006446 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006447 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006448 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006449 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006450 case ISD::READCYCLECOUNTER:
6451 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6452 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00006453 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006454 if (Res.getNode())
6455 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006456}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006457
Evan Cheng10043e22007-01-19 07:51:42 +00006458//===----------------------------------------------------------------------===//
6459// ARM Scheduler Hooks
6460//===----------------------------------------------------------------------===//
6461
Bill Wendling030b58e2011-10-06 22:18:16 +00006462/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6463/// registers the function context.
6464void ARMTargetLowering::
6465SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6466 MachineBasicBlock *DispatchBB, int FI) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006467 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling374ee192011-10-03 21:25:38 +00006468 DebugLoc dl = MI->getDebugLoc();
6469 MachineFunction *MF = MBB->getParent();
6470 MachineRegisterInfo *MRI = &MF->getRegInfo();
6471 MachineConstantPool *MCP = MF->getConstantPool();
6472 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6473 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006474
Bill Wendling374ee192011-10-03 21:25:38 +00006475 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006476 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006477
Bill Wendling374ee192011-10-03 21:25:38 +00006478 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006479 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006480 ARMConstantPoolValue *CPV =
6481 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6482 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6483
Craig Topper61e88f42014-11-21 05:58:21 +00006484 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
6485 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006486
Bill Wendling030b58e2011-10-06 22:18:16 +00006487 // Grab constant pool and fixed stack memory operands.
6488 MachineMemOperand *CPMMO =
6489 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6490 MachineMemOperand::MOLoad, 4, 4);
6491
6492 MachineMemOperand *FIMMOSt =
6493 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6494 MachineMemOperand::MOStore, 4, 4);
6495
6496 // Load the address of the dispatch MBB into the jump buffer.
6497 if (isThumb2) {
6498 // Incoming value: jbuf
6499 // ldr.n r5, LCPI1_1
6500 // orr r5, r5, #1
6501 // add r5, pc
6502 // str r5, [$jbuf, #+4] ; &jbuf[1]
6503 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6504 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6505 .addConstantPoolIndex(CPI)
6506 .addMemOperand(CPMMO));
6507 // Set the low bit because of thumb mode.
6508 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6509 AddDefaultCC(
6510 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6511 .addReg(NewVReg1, RegState::Kill)
6512 .addImm(0x01)));
6513 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6514 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6515 .addReg(NewVReg2, RegState::Kill)
6516 .addImm(PCLabelId);
6517 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6518 .addReg(NewVReg3, RegState::Kill)
6519 .addFrameIndex(FI)
6520 .addImm(36) // &jbuf[1] :: pc
6521 .addMemOperand(FIMMOSt));
6522 } else if (isThumb) {
6523 // Incoming value: jbuf
6524 // ldr.n r1, LCPI1_4
6525 // add r1, pc
6526 // mov r2, #1
6527 // orrs r1, r2
6528 // add r2, $jbuf, #+4 ; &jbuf[1]
6529 // str r1, [r2]
6530 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6531 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6532 .addConstantPoolIndex(CPI)
6533 .addMemOperand(CPMMO));
6534 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6535 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6536 .addReg(NewVReg1, RegState::Kill)
6537 .addImm(PCLabelId);
6538 // Set the low bit because of thumb mode.
6539 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6540 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6541 .addReg(ARM::CPSR, RegState::Define)
6542 .addImm(1));
6543 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6544 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6545 .addReg(ARM::CPSR, RegState::Define)
6546 .addReg(NewVReg2, RegState::Kill)
6547 .addReg(NewVReg3, RegState::Kill));
6548 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00006549 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
6550 .addFrameIndex(FI)
6551 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00006552 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6553 .addReg(NewVReg4, RegState::Kill)
6554 .addReg(NewVReg5, RegState::Kill)
6555 .addImm(0)
6556 .addMemOperand(FIMMOSt));
6557 } else {
6558 // Incoming value: jbuf
6559 // ldr r1, LCPI1_1
6560 // add r1, pc, r1
6561 // str r1, [$jbuf, #+4] ; &jbuf[1]
6562 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6563 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6564 .addConstantPoolIndex(CPI)
6565 .addImm(0)
6566 .addMemOperand(CPMMO));
6567 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6568 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6569 .addReg(NewVReg1, RegState::Kill)
6570 .addImm(PCLabelId));
6571 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6572 .addReg(NewVReg2, RegState::Kill)
6573 .addFrameIndex(FI)
6574 .addImm(36) // &jbuf[1] :: pc
6575 .addMemOperand(FIMMOSt));
6576 }
6577}
6578
Matthias Brauneec4efc2015-04-28 00:37:05 +00006579void ARMTargetLowering::EmitSjLjDispatchBlock(MachineInstr *MI,
6580 MachineBasicBlock *MBB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00006581 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00006582 DebugLoc dl = MI->getDebugLoc();
6583 MachineFunction *MF = MBB->getParent();
6584 MachineRegisterInfo *MRI = &MF->getRegInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00006585 MachineFrameInfo *MFI = MF->getFrameInfo();
6586 int FI = MFI->getFunctionContextIndex();
6587
Craig Topper61e88f42014-11-21 05:58:21 +00006588 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
6589 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00006590
Bill Wendling362c1b02011-10-06 21:29:56 +00006591 // Get a mapping of the call site numbers to all of the landing pads they're
6592 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00006593 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6594 unsigned MaxCSNum = 0;
6595 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00006596 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6597 ++BB) {
Bill Wendling202803e2011-10-05 00:02:33 +00006598 if (!BB->isLandingPad()) continue;
6599
6600 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6601 // pad.
6602 for (MachineBasicBlock::iterator
6603 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6604 if (!II->isEHLabel()) continue;
6605
6606 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006607 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006608
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006609 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6610 for (SmallVectorImpl<unsigned>::iterator
6611 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6612 CSI != CSE; ++CSI) {
6613 CallSiteNumToLPad[*CSI].push_back(BB);
6614 MaxCSNum = std::max(MaxCSNum, *CSI);
6615 }
Bill Wendling202803e2011-10-05 00:02:33 +00006616 break;
6617 }
6618 }
6619
6620 // Get an ordered list of the machine basic blocks for the jump table.
6621 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00006622 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00006623 LPadList.reserve(CallSiteNumToLPad.size());
6624 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6625 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6626 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006627 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00006628 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00006629 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6630 }
Bill Wendling202803e2011-10-05 00:02:33 +00006631 }
6632
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006633 assert(!LPadList.empty() &&
6634 "No landing pad destinations for the dispatch jump table!");
6635
Bill Wendling362c1b02011-10-06 21:29:56 +00006636 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00006637 MachineJumpTableInfo *JTI =
6638 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6639 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
Chad Rosier96603432013-03-01 18:30:38 +00006640 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00006641
Bill Wendling362c1b02011-10-06 21:29:56 +00006642 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00006643
6644 // Shove the dispatch's address into the return slot in the function context.
6645 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6646 DispatchBB->setIsLandingPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00006647
Bill Wendling324be982011-10-05 00:39:32 +00006648 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006649 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00006650 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006651 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00006652 else
6653 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6654
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006655 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00006656 DispatchBB->addSuccessor(TrapBB);
6657
6658 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6659 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00006660
Bill Wendling510fbcd2011-10-17 21:32:56 +00006661 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00006662 MF->insert(MF->end(), DispatchBB);
6663 MF->insert(MF->end(), DispContBB);
6664 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00006665
Bill Wendling030b58e2011-10-06 22:18:16 +00006666 // Insert code into the entry block that creates and registers the function
6667 // context.
6668 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6669
Bill Wendling030b58e2011-10-06 22:18:16 +00006670 MachineMemOperand *FIMMOLd =
Bill Wendling362c1b02011-10-06 21:29:56 +00006671 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendlingb3d46782011-10-06 23:37:36 +00006672 MachineMemOperand::MOLoad |
6673 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00006674
Chad Rosier1ec8e402012-11-06 23:05:24 +00006675 MachineInstrBuilder MIB;
6676 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6677
6678 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6679 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6680
6681 // Add a register mask with no preserved registers. This results in all
6682 // registers being marked as clobbered.
6683 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00006684
Bill Wendling85833f72011-10-18 22:49:07 +00006685 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00006686 if (Subtarget->isThumb2()) {
6687 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6688 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6689 .addFrameIndex(FI)
6690 .addImm(4)
6691 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006692
Bill Wendling85833f72011-10-18 22:49:07 +00006693 if (NumLPads < 256) {
6694 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6695 .addReg(NewVReg1)
6696 .addImm(LPadList.size()));
6697 } else {
6698 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6699 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006700 .addImm(NumLPads & 0xFFFF));
6701
6702 unsigned VReg2 = VReg1;
6703 if ((NumLPads & 0xFFFF0000) != 0) {
6704 VReg2 = MRI->createVirtualRegister(TRC);
6705 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6706 .addReg(VReg1)
6707 .addImm(NumLPads >> 16));
6708 }
6709
Bill Wendling85833f72011-10-18 22:49:07 +00006710 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6711 .addReg(NewVReg1)
6712 .addReg(VReg2));
6713 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006714
Bill Wendling5626c662011-10-06 22:53:00 +00006715 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6716 .addMBB(TrapBB)
6717 .addImm(ARMCC::HI)
6718 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00006719
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006720 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6721 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00006722 .addJumpTableIndex(MJTI));
Bill Wendling202803e2011-10-05 00:02:33 +00006723
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006724 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006725 AddDefaultCC(
6726 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006727 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6728 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00006729 .addReg(NewVReg1)
6730 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6731
6732 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006733 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00006734 .addReg(NewVReg1)
Tim Northover4998a472015-05-13 20:28:38 +00006735 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00006736 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00006737 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6738 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6739 .addFrameIndex(FI)
6740 .addImm(1)
6741 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00006742
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006743 if (NumLPads < 256) {
6744 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6745 .addReg(NewVReg1)
6746 .addImm(NumLPads));
6747 } else {
6748 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00006749 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6750 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6751
6752 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006753 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006754 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006755 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006756 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006757
6758 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6759 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6760 .addReg(VReg1, RegState::Define)
6761 .addConstantPoolIndex(Idx));
6762 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6763 .addReg(NewVReg1)
6764 .addReg(VReg1));
6765 }
6766
Bill Wendlingb3d46782011-10-06 23:37:36 +00006767 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6768 .addMBB(TrapBB)
6769 .addImm(ARMCC::HI)
6770 .addReg(ARM::CPSR);
6771
6772 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6773 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6774 .addReg(ARM::CPSR, RegState::Define)
6775 .addReg(NewVReg1)
6776 .addImm(2));
6777
6778 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00006779 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Tim Northover4998a472015-05-13 20:28:38 +00006780 .addJumpTableIndex(MJTI));
Bill Wendlingb3d46782011-10-06 23:37:36 +00006781
6782 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6783 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6784 .addReg(ARM::CPSR, RegState::Define)
6785 .addReg(NewVReg2, RegState::Kill)
6786 .addReg(NewVReg3));
6787
6788 MachineMemOperand *JTMMOLd =
6789 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6790 MachineMemOperand::MOLoad, 4, 4);
6791
6792 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6793 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6794 .addReg(NewVReg4, RegState::Kill)
6795 .addImm(0)
6796 .addMemOperand(JTMMOLd));
6797
Chad Rosier96603432013-03-01 18:30:38 +00006798 unsigned NewVReg6 = NewVReg5;
6799 if (RelocM == Reloc::PIC_) {
6800 NewVReg6 = MRI->createVirtualRegister(TRC);
6801 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6802 .addReg(ARM::CPSR, RegState::Define)
6803 .addReg(NewVReg5, RegState::Kill)
6804 .addReg(NewVReg3));
6805 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00006806
6807 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6808 .addReg(NewVReg6, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00006809 .addJumpTableIndex(MJTI);
Bill Wendling5626c662011-10-06 22:53:00 +00006810 } else {
6811 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6812 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6813 .addFrameIndex(FI)
6814 .addImm(4)
6815 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00006816
Bill Wendling4969dcd2011-10-18 22:52:20 +00006817 if (NumLPads < 256) {
6818 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6819 .addReg(NewVReg1)
6820 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00006821 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00006822 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6823 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006824 .addImm(NumLPads & 0xFFFF));
6825
6826 unsigned VReg2 = VReg1;
6827 if ((NumLPads & 0xFFFF0000) != 0) {
6828 VReg2 = MRI->createVirtualRegister(TRC);
6829 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6830 .addReg(VReg1)
6831 .addImm(NumLPads >> 16));
6832 }
6833
Bill Wendling4969dcd2011-10-18 22:52:20 +00006834 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6835 .addReg(NewVReg1)
6836 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00006837 } else {
6838 MachineConstantPool *ConstantPool = MF->getConstantPool();
6839 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6840 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6841
6842 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006843 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006844 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006845 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006846 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6847
6848 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6849 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6850 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00006851 .addConstantPoolIndex(Idx)
6852 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00006853 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6854 .addReg(NewVReg1)
6855 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00006856 }
6857
Bill Wendling5626c662011-10-06 22:53:00 +00006858 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6859 .addMBB(TrapBB)
6860 .addImm(ARMCC::HI)
6861 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00006862
Bill Wendling973c8172011-10-18 22:11:18 +00006863 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006864 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00006865 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006866 .addReg(NewVReg1)
6867 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00006868 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6869 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00006870 .addJumpTableIndex(MJTI));
Bill Wendling5626c662011-10-06 22:53:00 +00006871
6872 MachineMemOperand *JTMMOLd =
6873 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6874 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00006875 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006876 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00006877 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6878 .addReg(NewVReg3, RegState::Kill)
6879 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006880 .addImm(0)
6881 .addMemOperand(JTMMOLd));
6882
Chad Rosier96603432013-03-01 18:30:38 +00006883 if (RelocM == Reloc::PIC_) {
6884 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
6885 .addReg(NewVReg5, RegState::Kill)
6886 .addReg(NewVReg4)
Tim Northover4998a472015-05-13 20:28:38 +00006887 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00006888 } else {
6889 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
6890 .addReg(NewVReg5, RegState::Kill)
Tim Northover4998a472015-05-13 20:28:38 +00006891 .addJumpTableIndex(MJTI);
Chad Rosier96603432013-03-01 18:30:38 +00006892 }
Bill Wendling5626c662011-10-06 22:53:00 +00006893 }
Bill Wendling202803e2011-10-05 00:02:33 +00006894
Bill Wendling324be982011-10-05 00:39:32 +00006895 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006896 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00006897 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006898 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6899 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00006900 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00006901 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006902 }
6903
Bill Wendling26d27802011-10-17 05:25:09 +00006904 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00006905 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00006906 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00006907 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006908
6909 // Remove the landing pad successor from the invoke block and replace it
6910 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00006911 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6912 BB->succ_end());
6913 while (!Successors.empty()) {
6914 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling883ec972011-10-07 23:18:02 +00006915 if (SMBB->isLandingPad()) {
6916 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00006917 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006918 }
6919 }
6920
6921 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006922
6923 // Find the invoke call and mark all of the callee-saved registers as
6924 // 'implicit defined' so that they're spilled. This prevents code from
6925 // moving instructions to before the EH block, where they will never be
6926 // executed.
6927 for (MachineBasicBlock::reverse_iterator
6928 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00006929 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006930
6931 DenseMap<unsigned, bool> DefRegs;
6932 for (MachineInstr::mop_iterator
6933 OI = II->operands_begin(), OE = II->operands_end();
6934 OI != OE; ++OI) {
6935 if (!OI->isReg()) continue;
6936 DefRegs[OI->getReg()] = true;
6937 }
6938
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00006939 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006940
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006941 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00006942 unsigned Reg = SavedRegs[i];
6943 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00006944 !ARM::tGPRRegClass.contains(Reg) &&
6945 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006946 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006947 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006948 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006949 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006950 continue;
6951 if (!DefRegs[Reg])
6952 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006953 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006954
6955 break;
6956 }
Bill Wendling883ec972011-10-07 23:18:02 +00006957 }
Bill Wendling324be982011-10-05 00:39:32 +00006958
Bill Wendling617075f2011-10-18 18:30:49 +00006959 // Mark all former landing pads as non-landing pads. The dispatch is the only
6960 // landing pad now.
6961 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6962 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6963 (*I)->setIsLandingPad(false);
6964
Bill Wendling324be982011-10-05 00:39:32 +00006965 // The instruction is gone now.
6966 MI->eraseFromParent();
Bill Wendling374ee192011-10-03 21:25:38 +00006967}
6968
Evan Cheng0cc4ad92010-07-13 19:27:42 +00006969static
6970MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6971 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6972 E = MBB->succ_end(); I != E; ++I)
6973 if (*I != Succ)
6974 return *I;
6975 llvm_unreachable("Expecting a BB with two successors!");
6976}
6977
Manman Renb504f492013-10-29 22:27:32 +00006978/// Return the load opcode for a given load size. If load size >= 8,
6979/// neon opcode will be returned.
6980static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
6981 if (LdSize >= 8)
6982 return LdSize == 16 ? ARM::VLD1q32wb_fixed
6983 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
6984 if (IsThumb1)
6985 return LdSize == 4 ? ARM::tLDRi
6986 : LdSize == 2 ? ARM::tLDRHi
6987 : LdSize == 1 ? ARM::tLDRBi : 0;
6988 if (IsThumb2)
6989 return LdSize == 4 ? ARM::t2LDR_POST
6990 : LdSize == 2 ? ARM::t2LDRH_POST
6991 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
6992 return LdSize == 4 ? ARM::LDR_POST_IMM
6993 : LdSize == 2 ? ARM::LDRH_POST
6994 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
6995}
6996
6997/// Return the store opcode for a given store size. If store size >= 8,
6998/// neon opcode will be returned.
6999static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
7000 if (StSize >= 8)
7001 return StSize == 16 ? ARM::VST1q32wb_fixed
7002 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
7003 if (IsThumb1)
7004 return StSize == 4 ? ARM::tSTRi
7005 : StSize == 2 ? ARM::tSTRHi
7006 : StSize == 1 ? ARM::tSTRBi : 0;
7007 if (IsThumb2)
7008 return StSize == 4 ? ARM::t2STR_POST
7009 : StSize == 2 ? ARM::t2STRH_POST
7010 : StSize == 1 ? ARM::t2STRB_POST : 0;
7011 return StSize == 4 ? ARM::STR_POST_IMM
7012 : StSize == 2 ? ARM::STRH_POST
7013 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
7014}
7015
7016/// Emit a post-increment load operation with given size. The instructions
7017/// will be added to BB at Pos.
7018static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
7019 const TargetInstrInfo *TII, DebugLoc dl,
7020 unsigned LdSize, unsigned Data, unsigned AddrIn,
7021 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7022 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
7023 assert(LdOpc != 0 && "Should have a load opcode");
7024 if (LdSize >= 8) {
7025 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7026 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7027 .addImm(0));
7028 } else if (IsThumb1) {
7029 // load + update AddrIn
7030 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7031 .addReg(AddrIn).addImm(0));
7032 MachineInstrBuilder MIB =
7033 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7034 MIB = AddDefaultT1CC(MIB);
7035 MIB.addReg(AddrIn).addImm(LdSize);
7036 AddDefaultPred(MIB);
7037 } else if (IsThumb2) {
7038 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7039 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7040 .addImm(LdSize));
7041 } else { // arm
7042 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7043 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7044 .addReg(0).addImm(LdSize));
7045 }
7046}
7047
7048/// Emit a post-increment store operation with given size. The instructions
7049/// will be added to BB at Pos.
7050static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
7051 const TargetInstrInfo *TII, DebugLoc dl,
7052 unsigned StSize, unsigned Data, unsigned AddrIn,
7053 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7054 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
7055 assert(StOpc != 0 && "Should have a store opcode");
7056 if (StSize >= 8) {
7057 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7058 .addReg(AddrIn).addImm(0).addReg(Data));
7059 } else if (IsThumb1) {
7060 // store + update AddrIn
7061 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
7062 .addReg(AddrIn).addImm(0));
7063 MachineInstrBuilder MIB =
7064 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7065 MIB = AddDefaultT1CC(MIB);
7066 MIB.addReg(AddrIn).addImm(StSize);
7067 AddDefaultPred(MIB);
7068 } else if (IsThumb2) {
7069 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7070 .addReg(Data).addReg(AddrIn).addImm(StSize));
7071 } else { // arm
7072 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7073 .addReg(Data).addReg(AddrIn).addReg(0)
7074 .addImm(StSize));
7075 }
7076}
7077
David Peixottoc32e24a2013-10-17 19:49:22 +00007078MachineBasicBlock *
7079ARMTargetLowering::EmitStructByval(MachineInstr *MI,
7080 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00007081 // This pseudo instruction has 3 operands: dst, src, size
7082 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7083 // Otherwise, we will generate unrolled scalar copies.
Eric Christopher1889fdc2015-01-29 00:19:39 +00007084 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00007085 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7086 MachineFunction::iterator It = BB;
7087 ++It;
7088
7089 unsigned dest = MI->getOperand(0).getReg();
7090 unsigned src = MI->getOperand(1).getReg();
7091 unsigned SizeVal = MI->getOperand(2).getImm();
7092 unsigned Align = MI->getOperand(3).getImm();
7093 DebugLoc dl = MI->getDebugLoc();
7094
Manman Rene8735522012-06-01 19:33:18 +00007095 MachineFunction *MF = BB->getParent();
7096 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00007097 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00007098 const TargetRegisterClass *TRC = nullptr;
7099 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007100
7101 bool IsThumb1 = Subtarget->isThumb1Only();
7102 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00007103
7104 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00007105 UnitSize = 1;
7106 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00007107 UnitSize = 2;
7108 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00007109 // Check whether we can use NEON instructions.
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +00007110 if (!MF->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007111 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007112 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007113 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007114 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007115 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007116 }
7117 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007118 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007119 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007120 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007121
David Peixottob0653e532013-10-24 16:39:36 +00007122 // Select the correct opcode and register class for unit size load/store
7123 bool IsNeon = UnitSize >= 8;
Craig Topper61e88f42014-11-21 05:58:21 +00007124 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007125 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00007126 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
7127 : UnitSize == 8 ? &ARM::DPRRegClass
7128 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007129
Manman Rene8735522012-06-01 19:33:18 +00007130 unsigned BytesLeft = SizeVal % UnitSize;
7131 unsigned LoopSize = SizeVal - BytesLeft;
7132
7133 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7134 // Use LDR and STR to copy.
7135 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7136 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7137 unsigned srcIn = src;
7138 unsigned destIn = dest;
7139 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007140 unsigned srcOut = MRI.createVirtualRegister(TRC);
7141 unsigned destOut = MRI.createVirtualRegister(TRC);
7142 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007143 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7144 IsThumb1, IsThumb2);
7145 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7146 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007147 srcIn = srcOut;
7148 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007149 }
7150
7151 // Handle the leftover bytes with LDRB and STRB.
7152 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7153 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007154 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007155 unsigned srcOut = MRI.createVirtualRegister(TRC);
7156 unsigned destOut = MRI.createVirtualRegister(TRC);
7157 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007158 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7159 IsThumb1, IsThumb2);
7160 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7161 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007162 srcIn = srcOut;
7163 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007164 }
7165 MI->eraseFromParent(); // The instruction is gone now.
7166 return BB;
7167 }
7168
7169 // Expand the pseudo op to a loop.
7170 // thisMBB:
7171 // ...
7172 // movw varEnd, # --> with thumb2
7173 // movt varEnd, #
7174 // ldrcp varEnd, idx --> without thumb2
7175 // fallthrough --> loopMBB
7176 // loopMBB:
7177 // PHI varPhi, varEnd, varLoop
7178 // PHI srcPhi, src, srcLoop
7179 // PHI destPhi, dst, destLoop
7180 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7181 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7182 // subs varLoop, varPhi, #UnitSize
7183 // bne loopMBB
7184 // fallthrough --> exitMBB
7185 // exitMBB:
7186 // epilogue to handle left-over bytes
7187 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7188 // [destOut] = STRB_POST(scratch, destLoop, 1)
7189 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7190 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7191 MF->insert(It, loopMBB);
7192 MF->insert(It, exitMBB);
7193
7194 // Transfer the remainder of BB and its successor edges to exitMBB.
7195 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007196 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007197 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7198
7199 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007200 unsigned varEnd = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007201 if (Subtarget->useMovt(*MF)) {
David Peixottob0653e532013-10-24 16:39:36 +00007202 unsigned Vtmp = varEnd;
7203 if ((LoopSize & 0xFFFF0000) != 0)
7204 Vtmp = MRI.createVirtualRegister(TRC);
Derek Schuffb0513892015-03-26 22:11:00 +00007205 AddDefaultPred(BuildMI(BB, dl,
7206 TII->get(IsThumb2 ? ARM::t2MOVi16 : ARM::MOVi16),
7207 Vtmp).addImm(LoopSize & 0xFFFF));
David Peixottob0653e532013-10-24 16:39:36 +00007208
7209 if ((LoopSize & 0xFFFF0000) != 0)
Derek Schuffb0513892015-03-26 22:11:00 +00007210 AddDefaultPred(BuildMI(BB, dl,
7211 TII->get(IsThumb2 ? ARM::t2MOVTi16 : ARM::MOVTi16),
7212 varEnd)
7213 .addReg(Vtmp)
7214 .addImm(LoopSize >> 16));
David Peixottob0653e532013-10-24 16:39:36 +00007215 } else {
7216 MachineConstantPool *ConstantPool = MF->getConstantPool();
7217 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7218 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7219
7220 // MachineConstantPool wants an explicit alignment.
7221 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
7222 if (Align == 0)
7223 Align = getDataLayout()->getTypeAllocSize(C->getType());
7224 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7225
7226 if (IsThumb1)
7227 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7228 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7229 else
7230 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7231 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7232 }
Manman Rene8735522012-06-01 19:33:18 +00007233 BB->addSuccessor(loopMBB);
7234
7235 // Generate the loop body:
7236 // varPhi = PHI(varLoop, varEnd)
7237 // srcPhi = PHI(srcLoop, src)
7238 // destPhi = PHI(destLoop, dst)
7239 MachineBasicBlock *entryBB = BB;
7240 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007241 unsigned varLoop = MRI.createVirtualRegister(TRC);
7242 unsigned varPhi = MRI.createVirtualRegister(TRC);
7243 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7244 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7245 unsigned destLoop = MRI.createVirtualRegister(TRC);
7246 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007247
7248 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7249 .addReg(varLoop).addMBB(loopMBB)
7250 .addReg(varEnd).addMBB(entryBB);
7251 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7252 .addReg(srcLoop).addMBB(loopMBB)
7253 .addReg(src).addMBB(entryBB);
7254 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7255 .addReg(destLoop).addMBB(loopMBB)
7256 .addReg(dest).addMBB(entryBB);
7257
7258 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7259 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007260 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007261 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7262 IsThumb1, IsThumb2);
7263 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7264 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007265
7266 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007267 if (IsThumb1) {
7268 MachineInstrBuilder MIB =
7269 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7270 MIB = AddDefaultT1CC(MIB);
7271 MIB.addReg(varPhi).addImm(UnitSize);
7272 AddDefaultPred(MIB);
7273 } else {
7274 MachineInstrBuilder MIB =
7275 BuildMI(*BB, BB->end(), dl,
7276 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7277 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7278 MIB->getOperand(5).setReg(ARM::CPSR);
7279 MIB->getOperand(5).setIsDef(true);
7280 }
7281 BuildMI(*BB, BB->end(), dl,
7282 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7283 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007284
7285 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7286 BB->addSuccessor(loopMBB);
7287 BB->addSuccessor(exitMBB);
7288
7289 // Add epilogue to handle BytesLeft.
7290 BB = exitMBB;
7291 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007292
7293 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7294 // [destOut] = STRB_POST(scratch, destLoop, 1)
7295 unsigned srcIn = srcLoop;
7296 unsigned destIn = destLoop;
7297 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007298 unsigned srcOut = MRI.createVirtualRegister(TRC);
7299 unsigned destOut = MRI.createVirtualRegister(TRC);
7300 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007301 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7302 IsThumb1, IsThumb2);
7303 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7304 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007305 srcIn = srcOut;
7306 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007307 }
7308
7309 MI->eraseFromParent(); // The instruction is gone now.
7310 return BB;
7311}
7312
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007313MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007314ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
7315 MachineBasicBlock *MBB) const {
7316 const TargetMachine &TM = getTargetMachine();
Eric Christopher1889fdc2015-01-29 00:19:39 +00007317 const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007318 DebugLoc DL = MI->getDebugLoc();
7319
7320 assert(Subtarget->isTargetWindows() &&
7321 "__chkstk is only supported on Windows");
7322 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
7323
7324 // __chkstk takes the number of words to allocate on the stack in R4, and
7325 // returns the stack adjustment in number of bytes in R4. This will not
7326 // clober any other registers (other than the obvious lr).
7327 //
7328 // Although, technically, IP should be considered a register which may be
7329 // clobbered, the call itself will not touch it. Windows on ARM is a pure
7330 // thumb-2 environment, so there is no interworking required. As a result, we
7331 // do not expect a veneer to be emitted by the linker, clobbering IP.
7332 //
Alp Toker1d099d92014-06-19 19:41:26 +00007333 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007334 // required, again, ensuring that IP is not clobbered.
7335 //
7336 // Finally, although some linkers may theoretically provide a trampoline for
7337 // out of range calls (which is quite common due to a 32M range limitation of
7338 // branches for Thumb), we can generate the long-call version via
7339 // -mcmodel=large, alleviating the need for the trampoline which may clobber
7340 // IP.
7341
7342 switch (TM.getCodeModel()) {
7343 case CodeModel::Small:
7344 case CodeModel::Medium:
7345 case CodeModel::Default:
7346 case CodeModel::Kernel:
7347 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
7348 .addImm((unsigned)ARMCC::AL).addReg(0)
7349 .addExternalSymbol("__chkstk")
7350 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7351 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7352 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7353 break;
7354 case CodeModel::Large:
7355 case CodeModel::JITDefault: {
7356 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7357 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
7358
7359 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
7360 .addExternalSymbol("__chkstk");
7361 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
7362 .addImm((unsigned)ARMCC::AL).addReg(0)
7363 .addReg(Reg, RegState::Kill)
7364 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7365 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7366 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7367 break;
7368 }
7369 }
7370
7371 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
7372 ARM::SP)
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +00007373 .addReg(ARM::SP).addReg(ARM::R4)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007374
7375 MI->eraseFromParent();
7376 return MBB;
7377}
7378
7379MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007380ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007381 MachineBasicBlock *BB) const {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007382 const TargetInstrInfo *TII = Subtarget->getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007383 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007384 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007385 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007386 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007387 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007388 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007389 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007390 // The Thumb2 pre-indexed stores have the same MI operands, they just
7391 // define them differently in the .td files from the isel patterns, so
7392 // they need pseudos.
7393 case ARM::t2STR_preidx:
7394 MI->setDesc(TII->get(ARM::t2STR_PRE));
7395 return BB;
7396 case ARM::t2STRB_preidx:
7397 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7398 return BB;
7399 case ARM::t2STRH_preidx:
7400 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7401 return BB;
7402
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007403 case ARM::STRi_preidx:
7404 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007405 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007406 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7407 // Decode the offset.
7408 unsigned Offset = MI->getOperand(4).getImm();
7409 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7410 Offset = ARM_AM::getAM2Offset(Offset);
7411 if (isSub)
7412 Offset = -Offset;
7413
Jim Grosbachf402f692011-08-12 21:02:34 +00007414 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007415 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007416 .addOperand(MI->getOperand(0)) // Rn_wb
7417 .addOperand(MI->getOperand(1)) // Rt
7418 .addOperand(MI->getOperand(2)) // Rn
7419 .addImm(Offset) // offset (skip GPR==zero_reg)
7420 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007421 .addOperand(MI->getOperand(6))
7422 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007423 MI->eraseFromParent();
7424 return BB;
7425 }
7426 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007427 case ARM::STRBr_preidx:
7428 case ARM::STRH_preidx: {
7429 unsigned NewOpc;
7430 switch (MI->getOpcode()) {
7431 default: llvm_unreachable("unexpected opcode!");
7432 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7433 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7434 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7435 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007436 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7437 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7438 MIB.addOperand(MI->getOperand(i));
7439 MI->eraseFromParent();
7440 return BB;
7441 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007442
Evan Chengbb2af352009-08-12 05:17:19 +00007443 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007444 // To "insert" a SELECT_CC instruction, we actually have to insert the
7445 // diamond control-flow pattern. The incoming instruction knows the
7446 // destination vreg to set, the condition code register to branch on, the
7447 // true/false values to select between, and a branch opcode to use.
7448 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007449 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007450 ++It;
7451
7452 // thisMBB:
7453 // ...
7454 // TrueVal = ...
7455 // cmpTY ccX, r1, r2
7456 // bCC copy1MBB
7457 // fallthrough --> copy0MBB
7458 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007459 MachineFunction *F = BB->getParent();
7460 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7461 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007462 F->insert(It, copy0MBB);
7463 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007464
7465 // Transfer the remainder of BB and its successor edges to sinkMBB.
7466 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007467 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007468 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7469
Dan Gohmanf4f04102010-07-06 15:49:48 +00007470 BB->addSuccessor(copy0MBB);
7471 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007472
Dan Gohman34396292010-07-06 20:24:04 +00007473 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7474 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7475
Evan Cheng10043e22007-01-19 07:51:42 +00007476 // copy0MBB:
7477 // %FalseValue = ...
7478 // # fallthrough to sinkMBB
7479 BB = copy0MBB;
7480
7481 // Update machine-CFG edges
7482 BB->addSuccessor(sinkMBB);
7483
7484 // sinkMBB:
7485 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7486 // ...
7487 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007488 BuildMI(*BB, BB->begin(), dl,
7489 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007490 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7491 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7492
Dan Gohman34396292010-07-06 20:24:04 +00007493 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007494 return BB;
7495 }
Evan Chengb972e562009-08-07 00:34:42 +00007496
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007497 case ARM::BCCi64:
7498 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007499 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007500 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007501
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007502 // Compare both parts that make up the double comparison separately for
7503 // equality.
7504 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7505
7506 unsigned LHS1 = MI->getOperand(1).getReg();
7507 unsigned LHS2 = MI->getOperand(2).getReg();
7508 if (RHSisZero) {
7509 AddDefaultPred(BuildMI(BB, dl,
7510 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7511 .addReg(LHS1).addImm(0));
7512 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7513 .addReg(LHS2).addImm(0)
7514 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7515 } else {
7516 unsigned RHS1 = MI->getOperand(3).getReg();
7517 unsigned RHS2 = MI->getOperand(4).getReg();
7518 AddDefaultPred(BuildMI(BB, dl,
7519 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7520 .addReg(LHS1).addReg(RHS1));
7521 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7522 .addReg(LHS2).addReg(RHS2)
7523 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7524 }
7525
7526 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7527 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7528 if (MI->getOperand(0).getImm() == ARMCC::NE)
7529 std::swap(destMBB, exitMBB);
7530
7531 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7532 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007533 if (isThumb2)
7534 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7535 else
7536 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007537
7538 MI->eraseFromParent(); // The pseudo instruction is gone now.
7539 return BB;
7540 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007541
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007542 case ARM::Int_eh_sjlj_setjmp:
7543 case ARM::Int_eh_sjlj_setjmp_nofp:
7544 case ARM::tInt_eh_sjlj_setjmp:
7545 case ARM::t2Int_eh_sjlj_setjmp:
7546 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7547 EmitSjLjDispatchBlock(MI, BB);
7548 return BB;
7549
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007550 case ARM::ABS:
7551 case ARM::t2ABS: {
7552 // To insert an ABS instruction, we have to insert the
7553 // diamond control-flow pattern. The incoming instruction knows the
7554 // source vreg to test against 0, the destination vreg to set,
7555 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007556 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007557 // It transforms
7558 // V1 = ABS V0
7559 // into
7560 // V2 = MOVS V0
7561 // BCC (branch to SinkBB if V0 >= 0)
7562 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007563 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007564 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7565 MachineFunction::iterator BBI = BB;
7566 ++BBI;
7567 MachineFunction *Fn = BB->getParent();
7568 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7569 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7570 Fn->insert(BBI, RSBBB);
7571 Fn->insert(BBI, SinkBB);
7572
7573 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7574 unsigned int ABSDstReg = MI->getOperand(0).getReg();
Pete Cooper51118812015-04-30 22:15:59 +00007575 bool ABSSrcKIll = MI->getOperand(1).isKill();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007576 bool isThumb2 = Subtarget->isThumb2();
7577 MachineRegisterInfo &MRI = Fn->getRegInfo();
7578 // In Thumb mode S must not be specified if source register is the SP or
7579 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00007580 unsigned NewRsbDstReg =
7581 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007582
7583 // Transfer the remainder of BB and its successor edges to sinkMBB.
7584 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007585 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007586 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7587
7588 BB->addSuccessor(RSBBB);
7589 BB->addSuccessor(SinkBB);
7590
7591 // fall through to SinkMBB
7592 RSBBB->addSuccessor(SinkBB);
7593
Manman Rene0763c72012-06-15 21:32:12 +00007594 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00007595 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00007596 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7597 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007598
7599 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00007600 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007601 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7602 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7603
7604 // insert rsbri in RSBBB
7605 // Note: BCC and rsbri will be converted into predicated rsbmi
7606 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00007607 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007608 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Pete Cooper51118812015-04-30 22:15:59 +00007609 .addReg(ABSSrcReg, ABSSrcKIll ? RegState::Kill : 0)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007610 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7611
Andrew Trick3f07c422011-10-18 18:40:53 +00007612 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007613 // reuse ABSDstReg to not change uses of ABS instruction
7614 BuildMI(*SinkBB, SinkBB->begin(), dl,
7615 TII->get(ARM::PHI), ABSDstReg)
7616 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00007617 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007618
7619 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00007620 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007621
7622 // return last added BB
7623 return SinkBB;
7624 }
Manman Rene8735522012-06-01 19:33:18 +00007625 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00007626 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00007627 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007628 case ARM::WIN__CHKSTK:
7629 return EmitLowered__chkstk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00007630 }
7631}
7632
Peter Collingbourne450fbee2015-05-28 20:02:45 +00007633/// \brief Lowers MCOPY to either LDMIA/STMIA or LDMIA_UPD/STMID_UPD depending
7634/// on whether the result is used. This is done as a post-isel lowering instead
7635/// of as a custom inserter because we need the use list from the SDNode.
7636static void LowerMCOPY(const ARMSubtarget *Subtarget, MachineInstr *MI,
7637 SDNode *Node) {
7638 bool isThumb1 = Subtarget->isThumb1Only();
7639 bool isThumb2 = Subtarget->isThumb2();
7640 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
7641
7642 DebugLoc dl = MI->getDebugLoc();
7643 MachineBasicBlock *BB = MI->getParent();
7644 MachineFunction *MF = BB->getParent();
7645 MachineRegisterInfo &MRI = MF->getRegInfo();
7646
7647 MachineInstrBuilder LD, ST;
7648 if (isThumb1 || Node->hasAnyUseOfValue(1)) {
7649 LD = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA_UPD
7650 : isThumb1 ? ARM::tLDMIA_UPD
7651 : ARM::LDMIA_UPD))
7652 .addOperand(MI->getOperand(1));
7653 } else {
7654 LD = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2LDMIA : ARM::LDMIA));
7655 }
7656
7657 if (isThumb1 || Node->hasAnyUseOfValue(0)) {
7658 ST = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA_UPD
7659 : isThumb1 ? ARM::tSTMIA_UPD
7660 : ARM::STMIA_UPD))
7661 .addOperand(MI->getOperand(0));
7662 } else {
7663 ST = BuildMI(*BB, MI, dl, TII->get(isThumb2 ? ARM::t2STMIA : ARM::STMIA));
7664 }
7665
7666 LD.addOperand(MI->getOperand(3)).addImm(ARMCC::AL).addReg(0);
7667 ST.addOperand(MI->getOperand(2)).addImm(ARMCC::AL).addReg(0);
7668
7669 for (unsigned I = 0; I != MI->getOperand(4).getImm(); ++I) {
7670 unsigned TmpReg = MRI.createVirtualRegister(isThumb1 ? &ARM::tGPRRegClass
7671 : &ARM::GPRRegClass);
7672 LD.addReg(TmpReg, RegState::Define);
7673 ST.addReg(TmpReg, RegState::Kill);
7674 }
7675
7676 MI->eraseFromParent();
7677}
7678
Evan Chenge6fba772011-08-30 19:09:48 +00007679void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7680 SDNode *Node) const {
Peter Collingbourne450fbee2015-05-28 20:02:45 +00007681 if (MI->getOpcode() == ARM::MCOPY) {
7682 LowerMCOPY(Subtarget, MI, Node);
7683 return;
7684 }
7685
Evan Cheng7f8e5632011-12-07 07:15:52 +00007686 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00007687 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7688 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7689 // operand is still set to noreg. If needed, set the optional operand's
7690 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00007691 //
Andrew Trick88b24502011-10-18 19:18:52 +00007692 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00007693
Andrew Trick924123a2011-09-21 02:20:46 +00007694 // Rename pseudo opcodes.
7695 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7696 if (NewOpc) {
Eric Christopher1889fdc2015-01-29 00:19:39 +00007697 const ARMBaseInstrInfo *TII = Subtarget->getInstrInfo();
Andrew Trick88b24502011-10-18 19:18:52 +00007698 MCID = &TII->get(NewOpc);
7699
7700 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7701 "converted opcode should be the same except for cc_out");
7702
7703 MI->setDesc(*MCID);
7704
7705 // Add the optional cc_out operand
7706 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00007707 }
Andrew Trick88b24502011-10-18 19:18:52 +00007708 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00007709
7710 // Any ARM instruction that sets the 's' bit should specify an optional
7711 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00007712 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007713 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007714 return;
7715 }
Andrew Trick924123a2011-09-21 02:20:46 +00007716 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7717 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007718 bool definesCPSR = false;
7719 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00007720 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00007721 i != e; ++i) {
7722 const MachineOperand &MO = MI->getOperand(i);
7723 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7724 definesCPSR = true;
7725 if (MO.isDead())
7726 deadCPSR = true;
7727 MI->RemoveOperand(i);
7728 break;
Evan Chenge6fba772011-08-30 19:09:48 +00007729 }
7730 }
Andrew Trick8586e622011-09-20 03:17:40 +00007731 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00007732 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007733 return;
7734 }
7735 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00007736 if (deadCPSR) {
7737 assert(!MI->getOperand(ccOutIdx).getReg() &&
7738 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00007739 return;
Andrew Trick924123a2011-09-21 02:20:46 +00007740 }
Andrew Trick8586e622011-09-20 03:17:40 +00007741
Andrew Trick924123a2011-09-21 02:20:46 +00007742 // If this instruction was defined with an optional CPSR def and its dag node
7743 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007744 MachineOperand &MO = MI->getOperand(ccOutIdx);
7745 MO.setReg(ARM::CPSR);
7746 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00007747}
7748
Evan Cheng10043e22007-01-19 07:51:42 +00007749//===----------------------------------------------------------------------===//
7750// ARM Optimization Hooks
7751//===----------------------------------------------------------------------===//
7752
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007753// Helper function that checks if N is a null or all ones constant.
7754static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7755 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7756 if (!C)
7757 return false;
7758 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7759}
7760
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007761// Return true if N is conditionally 0 or all ones.
7762// Detects these expressions where cc is an i1 value:
7763//
7764// (select cc 0, y) [AllOnes=0]
7765// (select cc y, 0) [AllOnes=0]
7766// (zext cc) [AllOnes=0]
7767// (sext cc) [AllOnes=0/1]
7768// (select cc -1, y) [AllOnes=1]
7769// (select cc y, -1) [AllOnes=1]
7770//
7771// Invert is set when N is the null/all ones constant when CC is false.
7772// OtherOp is set to the alternative value of N.
7773static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7774 SDValue &CC, bool &Invert,
7775 SDValue &OtherOp,
7776 SelectionDAG &DAG) {
7777 switch (N->getOpcode()) {
7778 default: return false;
7779 case ISD::SELECT: {
7780 CC = N->getOperand(0);
7781 SDValue N1 = N->getOperand(1);
7782 SDValue N2 = N->getOperand(2);
7783 if (isZeroOrAllOnes(N1, AllOnes)) {
7784 Invert = false;
7785 OtherOp = N2;
7786 return true;
7787 }
7788 if (isZeroOrAllOnes(N2, AllOnes)) {
7789 Invert = true;
7790 OtherOp = N1;
7791 return true;
7792 }
7793 return false;
7794 }
7795 case ISD::ZERO_EXTEND:
7796 // (zext cc) can never be the all ones value.
7797 if (AllOnes)
7798 return false;
7799 // Fall through.
7800 case ISD::SIGN_EXTEND: {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007801 SDLoc dl(N);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007802 EVT VT = N->getValueType(0);
7803 CC = N->getOperand(0);
7804 if (CC.getValueType() != MVT::i1)
7805 return false;
7806 Invert = !AllOnes;
7807 if (AllOnes)
7808 // When looking for an AllOnes constant, N is an sext, and the 'other'
7809 // value is 0.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007810 OtherOp = DAG.getConstant(0, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007811 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7812 // When looking for a 0 constant, N can be zext or sext.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007813 OtherOp = DAG.getConstant(1, dl, VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007814 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007815 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl,
7816 VT);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007817 return true;
7818 }
7819 }
7820}
7821
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007822// Combine a constant select operand into its use:
7823//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007824// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7825// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7826// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7827// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7828// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007829//
7830// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007831// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007832//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007833// Also recognize sext/zext from i1:
7834//
7835// (add (zext cc), x) -> (select cc (add x, 1), x)
7836// (add (sext cc), x) -> (select cc (add x, -1), x)
7837//
7838// These transformations eventually create predicated instructions.
7839//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007840// @param N The node to transform.
7841// @param Slct The N operand that is a select.
7842// @param OtherOp The other N operand (x above).
7843// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007844// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007845// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00007846static
7847SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007848 TargetLowering::DAGCombinerInfo &DCI,
7849 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00007850 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007851 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007852 SDValue NonConstantVal;
7853 SDValue CCOp;
7854 bool SwapSelectOps;
7855 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7856 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007857 return SDValue();
7858
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007859 // Slct is now know to be the desired identity constant when CC is true.
7860 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007861 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007862 OtherOp, NonConstantVal);
7863 // Unless SwapSelectOps says CC should be false.
7864 if (SwapSelectOps)
7865 std::swap(TrueVal, FalseVal);
7866
Andrew Trickef9de2a2013-05-25 02:42:55 +00007867 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007868 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00007869}
7870
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007871// Attempt combineSelectAndUse on each operand of a commutative operator N.
7872static
7873SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7874 TargetLowering::DAGCombinerInfo &DCI) {
7875 SDValue N0 = N->getOperand(0);
7876 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007877 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007878 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7879 if (Result.getNode())
7880 return Result;
7881 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007882 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007883 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7884 if (Result.getNode())
7885 return Result;
7886 }
7887 return SDValue();
7888}
7889
Eric Christopher1b8b94192011-06-29 21:10:36 +00007890// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00007891// (only after legalization).
7892static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7893 TargetLowering::DAGCombinerInfo &DCI,
7894 const ARMSubtarget *Subtarget) {
7895
7896 // Only perform optimization if after legalize, and if NEON is available. We
7897 // also expected both operands to be BUILD_VECTORs.
7898 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7899 || N0.getOpcode() != ISD::BUILD_VECTOR
7900 || N1.getOpcode() != ISD::BUILD_VECTOR)
7901 return SDValue();
7902
7903 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7904 EVT VT = N->getValueType(0);
7905 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7906 return SDValue();
7907
7908 // Check that the vector operands are of the right form.
7909 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7910 // operands, where N is the size of the formed vector.
7911 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7912 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007913
7914 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00007915 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00007916 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00007917 SDValue Vec = N0->getOperand(0)->getOperand(0);
7918 SDNode *V = Vec.getNode();
7919 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00007920
Eric Christopher1b8b94192011-06-29 21:10:36 +00007921 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007922 // check to see if each of their operands are an EXTRACT_VECTOR with
7923 // the same vector and appropriate index.
7924 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7925 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7926 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00007927
Tanya Lattnere9e67052011-06-14 23:48:48 +00007928 SDValue ExtVec0 = N0->getOperand(i);
7929 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007930
Tanya Lattnere9e67052011-06-14 23:48:48 +00007931 // First operand is the vector, verify its the same.
7932 if (V != ExtVec0->getOperand(0).getNode() ||
7933 V != ExtVec1->getOperand(0).getNode())
7934 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00007935
Tanya Lattnere9e67052011-06-14 23:48:48 +00007936 // Second is the constant, verify its correct.
7937 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7938 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00007939
Tanya Lattnere9e67052011-06-14 23:48:48 +00007940 // For the constant, we want to see all the even or all the odd.
7941 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7942 || C1->getZExtValue() != nextIndex+1)
7943 return SDValue();
7944
7945 // Increment index.
7946 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007947 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00007948 return SDValue();
7949 }
7950
7951 // Create VPADDL node.
7952 SelectionDAG &DAG = DCI.DAG;
7953 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00007954
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007955 SDLoc dl(N);
7956
Tanya Lattnere9e67052011-06-14 23:48:48 +00007957 // Build operand list.
7958 SmallVector<SDValue, 8> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007959 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls, dl,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007960 TLI.getPointerTy()));
7961
7962 // Input is the vector.
7963 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007964
Tanya Lattnere9e67052011-06-14 23:48:48 +00007965 // Get widened type and narrowed type.
7966 MVT widenType;
7967 unsigned numElem = VT.getVectorNumElements();
Oliver Stannard6cb23462015-05-18 16:39:16 +00007968
Silviu Barangaa3106e62014-04-03 10:44:27 +00007969 EVT inputLaneType = Vec.getValueType().getVectorElementType();
7970 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00007971 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7972 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7973 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7974 default:
Craig Toppere55c5562012-02-07 02:50:20 +00007975 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00007976 }
7977
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007978 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00007979 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00007980 return DAG.getNode(ExtOp, dl, VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00007981}
7982
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007983static SDValue findMUL_LOHI(SDValue V) {
7984 if (V->getOpcode() == ISD::UMUL_LOHI ||
7985 V->getOpcode() == ISD::SMUL_LOHI)
7986 return V;
7987 return SDValue();
7988}
7989
7990static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7991 TargetLowering::DAGCombinerInfo &DCI,
7992 const ARMSubtarget *Subtarget) {
7993
7994 if (Subtarget->isThumb1Only()) return SDValue();
7995
7996 // Only perform the checks after legalize when the pattern is available.
7997 if (DCI.isBeforeLegalize()) return SDValue();
7998
7999 // Look for multiply add opportunities.
8000 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
8001 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
8002 // a glue link from the first add to the second add.
8003 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
8004 // a S/UMLAL instruction.
Matthias Braun60912082015-05-20 18:40:06 +00008005 // UMUL_LOHI
8006 // / :lo \ :hi
8007 // / \ [no multiline comment]
8008 // loAdd -> ADDE |
8009 // \ :glue /
8010 // \ /
8011 // ADDC <- hiAdd
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008012 //
8013 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
8014 SDValue AddcOp0 = AddcNode->getOperand(0);
8015 SDValue AddcOp1 = AddcNode->getOperand(1);
8016
8017 // Check if the two operands are from the same mul_lohi node.
8018 if (AddcOp0.getNode() == AddcOp1.getNode())
8019 return SDValue();
8020
8021 assert(AddcNode->getNumValues() == 2 &&
8022 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00008023 "Expect ADDC with two result values. First: i32");
8024
8025 // Check that we have a glued ADDC node.
8026 if (AddcNode->getValueType(1) != MVT::Glue)
8027 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008028
8029 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
8030 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
8031 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
8032 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
8033 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
8034 return SDValue();
8035
8036 // Look for the glued ADDE.
8037 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00008038 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008039 return SDValue();
8040
8041 // Make sure it is really an ADDE.
8042 if (AddeNode->getOpcode() != ISD::ADDE)
8043 return SDValue();
8044
8045 assert(AddeNode->getNumOperands() == 3 &&
8046 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
8047 "ADDE node has the wrong inputs");
8048
8049 // Check for the triangle shape.
8050 SDValue AddeOp0 = AddeNode->getOperand(0);
8051 SDValue AddeOp1 = AddeNode->getOperand(1);
8052
8053 // Make sure that the ADDE operands are not coming from the same node.
8054 if (AddeOp0.getNode() == AddeOp1.getNode())
8055 return SDValue();
8056
8057 // Find the MUL_LOHI node walking up ADDE's operands.
8058 bool IsLeftOperandMUL = false;
8059 SDValue MULOp = findMUL_LOHI(AddeOp0);
8060 if (MULOp == SDValue())
8061 MULOp = findMUL_LOHI(AddeOp1);
8062 else
8063 IsLeftOperandMUL = true;
8064 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00008065 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008066
8067 // Figure out the right opcode.
8068 unsigned Opc = MULOp->getOpcode();
8069 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
8070
8071 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00008072 SDValue* HiAdd = nullptr;
8073 SDValue* LoMul = nullptr;
8074 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008075
Jyoti Allurf1d70502015-01-23 09:10:03 +00008076 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
8077 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
8078 return SDValue();
8079
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008080 if (IsLeftOperandMUL)
8081 HiAdd = &AddeOp1;
8082 else
8083 HiAdd = &AddeOp0;
8084
8085
Jyoti Allurf1d70502015-01-23 09:10:03 +00008086 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
8087 // whose low result is fed to the ADDC we are checking.
8088
8089 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008090 LoMul = &AddcOp0;
8091 LowAdd = &AddcOp1;
8092 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00008093 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008094 LoMul = &AddcOp1;
8095 LowAdd = &AddcOp0;
8096 }
8097
Craig Topper062a2ba2014-04-25 05:30:21 +00008098 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008099 return SDValue();
8100
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008101 // Create the merged node.
8102 SelectionDAG &DAG = DCI.DAG;
8103
8104 // Build operand list.
8105 SmallVector<SDValue, 8> Ops;
8106 Ops.push_back(LoMul->getOperand(0));
8107 Ops.push_back(LoMul->getOperand(1));
8108 Ops.push_back(*LowAdd);
8109 Ops.push_back(*HiAdd);
8110
Andrew Trickef9de2a2013-05-25 02:42:55 +00008111 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00008112 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008113
8114 // Replace the ADDs' nodes uses by the MLA node's values.
8115 SDValue HiMLALResult(MLALNode.getNode(), 1);
8116 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
8117
8118 SDValue LoMLALResult(MLALNode.getNode(), 0);
8119 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
8120
8121 // Return original node to notify the driver to stop replacing.
8122 SDValue resNode(AddcNode, 0);
8123 return resNode;
8124}
8125
8126/// PerformADDCCombine - Target-specific dag combine transform from
8127/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
8128static SDValue PerformADDCCombine(SDNode *N,
8129 TargetLowering::DAGCombinerInfo &DCI,
8130 const ARMSubtarget *Subtarget) {
8131
8132 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
8133
8134}
8135
Bob Wilson728eb292010-07-29 20:34:14 +00008136/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
8137/// operands N0 and N1. This is a helper for PerformADDCombine that is
8138/// called with the default operands, and if that fails, with commuted
8139/// operands.
8140static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008141 TargetLowering::DAGCombinerInfo &DCI,
8142 const ARMSubtarget *Subtarget){
8143
8144 // Attempt to create vpaddl for this add.
8145 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
8146 if (Result.getNode())
8147 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008148
Chris Lattner4147f082009-03-12 06:52:53 +00008149 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008150 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008151 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
8152 if (Result.getNode()) return Result;
8153 }
Chris Lattner4147f082009-03-12 06:52:53 +00008154 return SDValue();
8155}
8156
Bob Wilson728eb292010-07-29 20:34:14 +00008157/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
8158///
8159static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008160 TargetLowering::DAGCombinerInfo &DCI,
8161 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00008162 SDValue N0 = N->getOperand(0);
8163 SDValue N1 = N->getOperand(1);
8164
8165 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008166 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008167 if (Result.getNode())
8168 return Result;
8169
8170 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008171 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008172}
8173
Chris Lattner4147f082009-03-12 06:52:53 +00008174/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00008175///
Chris Lattner4147f082009-03-12 06:52:53 +00008176static SDValue PerformSUBCombine(SDNode *N,
8177 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00008178 SDValue N0 = N->getOperand(0);
8179 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00008180
Chris Lattner4147f082009-03-12 06:52:53 +00008181 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008182 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008183 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
8184 if (Result.getNode()) return Result;
8185 }
Bob Wilson7117a912009-03-20 22:42:55 +00008186
Chris Lattner4147f082009-03-12 06:52:53 +00008187 return SDValue();
8188}
8189
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008190/// PerformVMULCombine
8191/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8192/// special multiplier accumulator forwarding.
8193/// vmul d3, d0, d2
8194/// vmla d3, d1, d2
8195/// is faster than
8196/// vadd d3, d0, d1
8197/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00008198// However, for (A + B) * (A + B),
8199// vadd d2, d0, d1
8200// vmul d3, d0, d2
8201// vmla d3, d1, d2
8202// is slower than
8203// vadd d2, d0, d1
8204// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008205static SDValue PerformVMULCombine(SDNode *N,
8206 TargetLowering::DAGCombinerInfo &DCI,
8207 const ARMSubtarget *Subtarget) {
8208 if (!Subtarget->hasVMLxForwarding())
8209 return SDValue();
8210
8211 SelectionDAG &DAG = DCI.DAG;
8212 SDValue N0 = N->getOperand(0);
8213 SDValue N1 = N->getOperand(1);
8214 unsigned Opcode = N0.getOpcode();
8215 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8216 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00008217 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008218 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8219 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8220 return SDValue();
8221 std::swap(N0, N1);
8222 }
8223
Weiming Zhao2052f482013-09-25 23:12:06 +00008224 if (N0 == N1)
8225 return SDValue();
8226
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008227 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008228 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008229 SDValue N00 = N0->getOperand(0);
8230 SDValue N01 = N0->getOperand(1);
8231 return DAG.getNode(Opcode, DL, VT,
8232 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8233 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8234}
8235
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008236static SDValue PerformMULCombine(SDNode *N,
8237 TargetLowering::DAGCombinerInfo &DCI,
8238 const ARMSubtarget *Subtarget) {
8239 SelectionDAG &DAG = DCI.DAG;
8240
8241 if (Subtarget->isThumb1Only())
8242 return SDValue();
8243
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008244 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8245 return SDValue();
8246
8247 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008248 if (VT.is64BitVector() || VT.is128BitVector())
8249 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008250 if (VT != MVT::i32)
8251 return SDValue();
8252
8253 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8254 if (!C)
8255 return SDValue();
8256
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008257 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008258 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008259
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008260 ShiftAmt = ShiftAmt & (32 - 1);
8261 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008262 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008263
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008264 SDValue Res;
8265 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008266
8267 if (MulAmt >= 0) {
8268 if (isPowerOf2_32(MulAmt - 1)) {
8269 // (mul x, 2^N + 1) => (add (shl x, N), x)
8270 Res = DAG.getNode(ISD::ADD, DL, VT,
8271 V,
8272 DAG.getNode(ISD::SHL, DL, VT,
8273 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008274 DAG.getConstant(Log2_32(MulAmt - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008275 MVT::i32)));
8276 } else if (isPowerOf2_32(MulAmt + 1)) {
8277 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8278 Res = DAG.getNode(ISD::SUB, DL, VT,
8279 DAG.getNode(ISD::SHL, DL, VT,
8280 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008281 DAG.getConstant(Log2_32(MulAmt + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008282 MVT::i32)),
8283 V);
8284 } else
8285 return SDValue();
8286 } else {
8287 uint64_t MulAmtAbs = -MulAmt;
8288 if (isPowerOf2_32(MulAmtAbs + 1)) {
8289 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8290 Res = DAG.getNode(ISD::SUB, DL, VT,
8291 V,
8292 DAG.getNode(ISD::SHL, DL, VT,
8293 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008294 DAG.getConstant(Log2_32(MulAmtAbs + 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008295 MVT::i32)));
8296 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8297 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8298 Res = DAG.getNode(ISD::ADD, DL, VT,
8299 V,
8300 DAG.getNode(ISD::SHL, DL, VT,
8301 V,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008302 DAG.getConstant(Log2_32(MulAmtAbs - 1), DL,
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008303 MVT::i32)));
8304 Res = DAG.getNode(ISD::SUB, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008305 DAG.getConstant(0, DL, MVT::i32), Res);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008306
8307 } else
8308 return SDValue();
8309 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008310
8311 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008312 Res = DAG.getNode(ISD::SHL, DL, VT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008313 Res, DAG.getConstant(ShiftAmt, DL, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008314
8315 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008316 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008317 return SDValue();
8318}
8319
Owen Anderson30c48922010-11-05 19:27:46 +00008320static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008321 TargetLowering::DAGCombinerInfo &DCI,
8322 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008323
Owen Anderson30c48922010-11-05 19:27:46 +00008324 // Attempt to use immediate-form VBIC
8325 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008326 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008327 EVT VT = N->getValueType(0);
8328 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008329
Tanya Lattner266792a2011-04-07 15:24:20 +00008330 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8331 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008332
Owen Anderson30c48922010-11-05 19:27:46 +00008333 APInt SplatBits, SplatUndef;
8334 unsigned SplatBitSize;
8335 bool HasAnyUndefs;
8336 if (BVN &&
8337 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8338 if (SplatBitSize <= 64) {
8339 EVT VbicVT;
8340 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8341 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008342 DAG, dl, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008343 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008344 if (Val.getNode()) {
8345 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008346 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008347 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008348 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008349 }
8350 }
8351 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008352
Evan Chenge87681c2012-02-23 01:19:06 +00008353 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008354 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8355 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8356 if (Result.getNode())
8357 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008358 }
8359
Owen Anderson30c48922010-11-05 19:27:46 +00008360 return SDValue();
8361}
8362
Jim Grosbach11013ed2010-07-16 23:05:05 +00008363/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8364static SDValue PerformORCombine(SDNode *N,
8365 TargetLowering::DAGCombinerInfo &DCI,
8366 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008367 // Attempt to use immediate-form VORR
8368 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008369 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008370 EVT VT = N->getValueType(0);
8371 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008372
Tanya Lattner266792a2011-04-07 15:24:20 +00008373 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8374 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008375
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008376 APInt SplatBits, SplatUndef;
8377 unsigned SplatBitSize;
8378 bool HasAnyUndefs;
8379 if (BVN && Subtarget->hasNEON() &&
8380 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8381 if (SplatBitSize <= 64) {
8382 EVT VorrVT;
8383 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8384 SplatUndef.getZExtValue(), SplatBitSize,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008385 DAG, dl, VorrVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008386 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008387 if (Val.getNode()) {
8388 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008389 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008390 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008391 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008392 }
8393 }
8394 }
8395
Evan Chenge87681c2012-02-23 01:19:06 +00008396 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008397 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8398 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8399 if (Result.getNode())
8400 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008401 }
8402
Nadav Rotem3a94c542012-08-13 18:52:44 +00008403 // The code below optimizes (or (and X, Y), Z).
8404 // The AND operand needs to have a single user to make these optimizations
8405 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008406 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008407 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008408 return SDValue();
8409 SDValue N1 = N->getOperand(1);
8410
8411 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8412 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8413 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8414 APInt SplatUndef;
8415 unsigned SplatBitSize;
8416 bool HasAnyUndefs;
8417
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008418 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008419 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008420 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8421 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008422 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008423 HasAnyUndefs) && !HasAnyUndefs) {
8424 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8425 HasAnyUndefs) && !HasAnyUndefs) {
8426 // Ensure that the bit width of the constants are the same and that
8427 // the splat arguments are logical inverses as per the pattern we
8428 // are trying to simplify.
8429 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8430 SplatBits0 == ~SplatBits1) {
8431 // Canonicalize the vector type to make instruction selection
8432 // simpler.
8433 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8434 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8435 N0->getOperand(1),
8436 N0->getOperand(0),
8437 N1->getOperand(0));
8438 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8439 }
8440 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008441 }
8442 }
8443
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008444 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8445 // reasonable.
8446
Jim Grosbach11013ed2010-07-16 23:05:05 +00008447 // BFI is only available on V6T2+
8448 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8449 return SDValue();
8450
Andrew Trickef9de2a2013-05-25 02:42:55 +00008451 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008452 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008453 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008454 //
8455 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008456 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008457 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008458 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008459 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008460 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008461
Jim Grosbach11013ed2010-07-16 23:05:05 +00008462 if (VT != MVT::i32)
8463 return SDValue();
8464
Evan Cheng2e51bb42010-12-13 20:32:54 +00008465 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008466
Jim Grosbach11013ed2010-07-16 23:05:05 +00008467 // The value and the mask need to be constants so we can verify this is
8468 // actually a bitfield set. If the mask is 0xffff, we can do better
8469 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008470 SDValue MaskOp = N0.getOperand(1);
8471 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8472 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008473 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008474 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008475 if (Mask == 0xffff)
8476 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008477 SDValue Res;
8478 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008479 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8480 if (N1C) {
8481 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008482 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008483 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008484
Evan Cheng34345752010-12-11 04:11:38 +00008485 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008486 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008487
Evan Cheng2e51bb42010-12-13 20:32:54 +00008488 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008489 DAG.getConstant(Val, DL, MVT::i32),
8490 DAG.getConstant(Mask, DL, MVT::i32));
Evan Cheng34345752010-12-11 04:11:38 +00008491
8492 // Do not add new nodes to DAG combiner worklist.
8493 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008494 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008495 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008496 } else if (N1.getOpcode() == ISD::AND) {
8497 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008498 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8499 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008500 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008501 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008502
Eric Christopherd5530962011-03-26 01:21:03 +00008503 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8504 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008505 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008506 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008507 // The pack halfword instruction works better for masks that fit it,
8508 // so use that when it's available.
8509 if (Subtarget->hasT2ExtractPack() &&
8510 (Mask == 0xffff || Mask == 0xffff0000))
8511 return SDValue();
8512 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008513 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008514 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008515 DAG.getConstant(amt, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008516 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008517 DAG.getConstant(Mask, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008518 // Do not add new nodes to DAG combiner worklist.
8519 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008520 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008521 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008522 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008523 // The pack halfword instruction works better for masks that fit it,
8524 // so use that when it's available.
8525 if (Subtarget->hasT2ExtractPack() &&
8526 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8527 return SDValue();
8528 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008529 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008530 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008531 DAG.getConstant(lsb, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008532 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008533 DAG.getConstant(Mask2, DL, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008534 // Do not add new nodes to DAG combiner worklist.
8535 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008536 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008537 }
8538 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008539
Evan Cheng2e51bb42010-12-13 20:32:54 +00008540 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8541 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8542 ARM::isBitFieldInvertedMask(~Mask)) {
8543 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8544 // where lsb(mask) == #shamt and masked bits of B are known zero.
8545 SDValue ShAmt = N00.getOperand(1);
8546 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008547 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008548 if (ShAmtC != LSB)
8549 return SDValue();
8550
8551 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008552 DAG.getConstant(~Mask, DL, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008553
8554 // Do not add new nodes to DAG combiner worklist.
8555 DCI.CombineTo(N, Res, false);
8556 }
8557
Jim Grosbach11013ed2010-07-16 23:05:05 +00008558 return SDValue();
8559}
8560
Evan Chenge87681c2012-02-23 01:19:06 +00008561static SDValue PerformXORCombine(SDNode *N,
8562 TargetLowering::DAGCombinerInfo &DCI,
8563 const ARMSubtarget *Subtarget) {
8564 EVT VT = N->getValueType(0);
8565 SelectionDAG &DAG = DCI.DAG;
8566
8567 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8568 return SDValue();
8569
8570 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008571 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8572 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8573 if (Result.getNode())
8574 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008575 }
8576
8577 return SDValue();
8578}
8579
Evan Cheng6d02d902011-06-15 01:12:31 +00008580/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8581/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00008582static SDValue PerformBFICombine(SDNode *N,
8583 TargetLowering::DAGCombinerInfo &DCI) {
8584 SDValue N1 = N->getOperand(1);
8585 if (N1.getOpcode() == ISD::AND) {
8586 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8587 if (!N11C)
8588 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008589 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008590 unsigned LSB = countTrailingZeros(~InvMask);
8591 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +00008592 assert(Width <
8593 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00008594 "undefined behavior");
8595 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +00008596 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008597 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008598 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00008599 N->getOperand(0), N1.getOperand(0),
8600 N->getOperand(2));
8601 }
8602 return SDValue();
8603}
8604
Bob Wilson22806742010-09-22 22:09:21 +00008605/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8606/// ARMISD::VMOVRRD.
8607static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008608 TargetLowering::DAGCombinerInfo &DCI,
8609 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +00008610 // vmovrrd(vmovdrr x, y) -> x,y
8611 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00008612 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +00008613 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008614
8615 // vmovrrd(load f64) -> (load i32), (load i32)
8616 SDNode *InNode = InDouble.getNode();
8617 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8618 InNode->getValueType(0) == MVT::f64 &&
8619 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8620 !cast<LoadSDNode>(InNode)->isVolatile()) {
8621 // TODO: Should this be done for non-FrameIndex operands?
8622 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8623
8624 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008625 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008626 SDValue BasePtr = LD->getBasePtr();
8627 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8628 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008629 LD->isNonTemporal(), LD->isInvariant(),
8630 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008631
8632 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008633 DAG.getConstant(4, DL, MVT::i32));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008634 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8635 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008636 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008637 std::min(4U, LD->getAlignment() / 2));
8638
8639 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Christian Pirker762b2c62014-06-01 09:30:52 +00008640 if (DCI.DAG.getTargetLoweringInfo().isBigEndian())
8641 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008642 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008643 return Result;
8644 }
8645
Bob Wilson22806742010-09-22 22:09:21 +00008646 return SDValue();
8647}
8648
8649/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8650/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8651static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8652 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8653 SDValue Op0 = N->getOperand(0);
8654 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00008655 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008656 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008657 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008658 Op1 = Op1.getOperand(0);
8659 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8660 Op0.getNode() == Op1.getNode() &&
8661 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008662 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00008663 N->getValueType(0), Op0.getOperand(0));
8664 return SDValue();
8665}
8666
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008667/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8668/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8669/// i64 vector to have f64 elements, since the value can then be loaded
8670/// directly into a VFP register.
8671static bool hasNormalLoadOperand(SDNode *N) {
8672 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8673 for (unsigned i = 0; i < NumElts; ++i) {
8674 SDNode *Elt = N->getOperand(i).getNode();
8675 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8676 return true;
8677 }
8678 return false;
8679}
8680
Bob Wilsoncb6db982010-09-17 22:59:05 +00008681/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8682/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008683static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008684 TargetLowering::DAGCombinerInfo &DCI,
8685 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +00008686 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8687 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8688 // into a pair of GPRs, which is fine when the value is used as a scalar,
8689 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008690 SelectionDAG &DAG = DCI.DAG;
8691 if (N->getNumOperands() == 2) {
8692 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8693 if (RV.getNode())
8694 return RV;
8695 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00008696
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008697 // Load i64 elements as f64 values so that type legalization does not split
8698 // them up into i32 values.
8699 EVT VT = N->getValueType(0);
8700 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8701 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008702 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008703 SmallVector<SDValue, 8> Ops;
8704 unsigned NumElts = VT.getVectorNumElements();
8705 for (unsigned i = 0; i < NumElts; ++i) {
8706 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8707 Ops.push_back(V);
8708 // Make the DAGCombiner fold the bitcast.
8709 DCI.AddToWorklist(V.getNode());
8710 }
8711 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00008712 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008713 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8714}
8715
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008716/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
8717static SDValue
8718PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8719 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
8720 // At that time, we may have inserted bitcasts from integer to float.
8721 // If these bitcasts have survived DAGCombine, change the lowering of this
8722 // BUILD_VECTOR in something more vector friendly, i.e., that does not
8723 // force to use floating point types.
8724
8725 // Make sure we can change the type of the vector.
8726 // This is possible iff:
8727 // 1. The vector is only used in a bitcast to a integer type. I.e.,
8728 // 1.1. Vector is used only once.
8729 // 1.2. Use is a bit convert to an integer type.
8730 // 2. The size of its operands are 32-bits (64-bits are not legal).
8731 EVT VT = N->getValueType(0);
8732 EVT EltVT = VT.getVectorElementType();
8733
8734 // Check 1.1. and 2.
8735 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
8736 return SDValue();
8737
8738 // By construction, the input type must be float.
8739 assert(EltVT == MVT::f32 && "Unexpected type!");
8740
8741 // Check 1.2.
8742 SDNode *Use = *N->use_begin();
8743 if (Use->getOpcode() != ISD::BITCAST ||
8744 Use->getValueType(0).isFloatingPoint())
8745 return SDValue();
8746
8747 // Check profitability.
8748 // Model is, if more than half of the relevant operands are bitcast from
8749 // i32, turn the build_vector into a sequence of insert_vector_elt.
8750 // Relevant operands are everything that is not statically
8751 // (i.e., at compile time) bitcasted.
8752 unsigned NumOfBitCastedElts = 0;
8753 unsigned NumElts = VT.getVectorNumElements();
8754 unsigned NumOfRelevantElts = NumElts;
8755 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
8756 SDValue Elt = N->getOperand(Idx);
8757 if (Elt->getOpcode() == ISD::BITCAST) {
8758 // Assume only bit cast to i32 will go away.
8759 if (Elt->getOperand(0).getValueType() == MVT::i32)
8760 ++NumOfBitCastedElts;
8761 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
8762 // Constants are statically casted, thus do not count them as
8763 // relevant operands.
8764 --NumOfRelevantElts;
8765 }
8766
8767 // Check if more than half of the elements require a non-free bitcast.
8768 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
8769 return SDValue();
8770
8771 SelectionDAG &DAG = DCI.DAG;
8772 // Create the new vector type.
8773 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
8774 // Check if the type is legal.
8775 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8776 if (!TLI.isTypeLegal(VecVT))
8777 return SDValue();
8778
8779 // Combine:
8780 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
8781 // => BITCAST INSERT_VECTOR_ELT
8782 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
8783 // (BITCAST EN), N.
8784 SDValue Vec = DAG.getUNDEF(VecVT);
8785 SDLoc dl(N);
8786 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
8787 SDValue V = N->getOperand(Idx);
8788 if (V.getOpcode() == ISD::UNDEF)
8789 continue;
8790 if (V.getOpcode() == ISD::BITCAST &&
8791 V->getOperand(0).getValueType() == MVT::i32)
8792 // Fold obvious case.
8793 V = V.getOperand(0);
8794 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00008795 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008796 // Make the DAGCombiner fold the bitcasts.
8797 DCI.AddToWorklist(V.getNode());
8798 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008799 SDValue LaneIdx = DAG.getConstant(Idx, dl, MVT::i32);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008800 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
8801 }
8802 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
8803 // Make the DAGCombiner fold the bitcasts.
8804 DCI.AddToWorklist(Vec.getNode());
8805 return Vec;
8806}
8807
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008808/// PerformInsertEltCombine - Target-specific dag combine xforms for
8809/// ISD::INSERT_VECTOR_ELT.
8810static SDValue PerformInsertEltCombine(SDNode *N,
8811 TargetLowering::DAGCombinerInfo &DCI) {
8812 // Bitcast an i64 load inserted into a vector to f64.
8813 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8814 EVT VT = N->getValueType(0);
8815 SDNode *Elt = N->getOperand(1).getNode();
8816 if (VT.getVectorElementType() != MVT::i64 ||
8817 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8818 return SDValue();
8819
8820 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008821 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008822 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8823 VT.getVectorNumElements());
8824 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8825 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8826 // Make the DAGCombiner fold the bitcasts.
8827 DCI.AddToWorklist(Vec.getNode());
8828 DCI.AddToWorklist(V.getNode());
8829 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8830 Vec, V, N->getOperand(2));
8831 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00008832}
8833
Bob Wilsonc7334a12010-10-27 20:38:28 +00008834/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8835/// ISD::VECTOR_SHUFFLE.
8836static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8837 // The LLVM shufflevector instruction does not require the shuffle mask
8838 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8839 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8840 // operands do not match the mask length, they are extended by concatenating
8841 // them with undef vectors. That is probably the right thing for other
8842 // targets, but for NEON it is better to concatenate two double-register
8843 // size vector operands into a single quad-register size vector. Do that
8844 // transformation here:
8845 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8846 // shuffle(concat(v1, v2), undef)
8847 SDValue Op0 = N->getOperand(0);
8848 SDValue Op1 = N->getOperand(1);
8849 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8850 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8851 Op0.getNumOperands() != 2 ||
8852 Op1.getNumOperands() != 2)
8853 return SDValue();
8854 SDValue Concat0Op1 = Op0.getOperand(1);
8855 SDValue Concat1Op1 = Op1.getOperand(1);
8856 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8857 Concat1Op1.getOpcode() != ISD::UNDEF)
8858 return SDValue();
8859 // Skip the transformation if any of the types are illegal.
8860 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8861 EVT VT = N->getValueType(0);
8862 if (!TLI.isTypeLegal(VT) ||
8863 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8864 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8865 return SDValue();
8866
Andrew Trickef9de2a2013-05-25 02:42:55 +00008867 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008868 Op0.getOperand(0), Op1.getOperand(0));
8869 // Translate the shuffle mask.
8870 SmallVector<int, 16> NewMask;
8871 unsigned NumElts = VT.getVectorNumElements();
8872 unsigned HalfElts = NumElts/2;
8873 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8874 for (unsigned n = 0; n < NumElts; ++n) {
8875 int MaskElt = SVN->getMaskElt(n);
8876 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00008877 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00008878 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00008879 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00008880 NewElt = HalfElts + MaskElt - NumElts;
8881 NewMask.push_back(NewElt);
8882 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00008883 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008884 DAG.getUNDEF(VT), NewMask.data());
8885}
8886
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008887/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
8888/// NEON load/store intrinsics, and generic vector load/stores, to merge
8889/// base address updates.
8890/// For generic load/stores, the memory type is assumed to be a vector.
8891/// The caller is assumed to have checked legality.
Bob Wilson06fce872011-02-07 17:43:21 +00008892static SDValue CombineBaseUpdate(SDNode *N,
8893 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson06fce872011-02-07 17:43:21 +00008894 SelectionDAG &DAG = DCI.DAG;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008895 const bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8896 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008897 const bool isStore = N->getOpcode() == ISD::STORE;
8898 const unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +00008899 SDValue Addr = N->getOperand(AddrOpIdx);
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008900 MemSDNode *MemN = cast<MemSDNode>(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00008901 SDLoc dl(N);
Bob Wilson06fce872011-02-07 17:43:21 +00008902
8903 // Search for a use of the address operand that is an increment.
8904 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8905 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8906 SDNode *User = *UI;
8907 if (User->getOpcode() != ISD::ADD ||
8908 UI.getUse().getResNo() != Addr.getResNo())
8909 continue;
8910
8911 // Check that the add is independent of the load/store. Otherwise, folding
8912 // it would create a cycle.
8913 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8914 continue;
8915
8916 // Find the new opcode for the updating load/store.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008917 bool isLoadOp = true;
Bob Wilson06fce872011-02-07 17:43:21 +00008918 bool isLaneOp = false;
8919 unsigned NewOpc = 0;
8920 unsigned NumVecs = 0;
8921 if (isIntrinsic) {
8922 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8923 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00008924 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008925 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8926 NumVecs = 1; break;
8927 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8928 NumVecs = 2; break;
8929 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8930 NumVecs = 3; break;
8931 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8932 NumVecs = 4; break;
8933 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8934 NumVecs = 2; isLaneOp = true; break;
8935 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8936 NumVecs = 3; isLaneOp = true; break;
8937 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8938 NumVecs = 4; isLaneOp = true; break;
8939 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008940 NumVecs = 1; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008941 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008942 NumVecs = 2; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008943 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008944 NumVecs = 3; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008945 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008946 NumVecs = 4; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008947 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008948 NumVecs = 2; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008949 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008950 NumVecs = 3; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008951 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00008952 NumVecs = 4; isLoadOp = false; isLaneOp = true; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008953 }
8954 } else {
8955 isLaneOp = true;
8956 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00008957 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008958 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8959 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8960 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008961 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD;
8962 NumVecs = 1; isLaneOp = false; break;
8963 case ISD::STORE: NewOpc = ARMISD::VST1_UPD;
8964 NumVecs = 1; isLaneOp = false; isLoadOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008965 }
8966 }
8967
8968 // Find the size of memory referenced by the load/store.
8969 EVT VecTy;
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008970 if (isLoadOp) {
Bob Wilson06fce872011-02-07 17:43:21 +00008971 VecTy = N->getValueType(0);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008972 } else if (isIntrinsic) {
Renato Golin2a5c0a52015-02-04 10:11:59 +00008973 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008974 } else {
8975 assert(isStore && "Node has to be a load, a store, or an intrinsic!");
8976 VecTy = N->getOperand(1).getValueType();
8977 }
8978
Bob Wilson06fce872011-02-07 17:43:21 +00008979 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8980 if (isLaneOp)
8981 NumBytes /= VecTy.getVectorNumElements();
8982
8983 // If the increment is a constant, it must match the memory ref size.
8984 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8985 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8986 uint64_t IncVal = CInc->getZExtValue();
8987 if (IncVal != NumBytes)
8988 continue;
8989 } else if (NumBytes >= 3 * 16) {
8990 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8991 // separate instructions that make it harder to use a non-constant update.
8992 continue;
8993 }
8994
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00008995 // OK, we found an ADD we can fold into the base update.
8996 // Now, create a _UPD node, taking care of not breaking alignment.
8997
8998 EVT AlignedVecTy = VecTy;
8999 unsigned Alignment = MemN->getAlignment();
9000
9001 // If this is a less-than-standard-aligned load/store, change the type to
9002 // match the standard alignment.
9003 // The alignment is overlooked when selecting _UPD variants; and it's
9004 // easier to introduce bitcasts here than fix that.
9005 // There are 3 ways to get to this base-update combine:
9006 // - intrinsics: they are assumed to be properly aligned (to the standard
9007 // alignment of the memory type), so we don't need to do anything.
9008 // - ARMISD::VLDx nodes: they are only generated from the aforementioned
9009 // intrinsics, so, likewise, there's nothing to do.
9010 // - generic load/store instructions: the alignment is specified as an
9011 // explicit operand, rather than implicitly as the standard alignment
9012 // of the memory type (like the intrisics). We need to change the
9013 // memory type to match the explicit alignment. That way, we don't
9014 // generate non-standard-aligned ARMISD::VLDx nodes.
9015 if (isa<LSBaseSDNode>(N)) {
9016 if (Alignment == 0)
9017 Alignment = 1;
9018 if (Alignment < VecTy.getScalarSizeInBits() / 8) {
9019 MVT EltTy = MVT::getIntegerVT(Alignment * 8);
9020 assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
9021 assert(!isLaneOp && "Unexpected generic load/store lane.");
9022 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
9023 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
9024 }
9025 // Don't set an explicit alignment on regular load/stores that we want
9026 // to transform to VLD/VST 1_UPD nodes.
9027 // This matches the behavior of regular load/stores, which only get an
9028 // explicit alignment if the MMO alignment is larger than the standard
9029 // alignment of the memory type.
9030 // Intrinsics, however, always get an explicit alignment, set to the
9031 // alignment of the MMO.
9032 Alignment = 1;
9033 }
9034
Bob Wilson06fce872011-02-07 17:43:21 +00009035 // Create the new updating load/store node.
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009036 // First, create an SDVTList for the new updating node's results.
Bob Wilson06fce872011-02-07 17:43:21 +00009037 EVT Tys[6];
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009038 unsigned NumResultVecs = (isLoadOp ? NumVecs : 0);
Bob Wilson06fce872011-02-07 17:43:21 +00009039 unsigned n;
9040 for (n = 0; n < NumResultVecs; ++n)
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009041 Tys[n] = AlignedVecTy;
Bob Wilson06fce872011-02-07 17:43:21 +00009042 Tys[n++] = MVT::i32;
9043 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009044 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009045
9046 // Then, gather the new node's operands.
Bob Wilson06fce872011-02-07 17:43:21 +00009047 SmallVector<SDValue, 8> Ops;
9048 Ops.push_back(N->getOperand(0)); // incoming chain
9049 Ops.push_back(N->getOperand(AddrOpIdx));
9050 Ops.push_back(Inc);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009051
9052 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
9053 // Try to match the intrinsic's signature
9054 Ops.push_back(StN->getValue());
9055 } else {
9056 // Loads (and of course intrinsics) match the intrinsics' signature,
9057 // so just add all but the alignment operand.
9058 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands() - 1; ++i)
9059 Ops.push_back(N->getOperand(i));
9060 }
9061
9062 // For all node types, the alignment operand is always the last one.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009063 Ops.push_back(DAG.getConstant(Alignment, dl, MVT::i32));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009064
9065 // If this is a non-standard-aligned STORE, the penultimate operand is the
9066 // stored value. Bitcast it to the aligned type.
9067 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
9068 SDValue &StVal = Ops[Ops.size()-2];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009069 StVal = DAG.getNode(ISD::BITCAST, dl, AlignedVecTy, StVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009070 }
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009071
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009072 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, dl, SDTys,
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009073 Ops, AlignedVecTy,
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009074 MemN->getMemOperand());
Bob Wilson06fce872011-02-07 17:43:21 +00009075
9076 // Update the uses.
Ahmed Bougacha4c2b0782015-02-19 23:13:10 +00009077 SmallVector<SDValue, 5> NewResults;
Ahmed Bougachadfdf54b2015-02-19 23:30:37 +00009078 for (unsigned i = 0; i < NumResultVecs; ++i)
Bob Wilson06fce872011-02-07 17:43:21 +00009079 NewResults.push_back(SDValue(UpdN.getNode(), i));
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009080
9081 // If this is an non-standard-aligned LOAD, the first result is the loaded
9082 // value. Bitcast it to the expected result type.
9083 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
9084 SDValue &LdVal = NewResults[0];
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009085 LdVal = DAG.getNode(ISD::BITCAST, dl, VecTy, LdVal);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009086 }
9087
Bob Wilson06fce872011-02-07 17:43:21 +00009088 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
9089 DCI.CombineTo(N, NewResults);
9090 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
9091
9092 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00009093 }
Bob Wilson06fce872011-02-07 17:43:21 +00009094 return SDValue();
9095}
9096
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009097static SDValue PerformVLDCombine(SDNode *N,
9098 TargetLowering::DAGCombinerInfo &DCI) {
9099 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9100 return SDValue();
9101
9102 return CombineBaseUpdate(N, DCI);
9103}
9104
Bob Wilson2d790df2010-11-28 06:51:26 +00009105/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
9106/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
9107/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
9108/// return true.
9109static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9110 SelectionDAG &DAG = DCI.DAG;
9111 EVT VT = N->getValueType(0);
9112 // vldN-dup instructions only support 64-bit vectors for N > 1.
9113 if (!VT.is64BitVector())
9114 return false;
9115
9116 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
9117 SDNode *VLD = N->getOperand(0).getNode();
9118 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
9119 return false;
9120 unsigned NumVecs = 0;
9121 unsigned NewOpc = 0;
9122 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
9123 if (IntNo == Intrinsic::arm_neon_vld2lane) {
9124 NumVecs = 2;
9125 NewOpc = ARMISD::VLD2DUP;
9126 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
9127 NumVecs = 3;
9128 NewOpc = ARMISD::VLD3DUP;
9129 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
9130 NumVecs = 4;
9131 NewOpc = ARMISD::VLD4DUP;
9132 } else {
9133 return false;
9134 }
9135
9136 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9137 // numbers match the load.
9138 unsigned VLDLaneNo =
9139 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9140 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9141 UI != UE; ++UI) {
9142 // Ignore uses of the chain result.
9143 if (UI.getUse().getResNo() == NumVecs)
9144 continue;
9145 SDNode *User = *UI;
9146 if (User->getOpcode() != ARMISD::VDUPLANE ||
9147 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9148 return false;
9149 }
9150
9151 // Create the vldN-dup node.
9152 EVT Tys[5];
9153 unsigned n;
9154 for (n = 0; n < NumVecs; ++n)
9155 Tys[n] = VT;
9156 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009157 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00009158 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9159 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009160 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009161 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00009162 VLDMemInt->getMemOperand());
9163
9164 // Update the uses.
9165 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9166 UI != UE; ++UI) {
9167 unsigned ResNo = UI.getUse().getResNo();
9168 // Ignore uses of the chain result.
9169 if (ResNo == NumVecs)
9170 continue;
9171 SDNode *User = *UI;
9172 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9173 }
9174
9175 // Now the vldN-lane intrinsic is dead except for its chain result.
9176 // Update uses of the chain.
9177 std::vector<SDValue> VLDDupResults;
9178 for (unsigned n = 0; n < NumVecs; ++n)
9179 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9180 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9181 DCI.CombineTo(VLD, VLDDupResults);
9182
9183 return true;
9184}
9185
Bob Wilson103a0dc2010-07-14 01:22:12 +00009186/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9187/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009188static SDValue PerformVDUPLANECombine(SDNode *N,
9189 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009190 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009191
Bob Wilson2d790df2010-11-28 06:51:26 +00009192 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9193 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9194 if (CombineVLDDUP(N, DCI))
9195 return SDValue(N, 0);
9196
9197 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9198 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009199 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009200 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009201 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009202 return SDValue();
9203
9204 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9205 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9206 // The canonical VMOV for a zero vector uses a 32-bit element size.
9207 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9208 unsigned EltBits;
9209 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9210 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00009211 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009212 if (EltSize > VT.getVectorElementType().getSizeInBits())
9213 return SDValue();
9214
Andrew Trickef9de2a2013-05-25 02:42:55 +00009215 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009216}
9217
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009218static SDValue PerformLOADCombine(SDNode *N,
9219 TargetLowering::DAGCombinerInfo &DCI) {
9220 EVT VT = N->getValueType(0);
9221
9222 // If this is a legal vector load, try to combine it into a VLD1_UPD.
9223 if (ISD::isNormalLoad(N) && VT.isVector() &&
9224 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9225 return CombineBaseUpdate(N, DCI);
9226
9227 return SDValue();
9228}
9229
Ahmed Bougacha23167462014-12-09 21:26:53 +00009230/// PerformSTORECombine - Target-specific dag combine xforms for
9231/// ISD::STORE.
9232static SDValue PerformSTORECombine(SDNode *N,
9233 TargetLowering::DAGCombinerInfo &DCI) {
9234 StoreSDNode *St = cast<StoreSDNode>(N);
9235 if (St->isVolatile())
9236 return SDValue();
9237
9238 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
9239 // pack all of the elements in one place. Next, store to memory in fewer
9240 // chunks.
9241 SDValue StVal = St->getValue();
9242 EVT VT = StVal.getValueType();
9243 if (St->isTruncatingStore() && VT.isVector()) {
9244 SelectionDAG &DAG = DCI.DAG;
9245 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9246 EVT StVT = St->getMemoryVT();
9247 unsigned NumElems = VT.getVectorNumElements();
9248 assert(StVT != VT && "Cannot truncate to the same type");
9249 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
9250 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
9251
9252 // From, To sizes and ElemCount must be pow of two
9253 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
9254
9255 // We are going to use the original vector elt for storing.
9256 // Accumulated smaller vector elements must be a multiple of the store size.
9257 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
9258
9259 unsigned SizeRatio = FromEltSz / ToEltSz;
9260 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
9261
9262 // Create a type on which we perform the shuffle.
9263 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
9264 NumElems*SizeRatio);
9265 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
9266
9267 SDLoc DL(St);
9268 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
9269 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
9270 for (unsigned i = 0; i < NumElems; ++i)
9271 ShuffleVec[i] = TLI.isBigEndian() ? (i+1) * SizeRatio - 1 : i * SizeRatio;
9272
9273 // Can't shuffle using an illegal type.
9274 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
9275
9276 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
9277 DAG.getUNDEF(WideVec.getValueType()),
9278 ShuffleVec.data());
9279 // At this point all of the data is stored at the bottom of the
9280 // register. We now need to save it to mem.
9281
9282 // Find the largest store unit
9283 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +00009284 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +00009285 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
9286 StoreType = Tp;
9287 }
9288 // Didn't find a legal store type.
9289 if (!TLI.isTypeLegal(StoreType))
9290 return SDValue();
9291
9292 // Bitcast the original vector into a vector of store-size units
9293 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
9294 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
9295 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
9296 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
9297 SmallVector<SDValue, 8> Chains;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009298 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8, DL,
Ahmed Bougacha23167462014-12-09 21:26:53 +00009299 TLI.getPointerTy());
9300 SDValue BasePtr = St->getBasePtr();
9301
9302 // Perform one or more big stores into memory.
9303 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
9304 for (unsigned I = 0; I < E; I++) {
9305 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
9306 StoreType, ShuffWide,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009307 DAG.getIntPtrConstant(I, DL));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009308 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
9309 St->getPointerInfo(), St->isVolatile(),
9310 St->isNonTemporal(), St->getAlignment());
9311 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
9312 Increment);
9313 Chains.push_back(Ch);
9314 }
9315 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
9316 }
9317
9318 if (!ISD::isNormalStore(St))
9319 return SDValue();
9320
9321 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
9322 // ARM stores of arguments in the same cache line.
9323 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
9324 StVal.getNode()->hasOneUse()) {
9325 SelectionDAG &DAG = DCI.DAG;
9326 bool isBigEndian = DAG.getTargetLoweringInfo().isBigEndian();
9327 SDLoc DL(St);
9328 SDValue BasePtr = St->getBasePtr();
9329 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
9330 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
9331 BasePtr, St->getPointerInfo(), St->isVolatile(),
9332 St->isNonTemporal(), St->getAlignment());
9333
9334 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009335 DAG.getConstant(4, DL, MVT::i32));
Ahmed Bougacha23167462014-12-09 21:26:53 +00009336 return DAG.getStore(NewST1.getValue(0), DL,
9337 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
9338 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
9339 St->isNonTemporal(),
9340 std::min(4U, St->getAlignment() / 2));
9341 }
9342
9343 if (StVal.getValueType() == MVT::i64 &&
9344 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9345
9346 // Bitcast an i64 store extracted from a vector to f64.
9347 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9348 SelectionDAG &DAG = DCI.DAG;
9349 SDLoc dl(StVal);
9350 SDValue IntVec = StVal.getOperand(0);
9351 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9352 IntVec.getValueType().getVectorNumElements());
9353 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
9354 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
9355 Vec, StVal.getOperand(1));
9356 dl = SDLoc(N);
9357 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
9358 // Make the DAGCombiner fold the bitcasts.
9359 DCI.AddToWorklist(Vec.getNode());
9360 DCI.AddToWorklist(ExtElt.getNode());
9361 DCI.AddToWorklist(V.getNode());
9362 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
9363 St->getPointerInfo(), St->isVolatile(),
9364 St->isNonTemporal(), St->getAlignment(),
9365 St->getAAInfo());
9366 }
9367
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009368 // If this is a legal vector store, try to combine it into a VST1_UPD.
9369 if (ISD::isNormalStore(N) && VT.isVector() &&
9370 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9371 return CombineBaseUpdate(N, DCI);
9372
Ahmed Bougacha23167462014-12-09 21:26:53 +00009373 return SDValue();
9374}
9375
Eric Christopher1b8b94192011-06-29 21:10:36 +00009376// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00009377// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
9378static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
9379{
Chad Rosier6b610b32011-06-28 17:26:57 +00009380 integerPart cN;
9381 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009382 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
9383 I != E; I++) {
9384 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
9385 if (!C)
9386 return false;
9387
Eric Christopher1b8b94192011-06-29 21:10:36 +00009388 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009389 APFloat APF = C->getValueAPF();
9390 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
9391 != APFloat::opOK || !isExact)
9392 return false;
9393
9394 c0 = (I == 0) ? cN : c0;
9395 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
9396 return false;
9397 }
9398 C = c0;
9399 return true;
9400}
9401
9402/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
9403/// can replace combinations of VMUL and VCVT (floating-point to integer)
9404/// when the VMUL has a constant operand that is a power of 2.
9405///
9406/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9407/// vmul.f32 d16, d17, d16
9408/// vcvt.s32.f32 d16, d16
9409/// becomes:
9410/// vcvt.s32.f32 d16, d16, #3
9411static SDValue PerformVCVTCombine(SDNode *N,
9412 TargetLowering::DAGCombinerInfo &DCI,
9413 const ARMSubtarget *Subtarget) {
9414 SelectionDAG &DAG = DCI.DAG;
9415 SDValue Op = N->getOperand(0);
9416
9417 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
9418 Op.getOpcode() != ISD::FMUL)
9419 return SDValue();
9420
9421 uint64_t C;
9422 SDValue N0 = Op->getOperand(0);
9423 SDValue ConstVec = Op->getOperand(1);
9424 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
9425
Eric Christopher1b8b94192011-06-29 21:10:36 +00009426 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00009427 !isConstVecPow2(ConstVec, isSigned, C))
9428 return SDValue();
9429
Tim Northover7cbc2152013-06-28 15:29:25 +00009430 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
9431 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Bradley Smithececb7f2014-12-16 10:59:27 +00009432 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9433 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32 ||
9434 NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +00009435 // These instructions only exist converting from f32 to i32. We can handle
9436 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +00009437 // be lossy. We also can't handle more then 4 lanes, since these intructions
9438 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +00009439 return SDValue();
9440 }
9441
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009442 SDLoc dl(N);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009443 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9444 Intrinsic::arm_neon_vcvtfp2fxu;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009445 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Tim Northover7cbc2152013-06-28 15:29:25 +00009446 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009447 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
9448 N0,
9449 DAG.getConstant(Log2_64(C), dl, MVT::i32));
Tim Northover7cbc2152013-06-28 15:29:25 +00009450
9451 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009452 FixConv = DAG.getNode(ISD::TRUNCATE, dl, N->getValueType(0), FixConv);
Tim Northover7cbc2152013-06-28 15:29:25 +00009453
9454 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009455}
9456
9457/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9458/// can replace combinations of VCVT (integer to floating-point) and VDIV
9459/// when the VDIV has a constant operand that is a power of 2.
9460///
9461/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9462/// vcvt.f32.s32 d16, d16
9463/// vdiv.f32 d16, d17, d16
9464/// becomes:
9465/// vcvt.f32.s32 d16, d16, #3
9466static SDValue PerformVDIVCombine(SDNode *N,
9467 TargetLowering::DAGCombinerInfo &DCI,
9468 const ARMSubtarget *Subtarget) {
9469 SelectionDAG &DAG = DCI.DAG;
9470 SDValue Op = N->getOperand(0);
9471 unsigned OpOpcode = Op.getNode()->getOpcode();
9472
9473 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
9474 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9475 return SDValue();
9476
9477 uint64_t C;
9478 SDValue ConstVec = N->getOperand(1);
9479 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
9480
9481 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9482 !isConstVecPow2(ConstVec, isSigned, C))
9483 return SDValue();
9484
Tim Northover7cbc2152013-06-28 15:29:25 +00009485 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
9486 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
9487 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9488 // These instructions only exist converting from i32 to f32. We can handle
9489 // smaller integers by generating an extra extend, but larger ones would
9490 // be lossy.
9491 return SDValue();
9492 }
9493
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009494 SDLoc dl(N);
Tim Northover7cbc2152013-06-28 15:29:25 +00009495 SDValue ConvInput = Op.getOperand(0);
9496 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9497 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9498 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009499 dl, NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
Tim Northover7cbc2152013-06-28 15:29:25 +00009500 ConvInput);
9501
Eric Christopher1b8b94192011-06-29 21:10:36 +00009502 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00009503 Intrinsic::arm_neon_vcvtfxu2fp;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009504 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl,
Chad Rosierfa8d8932011-06-24 19:23:04 +00009505 Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009506 DAG.getConstant(IntrinsicOpcode, dl, MVT::i32),
9507 ConvInput, DAG.getConstant(Log2_64(C), dl, MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00009508}
9509
9510/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00009511/// operand of a vector shift operation, where all the elements of the
9512/// build_vector must have the same constant integer value.
9513static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9514 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00009515 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00009516 Op = Op.getOperand(0);
9517 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9518 APInt SplatBits, SplatUndef;
9519 unsigned SplatBitSize;
9520 bool HasAnyUndefs;
9521 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9522 HasAnyUndefs, ElementBits) ||
9523 SplatBitSize > ElementBits)
9524 return false;
9525 Cnt = SplatBits.getSExtValue();
9526 return true;
9527}
9528
9529/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9530/// operand of a vector shift left operation. That value must be in the range:
9531/// 0 <= Value < ElementBits for a left shift; or
9532/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009533static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009534 assert(VT.isVector() && "vector shift count is not a vector type");
9535 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9536 if (! getVShiftImm(Op, ElementBits, Cnt))
9537 return false;
9538 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9539}
9540
9541/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9542/// operand of a vector shift right operation. For a shift opcode, the value
9543/// is positive, but for an intrinsic the value count must be negative. The
9544/// absolute value must be in the range:
9545/// 1 <= |Value| <= ElementBits for a right shift; or
9546/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009547static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00009548 int64_t &Cnt) {
9549 assert(VT.isVector() && "vector shift count is not a vector type");
9550 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9551 if (! getVShiftImm(Op, ElementBits, Cnt))
9552 return false;
9553 if (isIntrinsic)
9554 Cnt = -Cnt;
9555 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9556}
9557
9558/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9559static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9560 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9561 switch (IntNo) {
9562 default:
9563 // Don't do anything for most intrinsics.
9564 break;
9565
9566 // Vector shifts: check for immediate versions and lower them.
9567 // Note: This is done during DAG combining instead of DAG legalizing because
9568 // the build_vectors for 64-bit vector element shift counts are generally
9569 // not legal, and it is hard to see their values after they get legalized to
9570 // loads from a constant pool.
9571 case Intrinsic::arm_neon_vshifts:
9572 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009573 case Intrinsic::arm_neon_vrshifts:
9574 case Intrinsic::arm_neon_vrshiftu:
9575 case Intrinsic::arm_neon_vrshiftn:
9576 case Intrinsic::arm_neon_vqshifts:
9577 case Intrinsic::arm_neon_vqshiftu:
9578 case Intrinsic::arm_neon_vqshiftsu:
9579 case Intrinsic::arm_neon_vqshiftns:
9580 case Intrinsic::arm_neon_vqshiftnu:
9581 case Intrinsic::arm_neon_vqshiftnsu:
9582 case Intrinsic::arm_neon_vqrshiftns:
9583 case Intrinsic::arm_neon_vqrshiftnu:
9584 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009585 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009586 int64_t Cnt;
9587 unsigned VShiftOpc = 0;
9588
9589 switch (IntNo) {
9590 case Intrinsic::arm_neon_vshifts:
9591 case Intrinsic::arm_neon_vshiftu:
9592 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9593 VShiftOpc = ARMISD::VSHL;
9594 break;
9595 }
9596 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9597 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9598 ARMISD::VSHRs : ARMISD::VSHRu);
9599 break;
9600 }
9601 return SDValue();
9602
Bob Wilson2e076c42009-06-22 23:27:02 +00009603 case Intrinsic::arm_neon_vrshifts:
9604 case Intrinsic::arm_neon_vrshiftu:
9605 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9606 break;
9607 return SDValue();
9608
9609 case Intrinsic::arm_neon_vqshifts:
9610 case Intrinsic::arm_neon_vqshiftu:
9611 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9612 break;
9613 return SDValue();
9614
9615 case Intrinsic::arm_neon_vqshiftsu:
9616 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9617 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +00009618 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009619
Bob Wilson2e076c42009-06-22 23:27:02 +00009620 case Intrinsic::arm_neon_vrshiftn:
9621 case Intrinsic::arm_neon_vqshiftns:
9622 case Intrinsic::arm_neon_vqshiftnu:
9623 case Intrinsic::arm_neon_vqshiftnsu:
9624 case Intrinsic::arm_neon_vqrshiftns:
9625 case Intrinsic::arm_neon_vqrshiftnu:
9626 case Intrinsic::arm_neon_vqrshiftnsu:
9627 // Narrowing shifts require an immediate right shift.
9628 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9629 break;
Jim Grosbach84511e12010-06-02 21:53:11 +00009630 llvm_unreachable("invalid shift count for narrowing vector shift "
9631 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009632
9633 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00009634 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00009635 }
9636
9637 switch (IntNo) {
9638 case Intrinsic::arm_neon_vshifts:
9639 case Intrinsic::arm_neon_vshiftu:
9640 // Opcode already set above.
9641 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00009642 case Intrinsic::arm_neon_vrshifts:
9643 VShiftOpc = ARMISD::VRSHRs; break;
9644 case Intrinsic::arm_neon_vrshiftu:
9645 VShiftOpc = ARMISD::VRSHRu; break;
9646 case Intrinsic::arm_neon_vrshiftn:
9647 VShiftOpc = ARMISD::VRSHRN; break;
9648 case Intrinsic::arm_neon_vqshifts:
9649 VShiftOpc = ARMISD::VQSHLs; break;
9650 case Intrinsic::arm_neon_vqshiftu:
9651 VShiftOpc = ARMISD::VQSHLu; break;
9652 case Intrinsic::arm_neon_vqshiftsu:
9653 VShiftOpc = ARMISD::VQSHLsu; break;
9654 case Intrinsic::arm_neon_vqshiftns:
9655 VShiftOpc = ARMISD::VQSHRNs; break;
9656 case Intrinsic::arm_neon_vqshiftnu:
9657 VShiftOpc = ARMISD::VQSHRNu; break;
9658 case Intrinsic::arm_neon_vqshiftnsu:
9659 VShiftOpc = ARMISD::VQSHRNsu; break;
9660 case Intrinsic::arm_neon_vqrshiftns:
9661 VShiftOpc = ARMISD::VQRSHRNs; break;
9662 case Intrinsic::arm_neon_vqrshiftnu:
9663 VShiftOpc = ARMISD::VQRSHRNu; break;
9664 case Intrinsic::arm_neon_vqrshiftnsu:
9665 VShiftOpc = ARMISD::VQRSHRNsu; break;
9666 }
9667
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009668 SDLoc dl(N);
9669 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
9670 N->getOperand(1), DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009671 }
9672
9673 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009674 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009675 int64_t Cnt;
9676 unsigned VShiftOpc = 0;
9677
9678 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9679 VShiftOpc = ARMISD::VSLI;
9680 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9681 VShiftOpc = ARMISD::VSRI;
9682 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009683 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009684 }
9685
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009686 SDLoc dl(N);
9687 return DAG.getNode(VShiftOpc, dl, N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +00009688 N->getOperand(1), N->getOperand(2),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009689 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009690 }
9691
9692 case Intrinsic::arm_neon_vqrshifts:
9693 case Intrinsic::arm_neon_vqrshiftu:
9694 // No immediate versions of these to check for.
9695 break;
9696 }
9697
9698 return SDValue();
9699}
9700
9701/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9702/// lowers them. As with the vector shift intrinsics, this is done during DAG
9703/// combining instead of DAG legalizing because the build_vectors for 64-bit
9704/// vector element shift counts are generally not legal, and it is hard to see
9705/// their values after they get legalized to loads from a constant pool.
9706static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9707 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009708 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +00009709 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9710 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9711 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9712 SDValue N1 = N->getOperand(1);
9713 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9714 SDValue N0 = N->getOperand(0);
9715 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9716 DAG.MaskedValueIsZero(N0.getOperand(0),
9717 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009718 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +00009719 }
9720 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009721
9722 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +00009723 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9724 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +00009725 return SDValue();
9726
9727 assert(ST->hasNEON() && "unexpected vector shift");
9728 int64_t Cnt;
9729
9730 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009731 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009732
9733 case ISD::SHL:
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009734 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt)) {
9735 SDLoc dl(N);
9736 return DAG.getNode(ARMISD::VSHL, dl, VT, N->getOperand(0),
9737 DAG.getConstant(Cnt, dl, MVT::i32));
9738 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009739 break;
9740
9741 case ISD::SRA:
9742 case ISD::SRL:
9743 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9744 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9745 ARMISD::VSHRs : ARMISD::VSHRu);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00009746 SDLoc dl(N);
9747 return DAG.getNode(VShiftOpc, dl, VT, N->getOperand(0),
9748 DAG.getConstant(Cnt, dl, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009749 }
9750 }
9751 return SDValue();
9752}
9753
9754/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9755/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9756static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9757 const ARMSubtarget *ST) {
9758 SDValue N0 = N->getOperand(0);
9759
9760 // Check for sign- and zero-extensions of vector extract operations of 8-
9761 // and 16-bit vector elements. NEON supports these directly. They are
9762 // handled during DAG combining because type legalization will promote them
9763 // to 32-bit types and it is messy to recognize the operations after that.
9764 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9765 SDValue Vec = N0.getOperand(0);
9766 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009767 EVT VT = N->getValueType(0);
9768 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009769 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9770
Owen Anderson9f944592009-08-11 20:47:22 +00009771 if (VT == MVT::i32 &&
9772 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +00009773 TLI.isTypeLegal(Vec.getValueType()) &&
9774 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009775
9776 unsigned Opc = 0;
9777 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009778 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009779 case ISD::SIGN_EXTEND:
9780 Opc = ARMISD::VGETLANEs;
9781 break;
9782 case ISD::ZERO_EXTEND:
9783 case ISD::ANY_EXTEND:
9784 Opc = ARMISD::VGETLANEu;
9785 break;
9786 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009787 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +00009788 }
9789 }
9790
9791 return SDValue();
9792}
9793
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009794/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9795/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9796static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9797 const ARMSubtarget *ST) {
9798 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng55f0c6b2010-07-15 22:07:12 +00009799 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009800 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9801 // a NaN; only do the transformation when it matches that behavior.
9802
9803 // For now only do this when using NEON for FP operations; if using VFP, it
9804 // is not obvious that the benefit outweighs the cost of switching to the
9805 // NEON pipeline.
9806 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9807 N->getValueType(0) != MVT::f32)
9808 return SDValue();
9809
9810 SDValue CondLHS = N->getOperand(0);
9811 SDValue CondRHS = N->getOperand(1);
9812 SDValue LHS = N->getOperand(2);
9813 SDValue RHS = N->getOperand(3);
9814 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9815
9816 unsigned Opcode = 0;
9817 bool IsReversed;
Bob Wilsonba8ac742010-02-24 22:15:53 +00009818 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009819 IsReversed = false; // x CC y ? x : y
Bob Wilsonba8ac742010-02-24 22:15:53 +00009820 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009821 IsReversed = true ; // x CC y ? y : x
9822 } else {
9823 return SDValue();
9824 }
9825
Bob Wilsonba8ac742010-02-24 22:15:53 +00009826 bool IsUnordered;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009827 switch (CC) {
9828 default: break;
9829 case ISD::SETOLT:
9830 case ISD::SETOLE:
9831 case ISD::SETLT:
9832 case ISD::SETLE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009833 case ISD::SETULT:
9834 case ISD::SETULE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009835 // If LHS is NaN, an ordered comparison will be false and the result will
9836 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9837 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9838 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9839 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9840 break;
9841 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9842 // will return -0, so vmin can only be used for unsafe math or if one of
9843 // the operands is known to be nonzero.
9844 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009845 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009846 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9847 break;
9848 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009849 break;
9850
9851 case ISD::SETOGT:
9852 case ISD::SETOGE:
9853 case ISD::SETGT:
9854 case ISD::SETGE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009855 case ISD::SETUGT:
9856 case ISD::SETUGE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009857 // If LHS is NaN, an ordered comparison will be false and the result will
9858 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9859 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9860 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9861 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9862 break;
9863 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9864 // will return +0, so vmax can only be used for unsafe math or if one of
9865 // the operands is known to be nonzero.
9866 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009867 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009868 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9869 break;
9870 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009871 break;
9872 }
9873
9874 if (!Opcode)
9875 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009876 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009877}
9878
Evan Chengf863e3f2011-07-13 00:42:17 +00009879/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9880SDValue
9881ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9882 SDValue Cmp = N->getOperand(4);
9883 if (Cmp.getOpcode() != ARMISD::CMPZ)
9884 // Only looking at EQ and NE cases.
9885 return SDValue();
9886
9887 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009888 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +00009889 SDValue LHS = Cmp.getOperand(0);
9890 SDValue RHS = Cmp.getOperand(1);
9891 SDValue FalseVal = N->getOperand(0);
9892 SDValue TrueVal = N->getOperand(1);
9893 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +00009894 ARMCC::CondCodes CC =
9895 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +00009896
9897 // Simplify
9898 // mov r1, r0
9899 // cmp r1, x
9900 // mov r0, y
9901 // moveq r0, x
9902 // to
9903 // cmp r0, x
9904 // movne r0, y
9905 //
9906 // mov r1, r0
9907 // cmp r1, x
9908 // mov r0, x
9909 // movne r0, y
9910 // to
9911 // cmp r0, x
9912 // movne r0, y
9913 /// FIXME: Turn this into a target neutral optimization?
9914 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +00009915 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +00009916 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9917 N->getOperand(3), Cmp);
9918 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9919 SDValue ARMcc;
9920 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9921 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9922 N->getOperand(3), NewCmp);
9923 }
9924
9925 if (Res.getNode()) {
9926 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00009927 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +00009928 // Capture demanded bits information that would be otherwise lost.
9929 if (KnownZero == 0xfffffffe)
9930 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9931 DAG.getValueType(MVT::i1));
9932 else if (KnownZero == 0xffffff00)
9933 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9934 DAG.getValueType(MVT::i8));
9935 else if (KnownZero == 0xffff0000)
9936 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9937 DAG.getValueType(MVT::i16));
9938 }
9939
9940 return Res;
9941}
9942
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009943SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +00009944 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009945 switch (N->getOpcode()) {
9946 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009947 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +00009948 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009949 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009950 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009951 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +00009952 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9953 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +00009954 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009955 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +00009956 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009957 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009958 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009959 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +00009960 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +00009961 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009962 case ISD::FP_TO_SINT:
9963 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9964 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009965 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00009966 case ISD::SHL:
9967 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009968 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00009969 case ISD::SIGN_EXTEND:
9970 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009971 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9972 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +00009973 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009974 case ISD::LOAD: return PerformLOADCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009975 case ARMISD::VLD2DUP:
9976 case ARMISD::VLD3DUP:
9977 case ARMISD::VLD4DUP:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009978 return PerformVLDCombine(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009979 case ARMISD::BUILD_VECTOR:
9980 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009981 case ISD::INTRINSIC_VOID:
9982 case ISD::INTRINSIC_W_CHAIN:
9983 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9984 case Intrinsic::arm_neon_vld1:
9985 case Intrinsic::arm_neon_vld2:
9986 case Intrinsic::arm_neon_vld3:
9987 case Intrinsic::arm_neon_vld4:
9988 case Intrinsic::arm_neon_vld2lane:
9989 case Intrinsic::arm_neon_vld3lane:
9990 case Intrinsic::arm_neon_vld4lane:
9991 case Intrinsic::arm_neon_vst1:
9992 case Intrinsic::arm_neon_vst2:
9993 case Intrinsic::arm_neon_vst3:
9994 case Intrinsic::arm_neon_vst4:
9995 case Intrinsic::arm_neon_vst2lane:
9996 case Intrinsic::arm_neon_vst3lane:
9997 case Intrinsic::arm_neon_vst4lane:
Ahmed Bougachadb141ac2015-02-19 23:52:41 +00009998 return PerformVLDCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009999 default: break;
10000 }
10001 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010002 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010003 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +000010004}
10005
Evan Chengd42641c2011-02-02 01:06:55 +000010006bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
10007 EVT VT) const {
10008 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
10009}
10010
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010011bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
10012 unsigned,
10013 unsigned,
10014 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010015 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +000010016 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010017
10018 switch (VT.getSimpleVT().SimpleTy) {
10019 default:
10020 return false;
10021 case MVT::i8:
10022 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010023 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010024 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +000010025 if (AllowsUnaligned) {
10026 if (Fast)
10027 *Fast = Subtarget->hasV7Ops();
10028 return true;
10029 }
10030 return false;
10031 }
Evan Chengeec6bc62012-08-15 17:44:53 +000010032 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010033 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010034 // For any little-endian targets with neon, we can support unaligned ld/st
10035 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +000010036 // A big-endian target may also explicitly support unaligned accesses
Evan Cheng79e2ca92012-12-10 23:21:26 +000010037 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
10038 if (Fast)
10039 *Fast = true;
10040 return true;
10041 }
10042 return false;
10043 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010044 }
10045}
10046
Lang Hames9929c422011-11-02 22:52:45 +000010047static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
10048 unsigned AlignCheck) {
10049 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
10050 (DstAlign == 0 || DstAlign % AlignCheck == 0));
10051}
10052
10053EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
10054 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000010055 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +000010056 bool MemcpyStrSrc,
10057 MachineFunction &MF) const {
10058 const Function *F = MF.getFunction();
10059
10060 // See if we can use NEON instructions for this...
Duncan P. N. Exon Smith2cff9e12015-02-14 02:24:44 +000010061 if ((!IsMemset || ZeroMemset) && Subtarget->hasNEON() &&
10062 !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010063 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +000010064 if (Size >= 16 &&
10065 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010066 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010067 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +000010068 } else if (Size >= 8 &&
10069 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010070 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
10071 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010072 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +000010073 }
10074 }
10075
Lang Hamesb85fcd02011-11-08 18:56:23 +000010076 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +000010077 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010078 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +000010079 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010080 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +000010081
Lang Hames9929c422011-11-02 22:52:45 +000010082 // Let the target-independent logic figure it out.
10083 return MVT::Other;
10084}
10085
Evan Cheng9ec512d2012-12-06 19:13:27 +000010086bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
10087 if (Val.getOpcode() != ISD::LOAD)
10088 return false;
10089
10090 EVT VT1 = Val.getValueType();
10091 if (!VT1.isSimple() || !VT1.isInteger() ||
10092 !VT2.isSimple() || !VT2.isInteger())
10093 return false;
10094
10095 switch (VT1.getSimpleVT().SimpleTy) {
10096 default: break;
10097 case MVT::i1:
10098 case MVT::i8:
10099 case MVT::i16:
10100 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
10101 return true;
10102 }
10103
10104 return false;
10105}
10106
Ahmed Bougacha4200cc92015-03-05 19:37:53 +000010107bool ARMTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
10108 EVT VT = ExtVal.getValueType();
10109
10110 if (!isTypeLegal(VT))
10111 return false;
10112
10113 // Don't create a loadext if we can fold the extension into a wide/long
10114 // instruction.
10115 // If there's more than one user instruction, the loadext is desirable no
10116 // matter what. There can be two uses by the same instruction.
10117 if (ExtVal->use_empty() ||
10118 !ExtVal->use_begin()->isOnlyUserOf(ExtVal.getNode()))
10119 return true;
10120
10121 SDNode *U = *ExtVal->use_begin();
10122 if ((U->getOpcode() == ISD::ADD || U->getOpcode() == ISD::SUB ||
10123 U->getOpcode() == ISD::SHL || U->getOpcode() == ARMISD::VSHL))
10124 return false;
10125
10126 return true;
10127}
10128
Tim Northovercc2e9032013-08-06 13:58:03 +000010129bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
10130 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
10131 return false;
10132
10133 if (!isTypeLegal(EVT::getEVT(Ty1)))
10134 return false;
10135
10136 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
10137
10138 // Assuming the caller doesn't have a zeroext or signext return parameter,
10139 // truncation all the way down to i1 is valid.
10140 return true;
10141}
10142
10143
Evan Chengdc49a8d2009-08-14 20:09:37 +000010144static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
10145 if (V < 0)
10146 return false;
10147
10148 unsigned Scale = 1;
10149 switch (VT.getSimpleVT().SimpleTy) {
10150 default: return false;
10151 case MVT::i1:
10152 case MVT::i8:
10153 // Scale == 1;
10154 break;
10155 case MVT::i16:
10156 // Scale == 2;
10157 Scale = 2;
10158 break;
10159 case MVT::i32:
10160 // Scale == 4;
10161 Scale = 4;
10162 break;
10163 }
10164
10165 if ((V & (Scale - 1)) != 0)
10166 return false;
10167 V /= Scale;
10168 return V == (V & ((1LL << 5) - 1));
10169}
10170
10171static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
10172 const ARMSubtarget *Subtarget) {
10173 bool isNeg = false;
10174 if (V < 0) {
10175 isNeg = true;
10176 V = - V;
10177 }
10178
10179 switch (VT.getSimpleVT().SimpleTy) {
10180 default: return false;
10181 case MVT::i1:
10182 case MVT::i8:
10183 case MVT::i16:
10184 case MVT::i32:
10185 // + imm12 or - imm8
10186 if (isNeg)
10187 return V == (V & ((1LL << 8) - 1));
10188 return V == (V & ((1LL << 12) - 1));
10189 case MVT::f32:
10190 case MVT::f64:
10191 // Same as ARM mode. FIXME: NEON?
10192 if (!Subtarget->hasVFP2())
10193 return false;
10194 if ((V & 3) != 0)
10195 return false;
10196 V >>= 2;
10197 return V == (V & ((1LL << 8) - 1));
10198 }
10199}
10200
Evan Cheng2150b922007-03-12 23:30:29 +000010201/// isLegalAddressImmediate - Return true if the integer value can be used
10202/// as the offset of the target addressing mode for load / store of the
10203/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010204static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010205 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000010206 if (V == 0)
10207 return true;
10208
Evan Chengce5dfb62009-03-09 19:15:00 +000010209 if (!VT.isSimple())
10210 return false;
10211
Evan Chengdc49a8d2009-08-14 20:09:37 +000010212 if (Subtarget->isThumb1Only())
10213 return isLegalT1AddressImmediate(V, VT);
10214 else if (Subtarget->isThumb2())
10215 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000010216
Evan Chengdc49a8d2009-08-14 20:09:37 +000010217 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000010218 if (V < 0)
10219 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000010220 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000010221 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010222 case MVT::i1:
10223 case MVT::i8:
10224 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000010225 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010226 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010227 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000010228 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010229 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010230 case MVT::f32:
10231 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010232 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000010233 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000010234 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000010235 return false;
10236 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010237 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000010238 }
Evan Cheng10043e22007-01-19 07:51:42 +000010239}
10240
Evan Chengdc49a8d2009-08-14 20:09:37 +000010241bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
10242 EVT VT) const {
10243 int Scale = AM.Scale;
10244 if (Scale < 0)
10245 return false;
10246
10247 switch (VT.getSimpleVT().SimpleTy) {
10248 default: return false;
10249 case MVT::i1:
10250 case MVT::i8:
10251 case MVT::i16:
10252 case MVT::i32:
10253 if (Scale == 1)
10254 return true;
10255 // r + r << imm
10256 Scale = Scale & ~1;
10257 return Scale == 2 || Scale == 4 || Scale == 8;
10258 case MVT::i64:
10259 // r + r
10260 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
10261 return true;
10262 return false;
10263 case MVT::isVoid:
10264 // Note, we allow "void" uses (basically, uses that aren't loads or
10265 // stores), because arm allows folding a scale into many arithmetic
10266 // operations. This should be made more precise and revisited later.
10267
10268 // Allow r << imm, but the imm has to be a multiple of two.
10269 if (Scale & 1) return false;
10270 return isPowerOf2_32(Scale);
10271 }
10272}
10273
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010274/// isLegalAddressingMode - Return true if the addressing mode represented
10275/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson7117a912009-03-20 22:42:55 +000010276bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +000010277 Type *Ty,
10278 unsigned AS) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010279 EVT VT = getValueType(Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000010280 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000010281 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010282
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010283 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000010284 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010285 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010286
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010287 switch (AM.Scale) {
10288 case 0: // no scale reg, must be "r+i" or "r", or "i".
10289 break;
10290 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010291 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010292 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +000010293 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010294 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000010295 // ARM doesn't support any R+R*scale+imm addr modes.
10296 if (AM.BaseOffs)
10297 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010298
Bob Wilson866c1742009-04-08 17:55:28 +000010299 if (!VT.isSimple())
10300 return false;
10301
Evan Chengdc49a8d2009-08-14 20:09:37 +000010302 if (Subtarget->isThumb2())
10303 return isLegalT2ScaledAddressingMode(AM, VT);
10304
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010305 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000010306 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010307 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010308 case MVT::i1:
10309 case MVT::i8:
10310 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010311 if (Scale < 0) Scale = -Scale;
10312 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010313 return true;
10314 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000010315 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000010316 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010317 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010318 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010319 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010320 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000010321 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010322
Owen Anderson9f944592009-08-11 20:47:22 +000010323 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010324 // Note, we allow "void" uses (basically, uses that aren't loads or
10325 // stores), because arm allows folding a scale into many arithmetic
10326 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000010327
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010328 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000010329 if (Scale & 1) return false;
10330 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010331 }
Evan Cheng2150b922007-03-12 23:30:29 +000010332 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010333 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000010334}
10335
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010336/// isLegalICmpImmediate - Return true if the specified immediate is legal
10337/// icmp immediate, that is the target has icmp instructions which can compare
10338/// a register against the immediate without having to materialize the
10339/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000010340bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010341 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010342 if (!Subtarget->isThumb())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010343 return ARM_AM::getSOImmVal(std::abs(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010344 if (Subtarget->isThumb2())
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010345 return ARM_AM::getT2SOImmVal(std::abs(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010346 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000010347 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010348}
10349
Andrew Tricka22cdb72012-07-18 18:34:27 +000010350/// isLegalAddImmediate - Return true if the specified immediate is a legal add
10351/// *or sub* immediate, that is the target has add or sub instructions which can
10352/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000010353/// immediate into a register.
10354bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000010355 // Same encoding for add/sub, just flip the sign.
Benjamin Kramer7bd1f7c2015-03-09 20:20:16 +000010356 int64_t AbsImm = std::abs(Imm);
Andrew Tricka22cdb72012-07-18 18:34:27 +000010357 if (!Subtarget->isThumb())
10358 return ARM_AM::getSOImmVal(AbsImm) != -1;
10359 if (Subtarget->isThumb2())
10360 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
10361 // Thumb1 only has 8-bit unsigned immediate.
10362 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000010363}
10364
Owen Anderson53aa7a92009-08-10 22:56:29 +000010365static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010366 bool isSEXTLoad, SDValue &Base,
10367 SDValue &Offset, bool &isInc,
10368 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000010369 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10370 return false;
10371
Owen Anderson9f944592009-08-11 20:47:22 +000010372 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000010373 // AddressingMode 3
10374 Base = Ptr->getOperand(0);
10375 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010376 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010377 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010378 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010379 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010380 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000010381 return true;
10382 }
10383 }
10384 isInc = (Ptr->getOpcode() == ISD::ADD);
10385 Offset = Ptr->getOperand(1);
10386 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000010387 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000010388 // AddressingMode 2
10389 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010390 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010391 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010392 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010393 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010394 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng10043e22007-01-19 07:51:42 +000010395 Base = Ptr->getOperand(0);
10396 return true;
10397 }
10398 }
10399
10400 if (Ptr->getOpcode() == ISD::ADD) {
10401 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000010402 ARM_AM::ShiftOpc ShOpcVal=
10403 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000010404 if (ShOpcVal != ARM_AM::no_shift) {
10405 Base = Ptr->getOperand(1);
10406 Offset = Ptr->getOperand(0);
10407 } else {
10408 Base = Ptr->getOperand(0);
10409 Offset = Ptr->getOperand(1);
10410 }
10411 return true;
10412 }
10413
10414 isInc = (Ptr->getOpcode() == ISD::ADD);
10415 Base = Ptr->getOperand(0);
10416 Offset = Ptr->getOperand(1);
10417 return true;
10418 }
10419
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000010420 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000010421 return false;
10422}
10423
Owen Anderson53aa7a92009-08-10 22:56:29 +000010424static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010425 bool isSEXTLoad, SDValue &Base,
10426 SDValue &Offset, bool &isInc,
10427 SelectionDAG &DAG) {
10428 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10429 return false;
10430
10431 Base = Ptr->getOperand(0);
10432 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
10433 int RHSC = (int)RHS->getZExtValue();
10434 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
10435 assert(Ptr->getOpcode() == ISD::ADD);
10436 isInc = false;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010437 Offset = DAG.getConstant(-RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000010438 return true;
10439 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
10440 isInc = Ptr->getOpcode() == ISD::ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010441 Offset = DAG.getConstant(RHSC, SDLoc(Ptr), RHS->getValueType(0));
Evan Cheng84c6cda2009-07-02 07:28:31 +000010442 return true;
10443 }
10444 }
10445
10446 return false;
10447}
10448
Evan Cheng10043e22007-01-19 07:51:42 +000010449/// getPreIndexedAddressParts - returns true by value, base pointer and
10450/// offset pointer and addressing mode by reference if the node's address
10451/// can be legally represented as pre-indexed load / store address.
10452bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010453ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
10454 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010455 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010456 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010457 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010458 return false;
10459
Owen Anderson53aa7a92009-08-10 22:56:29 +000010460 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010461 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010462 bool isSEXTLoad = false;
10463 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10464 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010465 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010466 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10467 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10468 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010469 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010470 } else
10471 return false;
10472
10473 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010474 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010475 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010476 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10477 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010478 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010479 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000010480 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000010481 if (!isLegal)
10482 return false;
10483
10484 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10485 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010486}
10487
10488/// getPostIndexedAddressParts - returns true by value, base pointer and
10489/// offset pointer and addressing mode by reference if this node can be
10490/// combined with a load / store to form a post-indexed load / store.
10491bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010492 SDValue &Base,
10493 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010494 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010495 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010496 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010497 return false;
10498
Owen Anderson53aa7a92009-08-10 22:56:29 +000010499 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010500 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010501 bool isSEXTLoad = false;
10502 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010503 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010504 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010505 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10506 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010507 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010508 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010509 } else
10510 return false;
10511
10512 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010513 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010514 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010515 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000010516 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010517 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010518 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10519 isInc, DAG);
10520 if (!isLegal)
10521 return false;
10522
Evan Chengf19384d2010-05-18 21:31:17 +000010523 if (Ptr != Base) {
10524 // Swap base ptr and offset to catch more post-index load / store when
10525 // it's legal. In Thumb2 mode, offset must be an immediate.
10526 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10527 !Subtarget->isThumb2())
10528 std::swap(Base, Offset);
10529
10530 // Post-indexed load / store update the base pointer.
10531 if (Ptr != Base)
10532 return false;
10533 }
10534
Evan Cheng84c6cda2009-07-02 07:28:31 +000010535 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10536 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010537}
10538
Jay Foada0653a32014-05-14 21:14:37 +000010539void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10540 APInt &KnownZero,
10541 APInt &KnownOne,
10542 const SelectionDAG &DAG,
10543 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000010544 unsigned BitWidth = KnownOne.getBitWidth();
10545 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000010546 switch (Op.getOpcode()) {
10547 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000010548 case ARMISD::ADDC:
10549 case ARMISD::ADDE:
10550 case ARMISD::SUBC:
10551 case ARMISD::SUBE:
10552 // These nodes' second result is a boolean
10553 if (Op.getResNo() == 0)
10554 break;
10555 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10556 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010557 case ARMISD::CMOV: {
10558 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000010559 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010560 if (KnownZero == 0 && KnownOne == 0) return;
10561
Dan Gohmanf990faf2008-02-13 00:35:47 +000010562 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000010563 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010564 KnownZero &= KnownZeroRHS;
10565 KnownOne &= KnownOneRHS;
10566 return;
10567 }
Tim Northover01b4aa92014-04-03 15:10:35 +000010568 case ISD::INTRINSIC_W_CHAIN: {
10569 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
10570 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
10571 switch (IntID) {
10572 default: return;
10573 case Intrinsic::arm_ldaex:
10574 case Intrinsic::arm_ldrex: {
10575 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
10576 unsigned MemBits = VT.getScalarType().getSizeInBits();
10577 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
10578 return;
10579 }
10580 }
10581 }
Evan Cheng10043e22007-01-19 07:51:42 +000010582 }
10583}
10584
10585//===----------------------------------------------------------------------===//
10586// ARM Inline Assembly Support
10587//===----------------------------------------------------------------------===//
10588
Evan Cheng078b0b02011-01-08 01:24:27 +000010589bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10590 // Looking for "rev" which is V6+.
10591 if (!Subtarget->hasV6Ops())
10592 return false;
10593
10594 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10595 std::string AsmStr = IA->getAsmString();
10596 SmallVector<StringRef, 4> AsmPieces;
10597 SplitString(AsmStr, AsmPieces, ";\n");
10598
10599 switch (AsmPieces.size()) {
10600 default: return false;
10601 case 1:
10602 AsmStr = AsmPieces[0];
10603 AsmPieces.clear();
10604 SplitString(AsmStr, AsmPieces, " \t,");
10605
10606 // rev $0, $1
10607 if (AsmPieces.size() == 3 &&
10608 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10609 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010610 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010611 if (Ty && Ty->getBitWidth() == 32)
10612 return IntrinsicLowering::LowerToByteSwap(CI);
10613 }
10614 break;
10615 }
10616
10617 return false;
10618}
10619
Evan Cheng10043e22007-01-19 07:51:42 +000010620/// getConstraintType - Given a constraint letter, return the type of
10621/// constraint it is for this target.
10622ARMTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +000010623ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
10624 if (Constraint.size() == 1) {
10625 switch (Constraint[0]) {
10626 default: break;
10627 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010628 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010629 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010630 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010631 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010632 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010633 // An address with a single base register. Due to the way we
10634 // currently handle addresses it is the same as an 'r' memory constraint.
10635 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010636 }
Eric Christophere256cd02011-06-21 22:10:57 +000010637 } else if (Constraint.size() == 2) {
10638 switch (Constraint[0]) {
10639 default: break;
10640 // All 'U+' constraints are addresses.
10641 case 'U': return C_Memory;
10642 }
Evan Cheng10043e22007-01-19 07:51:42 +000010643 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010644 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010645}
10646
John Thompsone8360b72010-10-29 17:29:13 +000010647/// Examine constraint type and operand type and determine a weight value.
10648/// This object must already have been set up with the operand type
10649/// and the current alternative constraint selected.
10650TargetLowering::ConstraintWeight
10651ARMTargetLowering::getSingleConstraintMatchWeight(
10652 AsmOperandInfo &info, const char *constraint) const {
10653 ConstraintWeight weight = CW_Invalid;
10654 Value *CallOperandVal = info.CallOperandVal;
10655 // If we don't have a value, we can't do a match,
10656 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010657 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010658 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010659 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010660 // Look at the constraint type.
10661 switch (*constraint) {
10662 default:
10663 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10664 break;
10665 case 'l':
10666 if (type->isIntegerTy()) {
10667 if (Subtarget->isThumb())
10668 weight = CW_SpecificReg;
10669 else
10670 weight = CW_Register;
10671 }
10672 break;
10673 case 'w':
10674 if (type->isFloatingPointTy())
10675 weight = CW_Register;
10676 break;
10677 }
10678 return weight;
10679}
10680
Eric Christophercf2007c2011-06-30 23:50:52 +000010681typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10682RCPair
Eric Christopher11e4df72015-02-26 22:38:43 +000010683ARMTargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
10684 const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000010685 MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000010686 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010687 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000010688 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000010689 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010690 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010691 return RCPair(0U, &ARM::tGPRRegClass);
10692 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000010693 case 'h': // High regs or no regs.
10694 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010695 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000010696 break;
Chris Lattner6223e832007-04-02 17:24:08 +000010697 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000010698 if (Subtarget->isThumb1Only())
10699 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000010700 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010701 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000010702 if (VT == MVT::Other)
10703 break;
Owen Anderson9f944592009-08-11 20:47:22 +000010704 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010705 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000010706 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010707 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000010708 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010709 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010710 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010711 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000010712 if (VT == MVT::Other)
10713 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010714 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010715 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010716 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010717 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010718 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010719 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010720 break;
Eric Christopherc011d312011-07-01 00:30:46 +000010721 case 't':
10722 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010723 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000010724 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010725 }
10726 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000010727 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000010728 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000010729
Eric Christopher11e4df72015-02-26 22:38:43 +000010730 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Evan Cheng10043e22007-01-19 07:51:42 +000010731}
10732
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010733/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10734/// vector. If it is invalid, don't add anything to Ops.
10735void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010736 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010737 std::vector<SDValue>&Ops,
10738 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000010739 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010740
Eric Christopherde9399b2011-06-02 23:16:42 +000010741 // Currently only support length 1 constraints.
10742 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010743
Eric Christopherde9399b2011-06-02 23:16:42 +000010744 char ConstraintLetter = Constraint[0];
10745 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010746 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000010747 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010748 case 'I': case 'J': case 'K': case 'L':
10749 case 'M': case 'N': case 'O':
10750 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10751 if (!C)
10752 return;
10753
10754 int64_t CVal64 = C->getSExtValue();
10755 int CVal = (int) CVal64;
10756 // None of these constraints allow values larger than 32 bits. Check
10757 // that the value fits in an int.
10758 if (CVal != CVal64)
10759 return;
10760
Eric Christopherde9399b2011-06-02 23:16:42 +000010761 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000010762 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000010763 // Constant suitable for movw, must be between 0 and
10764 // 65535.
10765 if (Subtarget->hasV6T2Ops())
10766 if (CVal >= 0 && CVal <= 65535)
10767 break;
10768 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010769 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000010770 if (Subtarget->isThumb1Only()) {
10771 // This must be a constant between 0 and 255, for ADD
10772 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010773 if (CVal >= 0 && CVal <= 255)
10774 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010775 } else if (Subtarget->isThumb2()) {
10776 // A constant that can be used as an immediate value in a
10777 // data-processing instruction.
10778 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10779 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010780 } else {
10781 // A constant that can be used as an immediate value in a
10782 // data-processing instruction.
10783 if (ARM_AM::getSOImmVal(CVal) != -1)
10784 break;
10785 }
10786 return;
10787
10788 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000010789 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010790 // This must be a constant between -255 and -1, for negated ADD
10791 // immediates. This can be used in GCC with an "n" modifier that
10792 // prints the negated value, for use with SUB instructions. It is
10793 // not useful otherwise but is implemented for compatibility.
10794 if (CVal >= -255 && CVal <= -1)
10795 break;
10796 } else {
10797 // This must be a constant between -4095 and 4095. It is not clear
10798 // what this constraint is intended for. Implemented for
10799 // compatibility with GCC.
10800 if (CVal >= -4095 && CVal <= 4095)
10801 break;
10802 }
10803 return;
10804
10805 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000010806 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010807 // A 32-bit value where only one byte has a nonzero value. Exclude
10808 // zero to match GCC. This constraint is used by GCC internally for
10809 // constants that can be loaded with a move/shift combination.
10810 // It is not useful otherwise but is implemented for compatibility.
10811 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10812 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010813 } else if (Subtarget->isThumb2()) {
10814 // A constant whose bitwise inverse can be used as an immediate
10815 // value in a data-processing instruction. This can be used in GCC
10816 // with a "B" modifier that prints the inverted value, for use with
10817 // BIC and MVN instructions. It is not useful otherwise but is
10818 // implemented for compatibility.
10819 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10820 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010821 } else {
10822 // A constant whose bitwise inverse can be used as an immediate
10823 // value in a data-processing instruction. This can be used in GCC
10824 // with a "B" modifier that prints the inverted value, for use with
10825 // BIC and MVN instructions. It is not useful otherwise but is
10826 // implemented for compatibility.
10827 if (ARM_AM::getSOImmVal(~CVal) != -1)
10828 break;
10829 }
10830 return;
10831
10832 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000010833 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010834 // This must be a constant between -7 and 7,
10835 // for 3-operand ADD/SUB immediate instructions.
10836 if (CVal >= -7 && CVal < 7)
10837 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010838 } else if (Subtarget->isThumb2()) {
10839 // A constant whose negation can be used as an immediate value in a
10840 // data-processing instruction. This can be used in GCC with an "n"
10841 // modifier that prints the negated value, for use with SUB
10842 // instructions. It is not useful otherwise but is implemented for
10843 // compatibility.
10844 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10845 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010846 } else {
10847 // A constant whose negation can be used as an immediate value in a
10848 // data-processing instruction. This can be used in GCC with an "n"
10849 // modifier that prints the negated value, for use with SUB
10850 // instructions. It is not useful otherwise but is implemented for
10851 // compatibility.
10852 if (ARM_AM::getSOImmVal(-CVal) != -1)
10853 break;
10854 }
10855 return;
10856
10857 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000010858 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010859 // This must be a multiple of 4 between 0 and 1020, for
10860 // ADD sp + immediate.
10861 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10862 break;
10863 } else {
10864 // A power of two or a constant between 0 and 32. This is used in
10865 // GCC for the shift amount on shifted register operands, but it is
10866 // useful in general for any shift amounts.
10867 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10868 break;
10869 }
10870 return;
10871
10872 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000010873 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010874 // This must be a constant between 0 and 31, for shift amounts.
10875 if (CVal >= 0 && CVal <= 31)
10876 break;
10877 }
10878 return;
10879
10880 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000010881 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010882 // This must be a multiple of 4 between -508 and 508, for
10883 // ADD/SUB sp = sp + immediate.
10884 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10885 break;
10886 }
10887 return;
10888 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010889 Result = DAG.getTargetConstant(CVal, SDLoc(Op), Op.getValueType());
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010890 break;
10891 }
10892
10893 if (Result.getNode()) {
10894 Ops.push_back(Result);
10895 return;
10896 }
Dale Johannesence97d552010-06-25 21:55:36 +000010897 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010898}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010899
Renato Golin87610692013-07-16 09:32:17 +000010900SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
10901 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only");
10902 unsigned Opcode = Op->getOpcode();
10903 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000010904 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000010905 bool isSigned = (Opcode == ISD::SDIVREM);
10906 EVT VT = Op->getValueType(0);
10907 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
10908
10909 RTLIB::Libcall LC;
10910 switch (VT.getSimpleVT().SimpleTy) {
10911 default: llvm_unreachable("Unexpected request for libcall!");
Saleem Abdulrasool740be892014-08-17 22:50:59 +000010912 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
10913 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
10914 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
10915 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
Renato Golin87610692013-07-16 09:32:17 +000010916 }
10917
10918 SDValue InChain = DAG.getEntryNode();
10919
10920 TargetLowering::ArgListTy Args;
10921 TargetLowering::ArgListEntry Entry;
10922 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
10923 EVT ArgVT = Op->getOperand(i).getValueType();
10924 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
10925 Entry.Node = Op->getOperand(i);
10926 Entry.Ty = ArgTy;
10927 Entry.isSExt = isSigned;
10928 Entry.isZExt = !isSigned;
10929 Args.push_back(Entry);
10930 }
10931
10932 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
10933 getPointerTy());
10934
Reid Kleckner343c3952014-11-20 23:51:47 +000010935 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000010936
10937 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010938 TargetLowering::CallLoweringInfo CLI(DAG);
10939 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000010940 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010941 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000010942
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010943 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000010944 return CallInfo.first;
10945}
10946
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000010947SDValue
10948ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
10949 assert(Subtarget->isTargetWindows() && "unsupported target platform");
10950 SDLoc DL(Op);
10951
10952 // Get the inputs.
10953 SDValue Chain = Op.getOperand(0);
10954 SDValue Size = Op.getOperand(1);
10955
10956 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000010957 DAG.getConstant(2, DL, MVT::i32));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000010958
10959 SDValue Flag;
10960 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
10961 Flag = Chain.getValue(1);
10962
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000010963 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000010964 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
10965
10966 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
10967 Chain = NewSP.getValue(1);
10968
10969 SDValue Ops[2] = { NewSP, Chain };
10970 return DAG.getMergeValues(Ops, DL);
10971}
10972
Oliver Stannard51b1d462014-08-21 12:50:31 +000010973SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
10974 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
10975 "Unexpected type for custom-lowering FP_EXTEND");
10976
10977 RTLIB::Libcall LC;
10978 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
10979
10980 SDValue SrcVal = Op.getOperand(0);
10981 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
10982 /*isSigned*/ false, SDLoc(Op)).first;
10983}
10984
10985SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
10986 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
10987 Subtarget->isFPOnlySP() &&
10988 "Unexpected type for custom-lowering FP_ROUND");
10989
10990 RTLIB::Libcall LC;
10991 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
10992
10993 SDValue SrcVal = Op.getOperand(0);
10994 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
10995 /*isSigned*/ false, SDLoc(Op)).first;
10996}
10997
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010998bool
10999ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
11000 // The ARM target isn't yet aware of offsets.
11001 return false;
11002}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011003
Jim Grosbach11013ed2010-07-16 23:05:05 +000011004bool ARM::isBitFieldInvertedMask(unsigned v) {
11005 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000011006 return false;
11007
Jim Grosbach11013ed2010-07-16 23:05:05 +000011008 // there can be 1's on either or both "outsides", all the "inside"
11009 // bits must be 0's
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000011010 return isShiftedMask_32(~v);
Jim Grosbach11013ed2010-07-16 23:05:05 +000011011}
11012
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011013/// isFPImmLegal - Returns true if the target can instruction select the
11014/// specified FP immediate natively. If false, the legalizer will
11015/// materialize the FP immediate as a load from a constant pool.
11016bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
11017 if (!Subtarget->hasVFP3())
11018 return false;
11019 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000011020 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000011021 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000011022 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000011023 return false;
11024}
Bob Wilson5549d492010-09-21 17:56:22 +000011025
Wesley Peck527da1b2010-11-23 03:31:01 +000011026/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000011027/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
11028/// specified in the intrinsic calls.
11029bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
11030 const CallInst &I,
11031 unsigned Intrinsic) const {
11032 switch (Intrinsic) {
11033 case Intrinsic::arm_neon_vld1:
11034 case Intrinsic::arm_neon_vld2:
11035 case Intrinsic::arm_neon_vld3:
11036 case Intrinsic::arm_neon_vld4:
11037 case Intrinsic::arm_neon_vld2lane:
11038 case Intrinsic::arm_neon_vld3lane:
11039 case Intrinsic::arm_neon_vld4lane: {
11040 Info.opc = ISD::INTRINSIC_W_CHAIN;
11041 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmowcdfe20b2012-10-08 16:38:25 +000011042 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000011043 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11044 Info.ptrVal = I.getArgOperand(0);
11045 Info.offset = 0;
11046 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11047 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11048 Info.vol = false; // volatile loads with NEON intrinsics not supported
11049 Info.readMem = true;
11050 Info.writeMem = false;
11051 return true;
11052 }
11053 case Intrinsic::arm_neon_vst1:
11054 case Intrinsic::arm_neon_vst2:
11055 case Intrinsic::arm_neon_vst3:
11056 case Intrinsic::arm_neon_vst4:
11057 case Intrinsic::arm_neon_vst2lane:
11058 case Intrinsic::arm_neon_vst3lane:
11059 case Intrinsic::arm_neon_vst4lane: {
11060 Info.opc = ISD::INTRINSIC_VOID;
11061 // Conservatively set memVT to the entire set of vectors stored.
11062 unsigned NumElts = 0;
11063 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000011064 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000011065 if (!ArgTy->isVectorTy())
11066 break;
Micah Villmowcdfe20b2012-10-08 16:38:25 +000011067 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000011068 }
11069 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11070 Info.ptrVal = I.getArgOperand(0);
11071 Info.offset = 0;
11072 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11073 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11074 Info.vol = false; // volatile stores with NEON intrinsics not supported
11075 Info.readMem = false;
11076 Info.writeMem = true;
11077 return true;
11078 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011079 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011080 case Intrinsic::arm_ldrex: {
11081 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
11082 Info.opc = ISD::INTRINSIC_W_CHAIN;
11083 Info.memVT = MVT::getVT(PtrTy->getElementType());
11084 Info.ptrVal = I.getArgOperand(0);
11085 Info.offset = 0;
11086 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
11087 Info.vol = true;
11088 Info.readMem = true;
11089 Info.writeMem = false;
11090 return true;
11091 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011092 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011093 case Intrinsic::arm_strex: {
11094 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
11095 Info.opc = ISD::INTRINSIC_W_CHAIN;
11096 Info.memVT = MVT::getVT(PtrTy->getElementType());
11097 Info.ptrVal = I.getArgOperand(1);
11098 Info.offset = 0;
11099 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
11100 Info.vol = true;
11101 Info.readMem = false;
11102 Info.writeMem = true;
11103 return true;
11104 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011105 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011106 case Intrinsic::arm_strexd: {
11107 Info.opc = ISD::INTRINSIC_W_CHAIN;
11108 Info.memVT = MVT::i64;
11109 Info.ptrVal = I.getArgOperand(2);
11110 Info.offset = 0;
11111 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011112 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011113 Info.readMem = false;
11114 Info.writeMem = true;
11115 return true;
11116 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011117 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011118 case Intrinsic::arm_ldrexd: {
11119 Info.opc = ISD::INTRINSIC_W_CHAIN;
11120 Info.memVT = MVT::i64;
11121 Info.ptrVal = I.getArgOperand(0);
11122 Info.offset = 0;
11123 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011124 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011125 Info.readMem = true;
11126 Info.writeMem = false;
11127 return true;
11128 }
Bob Wilson5549d492010-09-21 17:56:22 +000011129 default:
11130 break;
11131 }
11132
11133 return false;
11134}
Juergen Ributzka659ce002014-01-28 01:20:14 +000011135
11136/// \brief Returns true if it is beneficial to convert a load of a constant
11137/// to just the constant itself.
11138bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
11139 Type *Ty) const {
11140 assert(Ty->isIntegerTy());
11141
11142 unsigned Bits = Ty->getPrimitiveSizeInBits();
11143 if (Bits == 0 || Bits > 32)
11144 return false;
11145 return true;
11146}
Tim Northover037f26f22014-04-17 18:22:47 +000011147
Robin Morisset25c8e312014-09-17 00:06:58 +000011148bool ARMTargetLowering::hasLoadLinkedStoreConditional() const { return true; }
11149
Robin Morisset5349e8e2014-09-18 18:56:04 +000011150Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
11151 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000011152 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000011153
11154 // First, if the target has no DMB, see what fallback we can use.
11155 if (!Subtarget->hasDataBarrier()) {
11156 // Some ARMv6 cpus can support data barriers with an mcr instruction.
11157 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
11158 // here.
11159 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
11160 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
11161 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
11162 Builder.getInt32(0), Builder.getInt32(7),
11163 Builder.getInt32(10), Builder.getInt32(5)};
11164 return Builder.CreateCall(MCR, args);
11165 } else {
11166 // Instead of using barriers, atomic accesses on these subtargets use
11167 // libcalls.
11168 llvm_unreachable("makeDMB on a target so old that it has no barriers");
11169 }
11170 } else {
11171 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
11172 // Only a full system barrier exists in the M-class architectures.
11173 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
11174 Constant *CDomain = Builder.getInt32(Domain);
11175 return Builder.CreateCall(DMB, CDomain);
11176 }
Robin Morisseta47cb412014-09-03 21:01:03 +000011177}
11178
11179// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000011180Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011181 AtomicOrdering Ord, bool IsStore,
11182 bool IsLoad) const {
11183 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011184 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011185
11186 switch (Ord) {
11187 case NotAtomic:
11188 case Unordered:
11189 llvm_unreachable("Invalid fence: unordered/non-atomic");
11190 case Monotonic:
11191 case Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000011192 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011193 case SequentiallyConsistent:
11194 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000011195 return nullptr; // Nothing to do
11196 /*FALLTHROUGH*/
Robin Morisseta47cb412014-09-03 21:01:03 +000011197 case Release:
11198 case AcquireRelease:
11199 if (Subtarget->isSwift())
Robin Morissetdedef332014-09-23 20:31:14 +000011200 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000011201 // FIXME: add a comment with a link to documentation justifying this.
11202 else
Robin Morissetdedef332014-09-23 20:31:14 +000011203 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011204 }
Robin Morissetdedef332014-09-23 20:31:14 +000011205 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011206}
11207
Robin Morissetdedef332014-09-23 20:31:14 +000011208Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011209 AtomicOrdering Ord, bool IsStore,
11210 bool IsLoad) const {
11211 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011212 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011213
11214 switch (Ord) {
11215 case NotAtomic:
11216 case Unordered:
11217 llvm_unreachable("Invalid fence: unordered/not-atomic");
11218 case Monotonic:
11219 case Release:
Robin Morissetdedef332014-09-23 20:31:14 +000011220 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011221 case Acquire:
11222 case AcquireRelease:
Robin Morissetdedef332014-09-23 20:31:14 +000011223 case SequentiallyConsistent:
11224 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011225 }
Robin Morissetdedef332014-09-23 20:31:14 +000011226 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011227}
11228
Robin Morisseted3d48f2014-09-03 21:29:59 +000011229// Loads and stores less than 64-bits are already atomic; ones above that
11230// are doomed anyway, so defer to the default libcall and blame the OS when
11231// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11232// anything for those.
11233bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
11234 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
11235 return (Size == 64) && !Subtarget->isMClass();
11236}
Tim Northover037f26f22014-04-17 18:22:47 +000011237
Robin Morisseted3d48f2014-09-03 21:29:59 +000011238// Loads and stores less than 64-bits are already atomic; ones above that
11239// are doomed anyway, so defer to the default libcall and blame the OS when
11240// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11241// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000011242// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
11243// guarantee, see DDI0406C ARM architecture reference manual,
11244// sections A8.8.72-74 LDRD)
Robin Morisseted3d48f2014-09-03 21:29:59 +000011245bool ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
11246 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
11247 return (Size == 64) && !Subtarget->isMClass();
11248}
11249
11250// For the real atomic operations, we have ldrex/strex up to 32 bits,
11251// and up to 64 bits on the non-M profiles
JF Bastienf14889e2015-03-04 15:47:57 +000011252TargetLoweringBase::AtomicRMWExpansionKind
11253ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
Robin Morisseted3d48f2014-09-03 21:29:59 +000011254 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
JF Bastienf14889e2015-03-04 15:47:57 +000011255 return (Size <= (Subtarget->isMClass() ? 32U : 64U))
11256 ? AtomicRMWExpansionKind::LLSC
11257 : AtomicRMWExpansionKind::None;
Tim Northover037f26f22014-04-17 18:22:47 +000011258}
11259
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011260// This has so far only been implemented for MachO.
11261bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000011262 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011263}
11264
Quentin Colombetc32615d2014-10-31 17:52:53 +000011265bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
11266 unsigned &Cost) const {
11267 // If we do not have NEON, vector types are not natively supported.
11268 if (!Subtarget->hasNEON())
11269 return false;
11270
11271 // Floating point values and vector values map to the same register file.
11272 // Therefore, althought we could do a store extract of a vector type, this is
11273 // better to leave at float as we have more freedom in the addressing mode for
11274 // those.
11275 if (VectorTy->isFPOrFPVectorTy())
11276 return false;
11277
11278 // If the index is unknown at compile time, this is very expensive to lower
11279 // and it is not possible to combine the store with the extract.
11280 if (!isa<ConstantInt>(Idx))
11281 return false;
11282
11283 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
11284 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
11285 // We can do a store + vector extract on any vector that fits perfectly in a D
11286 // or Q register.
11287 if (BitWidth == 64 || BitWidth == 128) {
11288 Cost = 0;
11289 return true;
11290 }
11291 return false;
11292}
11293
Tim Northover037f26f22014-04-17 18:22:47 +000011294Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
11295 AtomicOrdering Ord) const {
11296 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
11297 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
Robin Morissetb155f522014-08-18 16:48:58 +000011298 bool IsAcquire = isAtLeastAcquire(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011299
11300 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
11301 // intrinsic must return {i32, i32} and we have to recombine them into a
11302 // single i64 here.
11303 if (ValTy->getPrimitiveSizeInBits() == 64) {
11304 Intrinsic::ID Int =
11305 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
11306 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
11307
11308 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11309 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
11310
11311 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
11312 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011313 if (!Subtarget->isLittle())
11314 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011315 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
11316 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
11317 return Builder.CreateOr(
11318 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
11319 }
11320
11321 Type *Tys[] = { Addr->getType() };
11322 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
11323 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
11324
11325 return Builder.CreateTruncOrBitCast(
11326 Builder.CreateCall(Ldrex, Addr),
11327 cast<PointerType>(Addr->getType())->getElementType());
11328}
11329
11330Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
11331 Value *Addr,
11332 AtomicOrdering Ord) const {
11333 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morissetb155f522014-08-18 16:48:58 +000011334 bool IsRelease = isAtLeastRelease(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011335
11336 // Since the intrinsics must have legal type, the i64 intrinsics take two
11337 // parameters: "i32, i32". We must marshal Val into the appropriate form
11338 // before the call.
11339 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
11340 Intrinsic::ID Int =
11341 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
11342 Function *Strex = Intrinsic::getDeclaration(M, Int);
11343 Type *Int32Ty = Type::getInt32Ty(M->getContext());
11344
11345 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
11346 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011347 if (!Subtarget->isLittle())
11348 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011349 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
David Blaikieff6409d2015-05-18 22:13:54 +000011350 return Builder.CreateCall(Strex, {Lo, Hi, Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000011351 }
11352
11353 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
11354 Type *Tys[] = { Addr->getType() };
11355 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
11356
David Blaikieff6409d2015-05-18 22:13:54 +000011357 return Builder.CreateCall(
11358 Strex, {Builder.CreateZExtOrBitCast(
11359 Val, Strex->getFunctionType()->getParamType(0)),
11360 Addr});
Tim Northover037f26f22014-04-17 18:22:47 +000011361}
Oliver Stannardc24f2172014-05-09 14:01:47 +000011362
11363enum HABaseType {
11364 HA_UNKNOWN = 0,
11365 HA_FLOAT,
11366 HA_DOUBLE,
11367 HA_VECT64,
11368 HA_VECT128
11369};
11370
11371static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
11372 uint64_t &Members) {
11373 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
11374 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
11375 uint64_t SubMembers = 0;
11376 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
11377 return false;
11378 Members += SubMembers;
11379 }
11380 } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
11381 uint64_t SubMembers = 0;
11382 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
11383 return false;
11384 Members += SubMembers * AT->getNumElements();
11385 } else if (Ty->isFloatTy()) {
11386 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
11387 return false;
11388 Members = 1;
11389 Base = HA_FLOAT;
11390 } else if (Ty->isDoubleTy()) {
11391 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
11392 return false;
11393 Members = 1;
11394 Base = HA_DOUBLE;
11395 } else if (const VectorType *VT = dyn_cast<VectorType>(Ty)) {
11396 Members = 1;
11397 switch (Base) {
11398 case HA_FLOAT:
11399 case HA_DOUBLE:
11400 return false;
11401 case HA_VECT64:
11402 return VT->getBitWidth() == 64;
11403 case HA_VECT128:
11404 return VT->getBitWidth() == 128;
11405 case HA_UNKNOWN:
11406 switch (VT->getBitWidth()) {
11407 case 64:
11408 Base = HA_VECT64;
11409 return true;
11410 case 128:
11411 Base = HA_VECT128;
11412 return true;
11413 default:
11414 return false;
11415 }
11416 }
11417 }
11418
11419 return (Members > 0 && Members <= 4);
11420}
11421
Tim Northovere95c5b32015-02-24 17:22:34 +000011422/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate or one of
11423/// [N x i32] or [N x i64]. This allows front-ends to skip emitting padding when
11424/// passing according to AAPCS rules.
Oliver Stannardc24f2172014-05-09 14:01:47 +000011425bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
11426 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000011427 if (getEffectiveCallingConv(CallConv, isVarArg) !=
11428 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000011429 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000011430
11431 HABaseType Base = HA_UNKNOWN;
11432 uint64_t Members = 0;
Tim Northovere95c5b32015-02-24 17:22:34 +000011433 bool IsHA = isHomogeneousAggregate(Ty, Base, Members);
11434 DEBUG(dbgs() << "isHA: " << IsHA << " "; Ty->dump());
11435
11436 bool IsIntArray = Ty->isArrayTy() && Ty->getArrayElementType()->isIntegerTy();
11437 return IsHA || IsIntArray;
Oliver Stannardc24f2172014-05-09 14:01:47 +000011438}