blob: eab513e1d358009225d433dfc7d2b29a05bd5719 [file] [log] [blame]
Evan Cheng10043e22007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Cheng10043e22007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Craig Topper188ed9d2012-03-17 07:33:42 +000015#include "ARMISelLowering.h"
Eric Christopher1c069172010-09-10 22:42:06 +000016#include "ARMCallingConv.h"
Evan Cheng10043e22007-01-19 07:51:42 +000017#include "ARMConstantPoolValue.h"
Evan Cheng10043e22007-01-19 07:51:42 +000018#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov9a232f42009-08-21 12:41:24 +000019#include "ARMPerfectShuffle.h"
Evan Cheng10043e22007-01-19 07:51:42 +000020#include "ARMSubtarget.h"
21#include "ARMTargetMachine.h"
Chris Lattner4e7dfaf2009-08-02 00:34:36 +000022#include "ARMTargetObjectFile.h"
Evan Chenga20cde32011-07-20 23:34:39 +000023#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/ADT/Statistic.h"
25#include "llvm/ADT/StringExtras.h"
Bob Wilsona4c22902009-04-17 19:07:39 +000026#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng078b0b02011-01-08 01:24:27 +000027#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Cheng10043e22007-01-19 07:51:42 +000028#include "llvm/CodeGen/MachineBasicBlock.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000032#include "llvm/CodeGen/MachineJumpTableInfo.h"
Bill Wendling202803e2011-10-05 00:02:33 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng10043e22007-01-19 07:51:42 +000035#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000036#include "llvm/IR/CallingConv.h"
37#include "llvm/IR/Constants.h"
38#include "llvm/IR/Function.h"
39#include "llvm/IR/GlobalValue.h"
Tim Northover037f26f22014-04-17 18:22:47 +000040#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000041#include "llvm/IR/Instruction.h"
42#include "llvm/IR/Instructions.h"
43#include "llvm/IR/Intrinsics.h"
44#include "llvm/IR/Type.h"
Bill Wendling46ffefc2010-03-09 02:46:12 +000045#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach32bb3622010-04-14 22:28:31 +000046#include "llvm/Support/CommandLine.h"
Oliver Stannardc24f2172014-05-09 14:01:47 +000047#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000048#include "llvm/Support/ErrorHandling.h"
Evan Cheng2150b922007-03-12 23:30:29 +000049#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000050#include "llvm/Target/TargetOptions.h"
David Peixottoc32e24a2013-10-17 19:49:22 +000051#include <utility>
Evan Cheng10043e22007-01-19 07:51:42 +000052using namespace llvm;
53
Chandler Carruth84e68b22014-04-22 02:41:26 +000054#define DEBUG_TYPE "arm-isel"
55
Dale Johannesend679ff72010-06-03 21:09:53 +000056STATISTIC(NumTailCalls, "Number of tail calls");
Evan Cheng68aec142011-01-19 02:16:49 +000057STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren9f911162012-06-01 02:44:42 +000058STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesend679ff72010-06-03 21:09:53 +000059
Eric Christopher347f4c32010-12-15 23:47:29 +000060cl::opt<bool>
Jim Grosbach32bb3622010-04-14 22:28:31 +000061EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng25f93642010-07-08 02:08:50 +000062 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbach32bb3622010-04-14 22:28:31 +000063 cl::init(false));
64
Evan Chengf128bdc2010-06-16 07:35:02 +000065static cl::opt<bool>
66ARMInterworking("arm-interworking", cl::Hidden,
67 cl::desc("Enable / disable ARM interworking (for debugging only)"),
68 cl::init(true));
69
Benjamin Kramer7ba71be2011-11-26 23:01:57 +000070namespace {
Cameron Zwarich89019782011-06-10 20:59:24 +000071 class ARMCCState : public CCState {
72 public:
73 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Eric Christopherb5217502014-08-06 18:45:26 +000074 SmallVectorImpl<CCValAssign> &locs, LLVMContext &C,
75 ParmContext PC)
76 : CCState(CC, isVarArg, MF, locs, C) {
Cameron Zwarich89019782011-06-10 20:59:24 +000077 assert(((PC == Call) || (PC == Prologue)) &&
78 "ARMCCState users must specify whether their context is call"
79 "or prologue generation.");
80 CallOrPrologue = PC;
81 }
82 };
83}
84
Stuart Hastings45fe3c32011-04-20 16:47:52 +000085// The APCS parameter registers.
Craig Topper840beec2014-04-04 05:16:06 +000086static const MCPhysReg GPRArgRegs[] = {
Stuart Hastings45fe3c32011-04-20 16:47:52 +000087 ARM::R0, ARM::R1, ARM::R2, ARM::R3
88};
89
Craig Topper4fa625f2012-08-12 03:16:37 +000090void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
91 MVT PromotedBitwiseVT) {
Bob Wilson2e076c42009-06-22 23:27:02 +000092 if (VT != PromotedLdStVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +000093 setOperationAction(ISD::LOAD, VT, Promote);
94 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000095
Craig Topper4fa625f2012-08-12 03:16:37 +000096 setOperationAction(ISD::STORE, VT, Promote);
97 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000098 }
99
Craig Topper4fa625f2012-08-12 03:16:37 +0000100 MVT ElemTy = VT.getVectorElementType();
Owen Anderson9f944592009-08-11 20:47:22 +0000101 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Craig Topper4fa625f2012-08-12 03:16:37 +0000102 setOperationAction(ISD::SETCC, VT, Custom);
103 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
104 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000105 if (ElemTy == MVT::i32) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000106 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
107 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
108 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
109 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000110 } else {
Craig Topper4fa625f2012-08-12 03:16:37 +0000111 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
112 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
113 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
114 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson5d8cfb22009-09-16 20:20:44 +0000115 }
Craig Topper4fa625f2012-08-12 03:16:37 +0000116 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
117 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
118 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
119 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
120 setOperationAction(ISD::SELECT, VT, Expand);
121 setOperationAction(ISD::SELECT_CC, VT, Expand);
Jim Grosbach30af4422012-10-12 22:59:21 +0000122 setOperationAction(ISD::VSELECT, VT, Expand);
Craig Topper4fa625f2012-08-12 03:16:37 +0000123 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000124 if (VT.isInteger()) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000125 setOperationAction(ISD::SHL, VT, Custom);
126 setOperationAction(ISD::SRA, VT, Custom);
127 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson2e076c42009-06-22 23:27:02 +0000128 }
129
130 // Promote all bit-wise operations.
131 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000132 setOperationAction(ISD::AND, VT, Promote);
133 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
134 setOperationAction(ISD::OR, VT, Promote);
135 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
136 setOperationAction(ISD::XOR, VT, Promote);
137 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000138 }
Bob Wilson4ed397c2009-09-16 00:17:28 +0000139
140 // Neon does not support vector divide/remainder operations.
Craig Topper4fa625f2012-08-12 03:16:37 +0000141 setOperationAction(ISD::SDIV, VT, Expand);
142 setOperationAction(ISD::UDIV, VT, Expand);
143 setOperationAction(ISD::FDIV, VT, Expand);
144 setOperationAction(ISD::SREM, VT, Expand);
145 setOperationAction(ISD::UREM, VT, Expand);
146 setOperationAction(ISD::FREM, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000147}
148
Craig Topper4fa625f2012-08-12 03:16:37 +0000149void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000150 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000151 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000152}
153
Craig Topper4fa625f2012-08-12 03:16:37 +0000154void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000155 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000156 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000157}
158
Eric Christopher5312afe2014-10-03 00:17:59 +0000159ARMTargetLowering::ARMTargetLowering(const TargetMachine &TM)
Aditya Nandakumar30531552014-11-13 21:29:21 +0000160 : TargetLowering(TM) {
Evan Cheng10043e22007-01-19 07:51:42 +0000161 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Eric Christopherd9134482014-08-04 21:25:23 +0000162 RegInfo = TM.getSubtargetImpl()->getRegisterInfo();
163 Itins = TM.getSubtargetImpl()->getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000164
Duncan Sandsf2641e12011-09-06 19:07:46 +0000165 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
166
Tim Northoverd6a729b2014-01-06 14:28:05 +0000167 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000168 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000169 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
Tim Northover978d25f2014-04-22 10:10:09 +0000170 Subtarget->hasARMOps() && !TM.Options.UseSoftFloat) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000171 // Single-precision floating-point arithmetic.
172 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
173 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
174 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
175 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000176
Evan Chengc9f22fd12007-04-27 08:15:43 +0000177 // Double-precision floating-point arithmetic.
178 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
179 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
180 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
181 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng143576d2007-01-31 09:30:58 +0000182
Evan Chengc9f22fd12007-04-27 08:15:43 +0000183 // Single-precision comparisons.
184 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
185 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
186 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
187 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
188 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
189 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
190 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
191 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000192
Evan Chengc9f22fd12007-04-27 08:15:43 +0000193 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
194 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
195 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
196 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
197 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
198 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
199 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
200 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng143576d2007-01-31 09:30:58 +0000201
Evan Chengc9f22fd12007-04-27 08:15:43 +0000202 // Double-precision comparisons.
203 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
204 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
205 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
206 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
207 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
208 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
209 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
210 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000211
Evan Chengc9f22fd12007-04-27 08:15:43 +0000212 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
213 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
214 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
215 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
216 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
217 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
218 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
219 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Cheng10043e22007-01-19 07:51:42 +0000220
Evan Chengc9f22fd12007-04-27 08:15:43 +0000221 // Floating-point to integer conversions.
222 // i64 conversions are done via library routines even when generating VFP
223 // instructions, so use the same ones.
224 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
225 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
226 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
227 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000228
Evan Chengc9f22fd12007-04-27 08:15:43 +0000229 // Conversions between floating types.
230 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
231 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
232
233 // Integer to floating-point conversions.
234 // i64 conversions are done via library routines even when generating VFP
235 // instructions, so use the same ones.
Bob Wilsondc40d5a2009-03-20 23:16:43 +0000236 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
237 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengc9f22fd12007-04-27 08:15:43 +0000238 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
239 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
240 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
241 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
242 }
Evan Cheng10043e22007-01-19 07:51:42 +0000243 }
244
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000245 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000246 setLibcallName(RTLIB::SHL_I128, nullptr);
247 setLibcallName(RTLIB::SRL_I128, nullptr);
248 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000249
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000250 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetMachO() &&
251 !Subtarget->isTargetWindows()) {
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000252 static const struct {
253 const RTLIB::Libcall Op;
254 const char * const Name;
255 const CallingConv::ID CC;
256 const ISD::CondCode Cond;
257 } LibraryCalls[] = {
258 // Double-precision floating-point arithmetic helper functions
259 // RTABI chapter 4.1.2, Table 2
260 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
261 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
262 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
263 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000264
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000265 // Double-precision floating-point comparison helper functions
266 // RTABI chapter 4.1.2, Table 3
267 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
268 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
269 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
270 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
271 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
272 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
273 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
274 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000275
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000276 // Single-precision floating-point arithmetic helper functions
277 // RTABI chapter 4.1.2, Table 4
278 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
279 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
280 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
281 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000282
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000283 // Single-precision floating-point comparison helper functions
284 // RTABI chapter 4.1.2, Table 5
285 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
286 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
287 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
288 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
289 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
290 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
291 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
292 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000293
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000294 // Floating-point to integer conversions.
295 // RTABI chapter 4.1.2, Table 6
296 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
297 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
298 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
299 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
300 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
301 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
302 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
303 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000304
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000305 // Conversions between floating types.
306 // RTABI chapter 4.1.2, Table 7
307 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Saleem Abdulrasool017bd572014-08-17 22:51:02 +0000308 { RTLIB::FPROUND_F64_F16, "__aeabi_d2h", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Chad Rosierad7c9102014-08-23 18:29:43 +0000309 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000310
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000311 // Integer to floating-point conversions.
312 // RTABI chapter 4.1.2, Table 8
313 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
314 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
315 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
316 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
317 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
318 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
319 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
320 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000321
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000322 // Long long helper functions
323 // RTABI chapter 4.2, Table 9
Chad Rosierad7c9102014-08-23 18:29:43 +0000324 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
325 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
326 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
327 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000328
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000329 // Integer division functions
330 // RTABI chapter 4.3.1
Chad Rosierad7c9102014-08-23 18:29:43 +0000331 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
332 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
333 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
334 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
335 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
336 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
337 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
338 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Renato Golin4cd51872011-05-22 21:41:23 +0000339
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000340 // Memory operations
341 // RTABI chapter 4.3.4
342 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
343 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
344 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
345 };
346
347 for (const auto &LC : LibraryCalls) {
348 setLibcallName(LC.Op, LC.Name);
349 setLibcallCallingConv(LC.Op, LC.CC);
350 if (LC.Cond != ISD::SETCC_INVALID)
351 setCmpLibcallCC(LC.Op, LC.Cond);
352 }
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000353 }
354
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000355 if (Subtarget->isTargetWindows()) {
356 static const struct {
357 const RTLIB::Libcall Op;
358 const char * const Name;
359 const CallingConv::ID CC;
360 } LibraryCalls[] = {
361 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
362 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
363 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
364 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
365 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
366 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
367 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
368 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
369 };
370
371 for (const auto &LC : LibraryCalls) {
372 setLibcallName(LC.Op, LC.Name);
373 setLibcallCallingConv(LC.Op, LC.CC);
374 }
375 }
376
Bob Wilsonbc158992011-10-07 16:59:21 +0000377 // Use divmod compiler-rt calls for iOS 5.0 and later.
Cameron Esfahani943908b2013-08-29 20:23:14 +0000378 if (Subtarget->getTargetTriple().isiOS() &&
Bob Wilsonbc158992011-10-07 16:59:21 +0000379 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
380 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
381 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
382 }
383
Oliver Stannard11790b22014-08-11 09:12:32 +0000384 // The half <-> float conversion functions are always soft-float, but are
385 // needed for some targets which use a hard-float calling convention by
386 // default.
387 if (Subtarget->isAAPCS_ABI()) {
388 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_AAPCS);
389 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_AAPCS);
390 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_AAPCS);
391 } else {
392 setLibcallCallingConv(RTLIB::FPROUND_F32_F16, CallingConv::ARM_APCS);
393 setLibcallCallingConv(RTLIB::FPROUND_F64_F16, CallingConv::ARM_APCS);
394 setLibcallCallingConv(RTLIB::FPEXT_F16_F32, CallingConv::ARM_APCS);
395 }
396
David Goodwin22c2fba2009-07-08 23:10:31 +0000397 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000398 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000399 else
Craig Topperc7242e02012-04-20 07:30:17 +0000400 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000401 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
402 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000403 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Oliver Stannard51b1d462014-08-21 12:50:31 +0000404 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Evan Cheng10043e22007-01-19 07:51:42 +0000405 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000406
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000407 for (MVT VT : MVT::vector_valuetypes()) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000408 for (MVT InnerVT : MVT::vector_valuetypes()) {
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000409 setTruncStoreAction(VT, InnerVT, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000410 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
411 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
412 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
413 }
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000414
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000415 setOperationAction(ISD::MULHS, VT, Expand);
416 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
417 setOperationAction(ISD::MULHU, VT, Expand);
418 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000419
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +0000420 setOperationAction(ISD::BSWAP, VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000421 }
422
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000423 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000424 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000425
Bob Wilson2e076c42009-06-22 23:27:02 +0000426 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000427 addDRTypeForNEON(MVT::v2f32);
428 addDRTypeForNEON(MVT::v8i8);
429 addDRTypeForNEON(MVT::v4i16);
430 addDRTypeForNEON(MVT::v2i32);
431 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000432
Owen Anderson9f944592009-08-11 20:47:22 +0000433 addQRTypeForNEON(MVT::v4f32);
434 addQRTypeForNEON(MVT::v2f64);
435 addQRTypeForNEON(MVT::v16i8);
436 addQRTypeForNEON(MVT::v8i16);
437 addQRTypeForNEON(MVT::v4i32);
438 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000439
Bob Wilson194a2512009-09-15 23:55:57 +0000440 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
441 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000442 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
443 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000444 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
445 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
446 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000447 // FIXME: Code duplication: FDIV and FREM are expanded always, see
448 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000449 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
450 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000451 // FIXME: Create unittest.
452 // In another words, find a way when "copysign" appears in DAG with vector
453 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000454 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000455 // FIXME: Code duplication: SETCC has custom operation action, see
456 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000457 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000458 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000459 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
460 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
461 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
462 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
463 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
464 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
465 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
466 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
467 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
468 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
469 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
470 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000471 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000472 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
473 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
474 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
475 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
476 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000477 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000478
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000479 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
480 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
481 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
482 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
483 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
484 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
485 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
486 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
487 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
488 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000489 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
490 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
491 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
492 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000493 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000494
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000495 // Mark v2f32 intrinsics.
496 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
497 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
498 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
499 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
500 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
501 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
502 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
503 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
504 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
505 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
506 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
507 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
508 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
509 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
510 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
511
Bob Wilson6cc46572009-09-16 00:32:15 +0000512 // Neon does not support some operations on v1i64 and v2i64 types.
513 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000514 // Custom handling for some quad-vector types to detect VMULL.
515 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
516 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
517 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000518 // Custom handling for some vector types to avoid expensive expansions
519 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
520 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
521 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
522 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000523 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
524 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000525 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000526 // a destination type that is wider than the source, and nor does
527 // it have a FP_TO_[SU]INT instruction with a narrower destination than
528 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000529 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
530 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000531 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
532 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000533
Eli Friedmane6385e62012-11-15 22:44:27 +0000534 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000535 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000536
Evan Chengb4eae132012-12-04 22:41:50 +0000537 // NEON does not have single instruction CTPOP for vectors with element
538 // types wider than 8-bits. However, custom lowering can leverage the
539 // v8i8/v16i8 vcnt instruction.
540 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
541 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
542 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
543 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
544
Jim Grosbach5f215872013-02-27 21:31:12 +0000545 // NEON only has FMA instructions as of VFP4.
546 if (!Subtarget->hasVFP4()) {
547 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
548 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
549 }
550
Bob Wilson06fce872011-02-07 17:43:21 +0000551 setTargetDAGCombine(ISD::INTRINSIC_VOID);
552 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000553 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
554 setTargetDAGCombine(ISD::SHL);
555 setTargetDAGCombine(ISD::SRL);
556 setTargetDAGCombine(ISD::SRA);
557 setTargetDAGCombine(ISD::SIGN_EXTEND);
558 setTargetDAGCombine(ISD::ZERO_EXTEND);
559 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsonc6c13a32010-02-18 06:05:53 +0000560 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000561 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000562 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000563 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
564 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000565 setTargetDAGCombine(ISD::FP_TO_SINT);
566 setTargetDAGCombine(ISD::FP_TO_UINT);
567 setTargetDAGCombine(ISD::FDIV);
Ahmed Bougacha0cb86162014-12-13 23:22:12 +0000568 setTargetDAGCombine(ISD::LOAD);
Nadav Rotem097106b2011-10-15 20:03:12 +0000569
James Molloy547d4c02012-02-20 09:24:05 +0000570 // It is legal to extload from v4i8 to v4i16 or v4i32.
571 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
572 MVT::v4i16, MVT::v2i16,
573 MVT::v2i32};
574 for (unsigned i = 0; i < 6; ++i) {
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000575 for (MVT VT : MVT::integer_vector_valuetypes()) {
576 setLoadExtAction(ISD::EXTLOAD, VT, Tys[i], Legal);
577 setLoadExtAction(ISD::ZEXTLOAD, VT, Tys[i], Legal);
578 setLoadExtAction(ISD::SEXTLOAD, VT, Tys[i], Legal);
579 }
James Molloy547d4c02012-02-20 09:24:05 +0000580 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000581 }
582
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000583 // ARM and Thumb2 support UMLAL/SMLAL.
584 if (!Subtarget->isThumb1Only())
585 setTargetDAGCombine(ISD::ADDC);
586
Oliver Stannard51b1d462014-08-21 12:50:31 +0000587 if (Subtarget->isFPOnlySP()) {
588 // When targetting a floating-point unit with only single-precision
589 // operations, f64 is legal for the few double-precision instructions which
590 // are present However, no double-precision operations other than moves,
591 // loads and stores are provided by the hardware.
592 setOperationAction(ISD::FADD, MVT::f64, Expand);
593 setOperationAction(ISD::FSUB, MVT::f64, Expand);
594 setOperationAction(ISD::FMUL, MVT::f64, Expand);
595 setOperationAction(ISD::FMA, MVT::f64, Expand);
596 setOperationAction(ISD::FDIV, MVT::f64, Expand);
597 setOperationAction(ISD::FREM, MVT::f64, Expand);
598 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
599 setOperationAction(ISD::FGETSIGN, MVT::f64, Expand);
600 setOperationAction(ISD::FNEG, MVT::f64, Expand);
601 setOperationAction(ISD::FABS, MVT::f64, Expand);
602 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
603 setOperationAction(ISD::FSIN, MVT::f64, Expand);
604 setOperationAction(ISD::FCOS, MVT::f64, Expand);
605 setOperationAction(ISD::FPOWI, MVT::f64, Expand);
606 setOperationAction(ISD::FPOW, MVT::f64, Expand);
607 setOperationAction(ISD::FLOG, MVT::f64, Expand);
608 setOperationAction(ISD::FLOG2, MVT::f64, Expand);
609 setOperationAction(ISD::FLOG10, MVT::f64, Expand);
610 setOperationAction(ISD::FEXP, MVT::f64, Expand);
611 setOperationAction(ISD::FEXP2, MVT::f64, Expand);
612 setOperationAction(ISD::FCEIL, MVT::f64, Expand);
613 setOperationAction(ISD::FTRUNC, MVT::f64, Expand);
614 setOperationAction(ISD::FRINT, MVT::f64, Expand);
615 setOperationAction(ISD::FNEARBYINT, MVT::f64, Expand);
616 setOperationAction(ISD::FFLOOR, MVT::f64, Expand);
617 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
618 setOperationAction(ISD::FP_EXTEND, MVT::f64, Custom);
619 }
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000620
Evan Cheng6addd652007-05-18 00:19:34 +0000621 computeRegisterProperties();
Evan Cheng10043e22007-01-19 07:51:42 +0000622
Tim Northover4e80b582014-07-18 13:01:19 +0000623 // ARM does not have floating-point extending loads.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000624 for (MVT VT : MVT::fp_valuetypes()) {
625 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
626 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f16, Expand);
627 }
Tim Northover4e80b582014-07-18 13:01:19 +0000628
629 // ... or truncating stores
630 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
631 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
632 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000633
Duncan Sands95d46ef2008-01-23 20:39:46 +0000634 // ARM does not have i1 sign extending load.
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000635 for (MVT VT : MVT::integer_valuetypes())
636 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000637
Evan Cheng10043e22007-01-19 07:51:42 +0000638 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000639 if (!Subtarget->isThumb1Only()) {
640 for (unsigned im = (unsigned)ISD::PRE_INC;
641 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000642 setIndexedLoadAction(im, MVT::i1, Legal);
643 setIndexedLoadAction(im, MVT::i8, Legal);
644 setIndexedLoadAction(im, MVT::i16, Legal);
645 setIndexedLoadAction(im, MVT::i32, Legal);
646 setIndexedStoreAction(im, MVT::i1, Legal);
647 setIndexedStoreAction(im, MVT::i8, Legal);
648 setIndexedStoreAction(im, MVT::i16, Legal);
649 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000650 }
Evan Cheng10043e22007-01-19 07:51:42 +0000651 }
652
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000653 setOperationAction(ISD::SADDO, MVT::i32, Custom);
654 setOperationAction(ISD::UADDO, MVT::i32, Custom);
655 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
656 setOperationAction(ISD::USUBO, MVT::i32, Custom);
657
Evan Cheng10043e22007-01-19 07:51:42 +0000658 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000659 setOperationAction(ISD::MUL, MVT::i64, Expand);
660 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000661 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000662 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
663 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000664 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000665 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
666 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000667 setOperationAction(ISD::MULHS, MVT::i32, Expand);
668
Jim Grosbach5d994042009-10-31 19:38:01 +0000669 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000670 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000671 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000672 setOperationAction(ISD::SRL, MVT::i64, Custom);
673 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000674
Evan Chenge8916542011-08-30 01:34:54 +0000675 if (!Subtarget->isThumb1Only()) {
676 // FIXME: We should do this for Thumb1 as well.
677 setOperationAction(ISD::ADDC, MVT::i32, Custom);
678 setOperationAction(ISD::ADDE, MVT::i32, Custom);
679 setOperationAction(ISD::SUBC, MVT::i32, Custom);
680 setOperationAction(ISD::SUBE, MVT::i32, Custom);
681 }
682
Evan Cheng10043e22007-01-19 07:51:42 +0000683 // ARM does not have ROTL.
Owen Anderson9f944592009-08-11 20:47:22 +0000684 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach8546ec92010-01-18 19:58:49 +0000685 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000686 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000687 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000688 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000689
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000690 // These just redirect to CTTZ and CTLZ on ARM.
691 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
692 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
693
Tim Northoverbc933082013-05-23 19:11:20 +0000694 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
695
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000696 // Only ARMv6 has BSWAP.
697 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000698 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000699
Bob Wilsone8a549c2012-09-29 21:43:49 +0000700 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
701 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
702 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbach92d999002010-05-05 20:44:35 +0000703 setOperationAction(ISD::SDIV, MVT::i32, Expand);
704 setOperationAction(ISD::UDIV, MVT::i32, Expand);
705 }
Renato Golin87610692013-07-16 09:32:17 +0000706
707 // FIXME: Also set divmod for SREM on EABI
Chad Rosierad7c9102014-08-23 18:29:43 +0000708 setOperationAction(ISD::SREM, MVT::i32, Expand);
709 setOperationAction(ISD::UREM, MVT::i32, Expand);
710 // Register based DivRem for AEABI (RTABI 4.2)
711 if (Subtarget->isTargetAEABI()) {
712 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
713 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
714 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
715 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
716 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
717 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
718 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
719 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
720
721 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
722 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
723 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
724 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
725 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
726 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
727 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
728 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
729
730 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
731 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
732 } else {
Renato Golin87610692013-07-16 09:32:17 +0000733 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
734 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
735 }
Bob Wilson7117a912009-03-20 22:42:55 +0000736
Owen Anderson9f944592009-08-11 20:47:22 +0000737 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
738 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
739 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
740 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000741 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000742
Evan Cheng74d92c12011-04-08 21:37:21 +0000743 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000744
Evan Cheng10043e22007-01-19 07:51:42 +0000745 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000746 setOperationAction(ISD::VASTART, MVT::Other, Custom);
747 setOperationAction(ISD::VAARG, MVT::Other, Expand);
748 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
749 setOperationAction(ISD::VAEND, MVT::Other, Expand);
750 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
751 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000752
Tim Northoverd6a729b2014-01-06 14:28:05 +0000753 if (!Subtarget->isTargetMachO()) {
754 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000755 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000756 setExceptionPointerRegister(ARM::R0);
757 setExceptionSelectorRegister(ARM::R1);
758 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000759
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000760 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
761 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
762 else
763 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
764
Evan Cheng6e809de2010-08-11 06:22:01 +0000765 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
Jonathan Roelofs5e98ff92014-08-21 14:35:47 +0000766 // the default expansion. If we are targeting a single threaded system,
767 // then set them all for expand so we can lower them later into their
768 // non-atomic form.
769 if (TM.Options.ThreadModel == ThreadModel::Single)
770 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
771 else if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000772 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
773 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000774 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000775
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000776 // On v8, we have particularly efficient implementations of atomic fences
777 // if they can be combined with nearby atomic loads and stores.
778 if (!Subtarget->hasV8Ops()) {
Robin Morissetd18cda62014-08-15 22:17:28 +0000779 // Automatically insert fences (dmb ish) around ATOMIC_SWAP etc.
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000780 setInsertFencesForAtomic(true);
781 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000782 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000783 // If there's anything we can use as a barrier, go through custom lowering
784 // for ATOMIC_FENCE.
785 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
786 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
787
Jim Grosbach6860bb72010-06-18 22:35:32 +0000788 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000789 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000790 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000791 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000792 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000793 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000794 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000795 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000796 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000797 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000798 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000799 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000800 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000801 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
802 // Unordered/Monotonic case.
803 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
804 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000805 }
Evan Cheng10043e22007-01-19 07:51:42 +0000806
Evan Cheng21acf9f2010-11-04 05:19:35 +0000807 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000808
Eli Friedman8cfa7712010-06-26 04:36:50 +0000809 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
810 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000811 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
812 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000813 }
Owen Anderson9f944592009-08-11 20:47:22 +0000814 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000815
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000816 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
817 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000818 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000819 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000820 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000821 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
822 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000823
824 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000825 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbach31984832010-07-07 00:07:57 +0000826 if (Subtarget->isTargetDarwin()) {
827 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
828 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall7d84ece2011-05-29 19:50:32 +0000829 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbach31984832010-07-07 00:07:57 +0000830 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000831
Owen Anderson9f944592009-08-11 20:47:22 +0000832 setOperationAction(ISD::SETCC, MVT::i32, Expand);
833 setOperationAction(ISD::SETCC, MVT::f32, Expand);
834 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000835 setOperationAction(ISD::SELECT, MVT::i32, Custom);
836 setOperationAction(ISD::SELECT, MVT::f32, Custom);
837 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000838 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
839 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
840 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000841
Owen Anderson9f944592009-08-11 20:47:22 +0000842 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
843 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
844 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
845 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
846 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000847
Dan Gohman482732a2007-10-11 23:21:31 +0000848 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000849 setOperationAction(ISD::FSIN, MVT::f64, Expand);
850 setOperationAction(ISD::FSIN, MVT::f32, Expand);
851 setOperationAction(ISD::FCOS, MVT::f32, Expand);
852 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000853 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
854 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000855 setOperationAction(ISD::FREM, MVT::f64, Expand);
856 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000857 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
858 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000859 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
860 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000861 }
Owen Anderson9f944592009-08-11 20:47:22 +0000862 setOperationAction(ISD::FPOW, MVT::f64, Expand);
863 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000864
Evan Chengd0007f32012-04-10 21:40:28 +0000865 if (!Subtarget->hasVFP4()) {
866 setOperationAction(ISD::FMA, MVT::f64, Expand);
867 setOperationAction(ISD::FMA, MVT::f32, Expand);
868 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000869
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000870 // Various VFP goodness
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000871 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilsone4191e72010-03-19 22:51:32 +0000872 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
873 if (Subtarget->hasVFP2()) {
874 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
875 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
876 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
877 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
878 }
Tim Northover53f3bcf2014-07-17 11:27:04 +0000879
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000880 // FP-ARMv8 adds f64 <-> f16 conversion. Before that it should be expanded.
881 if (!Subtarget->hasFPARMv8() || Subtarget->isFPOnlySP()) {
Tim Northover53f3bcf2014-07-17 11:27:04 +0000882 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
883 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
884 }
885
886 // fp16 is a special v7 extension that adds f16 <-> f32 conversions.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000887 if (!Subtarget->hasFP16()) {
Tim Northoverfd7e4242014-07-17 10:51:23 +0000888 setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
889 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000890 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000891 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000892
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000893 // Combine sin / cos into one node or libcall if possible.
894 if (Subtarget->hasSinCos()) {
895 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
896 setLibcallName(RTLIB::SINCOS_F64, "sincos");
Bob Wilson9868d712014-10-09 05:43:30 +0000897 if (Subtarget->getTargetTriple().isiOS()) {
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000898 // For iOS, we don't want to the normal expansion of a libcall to
899 // sincos. We want to issue a libcall to __sincos_stret.
900 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
901 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
902 }
903 }
Evan Cheng10043e22007-01-19 07:51:42 +0000904
Oliver Stannardd4e0a4f2014-10-01 13:13:18 +0000905 // FP-ARMv8 implements a lot of rounding-like FP operations.
906 if (Subtarget->hasFPARMv8()) {
907 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
908 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
909 setOperationAction(ISD::FROUND, MVT::f32, Legal);
910 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
911 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
912 setOperationAction(ISD::FRINT, MVT::f32, Legal);
913 if (!Subtarget->isFPOnlySP()) {
914 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
915 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
916 setOperationAction(ISD::FROUND, MVT::f64, Legal);
917 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
918 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
919 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Chad Rosierb1bbf6f2014-08-15 21:38:16 +0000920 }
921 }
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000922 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000923 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000924 setTargetDAGCombine(ISD::ADD);
925 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000926 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000927 setTargetDAGCombine(ISD::AND);
928 setTargetDAGCombine(ISD::OR);
929 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000930
Evan Chengf258a152012-02-23 02:58:19 +0000931 if (Subtarget->hasV6Ops())
932 setTargetDAGCombine(ISD::SRL);
933
Evan Cheng10043e22007-01-19 07:51:42 +0000934 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +0000935
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000936 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
937 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +0000938 setSchedulingPreference(Sched::RegPressure);
939 else
940 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +0000941
Evan Cheng3ae2b792011-01-06 06:52:41 +0000942 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000943 MaxStoresPerMemset = 8;
944 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
945 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
946 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
947 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
948 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengb71233f2010-06-26 01:52:05 +0000949
Rafael Espindolaa76eccf2010-07-11 04:01:49 +0000950 // On ARM arguments smaller than 4 bytes are extended, so all arguments
951 // are at least 4 bytes aligned.
952 setMinStackArgumentAlignment(4);
953
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000954 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000955 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000956
Eli Friedman2518f832011-05-06 20:34:06 +0000957 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +0000958}
959
Andrew Trick43f25632011-01-19 02:35:27 +0000960// FIXME: It might make sense to define the representative register class as the
961// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
962// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
963// SPR's representative would be DPR_VFP2. This should work well if register
964// pressure tracking were modified such that a register use would increment the
965// pressure of the register class's representative and all of it's super
966// classes' representatives transitively. We have not implemented this because
967// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000968// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +0000969// and extractions.
Evan Chenga77f3d32010-07-21 06:09:07 +0000970std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000971ARMTargetLowering::findRepresentativeClass(MVT VT) const{
Craig Topper062a2ba2014-04-25 05:30:21 +0000972 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +0000973 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000974 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +0000975 default:
Evan Chenga77f3d32010-07-21 06:09:07 +0000976 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng28590382010-07-21 23:53:58 +0000977 // Use DPR as representative register class for all floating point
978 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
979 // the cost is 1 for both f32 and f64.
980 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +0000981 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +0000982 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +0000983 // When NEON is used for SP, only half of the register file is available
984 // because operations that define both SP and DP results will be constrained
985 // to the VFP2 class (D0-D15). We currently model this constraint prior to
986 // coalescing by double-counting the SP regs. See the FIXME above.
987 if (Subtarget->useNEONForSinglePrecisionFP())
988 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000989 break;
990 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
991 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +0000992 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000993 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000994 break;
995 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000996 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000997 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +0000998 break;
999 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +00001000 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +00001001 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +00001002 break;
Evan Cheng10f99a32010-07-19 22:15:08 +00001003 }
Evan Chenga77f3d32010-07-21 06:09:07 +00001004 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +00001005}
1006
Evan Cheng10043e22007-01-19 07:51:42 +00001007const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
1008 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001009 default: return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +00001010 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +00001011 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +00001012 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
1013 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +00001014 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +00001015 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
1016 case ARMISD::tCALL: return "ARMISD::tCALL";
1017 case ARMISD::BRCOND: return "ARMISD::BRCOND";
1018 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +00001019 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +00001020 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001021 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001022 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1023 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001024 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001025 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001026 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1027 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001028 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001029 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001030
Evan Cheng10043e22007-01-19 07:51:42 +00001031 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001032
Jim Grosbach8546ec92010-01-18 19:58:49 +00001033 case ARMISD::RBIT: return "ARMISD::RBIT";
1034
Bob Wilsone4191e72010-03-19 22:51:32 +00001035 case ARMISD::FTOSI: return "ARMISD::FTOSI";
1036 case ARMISD::FTOUI: return "ARMISD::FTOUI";
1037 case ARMISD::SITOF: return "ARMISD::SITOF";
1038 case ARMISD::UITOF: return "ARMISD::UITOF";
1039
Evan Cheng10043e22007-01-19 07:51:42 +00001040 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1041 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1042 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001043
Evan Chenge8916542011-08-30 01:34:54 +00001044 case ARMISD::ADDC: return "ARMISD::ADDC";
1045 case ARMISD::ADDE: return "ARMISD::ADDE";
1046 case ARMISD::SUBC: return "ARMISD::SUBC";
1047 case ARMISD::SUBE: return "ARMISD::SUBE";
1048
Bob Wilson22806742010-09-22 22:09:21 +00001049 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1050 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001051
Evan Chengec6d7c92009-10-28 06:55:03 +00001052 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
1053 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
1054
Dale Johannesend679ff72010-06-03 21:09:53 +00001055 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001056
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001057 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001058
Evan Chengb972e562009-08-07 00:34:42 +00001059 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1060
Bob Wilson7ed59712010-10-30 00:54:37 +00001061 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001062
Evan Cheng8740ee32010-11-03 06:34:55 +00001063 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1064
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00001065 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
1066
Bob Wilson2e076c42009-06-22 23:27:02 +00001067 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001068 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001069 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001070 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1071 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001072 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1073 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001074 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1075 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001076 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1077 case ARMISD::VTST: return "ARMISD::VTST";
1078
1079 case ARMISD::VSHL: return "ARMISD::VSHL";
1080 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1081 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001082 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1083 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1084 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1085 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1086 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1087 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1088 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1089 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1090 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1091 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1092 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1093 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
1094 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1095 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001096 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001097 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001098 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001099 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001100 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001101 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001102 case ARMISD::VREV64: return "ARMISD::VREV64";
1103 case ARMISD::VREV32: return "ARMISD::VREV32";
1104 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001105 case ARMISD::VZIP: return "ARMISD::VZIP";
1106 case ARMISD::VUZP: return "ARMISD::VUZP";
1107 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001108 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1109 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001110 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1111 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001112 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1113 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001114 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilsonc6c13a32010-02-18 06:05:53 +00001115 case ARMISD::FMAX: return "ARMISD::FMAX";
1116 case ARMISD::FMIN: return "ARMISD::FMIN";
Joey Goulye3dd6842013-08-23 12:01:13 +00001117 case ARMISD::VMAXNM: return "ARMISD::VMAX";
1118 case ARMISD::VMINNM: return "ARMISD::VMIN";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001119 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001120 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1121 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001122 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilson2d790df2010-11-28 06:51:26 +00001123 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1124 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1125 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001126 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1127 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1128 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1129 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1130 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1131 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1132 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1133 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1134 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1135 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1136 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1137 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1138 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1139 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1140 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1141 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1142 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001143 }
1144}
1145
Matt Arsenault758659232013-05-18 00:21:46 +00001146EVT ARMTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Duncan Sandsf2641e12011-09-06 19:07:46 +00001147 if (!VT.isVector()) return getPointerTy();
1148 return VT.changeVectorElementTypeToInteger();
1149}
1150
Evan Cheng4cad68e2010-05-15 02:18:07 +00001151/// getRegClassFor - Return the register class that should be used for the
1152/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001153const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001154 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1155 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1156 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001157 if (Subtarget->hasNEON()) {
1158 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001159 return &ARM::QQPRRegClass;
1160 if (VT == MVT::v8i64)
1161 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001162 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001163 return TargetLowering::getRegClassFor(VT);
1164}
1165
Eric Christopher84bdfd82010-07-21 22:26:11 +00001166// Create a fast isel object.
1167FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001168ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1169 const TargetLibraryInfo *libInfo) const {
1170 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001171}
1172
Anton Korobeynikov19edda02010-07-24 21:52:08 +00001173/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1174/// be used for loads / stores from the global.
1175unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1176 return (Subtarget->isThumb1Only() ? 127 : 4095);
1177}
1178
Evan Cheng4401f882010-05-20 23:26:43 +00001179Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001180 unsigned NumVals = N->getNumValues();
1181 if (!NumVals)
1182 return Sched::RegPressure;
1183
1184 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001185 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001186 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001187 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001188 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001189 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001190 }
Evan Chengbf914992010-05-28 23:25:23 +00001191
1192 if (!N->isMachineOpcode())
1193 return Sched::RegPressure;
1194
1195 // Load are scheduled for latency even if there instruction itinerary
1196 // is not available.
Eric Christopherd9134482014-08-04 21:25:23 +00001197 const TargetInstrInfo *TII =
1198 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001199 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001200
Evan Cheng6cc775f2011-06-28 19:10:37 +00001201 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001202 return Sched::RegPressure;
1203 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001204 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001205 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001206
Evan Cheng4401f882010-05-20 23:26:43 +00001207 return Sched::RegPressure;
1208}
1209
Evan Cheng10043e22007-01-19 07:51:42 +00001210//===----------------------------------------------------------------------===//
1211// Lowering Code
1212//===----------------------------------------------------------------------===//
1213
Evan Cheng10043e22007-01-19 07:51:42 +00001214/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1215static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1216 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001217 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001218 case ISD::SETNE: return ARMCC::NE;
1219 case ISD::SETEQ: return ARMCC::EQ;
1220 case ISD::SETGT: return ARMCC::GT;
1221 case ISD::SETGE: return ARMCC::GE;
1222 case ISD::SETLT: return ARMCC::LT;
1223 case ISD::SETLE: return ARMCC::LE;
1224 case ISD::SETUGT: return ARMCC::HI;
1225 case ISD::SETUGE: return ARMCC::HS;
1226 case ISD::SETULT: return ARMCC::LO;
1227 case ISD::SETULE: return ARMCC::LS;
1228 }
1229}
1230
Bob Wilsona2e83332009-09-09 23:14:54 +00001231/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1232static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001233 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001234 CondCode2 = ARMCC::AL;
1235 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001236 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001237 case ISD::SETEQ:
1238 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1239 case ISD::SETGT:
1240 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1241 case ISD::SETGE:
1242 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1243 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001244 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001245 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1246 case ISD::SETO: CondCode = ARMCC::VC; break;
1247 case ISD::SETUO: CondCode = ARMCC::VS; break;
1248 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1249 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1250 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1251 case ISD::SETLT:
1252 case ISD::SETULT: CondCode = ARMCC::LT; break;
1253 case ISD::SETLE:
1254 case ISD::SETULE: CondCode = ARMCC::LE; break;
1255 case ISD::SETNE:
1256 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1257 }
Evan Cheng10043e22007-01-19 07:51:42 +00001258}
1259
Bob Wilsona4c22902009-04-17 19:07:39 +00001260//===----------------------------------------------------------------------===//
1261// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001262//===----------------------------------------------------------------------===//
1263
1264#include "ARMGenCallingConv.inc"
1265
Oliver Stannardc24f2172014-05-09 14:01:47 +00001266/// getEffectiveCallingConv - Get the effective calling convention, taking into
1267/// account presence of floating point hardware and calling convention
1268/// limitations, such as support for variadic functions.
1269CallingConv::ID
1270ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1271 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001272 switch (CC) {
1273 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001274 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001275 case CallingConv::ARM_AAPCS:
1276 case CallingConv::ARM_APCS:
1277 case CallingConv::GHC:
1278 return CC;
1279 case CallingConv::ARM_AAPCS_VFP:
1280 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1281 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001282 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001283 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001284 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001285 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1286 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001287 return CallingConv::ARM_AAPCS_VFP;
1288 else
1289 return CallingConv::ARM_AAPCS;
1290 case CallingConv::Fast:
1291 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001292 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001293 return CallingConv::Fast;
1294 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001295 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001296 return CallingConv::ARM_AAPCS_VFP;
1297 else
1298 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001299 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001300}
1301
1302/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1303/// CallingConvention.
1304CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1305 bool Return,
1306 bool isVarArg) const {
1307 switch (getEffectiveCallingConv(CC, isVarArg)) {
1308 default:
1309 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001310 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001311 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001312 case CallingConv::ARM_AAPCS:
1313 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1314 case CallingConv::ARM_AAPCS_VFP:
1315 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1316 case CallingConv::Fast:
1317 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001318 case CallingConv::GHC:
1319 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001320 }
1321}
1322
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001323/// LowerCallResult - Lower the result values of a call into the
1324/// appropriate copies out of appropriate physical registers.
1325SDValue
1326ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001327 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001328 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001329 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001330 SmallVectorImpl<SDValue> &InVals,
1331 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001332
Bob Wilsona4c22902009-04-17 19:07:39 +00001333 // Assign locations to each value returned by this call.
1334 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001335 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1336 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001337 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001338 CCAssignFnForNode(CallConv, /* Return*/ true,
1339 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001340
1341 // Copy all of the result registers out of their specified physreg.
1342 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1343 CCValAssign VA = RVLocs[i];
1344
Stephen Linb8bd2322013-04-20 05:14:40 +00001345 // Pass 'this' value directly from the argument to return value, to avoid
1346 // reg unit interference
1347 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001348 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1349 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001350 InVals.push_back(ThisVal);
1351 continue;
1352 }
1353
Bob Wilson0041bd32009-04-25 00:33:20 +00001354 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001355 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001356 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001357 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001358 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001359 Chain = Lo.getValue(1);
1360 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001361 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001362 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001363 InFlag);
1364 Chain = Hi.getValue(1);
1365 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001366 if (!Subtarget->isLittle())
1367 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001368 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001369
Owen Anderson9f944592009-08-11 20:47:22 +00001370 if (VA.getLocVT() == MVT::v2f64) {
1371 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1372 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1373 DAG.getConstant(0, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001374
1375 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001376 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001377 Chain = Lo.getValue(1);
1378 InFlag = Lo.getValue(2);
1379 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001380 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001381 Chain = Hi.getValue(1);
1382 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001383 if (!Subtarget->isLittle())
1384 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001385 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001386 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1387 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001388 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001389 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001390 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1391 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001392 Chain = Val.getValue(1);
1393 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001394 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001395
1396 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001397 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001398 case CCValAssign::Full: break;
1399 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001400 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001401 break;
1402 }
1403
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001404 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001405 }
1406
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001407 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001408}
1409
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001410/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001411SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001412ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1413 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001414 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001415 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001416 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001417 unsigned LocMemOffset = VA.getLocMemOffset();
1418 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1419 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilsona4c22902009-04-17 19:07:39 +00001420 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner886250c2010-09-21 18:51:21 +00001421 MachinePointerInfo::getStack(LocMemOffset),
David Greene0d0149f2010-02-15 16:55:24 +00001422 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001423}
1424
Andrew Trickef9de2a2013-05-25 02:42:55 +00001425void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001426 SDValue Chain, SDValue &Arg,
1427 RegsToPassVector &RegsToPass,
1428 CCValAssign &VA, CCValAssign &NextVA,
1429 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001430 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001431 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001432
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001433 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001434 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001435 unsigned id = Subtarget->isLittle() ? 0 : 1;
1436 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001437
1438 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001439 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001440 else {
1441 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001442 if (!StackPtr.getNode())
Bob Wilson2e076c42009-06-22 23:27:02 +00001443 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1444
Christian Pirkerb5728192014-05-08 14:06:24 +00001445 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001446 dl, DAG, NextVA,
1447 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001448 }
1449}
1450
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001451/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001452/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1453/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001454SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001455ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001456 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001457 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001458 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001459 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1460 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1461 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001462 SDValue Chain = CLI.Chain;
1463 SDValue Callee = CLI.Callee;
1464 bool &isTailCall = CLI.IsTailCall;
1465 CallingConv::ID CallConv = CLI.CallConv;
1466 bool doesNotRet = CLI.DoesNotReturn;
1467 bool isVarArg = CLI.IsVarArg;
1468
Dale Johannesend679ff72010-06-03 21:09:53 +00001469 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001470 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1471 bool isThisReturn = false;
1472 bool isSibCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001473
Bob Wilson8decdc42011-10-07 17:17:49 +00001474 // Disable tail calls if they're not supported.
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001475 if (!Subtarget->supportsTailCall() || MF.getTarget().Options.DisableTailCalls)
Bob Wilson3c9ed762010-08-13 22:43:33 +00001476 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001477
Dale Johannesend679ff72010-06-03 21:09:53 +00001478 if (isTailCall) {
1479 // Check if it's really possible to do a tail call.
1480 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001481 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001482 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001483 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1484 report_fatal_error("failed to perform tail call elimination on a call "
1485 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001486 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1487 // detected sibcalls.
1488 if (isTailCall) {
1489 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001490 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001491 }
1492 }
Evan Cheng10043e22007-01-19 07:51:42 +00001493
Bob Wilsona4c22902009-04-17 19:07:39 +00001494 // Analyze operands of the call, assigning locations to each operand.
1495 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001496 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1497 *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001498 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001499 CCAssignFnForNode(CallConv, /* Return*/ false,
1500 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001501
Bob Wilsona4c22902009-04-17 19:07:39 +00001502 // Get a count of how many bytes are to be pushed on the stack.
1503 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001504
Dale Johannesend679ff72010-06-03 21:09:53 +00001505 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001506 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001507 NumBytes = 0;
1508
Evan Cheng10043e22007-01-19 07:51:42 +00001509 // Adjust the stack pointer for the new arguments...
1510 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001511 if (!isSibCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00001512 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
1513 dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001514
Jim Grosbach6ad4bcb2010-02-24 01:43:03 +00001515 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +00001516
Bob Wilson2e076c42009-06-22 23:27:02 +00001517 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001518 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001519
Bob Wilsona4c22902009-04-17 19:07:39 +00001520 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001521 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001522 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1523 i != e;
1524 ++i, ++realArgIdx) {
1525 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001526 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001527 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001528 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001529
Bob Wilsona4c22902009-04-17 19:07:39 +00001530 // Promote the value if needed.
1531 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001532 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001533 case CCValAssign::Full: break;
1534 case CCValAssign::SExt:
1535 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1536 break;
1537 case CCValAssign::ZExt:
1538 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1539 break;
1540 case CCValAssign::AExt:
1541 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1542 break;
1543 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001544 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001545 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001546 }
1547
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001548 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001549 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001550 if (VA.getLocVT() == MVT::v2f64) {
1551 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1552 DAG.getConstant(0, MVT::i32));
1553 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1554 DAG.getConstant(1, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001555
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001556 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001557 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1558
1559 VA = ArgLocs[++i]; // skip ahead to next loc
1560 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001561 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001562 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1563 } else {
1564 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001565
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001566 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1567 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001568 }
1569 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001570 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001571 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001572 }
1573 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001574 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1575 assert(VA.getLocVT() == MVT::i32 &&
1576 "unexpected calling convention register assignment");
1577 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001578 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001579 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001580 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001581 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001582 } else if (isByVal) {
1583 assert(VA.isMemLoc());
1584 unsigned offset = 0;
1585
1586 // True if this byval aggregate will be split between registers
1587 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001588 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
Daniel Sanders8104b752014-11-01 19:32:23 +00001589 unsigned CurByValIdx = CCInfo.getInRegsParamsProcessed();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001590
1591 if (CurByValIdx < ByValArgsCount) {
1592
1593 unsigned RegBegin, RegEnd;
1594 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1595
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001596 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1597 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001598 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001599 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1600 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1601 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1602 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001603 false, false, false,
1604 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001605 MemOpChains.push_back(Load.getValue(1));
1606 RegsToPass.push_back(std::make_pair(j, Load));
1607 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001608
1609 // If parameter size outsides register area, "offset" value
1610 // helps us to calculate stack slot for remained part properly.
1611 offset = RegEnd - RegBegin;
1612
1613 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001614 }
1615
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001616 if (Flags.getByValSize() > 4*offset) {
Manman Ren9f911162012-06-01 02:44:42 +00001617 unsigned LocMemOffset = VA.getLocMemOffset();
1618 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1619 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1620 StkPtrOff);
1621 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1622 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1623 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1624 MVT::i32);
Manman Rene8735522012-06-01 19:33:18 +00001625 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001626
Manman Ren9f911162012-06-01 02:44:42 +00001627 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001628 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001629 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001630 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001631 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001632 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001633 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001634
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001635 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1636 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001637 }
Evan Cheng10043e22007-01-19 07:51:42 +00001638 }
1639
1640 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001641 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001642
1643 // Build a sequence of copy-to-reg nodes chained together with token chain
1644 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001645 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001646 // Tail call byval lowering might overwrite argument registers so in case of
1647 // tail call optimization the copies to registers are lowered later.
1648 if (!isTailCall)
1649 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1650 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1651 RegsToPass[i].second, InFlag);
1652 InFlag = Chain.getValue(1);
1653 }
Evan Cheng10043e22007-01-19 07:51:42 +00001654
Dale Johannesend679ff72010-06-03 21:09:53 +00001655 // For tail calls lower the arguments to the 'real' stack slot.
1656 if (isTailCall) {
1657 // Force all the incoming stack arguments to be loaded from the stack
1658 // before any new outgoing arguments are stored to the stack, because the
1659 // outgoing stack slots may alias the incoming argument stack slots, and
1660 // the alias isn't otherwise explicit. This is slightly more conservative
1661 // than necessary, because it means that each store effectively depends
1662 // on every argument instead of just those arguments it would clobber.
1663
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001664 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001665 InFlag = SDValue();
1666 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1667 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1668 RegsToPass[i].second, InFlag);
1669 InFlag = Chain.getValue(1);
1670 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001671 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001672 }
1673
Bill Wendling24c79f22008-09-16 21:48:12 +00001674 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1675 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1676 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001677 bool isDirect = false;
1678 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001679 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001680 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001681
1682 if (EnableARMLongCalls) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001683 assert((Subtarget->isTargetWindows() ||
1684 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1685 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001686 // Handle a global address or an external symbol. If it's not one of
1687 // those, the target's already in a register, so we don't need to do
1688 // anything extra.
1689 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001690 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001691 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001692 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001693 ARMConstantPoolValue *CPV =
1694 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1695
Jim Grosbach32bb3622010-04-14 22:28:31 +00001696 // Get the address of the callee into a register
1697 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1698 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1699 Callee = DAG.getLoad(getPointerTy(), dl,
1700 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001701 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001702 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001703 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1704 const char *Sym = S->getSymbol();
1705
1706 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001707 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001708 ARMConstantPoolValue *CPV =
1709 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1710 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001711 // Get the address of the callee into a register
1712 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1713 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1714 Callee = DAG.getLoad(getPointerTy(), dl,
1715 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001716 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001717 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001718 }
1719 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001720 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001721 isDirect = true;
Chris Lattner55452c22009-07-15 04:12:33 +00001722 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Tim Northoverd6a729b2014-01-06 14:28:05 +00001723 bool isStub = (isExt && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001724 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001725 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Chengc3c949b42007-06-19 21:05:09 +00001726 // ARM call to a local ARM function is predicable.
Evan Chengf128bdc2010-06-16 07:35:02 +00001727 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001728 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001729 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001730 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Tim Northover72360d22013-12-02 10:35:41 +00001731 Callee = DAG.getNode(ARMISD::WrapperPIC, dl, getPointerTy(),
Tim Northoverd4d294d2014-08-06 11:13:06 +00001732 DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
1733 0, ARMII::MO_NONLAZY));
1734 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee,
1735 MachinePointerInfo::getGOT(), false, false, true, 0);
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00001736 } else if (Subtarget->isTargetCOFF()) {
1737 assert(Subtarget->isTargetWindows() &&
1738 "Windows is the only supported COFF target");
1739 unsigned TargetFlags = GV->hasDLLImportStorageClass()
1740 ? ARMII::MO_DLLIMPORT
1741 : ARMII::MO_NO_FLAG;
1742 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), /*Offset=*/0,
1743 TargetFlags);
1744 if (GV->hasDLLImportStorageClass())
1745 Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
1746 DAG.getNode(ARMISD::Wrapper, dl, getPointerTy(),
1747 Callee), MachinePointerInfo::getGOT(),
1748 false, false, false, 0);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001749 } else {
1750 // On ELF targets for PIC code, direct calls should go through the PLT
1751 unsigned OpFlags = 0;
1752 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001753 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001754 OpFlags = ARMII::MO_PLT;
1755 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1756 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001757 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001758 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001759 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001760 getTargetMachine().getRelocationModel() != Reloc::Static;
Tim Northover2a417b92014-08-06 11:13:14 +00001761 isARMFunc = !Subtarget->isThumb() || (isStub && !Subtarget->isMClass());
Evan Cheng83f35172007-01-30 20:37:08 +00001762 // tBX takes a register source operand.
1763 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001764 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001765 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001766 ARMConstantPoolValue *CPV =
1767 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1768 ARMPCLabelIndex, 4);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001769 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001770 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen021052a2009-02-04 20:06:27 +00001771 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Chengcdbb70c2009-10-31 03:39:36 +00001772 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001773 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001774 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00001775 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson7117a912009-03-20 22:42:55 +00001776 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen021052a2009-02-04 20:06:27 +00001777 getPointerTy(), Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001778 } else {
1779 unsigned OpFlags = 0;
1780 // On ELF targets for PIC code, direct calls should go through the PLT
1781 if (Subtarget->isTargetELF() &&
1782 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1783 OpFlags = ARMII::MO_PLT;
1784 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1785 }
Evan Cheng10043e22007-01-19 07:51:42 +00001786 }
1787
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001788 // FIXME: handle tail calls differently.
1789 unsigned CallOpc;
Eric Christopherc1058df2014-07-04 01:55:26 +00001790 bool HasMinSizeAttr = MF.getFunction()->getAttributes().hasAttribute(
1791 AttributeSet::FunctionIndex, Attribute::MinSize);
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001792 if (Subtarget->isThumb()) {
1793 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001794 CallOpc = ARMISD::CALL_NOLINK;
1795 else
1796 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1797 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001798 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001799 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001800 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet8e1fe842012-11-02 21:32:17 +00001801 // Emit regular call when code size is the priority
1802 !HasMinSizeAttr)
Evan Cheng65f9d192012-02-28 18:51:51 +00001803 // "mov lr, pc; b _foo" to avoid confusing the RSP
1804 CallOpc = ARMISD::CALL_NOLINK;
1805 else
1806 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001807 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001808
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001809 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001810 Ops.push_back(Chain);
1811 Ops.push_back(Callee);
1812
1813 // Add argument registers to the end of the list so that they are known live
1814 // into the call.
1815 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1816 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1817 RegsToPass[i].second.getValueType()));
1818
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001819 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001820 if (!isTailCall) {
1821 const uint32_t *Mask;
Eric Christopherd9134482014-08-04 21:25:23 +00001822 const TargetRegisterInfo *TRI =
1823 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
Matthias Braunc22630e2013-10-04 16:52:54 +00001824 const ARMBaseRegisterInfo *ARI = static_cast<const ARMBaseRegisterInfo*>(TRI);
1825 if (isThisReturn) {
1826 // For 'this' returns, use the R0-preserving mask if applicable
1827 Mask = ARI->getThisReturnPreservedMask(CallConv);
1828 if (!Mask) {
1829 // Set isThisReturn to false if the calling convention is not one that
1830 // allows 'returned' to be modeled in this way, so LowerCallResult does
1831 // not try to pass 'this' straight through
1832 isThisReturn = false;
1833 Mask = ARI->getCallPreservedMask(CallConv);
1834 }
1835 } else
Stephen Linff7fcee2013-06-26 21:42:14 +00001836 Mask = ARI->getCallPreservedMask(CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001837
Matthias Braunc22630e2013-10-04 16:52:54 +00001838 assert(Mask && "Missing call preserved mask for calling convention");
1839 Ops.push_back(DAG.getRegisterMask(Mask));
1840 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001841
Gabor Greiff304a7a2008-08-28 21:40:38 +00001842 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001843 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001844
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001845 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001846 if (isTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001847 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Dale Johannesend679ff72010-06-03 21:09:53 +00001848
Duncan Sands739a0542008-07-02 17:40:58 +00001849 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001850 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001851 InFlag = Chain.getValue(1);
1852
Chris Lattner27539552008-10-11 22:08:30 +00001853 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001854 DAG.getIntPtrConstant(0, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001855 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001856 InFlag = Chain.getValue(1);
1857
Bob Wilsona4c22902009-04-17 19:07:39 +00001858 // Handle result values, copying them out of physregs into vregs that we
1859 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001860 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001861 InVals, isThisReturn,
1862 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001863}
1864
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001865/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001866/// on the stack. Remember the next parameter register to allocate,
1867/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001868/// this.
1869void
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001870ARMTargetLowering::HandleByVal(
1871 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001872 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1873 assert((State->getCallOrPrologue() == Prologue ||
1874 State->getCallOrPrologue() == Call) &&
1875 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001876
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001877 if ((ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001878 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1879 unsigned AlignInRegs = Align / 4;
1880 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1881 for (unsigned i = 0; i < Waste; ++i)
1882 reg = State->AllocateReg(GPRArgRegs, 4);
1883 }
1884 if (reg != 0) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001885 unsigned excess = 4 * (ARM::R4 - reg);
1886
1887 // Special case when NSAA != SP and parameter size greater than size of
1888 // all remained GPR regs. In that case we can't split parameter, we must
1889 // send it to stack. We also must set NCRN to R4, so waste all
1890 // remained registers.
Oliver Stannardd55e1152014-03-05 15:25:27 +00001891 const unsigned NSAAOffset = State->getNextStackOffset();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001892 if (Subtarget->isAAPCS_ABI() && NSAAOffset != 0 && size > excess) {
1893 while (State->AllocateReg(GPRArgRegs, 4))
1894 ;
1895 return;
1896 }
1897
1898 // First register for byval parameter is the first register that wasn't
1899 // allocated before this method call, so it would be "reg".
1900 // If parameter is small enough to be saved in range [reg, r4), then
1901 // the end (first after last) register would be reg + param-size-in-regs,
1902 // else parameter would be splitted between registers and stack,
1903 // end register would be r4 in this case.
1904 unsigned ByValRegBegin = reg;
Stepan Dyatkovskiy2703bca2013-05-08 14:51:27 +00001905 unsigned ByValRegEnd = (size < excess) ? reg + size/4 : (unsigned)ARM::R4;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001906 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1907 // Note, first register is allocated in the beginning of function already,
1908 // allocate remained amount of registers we need.
1909 for (unsigned i = reg+1; i != ByValRegEnd; ++i)
1910 State->AllocateReg(GPRArgRegs, 4);
Oliver Stannardd55e1152014-03-05 15:25:27 +00001911 // A byval parameter that is split between registers and memory needs its
1912 // size truncated here.
1913 // In the case where the entire structure fits in registers, we set the
1914 // size in memory to zero.
1915 if (size < excess)
1916 size = 0;
1917 else
1918 size -= excess;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001919 }
1920 }
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001921}
1922
Dale Johannesend679ff72010-06-03 21:09:53 +00001923/// MatchingStackOffset - Return true if the given stack call argument is
1924/// already available in the same position (relatively) of the caller's
1925/// incoming argument stack.
1926static
1927bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1928 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00001929 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001930 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1931 int FI = INT_MAX;
1932 if (Arg.getOpcode() == ISD::CopyFromReg) {
1933 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001934 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00001935 return false;
1936 MachineInstr *Def = MRI->getVRegDef(VR);
1937 if (!Def)
1938 return false;
1939 if (!Flags.isByVal()) {
1940 if (!TII->isLoadFromStackSlot(Def, FI))
1941 return false;
1942 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00001943 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00001944 }
1945 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1946 if (Flags.isByVal())
1947 // ByVal argument is passed in as a pointer but it's now being
1948 // dereferenced. e.g.
1949 // define @foo(%struct.X* %A) {
1950 // tail call @bar(%struct.X* byval %A)
1951 // }
1952 return false;
1953 SDValue Ptr = Ld->getBasePtr();
1954 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1955 if (!FINode)
1956 return false;
1957 FI = FINode->getIndex();
1958 } else
1959 return false;
1960
1961 assert(FI != INT_MAX);
1962 if (!MFI->isFixedObjectIndex(FI))
1963 return false;
1964 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1965}
1966
1967/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1968/// for tail call optimization. Targets which want to do tail call
1969/// optimization should implement this function.
1970bool
1971ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1972 CallingConv::ID CalleeCC,
1973 bool isVarArg,
1974 bool isCalleeStructRet,
1975 bool isCallerStructRet,
1976 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001977 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00001978 const SmallVectorImpl<ISD::InputArg> &Ins,
1979 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00001980 const Function *CallerF = DAG.getMachineFunction().getFunction();
1981 CallingConv::ID CallerCC = CallerF->getCallingConv();
1982 bool CCMatch = CallerCC == CalleeCC;
1983
1984 // Look for obvious safe cases to perform tail call optimization that do not
1985 // require ABI changes. This is what gcc calls sibcall.
1986
Jim Grosbache3864cc2010-06-16 23:45:49 +00001987 // Do not sibcall optimize vararg calls unless the call site is not passing
1988 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00001989 if (isVarArg && !Outs.empty())
1990 return false;
1991
Tim Northoverd8407452013-10-01 14:33:28 +00001992 // Exception-handling functions need a special set of instructions to indicate
1993 // a return to the hardware. Tail-calling another function would probably
1994 // break this.
1995 if (CallerF->hasFnAttribute("interrupt"))
1996 return false;
1997
Dale Johannesend679ff72010-06-03 21:09:53 +00001998 // Also avoid sibcall optimization if either caller or callee uses struct
1999 // return semantics.
2000 if (isCalleeStructRet || isCallerStructRet)
2001 return false;
2002
Dale Johannesend24c66b2010-06-23 18:52:34 +00002003 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach3840c902011-07-08 20:18:11 +00002004 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
2005 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
2006 // support in the assembler and linker to be used. This would need to be
2007 // fixed to fully support tail calls in Thumb1.
2008 //
Dale Johannesene2289282010-07-08 01:18:23 +00002009 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
2010 // LR. This means if we need to reload LR, it takes an extra instructions,
2011 // which outweighs the value of the tail call; but here we don't know yet
2012 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach535d3b42010-09-08 03:54:02 +00002013 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesene2289282010-07-08 01:18:23 +00002014 // emitEpilogue if LR is used.
Dale Johannesene2289282010-07-08 01:18:23 +00002015
2016 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
2017 // but we need to make sure there are enough registers; the only valid
2018 // registers are the 4 used for parameters. We don't currently do this
2019 // case.
Evan Chengd4b08732010-11-30 23:55:39 +00002020 if (Subtarget->isThumb1Only())
2021 return false;
Dale Johannesen3ac52b32010-06-18 18:13:11 +00002022
Oliver Stannard12993dd2014-08-18 12:42:15 +00002023 // Externally-defined functions with weak linkage should not be
2024 // tail-called on ARM when the OS does not support dynamic
2025 // pre-emption of symbols, as the AAELF spec requires normal calls
2026 // to undefined weak functions to be replaced with a NOP or jump to the
2027 // next instruction. The behaviour of branch instructions in this
2028 // situation (as used for tail calls) is implementation-defined, so we
2029 // cannot rely on the linker replacing the tail call with a return.
2030 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2031 const GlobalValue *GV = G->getGlobal();
Saleem Abdulrasool67f72992015-01-03 21:35:00 +00002032 const Triple TT(getTargetMachine().getTargetTriple());
2033 if (GV->hasExternalWeakLinkage() &&
2034 (!TT.isOSWindows() || TT.isOSBinFormatELF() || TT.isOSBinFormatMachO()))
Oliver Stannard12993dd2014-08-18 12:42:15 +00002035 return false;
2036 }
2037
Dale Johannesend679ff72010-06-03 21:09:53 +00002038 // If the calling conventions do not match, then we'd better make sure the
2039 // results are returned in the same way as what the caller expects.
2040 if (!CCMatch) {
2041 SmallVector<CCValAssign, 16> RVLocs1;
Eric Christopherb5217502014-08-06 18:45:26 +00002042 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
2043 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002044 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
2045
2046 SmallVector<CCValAssign, 16> RVLocs2;
Eric Christopherb5217502014-08-06 18:45:26 +00002047 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
2048 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002049 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
2050
2051 if (RVLocs1.size() != RVLocs2.size())
2052 return false;
2053 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2054 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2055 return false;
2056 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2057 return false;
2058 if (RVLocs1[i].isRegLoc()) {
2059 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2060 return false;
2061 } else {
2062 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2063 return false;
2064 }
2065 }
2066 }
2067
Manman Ren7e48b252012-10-12 23:39:43 +00002068 // If Caller's vararg or byval argument has been split between registers and
2069 // stack, do not perform tail call, since part of the argument is in caller's
2070 // local frame.
2071 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
2072 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002073 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002074 return false;
2075
Dale Johannesend679ff72010-06-03 21:09:53 +00002076 // If the callee takes no arguments then go on to check the results of the
2077 // call.
2078 if (!Outs.empty()) {
2079 // Check if stack adjustment is needed. For now, do not do this if any
2080 // argument is passed on the stack.
2081 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002082 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
2083 *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002084 CCInfo.AnalyzeCallOperands(Outs,
2085 CCAssignFnForNode(CalleeCC, false, isVarArg));
2086 if (CCInfo.getNextStackOffset()) {
2087 MachineFunction &MF = DAG.getMachineFunction();
2088
2089 // Check if the arguments are already laid out in the right way as
2090 // the caller's fixed stack objects.
2091 MachineFrameInfo *MFI = MF.getFrameInfo();
2092 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Eric Christopherd9134482014-08-04 21:25:23 +00002093 const TargetInstrInfo *TII =
2094 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002095 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2096 i != e;
2097 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002098 CCValAssign &VA = ArgLocs[i];
2099 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002100 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002101 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002102 if (VA.getLocInfo() == CCValAssign::Indirect)
2103 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002104 if (VA.needsCustom()) {
2105 // f64 and vector types are split into multiple registers or
2106 // register/stack-slot combinations. The types will not match
2107 // the registers; give up on memory f64 refs until we figure
2108 // out what to do about this.
2109 if (!VA.isRegLoc())
2110 return false;
2111 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002112 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002113 if (RegVT == MVT::v2f64) {
2114 if (!ArgLocs[++i].isRegLoc())
2115 return false;
2116 if (!ArgLocs[++i].isRegLoc())
2117 return false;
2118 }
2119 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002120 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2121 MFI, MRI, TII))
2122 return false;
2123 }
2124 }
2125 }
2126 }
2127
2128 return true;
2129}
2130
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002131bool
2132ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2133 MachineFunction &MF, bool isVarArg,
2134 const SmallVectorImpl<ISD::OutputArg> &Outs,
2135 LLVMContext &Context) const {
2136 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00002137 CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002138 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2139 isVarArg));
2140}
2141
Tim Northoverd8407452013-10-01 14:33:28 +00002142static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2143 SDLoc DL, SelectionDAG &DAG) {
2144 const MachineFunction &MF = DAG.getMachineFunction();
2145 const Function *F = MF.getFunction();
2146
2147 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2148
2149 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2150 // version of the "preferred return address". These offsets affect the return
2151 // instruction if this is a return from PL1 without hypervisor extensions.
2152 // IRQ/FIQ: +4 "subs pc, lr, #4"
2153 // SWI: 0 "subs pc, lr, #0"
2154 // ABORT: +4 "subs pc, lr, #4"
2155 // UNDEF: +4/+2 "subs pc, lr, #0"
2156 // UNDEF varies depending on where the exception came from ARM or Thumb
2157 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2158
2159 int64_t LROffset;
2160 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2161 IntKind == "ABORT")
2162 LROffset = 4;
2163 else if (IntKind == "SWI" || IntKind == "UNDEF")
2164 LROffset = 0;
2165 else
2166 report_fatal_error("Unsupported interrupt attribute. If present, value "
2167 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2168
2169 RetOps.insert(RetOps.begin() + 1, DAG.getConstant(LROffset, MVT::i32, false));
2170
Craig Topper48d114b2014-04-26 18:35:24 +00002171 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002172}
2173
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002174SDValue
2175ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002176 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002177 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002178 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002179 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002180
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002181 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002182 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002183
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002184 // CCState - Info about the registers and stack slots.
Eric Christopherb5217502014-08-06 18:45:26 +00002185 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2186 *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002187
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002188 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002189 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2190 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002191
Bob Wilsona4c22902009-04-17 19:07:39 +00002192 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002193 SmallVector<SDValue, 4> RetOps;
2194 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002195 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002196
Jonathan Roelofsef84bda2014-08-05 21:32:21 +00002197 MachineFunction &MF = DAG.getMachineFunction();
2198 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2199 AFI->setReturnRegsCount(RVLocs.size());
2200
Bob Wilsona4c22902009-04-17 19:07:39 +00002201 // Copy the result values into the output registers.
2202 for (unsigned i = 0, realRVLocIdx = 0;
2203 i != RVLocs.size();
2204 ++i, ++realRVLocIdx) {
2205 CCValAssign &VA = RVLocs[i];
2206 assert(VA.isRegLoc() && "Can only return in registers!");
2207
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002208 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002209
2210 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002211 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002212 case CCValAssign::Full: break;
2213 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002214 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002215 break;
2216 }
2217
Bob Wilsona4c22902009-04-17 19:07:39 +00002218 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002219 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002220 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002221 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2222 DAG.getConstant(0, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002223 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002224 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002225
Christian Pirkerb5728192014-05-08 14:06:24 +00002226 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2227 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2228 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002229 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002230 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002231 VA = RVLocs[++i]; // skip ahead to next loc
2232 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002233 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2234 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002235 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002236 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002237 VA = RVLocs[++i]; // skip ahead to next loc
2238
2239 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002240 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2241 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002242 }
2243 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2244 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002245 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002246 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002247 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2248 fmrrd.getValue(isLittleEndian ? 0 : 1),
2249 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002250 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002251 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002252 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002253 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2254 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002255 Flag);
2256 } else
2257 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2258
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002259 // Guarantee that all emitted copies are
2260 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +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 }
2264
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002265 // Update chain and glue.
2266 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002267 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002268 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002269
Tim Northoverd8407452013-10-01 14:33:28 +00002270 // CPUs which aren't M-class use a special sequence to return from
2271 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2272 // though we use "subs pc, lr, #N").
2273 //
2274 // M-class CPUs actually use a normal return sequence with a special
2275 // (hardware-provided) value in LR, so the normal code path works.
2276 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2277 !Subtarget->isMClass()) {
2278 if (Subtarget->isThumb1Only())
2279 report_fatal_error("interrupt attribute is not supported in Thumb1");
2280 return LowerInterruptReturn(RetOps, dl, DAG);
2281 }
2282
Craig Topper48d114b2014-04-26 18:35:24 +00002283 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002284}
2285
Evan Chengf8bad082012-04-10 01:51:00 +00002286bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002287 if (N->getNumValues() != 1)
2288 return false;
2289 if (!N->hasNUsesOfValue(1, 0))
2290 return false;
2291
Evan Chengf8bad082012-04-10 01:51:00 +00002292 SDValue TCChain = Chain;
2293 SDNode *Copy = *N->use_begin();
2294 if (Copy->getOpcode() == ISD::CopyToReg) {
2295 // If the copy has a glue operand, we conservatively assume it isn't safe to
2296 // perform a tail call.
2297 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2298 return false;
2299 TCChain = Copy->getOperand(0);
2300 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2301 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002302 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002303 SmallPtrSet<SDNode*, 2> Copies;
2304 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002305 UI != UE; ++UI) {
2306 if (UI->getOpcode() != ISD::CopyToReg)
2307 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002308 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002309 }
Evan Chengf8bad082012-04-10 01:51:00 +00002310 if (Copies.size() > 2)
2311 return false;
2312
2313 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2314 UI != UE; ++UI) {
2315 SDValue UseChain = UI->getOperand(0);
2316 if (Copies.count(UseChain.getNode()))
2317 // Second CopyToReg
2318 Copy = *UI;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002319 else {
2320 // We are at the top of this chain.
2321 // If the copy has a glue operand, we conservatively assume it
2322 // isn't safe to perform a tail call.
2323 if (UI->getOperand(UI->getNumOperands()-1).getValueType() == MVT::Glue)
2324 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002325 // First CopyToReg
2326 TCChain = UseChain;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002327 }
Evan Chengf8bad082012-04-10 01:51:00 +00002328 }
2329 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002330 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002331 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002332 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002333 Copy = *Copy->use_begin();
2334 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002335 return false;
Quentin Colombet17799fe2014-09-18 21:17:50 +00002336 // If the copy has a glue operand, we conservatively assume it isn't safe to
2337 // perform a tail call.
2338 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2339 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002340 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002341 } else {
2342 return false;
2343 }
2344
Evan Cheng419ea282010-12-01 22:59:46 +00002345 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002346 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2347 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002348 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2349 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002350 return false;
2351 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002352 }
2353
Evan Chengf8bad082012-04-10 01:51:00 +00002354 if (!HasRet)
2355 return false;
2356
2357 Chain = TCChain;
2358 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002359}
2360
Evan Cheng0663f232011-03-21 01:19:09 +00002361bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002362 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002363 return false;
2364
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00002365 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng0663f232011-03-21 01:19:09 +00002366 return false;
2367
2368 return !Subtarget->isThumb1Only();
2369}
2370
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002371// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2372// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2373// one of the above mentioned nodes. It has to be wrapped because otherwise
2374// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2375// be used to form addressing mode. These wrapped nodes will be selected
2376// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002377static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002378 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002379 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002380 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002381 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002382 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002383 if (CP->isMachineConstantPoolEntry())
2384 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2385 CP->getAlignment());
2386 else
2387 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2388 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002389 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002390}
2391
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002392unsigned ARMTargetLowering::getJumpTableEncoding() const {
2393 return MachineJumpTableInfo::EK_Inline;
2394}
2395
Dan Gohman21cea8a2010-04-17 15:26:15 +00002396SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2397 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002398 MachineFunction &MF = DAG.getMachineFunction();
2399 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2400 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002401 SDLoc DL(Op);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002402 EVT PtrVT = getPointerTy();
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002403 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002404 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2405 SDValue CPAddr;
2406 if (RelocM == Reloc::Static) {
2407 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2408 } else {
2409 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002410 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002411 ARMConstantPoolValue *CPV =
2412 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2413 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002414 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2415 }
2416 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2417 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002418 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002419 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002420 if (RelocM == Reloc::Static)
2421 return Result;
Evan Cheng408aa562009-11-06 22:24:13 +00002422 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002423 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002424}
2425
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002426// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002427SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002428ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002429 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002430 SDLoc dl(GA);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002431 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002432 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002433 MachineFunction &MF = DAG.getMachineFunction();
2434 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002435 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002436 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002437 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2438 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002439 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002440 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002441 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattner7727d052010-09-21 06:44:06 +00002442 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002443 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002444 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002445
Evan Cheng408aa562009-11-06 22:24:13 +00002446 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002447 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002448
2449 // call __tls_get_addr.
2450 ArgListTy Args;
2451 ArgListEntry Entry;
2452 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002453 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002454 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002455
Dale Johannesen555a3752009-01-30 23:10:59 +00002456 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002457 TargetLowering::CallLoweringInfo CLI(DAG);
2458 CLI.setDebugLoc(dl).setChain(Chain)
2459 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00002460 DAG.getExternalSymbol("__tls_get_addr", PtrVT), std::move(Args),
2461 0);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002462
Justin Holewinskiaa583972012-05-25 16:35:28 +00002463 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002464 return CallResult.first;
2465}
2466
2467// Lower ISD::GlobalTLSAddress using the "initial exec" or
2468// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002469SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002470ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002471 SelectionDAG &DAG,
2472 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002473 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002474 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002475 SDValue Offset;
2476 SDValue Chain = DAG.getEntryNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002477 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002478 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002479 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002480
Hans Wennborgaea41202012-05-04 09:40:39 +00002481 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002482 MachineFunction &MF = DAG.getMachineFunction();
2483 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002484 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002485 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002486 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2487 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002488 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2489 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2490 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002491 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002492 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002493 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002494 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002495 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002496 Chain = Offset.getValue(1);
2497
Evan Cheng408aa562009-11-06 22:24:13 +00002498 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002499 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002500
Evan Chengcdbb70c2009-10-31 03:39:36 +00002501 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002502 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002503 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002504 } else {
2505 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002506 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002507 ARMConstantPoolValue *CPV =
2508 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002509 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002510 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002511 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002512 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002513 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002514 }
2515
2516 // The address of the thread local variable is the add of the thread
2517 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002518 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002519}
2520
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002521SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002522ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002523 // TODO: implement the "local dynamic" model
2524 assert(Subtarget->isTargetELF() &&
2525 "TLS not implemented for non-ELF targets");
2526 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgaea41202012-05-04 09:40:39 +00002527
2528 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2529
2530 switch (model) {
2531 case TLSModel::GeneralDynamic:
2532 case TLSModel::LocalDynamic:
2533 return LowerToTLSGeneralDynamicModel(GA, DAG);
2534 case TLSModel::InitialExec:
2535 case TLSModel::LocalExec:
2536 return LowerToTLSExecModels(GA, DAG, model);
2537 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002538 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002539}
2540
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002541SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002542 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002543 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002544 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002545 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002546 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002547 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002548 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002549 ARMConstantPoolConstant::Create(GV,
2550 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002551 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002552 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson7117a912009-03-20 22:42:55 +00002553 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002554 CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002555 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002556 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002557 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002558 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002559 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002560 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002561 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002562 MachinePointerInfo::getGOT(),
2563 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002564 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002565 }
2566
2567 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002568 // pair. This is always cheaper.
Eric Christopherc1058df2014-07-04 01:55:26 +00002569 if (Subtarget->useMovt(DAG.getMachineFunction())) {
Evan Cheng68aec142011-01-19 02:16:49 +00002570 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002571 // FIXME: Once remat is capable of dealing with instructions with register
2572 // operands, expand this into two nodes.
2573 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2574 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002575 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002576 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2577 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2578 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2579 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002580 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002581 }
2582}
2583
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002584SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002585 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002586 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002587 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002588 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002589 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002590
Eric Christopherc1058df2014-07-04 01:55:26 +00002591 if (Subtarget->useMovt(DAG.getMachineFunction()))
Evan Cheng68aec142011-01-19 02:16:49 +00002592 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002593
Tim Northover72360d22013-12-02 10:35:41 +00002594 // FIXME: Once remat is capable of dealing with instructions with register
2595 // operands, expand this into multiple nodes
2596 unsigned Wrapper =
2597 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002598
Tim Northover72360d22013-12-02 10:35:41 +00002599 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2600 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002601
Evan Cheng1b389522009-09-03 07:04:02 +00002602 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002603 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2604 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002605 return Result;
2606}
2607
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002608SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2609 SelectionDAG &DAG) const {
2610 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
Eric Christopherc1058df2014-07-04 01:55:26 +00002611 assert(Subtarget->useMovt(DAG.getMachineFunction()) &&
2612 "Windows on ARM expects to use movw/movt");
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002613
2614 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002615 const ARMII::TOF TargetFlags =
2616 (GV->hasDLLImportStorageClass() ? ARMII::MO_DLLIMPORT : ARMII::MO_NO_FLAG);
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002617 EVT PtrVT = getPointerTy();
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002618 SDValue Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002619 SDLoc DL(Op);
2620
2621 ++NumMovwMovt;
2622
2623 // FIXME: Once remat is capable of dealing with instructions with register
2624 // operands, expand this into two nodes.
Saleem Abdulrasool763f9a52014-07-07 05:18:35 +00002625 Result = DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2626 DAG.getTargetGlobalAddress(GV, DL, PtrVT, /*Offset=*/0,
2627 TargetFlags));
2628 if (GV->hasDLLImportStorageClass())
2629 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
2630 MachinePointerInfo::getGOT(), false, false, false, 0);
2631 return Result;
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002632}
2633
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002634SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002635 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002636 assert(Subtarget->isTargetELF() &&
2637 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002638 MachineFunction &MF = DAG.getMachineFunction();
2639 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002640 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002641 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002642 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002643 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002644 ARMConstantPoolValue *CPV =
2645 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2646 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002647 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002648 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002649 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002650 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002651 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00002652 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002653 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002654}
2655
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002656SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002657ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002658 SDLoc dl(Op);
Jim Grosbachfaa3abb2010-05-27 23:49:24 +00002659 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002660 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2661 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002662 Op.getOperand(1), Val);
2663}
2664
2665SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002666ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002667 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002668 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2669 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2670}
2671
2672SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002673ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002674 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002675 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002676 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002677 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002678 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002679 case Intrinsic::arm_rbit: {
Yi Kongc655f0c2014-08-20 10:40:20 +00002680 assert(Op.getOperand(1).getValueType() == MVT::i32 &&
Jim Grosbach07393ba2014-06-16 21:55:30 +00002681 "RBIT intrinsic must have i32 type!");
Yi Kongc655f0c2014-08-20 10:40:20 +00002682 return DAG.getNode(ARMISD::RBIT, dl, MVT::i32, Op.getOperand(1));
Jim Grosbach07393ba2014-06-16 21:55:30 +00002683 }
Bob Wilson17f88782009-08-04 00:25:01 +00002684 case Intrinsic::arm_thread_pointer: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002685 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson17f88782009-08-04 00:25:01 +00002686 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2687 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002688 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002689 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002690 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002691 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002692 EVT PtrVT = getPointerTy();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002693 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2694 SDValue CPAddr;
2695 unsigned PCAdj = (RelocM != Reloc::PIC_)
2696 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002697 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002698 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2699 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002700 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002701 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002702 SDValue Result =
Evan Chengcdbb70c2009-10-31 03:39:36 +00002703 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002704 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002705 false, false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002706
2707 if (RelocM == Reloc::PIC_) {
Evan Cheng408aa562009-11-06 22:24:13 +00002708 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002709 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2710 }
2711 return Result;
2712 }
Evan Cheng18381b42011-03-29 23:06:19 +00002713 case Intrinsic::arm_neon_vmulls:
2714 case Intrinsic::arm_neon_vmullu: {
2715 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2716 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002717 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002718 Op.getOperand(1), Op.getOperand(2));
2719 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002720 }
2721}
2722
Eli Friedman30a49e92011-08-03 21:06:02 +00002723static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2724 const ARMSubtarget *Subtarget) {
2725 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002726 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002727 if (!Subtarget->hasDataBarrier()) {
2728 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2729 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2730 // here.
2731 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002732 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002733 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002734 DAG.getConstant(0, MVT::i32));
2735 }
2736
Tim Northover36b24172013-07-03 09:20:36 +00002737 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2738 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
Robin Morisseta47cb412014-09-03 21:01:03 +00002739 ARM_MB::MemBOpt Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002740 if (Subtarget->isMClass()) {
2741 // Only a full system barrier exists in the M-class architectures.
2742 Domain = ARM_MB::SY;
2743 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002744 // Swift happens to implement ISHST barriers in a way that's compatible with
2745 // Release semantics but weaker than ISH so we'd be fools not to use
2746 // it. Beware: other processors probably don't!
2747 Domain = ARM_MB::ISHST;
2748 }
2749
Joey Gouly926d3f52013-09-05 15:35:24 +00002750 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
2751 DAG.getConstant(Intrinsic::arm_dmb, MVT::i32),
Tim Northover36b24172013-07-03 09:20:36 +00002752 DAG.getConstant(Domain, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002753}
2754
Evan Cheng8740ee32010-11-03 06:34:55 +00002755static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2756 const ARMSubtarget *Subtarget) {
2757 // ARM pre v5TE and Thumb1 does not have preload instructions.
2758 if (!(Subtarget->isThumb2() ||
2759 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2760 // Just preserve the chain.
2761 return Op.getOperand(0);
2762
Andrew Trickef9de2a2013-05-25 02:42:55 +00002763 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002764 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2765 if (!isRead &&
2766 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2767 // ARMv7 with MP extension has PLDW.
2768 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002769
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002770 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2771 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002772 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002773 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002774 isData = ~isData & 1;
2775 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002776
2777 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng21acf9f2010-11-04 05:19:35 +00002778 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2779 DAG.getConstant(isData, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002780}
2781
Dan Gohman31ae5862010-04-17 14:41:14 +00002782static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2783 MachineFunction &MF = DAG.getMachineFunction();
2784 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2785
Evan Cheng10043e22007-01-19 07:51:42 +00002786 // vastart just stores the address of the VarArgsFrameIndex slot into the
2787 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002788 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002789 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002790 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002791 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002792 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2793 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002794}
2795
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002796SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002797ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2798 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002799 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002800 MachineFunction &MF = DAG.getMachineFunction();
2801 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2802
Craig Topper760b1342012-02-22 05:59:10 +00002803 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002804 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002805 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002806 else
Craig Topperc7242e02012-04-20 07:30:17 +00002807 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002808
2809 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002810 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002811 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002812
2813 SDValue ArgValue2;
2814 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002815 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002816 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002817
2818 // Create load node to retrieve arguments from the stack.
2819 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Chengcdbb70c2009-10-31 03:39:36 +00002820 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002821 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002822 false, false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002823 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002824 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002825 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002826 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002827 if (!Subtarget->isLittle())
2828 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002829 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002830}
2831
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002832void
2833ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002834 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002835 unsigned ArgSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002836 unsigned &ArgRegsSize,
2837 unsigned &ArgRegsSaveSize)
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002838 const {
2839 unsigned NumGPRs;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002840 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2841 unsigned RBegin, REnd;
2842 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2843 NumGPRs = REnd - RBegin;
2844 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002845 unsigned int firstUnalloced;
2846 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2847 sizeof(GPRArgRegs) /
2848 sizeof(GPRArgRegs[0]));
2849 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2850 }
2851
Eric Christopherd9134482014-08-04 21:25:23 +00002852 unsigned Align = MF.getTarget()
2853 .getSubtargetImpl()
2854 ->getFrameLowering()
2855 ->getStackAlignment();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002856 ArgRegsSize = NumGPRs * 4;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002857
2858 // If parameter is split between stack and GPRs...
Mark Seabornbe266aa2014-02-16 18:59:48 +00002859 if (NumGPRs && Align > 4 &&
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002860 (ArgRegsSize < ArgSize ||
2861 InRegsParamRecordIdx >= CCInfo.getInRegsParamsCount())) {
Mark Seabornbe266aa2014-02-16 18:59:48 +00002862 // Add padding for part of param recovered from GPRs. For example,
2863 // if Align == 8, its last byte must be at address K*8 - 1.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002864 // We need to do it, since remained (stack) part of parameter has
2865 // stack alignment, and we need to "attach" "GPRs head" without gaps
2866 // to it:
2867 // Stack:
2868 // |---- 8 bytes block ----| |---- 8 bytes block ----| |---- 8 bytes...
2869 // [ [padding] [GPRs head] ] [ Tail passed via stack ....
2870 //
2871 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2872 unsigned Padding =
Mark Seabornbe266aa2014-02-16 18:59:48 +00002873 OffsetToAlignment(ArgRegsSize + AFI->getArgRegsSaveSize(), Align);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002874 ArgRegsSaveSize = ArgRegsSize + Padding;
2875 } else
2876 // We don't need to extend regs save size for byval parameters if they
2877 // are passed via GPRs only.
2878 ArgRegsSaveSize = ArgRegsSize;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002879}
2880
2881// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002882// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002883// byval). Either way, we allocate stack slots adjacent to the data
2884// provided by our caller, and store the unallocated registers there.
2885// If this is a variadic function, the va_list pointer will begin with
2886// these values; otherwise, this reassembles a (byval) structure that
2887// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002888// Return: The frame index registers were stored into.
2889int
2890ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002891 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002892 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002893 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002894 unsigned OffsetFromOrigArg,
2895 unsigned ArgOffset,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002896 unsigned ArgSize,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002897 bool ForceMutable,
2898 unsigned ByValStoreOffset,
2899 unsigned TotalArgRegsSaveSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002900
2901 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002902 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002903 // Setup first unallocated register as first byval register;
2904 // eat all remained registers
2905 // (these two actions are performed by HandleByVal method).
2906 // Then, here, we initialize stack frame with
2907 // "store-reg" instructions.
2908 // Case #2. Var-args function, that doesn't contain byval parameters.
2909 // The same: eat all remained unallocated registers,
2910 // initialize stack frame.
2911
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002912 MachineFunction &MF = DAG.getMachineFunction();
2913 MachineFrameInfo *MFI = MF.getFrameInfo();
2914 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002915 unsigned firstRegToSaveIndex, lastRegToSaveIndex;
2916 unsigned RBegin, REnd;
2917 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2918 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2919 firstRegToSaveIndex = RBegin - ARM::R0;
2920 lastRegToSaveIndex = REnd - ARM::R0;
2921 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002922 firstRegToSaveIndex = CCInfo.getFirstUnallocated
Craig Topper58713212013-07-15 04:27:47 +00002923 (GPRArgRegs, array_lengthof(GPRArgRegs));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002924 lastRegToSaveIndex = 4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002925 }
2926
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002927 unsigned ArgRegsSize, ArgRegsSaveSize;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002928 computeRegArea(CCInfo, MF, InRegsParamRecordIdx, ArgSize,
2929 ArgRegsSize, ArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002930
2931 // Store any by-val regs to their spots on the stack so that they may be
2932 // loaded by deferencing the result of formal parameter pointer or va_next.
2933 // Note: once stack area for byval/varargs registers
2934 // was initialized, it can't be initialized again.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002935 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002936 unsigned Padding = ArgRegsSaveSize - ArgRegsSize;
2937
2938 if (Padding) {
2939 assert(AFI->getStoredByValParamsPadding() == 0 &&
2940 "The only parameter may be padded.");
2941 AFI->setStoredByValParamsPadding(Padding);
2942 }
2943
Oliver Stannardd55e1152014-03-05 15:25:27 +00002944 int FrameIndex = MFI->CreateFixedObject(ArgRegsSaveSize,
2945 Padding +
2946 ByValStoreOffset -
2947 (int64_t)TotalArgRegsSaveSize,
2948 false);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002949 SDValue FIN = DAG.getFrameIndex(FrameIndex, getPointerTy());
Oliver Stannardd55e1152014-03-05 15:25:27 +00002950 if (Padding) {
2951 MFI->CreateFixedObject(Padding,
2952 ArgOffset + ByValStoreOffset -
2953 (int64_t)ArgRegsSaveSize,
2954 false);
2955 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002956
2957 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002958 for (unsigned i = 0; firstRegToSaveIndex < lastRegToSaveIndex;
2959 ++firstRegToSaveIndex, ++i) {
Craig Topper760b1342012-02-22 05:59:10 +00002960 const TargetRegisterClass *RC;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002961 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002962 RC = &ARM::tGPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002963 else
Craig Topperc7242e02012-04-20 07:30:17 +00002964 RC = &ARM::GPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002965
2966 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2967 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2968 SDValue Store =
2969 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002970 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002971 false, false, 0);
2972 MemOps.push_back(Store);
2973 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2974 DAG.getConstant(4, getPointerTy()));
2975 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002976
2977 AFI->setArgRegsSaveSize(ArgRegsSaveSize + AFI->getArgRegsSaveSize());
2978
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002979 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002980 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002981 return FrameIndex;
Oliver Stannardd55e1152014-03-05 15:25:27 +00002982 } else {
2983 if (ArgSize == 0) {
2984 // We cannot allocate a zero-byte object for the first variadic argument,
2985 // so just make up a size.
2986 ArgSize = 4;
2987 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002988 // This will point to the next argument passed via stack.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002989 return MFI->CreateFixedObject(
Oliver Stannardd55e1152014-03-05 15:25:27 +00002990 ArgSize, ArgOffset, !ForceMutable);
2991 }
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002992}
2993
2994// Setup stack frame, the va_list pointer will start from.
2995void
2996ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002997 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002998 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002999 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003000 bool ForceMutable) const {
3001 MachineFunction &MF = DAG.getMachineFunction();
3002 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3003
3004 // Try to store any remaining integer argument regs
3005 // to their spots on the stack so that they may be loaded by deferencing
3006 // the result of va_next.
3007 // If there is no regs to be stored, just point address after last
3008 // argument passed via stack.
3009 int FrameIndex =
Craig Topper062a2ba2014-04-25 05:30:21 +00003010 StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
3011 CCInfo.getInRegsParamsCount(), 0, ArgOffset, 0, ForceMutable,
3012 0, TotalArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003013
3014 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003015}
3016
Bob Wilson2e076c42009-06-22 23:27:02 +00003017SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003018ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00003019 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003020 const SmallVectorImpl<ISD::InputArg>
3021 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003022 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00003023 SmallVectorImpl<SDValue> &InVals)
3024 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00003025 MachineFunction &MF = DAG.getMachineFunction();
3026 MachineFrameInfo *MFI = MF.getFrameInfo();
3027
Bob Wilsona4c22902009-04-17 19:07:39 +00003028 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
3029
3030 // Assign locations to all of the incoming arguments.
3031 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00003032 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
3033 *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003034 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003035 CCAssignFnForNode(CallConv, /* Return*/ false,
3036 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00003037
Bob Wilsona4c22902009-04-17 19:07:39 +00003038 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003039 int lastInsIndex = -1;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003040 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003041 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
3042 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003043
3044 // Initially ArgRegsSaveSize is zero.
3045 // Then we increase this value each time we meet byval parameter.
3046 // We also increase this value in case of varargs function.
3047 AFI->setArgRegsSaveSize(0);
3048
Oliver Stannardd55e1152014-03-05 15:25:27 +00003049 unsigned ByValStoreOffset = 0;
3050 unsigned TotalArgRegsSaveSize = 0;
3051 unsigned ArgRegsSaveSizeMaxAlign = 4;
3052
3053 // Calculate the amount of stack space that we need to allocate to store
3054 // byval and variadic arguments that are passed in registers.
3055 // We need to know this before we allocate the first byval or variadic
3056 // argument, as they will be allocated a stack slot below the CFA (Canonical
3057 // Frame Address, the stack pointer at entry to the function).
3058 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3059 CCValAssign &VA = ArgLocs[i];
3060 if (VA.isMemLoc()) {
3061 int index = VA.getValNo();
3062 if (index != lastInsIndex) {
3063 ISD::ArgFlagsTy Flags = Ins[index].Flags;
3064 if (Flags.isByVal()) {
3065 unsigned ExtraArgRegsSize;
3066 unsigned ExtraArgRegsSaveSize;
Daniel Sanders8104b752014-11-01 19:32:23 +00003067 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsProcessed(),
Oliver Stannardd55e1152014-03-05 15:25:27 +00003068 Flags.getByValSize(),
3069 ExtraArgRegsSize, ExtraArgRegsSaveSize);
3070
3071 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
3072 if (Flags.getByValAlign() > ArgRegsSaveSizeMaxAlign)
3073 ArgRegsSaveSizeMaxAlign = Flags.getByValAlign();
3074 CCInfo.nextInRegsParam();
3075 }
3076 lastInsIndex = index;
3077 }
3078 }
3079 }
3080 CCInfo.rewindByValRegsInfo();
3081 lastInsIndex = -1;
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003082 if (isVarArg && MFI->hasVAStart()) {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003083 unsigned ExtraArgRegsSize;
3084 unsigned ExtraArgRegsSaveSize;
3085 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsCount(), 0,
3086 ExtraArgRegsSize, ExtraArgRegsSaveSize);
3087 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
3088 }
3089 // If the arg regs save area contains N-byte aligned values, the
3090 // bottom of it must be at least N-byte aligned.
3091 TotalArgRegsSaveSize = RoundUpToAlignment(TotalArgRegsSaveSize, ArgRegsSaveSizeMaxAlign);
3092 TotalArgRegsSaveSize = std::min(TotalArgRegsSaveSize, 16U);
3093
Bob Wilsona4c22902009-04-17 19:07:39 +00003094 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3095 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003096 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
3097 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003098 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003099 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003100 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003101
Bob Wilsona4c22902009-04-17 19:07:39 +00003102 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003103 // f64 and vector types are split up into multiple registers or
3104 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003105 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003106 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003107 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003108 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003109 SDValue ArgValue2;
3110 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003111 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson699bdf72010-04-13 22:03:22 +00003112 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3113 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003114 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003115 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003116 } else {
3117 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3118 Chain, DAG, dl);
3119 }
Owen Anderson9f944592009-08-11 20:47:22 +00003120 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3121 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00003122 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson9f944592009-08-11 20:47:22 +00003123 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00003124 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
3125 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003126 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003127
Bob Wilson2e076c42009-06-22 23:27:02 +00003128 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003129 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003130
Owen Anderson9f944592009-08-11 20:47:22 +00003131 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003132 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003133 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003134 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003135 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003136 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003137 else if (RegVT == MVT::i32)
Craig Topper61e88f42014-11-21 05:58:21 +00003138 RC = AFI->isThumb1OnlyFunction() ? &ARM::tGPRRegClass
3139 : &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003140 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003141 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003142
3143 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003144 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003145 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003146 }
3147
3148 // If this is an 8 or 16-bit value, it is really passed promoted
3149 // to 32 bits. Insert an assert[sz]ext to capture this, then
3150 // truncate to the right size.
3151 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003152 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003153 case CCValAssign::Full: break;
3154 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003155 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003156 break;
3157 case CCValAssign::SExt:
3158 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3159 DAG.getValueType(VA.getValVT()));
3160 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3161 break;
3162 case CCValAssign::ZExt:
3163 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3164 DAG.getValueType(VA.getValVT()));
3165 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3166 break;
3167 }
3168
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003169 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003170
3171 } else { // VA.isRegLoc()
3172
3173 // sanity check
3174 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003175 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003176
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003177 int index = ArgLocs[i].getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003178
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003179 // Some Ins[] entries become multiple ArgLoc[] entries.
3180 // Process them only once.
3181 if (index != lastInsIndex)
3182 {
3183 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003184 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003185 // This can be changed with more analysis.
3186 // In case of tail call optimization mark all arguments mutable.
3187 // Since they could be overwritten by lowering of arguments in case of
3188 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003189 if (Flags.isByVal()) {
Daniel Sanders8104b752014-11-01 19:32:23 +00003190 unsigned CurByValIndex = CCInfo.getInRegsParamsProcessed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003191
3192 ByValStoreOffset = RoundUpToAlignment(ByValStoreOffset, Flags.getByValAlign());
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003193 int FrameIndex = StoreByValRegs(
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003194 CCInfo, DAG, dl, Chain, CurOrigArg,
3195 CurByValIndex,
3196 Ins[VA.getValNo()].PartOffset,
3197 VA.getLocMemOffset(),
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003198 Flags.getByValSize(),
Oliver Stannardd55e1152014-03-05 15:25:27 +00003199 true /*force mutable frames*/,
3200 ByValStoreOffset,
3201 TotalArgRegsSaveSize);
3202 ByValStoreOffset += Flags.getByValSize();
3203 ByValStoreOffset = std::min(ByValStoreOffset, 16U);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003204 InVals.push_back(DAG.getFrameIndex(FrameIndex, getPointerTy()));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003205 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003206 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003207 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003208 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003209 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003210
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003211 // Create load nodes to retrieve arguments from the stack.
3212 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3213 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3214 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003215 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003216 }
3217 lastInsIndex = index;
3218 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003219 }
3220 }
3221
3222 // varargs
Reid Kleckner2d9bb652014-08-22 21:59:26 +00003223 if (isVarArg && MFI->hasVAStart())
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003224 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003225 CCInfo.getNextStackOffset(),
3226 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003227
Oliver Stannardb14c6252014-04-02 16:10:33 +00003228 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3229
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003230 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003231}
3232
3233/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003234static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003235 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003236 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003237 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003238 // Maybe this has already been legalized into the constant pool?
3239 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003240 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003241 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003242 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003243 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003244 }
Renato Golin6fb9c2e2014-10-23 15:31:50 +00003245 } else if (Op->getOpcode() == ISD::BITCAST &&
3246 Op->getValueType(0) == MVT::f64) {
3247 // Handle (ISD::BITCAST (ARMISD::VMOVIMM (ISD::TargetConstant 0)) MVT::f64)
3248 // created by LowerConstantFP().
3249 SDValue BitcastOp = Op->getOperand(0);
3250 if (BitcastOp->getOpcode() == ARMISD::VMOVIMM) {
3251 SDValue MoveOp = BitcastOp->getOperand(0);
3252 if (MoveOp->getOpcode() == ISD::TargetConstant &&
3253 cast<ConstantSDNode>(MoveOp)->getZExtValue() == 0) {
3254 return true;
3255 }
3256 }
Evan Cheng10043e22007-01-19 07:51:42 +00003257 }
3258 return false;
3259}
3260
Evan Cheng10043e22007-01-19 07:51:42 +00003261/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3262/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003263SDValue
3264ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003265 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003266 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003267 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003268 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003269 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003270 // Constant does not fit, try adjusting it by one?
3271 switch (CC) {
3272 default: break;
3273 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003274 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003275 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003276 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003277 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003278 }
3279 break;
3280 case ISD::SETULT:
3281 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003282 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003283 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003284 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003285 }
3286 break;
3287 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003288 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003289 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003290 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003291 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003292 }
3293 break;
3294 case ISD::SETULE:
3295 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003296 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003297 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003298 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003299 }
3300 break;
3301 }
3302 }
3303 }
3304
3305 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003306 ARMISD::NodeType CompareType;
3307 switch (CondCode) {
3308 default:
3309 CompareType = ARMISD::CMP;
3310 break;
3311 case ARMCC::EQ:
3312 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003313 // Uses only Z Flag
3314 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003315 break;
3316 }
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003317 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003318 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003319}
3320
3321/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003322SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003323ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003324 SDLoc dl) const {
Oliver Stannard51b1d462014-08-21 12:50:31 +00003325 assert(!Subtarget->isFPOnlySP() || RHS.getValueType() != MVT::f64);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003326 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003327 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003328 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003329 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003330 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3331 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003332}
3333
Bob Wilson45acbd02011-03-08 01:17:20 +00003334/// duplicateCmp - Glue values can have only one use, so this function
3335/// duplicates a comparison node.
3336SDValue
3337ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3338 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003339 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003340 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3341 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3342
3343 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3344 Cmp = Cmp.getOperand(0);
3345 Opc = Cmp.getOpcode();
3346 if (Opc == ARMISD::CMPFP)
3347 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3348 else {
3349 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3350 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3351 }
3352 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3353}
3354
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003355std::pair<SDValue, SDValue>
3356ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3357 SDValue &ARMcc) const {
3358 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3359
3360 SDValue Value, OverflowCmp;
3361 SDValue LHS = Op.getOperand(0);
3362 SDValue RHS = Op.getOperand(1);
3363
3364
3365 // FIXME: We are currently always generating CMPs because we don't support
3366 // generating CMN through the backend. This is not as good as the natural
3367 // CMP case because it causes a register dependency and cannot be folded
3368 // later.
3369
3370 switch (Op.getOpcode()) {
3371 default:
3372 llvm_unreachable("Unknown overflow instruction!");
3373 case ISD::SADDO:
3374 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3375 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3376 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3377 break;
3378 case ISD::UADDO:
3379 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3380 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3381 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3382 break;
3383 case ISD::SSUBO:
3384 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3385 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3386 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3387 break;
3388 case ISD::USUBO:
3389 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3390 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3391 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3392 break;
3393 } // switch (...)
3394
3395 return std::make_pair(Value, OverflowCmp);
3396}
3397
3398
3399SDValue
3400ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3401 // Let legalize expand this if it isn't a legal type yet.
3402 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3403 return SDValue();
3404
3405 SDValue Value, OverflowCmp;
3406 SDValue ARMcc;
3407 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3408 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3409 // We use 0 and 1 as false and true values.
3410 SDValue TVal = DAG.getConstant(1, MVT::i32);
3411 SDValue FVal = DAG.getConstant(0, MVT::i32);
3412 EVT VT = Op.getValueType();
3413
3414 SDValue Overflow = DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, TVal, FVal,
3415 ARMcc, CCR, OverflowCmp);
3416
3417 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
3418 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), VTs, Value, Overflow);
3419}
3420
3421
Bill Wendling6a981312010-08-11 08:43:16 +00003422SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3423 SDValue Cond = Op.getOperand(0);
3424 SDValue SelectTrue = Op.getOperand(1);
3425 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003426 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003427 unsigned Opc = Cond.getOpcode();
3428
3429 if (Cond.getResNo() == 1 &&
3430 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3431 Opc == ISD::USUBO)) {
3432 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3433 return SDValue();
3434
3435 SDValue Value, OverflowCmp;
3436 SDValue ARMcc;
3437 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3438 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3439 EVT VT = Op.getValueType();
3440
Oliver Stannard51b1d462014-08-21 12:50:31 +00003441 return getCMOV(SDLoc(Op), VT, SelectTrue, SelectFalse, ARMcc, CCR,
3442 OverflowCmp, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003443 }
Bill Wendling6a981312010-08-11 08:43:16 +00003444
3445 // Convert:
3446 //
3447 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3448 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3449 //
3450 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3451 const ConstantSDNode *CMOVTrue =
3452 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3453 const ConstantSDNode *CMOVFalse =
3454 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3455
3456 if (CMOVTrue && CMOVFalse) {
3457 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3458 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3459
3460 SDValue True;
3461 SDValue False;
3462 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3463 True = SelectTrue;
3464 False = SelectFalse;
3465 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3466 True = SelectFalse;
3467 False = SelectTrue;
3468 }
3469
3470 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003471 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003472 SDValue ARMcc = Cond.getOperand(2);
3473 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003474 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003475 assert(True.getValueType() == VT);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003476 return getCMOV(dl, VT, True, False, ARMcc, CCR, Cmp, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00003477 }
3478 }
3479 }
3480
Dan Gohmand4a77c42012-02-24 00:09:36 +00003481 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3482 // undefined bits before doing a full-word comparison with zero.
3483 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
3484 DAG.getConstant(1, Cond.getValueType()));
3485
Bill Wendling6a981312010-08-11 08:43:16 +00003486 return DAG.getSelectCC(dl, Cond,
3487 DAG.getConstant(0, Cond.getValueType()),
3488 SelectTrue, SelectFalse, ISD::SETNE);
3489}
3490
Joey Gouly881eab52013-08-22 15:29:11 +00003491static ISD::CondCode getInverseCCForVSEL(ISD::CondCode CC) {
3492 if (CC == ISD::SETNE)
3493 return ISD::SETEQ;
Weiming Zhao63871d22013-12-18 22:25:17 +00003494 return ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003495}
3496
3497static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3498 bool &swpCmpOps, bool &swpVselOps) {
3499 // Start by selecting the GE condition code for opcodes that return true for
3500 // 'equality'
3501 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3502 CC == ISD::SETULE)
3503 CondCode = ARMCC::GE;
3504
3505 // and GT for opcodes that return false for 'equality'.
3506 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3507 CC == ISD::SETULT)
3508 CondCode = ARMCC::GT;
3509
3510 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3511 // to swap the compare operands.
3512 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3513 CC == ISD::SETULT)
3514 swpCmpOps = true;
3515
3516 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3517 // If we have an unordered opcode, we need to swap the operands to the VSEL
3518 // instruction (effectively negating the condition).
3519 //
3520 // This also has the effect of swapping which one of 'less' or 'greater'
3521 // returns true, so we also swap the compare operands. It also switches
3522 // whether we return true for 'equality', so we compensate by picking the
3523 // opposite condition code to our original choice.
3524 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3525 CC == ISD::SETUGT) {
3526 swpCmpOps = !swpCmpOps;
3527 swpVselOps = !swpVselOps;
3528 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3529 }
3530
3531 // 'ordered' is 'anything but unordered', so use the VS condition code and
3532 // swap the VSEL operands.
3533 if (CC == ISD::SETO) {
3534 CondCode = ARMCC::VS;
3535 swpVselOps = true;
3536 }
3537
3538 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3539 // code and swap the VSEL operands.
3540 if (CC == ISD::SETUNE) {
3541 CondCode = ARMCC::EQ;
3542 swpVselOps = true;
3543 }
3544}
3545
Oliver Stannard51b1d462014-08-21 12:50:31 +00003546SDValue ARMTargetLowering::getCMOV(SDLoc dl, EVT VT, SDValue FalseVal,
3547 SDValue TrueVal, SDValue ARMcc, SDValue CCR,
3548 SDValue Cmp, SelectionDAG &DAG) const {
3549 if (Subtarget->isFPOnlySP() && VT == MVT::f64) {
3550 FalseVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3551 DAG.getVTList(MVT::i32, MVT::i32), FalseVal);
3552 TrueVal = DAG.getNode(ARMISD::VMOVRRD, dl,
3553 DAG.getVTList(MVT::i32, MVT::i32), TrueVal);
3554
3555 SDValue TrueLow = TrueVal.getValue(0);
3556 SDValue TrueHigh = TrueVal.getValue(1);
3557 SDValue FalseLow = FalseVal.getValue(0);
3558 SDValue FalseHigh = FalseVal.getValue(1);
3559
3560 SDValue Low = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseLow, TrueLow,
3561 ARMcc, CCR, Cmp);
3562 SDValue High = DAG.getNode(ARMISD::CMOV, dl, MVT::i32, FalseHigh, TrueHigh,
3563 ARMcc, CCR, duplicateCmp(Cmp, DAG));
3564
3565 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Low, High);
3566 } else {
3567 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3568 Cmp);
3569 }
3570}
3571
Dan Gohman21cea8a2010-04-17 15:26:15 +00003572SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003573 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003574 SDValue LHS = Op.getOperand(0);
3575 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003576 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003577 SDValue TrueVal = Op.getOperand(2);
3578 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003579 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003580
Oliver Stannard51b1d462014-08-21 12:50:31 +00003581 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3582 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3583 dl);
3584
3585 // If softenSetCCOperands only returned one value, we should compare it to
3586 // zero.
3587 if (!RHS.getNode()) {
3588 RHS = DAG.getConstant(0, LHS.getValueType());
3589 CC = ISD::SETNE;
3590 }
3591 }
3592
Owen Anderson9f944592009-08-11 20:47:22 +00003593 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003594 // Try to generate VSEL on ARMv8.
3595 // The VSEL instruction can't use all the usual ARM condition
3596 // codes: it only has two bits to select the condition code, so it's
3597 // constrained to use only GE, GT, VS and EQ.
3598 //
3599 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3600 // swap the operands of the previous compare instruction (effectively
3601 // inverting the compare condition, swapping 'less' and 'greater') and
3602 // sometimes need to swap the operands to the VSEL (which inverts the
3603 // condition in the sense of firing whenever the previous condition didn't)
Joey Goulyccd04892013-09-13 13:46:57 +00003604 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003605 TrueVal.getValueType() == MVT::f64)) {
3606 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3607 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3608 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
3609 CC = getInverseCCForVSEL(CC);
3610 std::swap(TrueVal, FalseVal);
3611 }
3612 }
3613
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003614 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003615 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003616 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003617 return getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003618 }
3619
3620 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003621 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003622
Joey Gouly881eab52013-08-22 15:29:11 +00003623 // Try to generate VSEL on ARMv8.
Joey Goulyccd04892013-09-13 13:46:57 +00003624 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003625 TrueVal.getValueType() == MVT::f64)) {
Joey Goulye3dd6842013-08-23 12:01:13 +00003626 // We can select VMAXNM/VMINNM from a compare followed by a select with the
3627 // same operands, as follows:
3628 // c = fcmp [ogt, olt, ugt, ult] a, b
3629 // select c, a, b
3630 // We only do this in unsafe-fp-math, because signed zeros and NaNs are
3631 // handled differently than the original code sequence.
Oliver Stannard79efe412014-10-27 09:23:02 +00003632 if (getTargetMachine().Options.UnsafeFPMath) {
3633 if (LHS == TrueVal && RHS == FalseVal) {
3634 if (CC == ISD::SETOGT || CC == ISD::SETUGT)
3635 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal);
3636 if (CC == ISD::SETOLT || CC == ISD::SETULT)
3637 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal);
3638 } else if (LHS == FalseVal && RHS == TrueVal) {
3639 if (CC == ISD::SETOLT || CC == ISD::SETULT)
3640 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal);
3641 if (CC == ISD::SETOGT || CC == ISD::SETUGT)
3642 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal);
3643 }
Joey Goulye3dd6842013-08-23 12:01:13 +00003644 }
3645
Joey Gouly881eab52013-08-22 15:29:11 +00003646 bool swpCmpOps = false;
3647 bool swpVselOps = false;
3648 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3649
3650 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3651 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3652 if (swpCmpOps)
3653 std::swap(LHS, RHS);
3654 if (swpVselOps)
3655 std::swap(TrueVal, FalseVal);
3656 }
3657 }
3658
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003659 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3660 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003661 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003662 SDValue Result = getCMOV(dl, VT, FalseVal, TrueVal, ARMcc, CCR, Cmp, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003663 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003664 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003665 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003666 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Oliver Stannard51b1d462014-08-21 12:50:31 +00003667 Result = getCMOV(dl, VT, Result, TrueVal, ARMcc2, CCR, Cmp2, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00003668 }
3669 return Result;
3670}
3671
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003672/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3673/// to morph to an integer compare sequence.
3674static bool canChangeToInt(SDValue Op, bool &SeenZero,
3675 const ARMSubtarget *Subtarget) {
3676 SDNode *N = Op.getNode();
3677 if (!N->hasOneUse())
3678 // Otherwise it requires moving the value from fp to integer registers.
3679 return false;
3680 if (!N->getNumValues())
3681 return false;
3682 EVT VT = Op.getValueType();
3683 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3684 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3685 // vmrs are very slow, e.g. cortex-a8.
3686 return false;
3687
3688 if (isFloatingPointZero(Op)) {
3689 SeenZero = true;
3690 return true;
3691 }
3692 return ISD::isNormalLoad(N);
3693}
3694
3695static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3696 if (isFloatingPointZero(Op))
3697 return DAG.getConstant(0, MVT::i32);
3698
3699 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003700 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003701 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003702 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003703 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003704
3705 llvm_unreachable("Unknown VFP cmp argument!");
3706}
3707
3708static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3709 SDValue &RetVal1, SDValue &RetVal2) {
3710 if (isFloatingPointZero(Op)) {
3711 RetVal1 = DAG.getConstant(0, MVT::i32);
3712 RetVal2 = DAG.getConstant(0, MVT::i32);
3713 return;
3714 }
3715
3716 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3717 SDValue Ptr = Ld->getBasePtr();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003718 RetVal1 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003719 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003720 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003721 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003722 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003723
3724 EVT PtrType = Ptr.getValueType();
3725 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003726 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003727 PtrType, Ptr, DAG.getConstant(4, PtrType));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003728 RetVal2 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003729 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003730 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003731 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003732 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003733 return;
3734 }
3735
3736 llvm_unreachable("Unknown VFP cmp argument!");
3737}
3738
3739/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3740/// f32 and even f64 comparisons to integer ones.
3741SDValue
3742ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3743 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003744 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003745 SDValue LHS = Op.getOperand(2);
3746 SDValue RHS = Op.getOperand(3);
3747 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003748 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003749
Evan Chengd12af5d2012-03-01 23:27:13 +00003750 bool LHSSeenZero = false;
3751 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3752 bool RHSSeenZero = false;
3753 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3754 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003755 // If unsafe fp math optimization is enabled and there are no other uses of
3756 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003757 // to an integer comparison.
3758 if (CC == ISD::SETOEQ)
3759 CC = ISD::SETEQ;
3760 else if (CC == ISD::SETUNE)
3761 CC = ISD::SETNE;
3762
Evan Chengd12af5d2012-03-01 23:27:13 +00003763 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003764 SDValue ARMcc;
3765 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003766 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3767 bitcastf32Toi32(LHS, DAG), Mask);
3768 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3769 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003770 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3771 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3772 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3773 Chain, Dest, ARMcc, CCR, Cmp);
3774 }
3775
3776 SDValue LHS1, LHS2;
3777 SDValue RHS1, RHS2;
3778 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3779 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003780 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3781 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003782 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3783 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003784 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003785 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003786 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003787 }
3788
3789 return SDValue();
3790}
3791
3792SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3793 SDValue Chain = Op.getOperand(0);
3794 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3795 SDValue LHS = Op.getOperand(2);
3796 SDValue RHS = Op.getOperand(3);
3797 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003798 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003799
Oliver Stannard51b1d462014-08-21 12:50:31 +00003800 if (Subtarget->isFPOnlySP() && LHS.getValueType() == MVT::f64) {
3801 DAG.getTargetLoweringInfo().softenSetCCOperands(DAG, MVT::f64, LHS, RHS, CC,
3802 dl);
3803
3804 // If softenSetCCOperands only returned one value, we should compare it to
3805 // zero.
3806 if (!RHS.getNode()) {
3807 RHS = DAG.getConstant(0, LHS.getValueType());
3808 CC = ISD::SETNE;
3809 }
3810 }
3811
Owen Anderson9f944592009-08-11 20:47:22 +00003812 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003813 SDValue ARMcc;
3814 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003815 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003816 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003817 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003818 }
3819
Owen Anderson9f944592009-08-11 20:47:22 +00003820 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003821
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003822 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003823 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3824 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3825 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3826 if (Result.getNode())
3827 return Result;
3828 }
3829
Evan Cheng10043e22007-01-19 07:51:42 +00003830 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003831 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003832
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003833 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3834 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003835 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003836 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003837 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003838 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003839 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003840 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3841 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003842 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003843 }
3844 return Res;
3845}
3846
Dan Gohman21cea8a2010-04-17 15:26:15 +00003847SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003848 SDValue Chain = Op.getOperand(0);
3849 SDValue Table = Op.getOperand(1);
3850 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003851 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003852
Owen Anderson53aa7a92009-08-10 22:56:29 +00003853 EVT PTy = getPointerTy();
Evan Cheng10043e22007-01-19 07:51:42 +00003854 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3855 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3f17aee2009-07-14 18:44:34 +00003856 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003857 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson9f944592009-08-11 20:47:22 +00003858 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chengc8bed032009-07-28 20:53:24 +00003859 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3860 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003861 if (Subtarget->isThumb2()) {
3862 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3863 // which does another jump to the destination. This also makes it easier
3864 // to translate it to TBB / TBH later.
3865 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003866 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Chengc6d70ae2009-07-29 02:18:14 +00003867 Addr, Op.getOperand(2), JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003868 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003869 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003870 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattner7727d052010-09-21 06:44:06 +00003871 MachinePointerInfo::getJumpTable(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003872 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003873 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003874 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson9f944592009-08-11 20:47:22 +00003875 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003876 } else {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003877 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003878 MachinePointerInfo::getJumpTable(),
3879 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003880 Chain = Addr.getValue(1);
Owen Anderson9f944592009-08-11 20:47:22 +00003881 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003882 }
Evan Cheng10043e22007-01-19 07:51:42 +00003883}
3884
Eli Friedman2d4055b2011-11-09 23:36:02 +00003885static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003886 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003887 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003888
James Molloy547d4c02012-02-20 09:24:05 +00003889 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3890 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3891 return Op;
3892 return DAG.UnrollVectorOp(Op.getNode());
3893 }
3894
3895 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3896 "Invalid type for custom lowering!");
3897 if (VT != MVT::v4i16)
3898 return DAG.UnrollVectorOp(Op.getNode());
3899
3900 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3901 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003902}
3903
Oliver Stannard51b1d462014-08-21 12:50:31 +00003904SDValue ARMTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003905 EVT VT = Op.getValueType();
3906 if (VT.isVector())
3907 return LowerVectorFP_TO_INT(Op, DAG);
3908
Oliver Stannard51b1d462014-08-21 12:50:31 +00003909 if (Subtarget->isFPOnlySP() && Op.getOperand(0).getValueType() == MVT::f64) {
3910 RTLIB::Libcall LC;
3911 if (Op.getOpcode() == ISD::FP_TO_SINT)
3912 LC = RTLIB::getFPTOSINT(Op.getOperand(0).getValueType(),
3913 Op.getValueType());
3914 else
3915 LC = RTLIB::getFPTOUINT(Op.getOperand(0).getValueType(),
3916 Op.getValueType());
3917 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3918 /*isSigned*/ false, SDLoc(Op)).first;
3919 }
3920
Andrew Trickef9de2a2013-05-25 02:42:55 +00003921 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003922 unsigned Opc;
3923
3924 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003925 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003926 case ISD::FP_TO_SINT:
3927 Opc = ARMISD::FTOSI;
3928 break;
3929 case ISD::FP_TO_UINT:
3930 Opc = ARMISD::FTOUI;
3931 break;
3932 }
3933 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peck527da1b2010-11-23 03:31:01 +00003934 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003935}
3936
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003937static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3938 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003939 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003940
Eli Friedman2d4055b2011-11-09 23:36:02 +00003941 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3942 if (VT.getVectorElementType() == MVT::f32)
3943 return Op;
3944 return DAG.UnrollVectorOp(Op.getNode());
3945 }
3946
Duncan Sandsa41634e2011-08-12 14:54:45 +00003947 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3948 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003949 if (VT != MVT::v4f32)
3950 return DAG.UnrollVectorOp(Op.getNode());
3951
3952 unsigned CastOpc;
3953 unsigned Opc;
3954 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003955 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003956 case ISD::SINT_TO_FP:
3957 CastOpc = ISD::SIGN_EXTEND;
3958 Opc = ISD::SINT_TO_FP;
3959 break;
3960 case ISD::UINT_TO_FP:
3961 CastOpc = ISD::ZERO_EXTEND;
3962 Opc = ISD::UINT_TO_FP;
3963 break;
3964 }
3965
3966 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3967 return DAG.getNode(Opc, dl, VT, Op);
3968}
3969
Oliver Stannard51b1d462014-08-21 12:50:31 +00003970SDValue ARMTargetLowering::LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const {
Bob Wilsone4191e72010-03-19 22:51:32 +00003971 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003972 if (VT.isVector())
3973 return LowerVectorINT_TO_FP(Op, DAG);
3974
Oliver Stannard51b1d462014-08-21 12:50:31 +00003975 if (Subtarget->isFPOnlySP() && Op.getValueType() == MVT::f64) {
3976 RTLIB::Libcall LC;
3977 if (Op.getOpcode() == ISD::SINT_TO_FP)
3978 LC = RTLIB::getSINTTOFP(Op.getOperand(0).getValueType(),
3979 Op.getValueType());
3980 else
3981 LC = RTLIB::getUINTTOFP(Op.getOperand(0).getValueType(),
3982 Op.getValueType());
3983 return makeLibCall(DAG, LC, Op.getValueType(), &Op.getOperand(0), 1,
3984 /*isSigned*/ false, SDLoc(Op)).first;
3985 }
3986
Andrew Trickef9de2a2013-05-25 02:42:55 +00003987 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003988 unsigned Opc;
3989
3990 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003991 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003992 case ISD::SINT_TO_FP:
3993 Opc = ARMISD::SITOF;
3994 break;
3995 case ISD::UINT_TO_FP:
3996 Opc = ARMISD::UITOF;
3997 break;
3998 }
3999
Wesley Peck527da1b2010-11-23 03:31:01 +00004000 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilsone4191e72010-03-19 22:51:32 +00004001 return DAG.getNode(Opc, dl, VT, Op);
4002}
4003
Evan Cheng25f93642010-07-08 02:08:50 +00004004SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00004005 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004006 SDValue Tmp0 = Op.getOperand(0);
4007 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004008 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004009 EVT VT = Op.getValueType();
4010 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00004011 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
4012 Tmp0.getOpcode() == ARMISD::VMOVDRR;
4013 bool UseNEON = !InGPR && Subtarget->hasNEON();
4014
4015 if (UseNEON) {
4016 // Use VBSL to copy the sign bit.
4017 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
4018 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
4019 DAG.getTargetConstant(EncodedVal, MVT::i32));
4020 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
4021 if (VT == MVT::f64)
4022 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4023 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
4024 DAG.getConstant(32, MVT::i32));
4025 else /*if (VT == MVT::f32)*/
4026 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
4027 if (SrcVT == MVT::f32) {
4028 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
4029 if (VT == MVT::f64)
4030 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
4031 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
4032 DAG.getConstant(32, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00004033 } else if (VT == MVT::f32)
4034 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
4035 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
4036 DAG.getConstant(32, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00004037 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
4038 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
4039
4040 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
4041 MVT::i32);
4042 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
4043 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
4044 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00004045
Evan Chengd6b641e2011-02-23 02:24:55 +00004046 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
4047 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
4048 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00004049 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00004050 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
4051 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
4052 DAG.getConstant(0, MVT::i32));
4053 } else {
4054 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
4055 }
4056
4057 return Res;
4058 }
Evan Cheng2da1c952011-02-11 02:28:55 +00004059
4060 // Bitcast operand 1 to i32.
4061 if (SrcVT == MVT::f64)
4062 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004063 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00004064 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
4065
Evan Chengd6b641e2011-02-23 02:24:55 +00004066 // Or in the signbit with integer operations.
4067 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
4068 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
4069 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
4070 if (VT == MVT::f32) {
4071 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
4072 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
4073 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
4074 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00004075 }
4076
Evan Chengd6b641e2011-02-23 02:24:55 +00004077 // f64: Or the high part with signbit and then combine two parts.
4078 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00004079 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00004080 SDValue Lo = Tmp0.getValue(0);
4081 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
4082 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
4083 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00004084}
4085
Evan Cheng168ced92010-05-22 01:47:14 +00004086SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
4087 MachineFunction &MF = DAG.getMachineFunction();
4088 MachineFrameInfo *MFI = MF.getFrameInfo();
4089 MFI->setReturnAddressIsTaken(true);
4090
Bill Wendling908bf812014-01-06 00:43:20 +00004091 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004092 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00004093
Evan Cheng168ced92010-05-22 01:47:14 +00004094 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004095 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00004096 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
4097 if (Depth) {
4098 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
4099 SDValue Offset = DAG.getConstant(4, MVT::i32);
4100 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
4101 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004102 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00004103 }
4104
4105 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00004106 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00004107 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
4108}
4109
Dan Gohman21cea8a2010-04-17 15:26:15 +00004110SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004111 const ARMBaseRegisterInfo &ARI =
4112 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
4113 MachineFunction &MF = DAG.getMachineFunction();
4114 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004115 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00004116
Owen Anderson53aa7a92009-08-10 22:56:29 +00004117 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004118 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004119 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00004120 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004121 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
4122 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00004123 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
4124 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00004125 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00004126 return FrameAddr;
4127}
4128
Renato Golinc7aea402014-05-06 16:51:25 +00004129// FIXME? Maybe this could be a TableGen attribute on some registers and
4130// this table could be generated automatically from RegInfo.
Hal Finkelf0e086a2014-05-11 19:29:07 +00004131unsigned ARMTargetLowering::getRegisterByName(const char* RegName,
4132 EVT VT) const {
Renato Golinc7aea402014-05-06 16:51:25 +00004133 unsigned Reg = StringSwitch<unsigned>(RegName)
4134 .Case("sp", ARM::SP)
4135 .Default(0);
4136 if (Reg)
4137 return Reg;
4138 report_fatal_error("Invalid register name global variable");
4139}
4140
Wesley Peck527da1b2010-11-23 03:31:01 +00004141/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00004142/// expand a bit convert where either the source or destination type is i64 to
4143/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
4144/// operand type is illegal (e.g., v2f32 for a target that doesn't support
4145/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00004146static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00004147 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004148 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004149 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00004150
Bob Wilson59b70ea2010-04-17 05:30:19 +00004151 // This function is only supposed to be called for i64 types, either as the
4152 // source or destination of the bit convert.
4153 EVT SrcVT = Op.getValueType();
4154 EVT DstVT = N->getValueType(0);
4155 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00004156 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00004157
Bob Wilson59b70ea2010-04-17 05:30:19 +00004158 // Turn i64->f64 into VMOVDRR.
4159 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00004160 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
4161 DAG.getConstant(0, MVT::i32));
4162 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
4163 DAG.getConstant(1, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00004164 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00004165 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00004166 }
Bob Wilson7117a912009-03-20 22:42:55 +00004167
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00004168 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00004169 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00004170 SDValue Cvt;
Christian Pirker6692e7c2014-05-14 16:59:44 +00004171 if (TLI.isBigEndian() && SrcVT.isVector() &&
4172 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00004173 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4174 DAG.getVTList(MVT::i32, MVT::i32),
4175 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
4176 else
4177 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
4178 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00004179 // Merge the pieces into a single i64 value.
4180 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4181 }
Bob Wilson7117a912009-03-20 22:42:55 +00004182
Bob Wilson59b70ea2010-04-17 05:30:19 +00004183 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004184}
4185
Bob Wilson2e076c42009-06-22 23:27:02 +00004186/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004187/// Zero vectors are used to represent vector negation and in those cases
4188/// will be implemented with the NEON VNEG instruction. However, VNEG does
4189/// not support i64 elements, so sometimes the zero vectors will need to be
4190/// explicitly constructed. Regardless, use a canonical VMOV to create the
4191/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004192static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004193 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004194 // The canonical modified immediate encoding of a zero vector is....0!
4195 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
4196 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4197 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004198 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004199}
4200
Jim Grosbach624fcb22009-10-31 21:00:56 +00004201/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4202/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004203SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4204 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004205 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4206 EVT VT = Op.getValueType();
4207 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004208 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004209 SDValue ShOpLo = Op.getOperand(0);
4210 SDValue ShOpHi = Op.getOperand(1);
4211 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004212 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004213 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004214
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004215 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4216
Jim Grosbach624fcb22009-10-31 21:00:56 +00004217 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4218 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4219 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4220 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4221 DAG.getConstant(VTBits, MVT::i32));
4222 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4223 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004224 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004225
4226 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4227 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004228 ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004229 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004230 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004231 CCR, Cmp);
4232
4233 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004234 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004235}
4236
Jim Grosbach5d994042009-10-31 19:38:01 +00004237/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4238/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004239SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4240 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004241 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4242 EVT VT = Op.getValueType();
4243 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004244 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004245 SDValue ShOpLo = Op.getOperand(0);
4246 SDValue ShOpHi = Op.getOperand(1);
4247 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004248 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004249
4250 assert(Op.getOpcode() == ISD::SHL_PARTS);
4251 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4252 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4253 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4254 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4255 DAG.getConstant(VTBits, MVT::i32));
4256 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4257 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4258
4259 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4260 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4261 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004262 ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004263 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004264 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004265 CCR, Cmp);
4266
4267 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004268 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004269}
4270
Jim Grosbach535d3b42010-09-08 03:54:02 +00004271SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004272 SelectionDAG &DAG) const {
4273 // The rounding mode is in bits 23:22 of the FPSCR.
4274 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4275 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4276 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004277 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004278 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
4279 DAG.getConstant(Intrinsic::arm_get_fpscr,
4280 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004281 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemanb69b1822010-08-03 21:31:55 +00004282 DAG.getConstant(1U << 22, MVT::i32));
4283 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
4284 DAG.getConstant(22, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004285 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemanb69b1822010-08-03 21:31:55 +00004286 DAG.getConstant(3, MVT::i32));
4287}
4288
Jim Grosbach8546ec92010-01-18 19:58:49 +00004289static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4290 const ARMSubtarget *ST) {
4291 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004292 SDLoc dl(N);
Jim Grosbach8546ec92010-01-18 19:58:49 +00004293
4294 if (!ST->hasV6T2Ops())
4295 return SDValue();
4296
4297 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
4298 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4299}
4300
Evan Chengb4eae132012-12-04 22:41:50 +00004301/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4302/// for each 16-bit element from operand, repeated. The basic idea is to
4303/// leverage vcnt to get the 8-bit counts, gather and add the results.
4304///
4305/// Trace for v4i16:
4306/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4307/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4308/// 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 +00004309/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004310/// [b0 b1 b2 b3 b4 b5 b6 b7]
4311/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4312/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4313/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4314static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4315 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004316 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004317
4318 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4319 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4320 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4321 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4322 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4323 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4324}
4325
4326/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4327/// bit-count for each 16-bit element from the operand. We need slightly
4328/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4329/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004330///
Evan Chengb4eae132012-12-04 22:41:50 +00004331/// Trace for v4i16:
4332/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4333/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4334/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4335/// v4i16:Extracted = [k0 k1 k2 k3 ]
4336static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4337 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004338 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004339
4340 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4341 if (VT.is64BitVector()) {
4342 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4343 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
4344 DAG.getIntPtrConstant(0));
4345 } else {
4346 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
4347 BitCounts, DAG.getIntPtrConstant(0));
4348 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4349 }
4350}
4351
4352/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4353/// bit-count for each 32-bit element from the operand. The idea here is
4354/// to split the vector into 16-bit elements, leverage the 16-bit count
4355/// routine, and then combine the results.
4356///
4357/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4358/// input = [v0 v1 ] (vi: 32-bit elements)
4359/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4360/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004361/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004362/// [k0 k1 k2 k3 ]
4363/// N1 =+[k1 k0 k3 k2 ]
4364/// [k0 k2 k1 k3 ]
4365/// N2 =+[k1 k3 k0 k2 ]
4366/// [k0 k2 k1 k3 ]
4367/// Extended =+[k1 k3 k0 k2 ]
4368/// [k0 k2 ]
4369/// Extracted=+[k1 k3 ]
4370///
4371static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4372 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004373 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004374
4375 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4376
4377 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4378 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4379 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4380 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4381 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4382
4383 if (VT.is64BitVector()) {
4384 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4385 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
4386 DAG.getIntPtrConstant(0));
4387 } else {
4388 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
4389 DAG.getIntPtrConstant(0));
4390 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4391 }
4392}
4393
4394static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4395 const ARMSubtarget *ST) {
4396 EVT VT = N->getValueType(0);
4397
4398 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004399 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4400 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004401 "Unexpected type for custom ctpop lowering");
4402
4403 if (VT.getVectorElementType() == MVT::i32)
4404 return lowerCTPOP32BitElements(N, DAG);
4405 else
4406 return lowerCTPOP16BitElements(N, DAG);
4407}
4408
Bob Wilson2e076c42009-06-22 23:27:02 +00004409static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4410 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004411 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004412 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004413
Bob Wilson7d471332010-11-18 21:16:28 +00004414 if (!VT.isVector())
4415 return SDValue();
4416
Bob Wilson2e076c42009-06-22 23:27:02 +00004417 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004418 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004419
Bob Wilson7d471332010-11-18 21:16:28 +00004420 // Left shifts translate directly to the vshiftu intrinsic.
4421 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004422 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilson7d471332010-11-18 21:16:28 +00004423 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
4424 N->getOperand(0), N->getOperand(1));
4425
4426 assert((N->getOpcode() == ISD::SRA ||
4427 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4428
4429 // NEON uses the same intrinsics for both left and right shifts. For
4430 // right shifts, the shift amounts are negative, so negate the vector of
4431 // shift amounts.
4432 EVT ShiftVT = N->getOperand(1).getValueType();
4433 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4434 getZeroVector(ShiftVT, DAG, dl),
4435 N->getOperand(1));
4436 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4437 Intrinsic::arm_neon_vshifts :
4438 Intrinsic::arm_neon_vshiftu);
4439 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4440 DAG.getConstant(vshiftInt, MVT::i32),
4441 N->getOperand(0), NegatedCount);
4442}
4443
4444static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4445 const ARMSubtarget *ST) {
4446 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004447 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004448
Eli Friedman682d8c12009-08-22 03:13:10 +00004449 // We can get here for a node like i32 = ISD::SHL i32, i64
4450 if (VT != MVT::i64)
4451 return SDValue();
4452
4453 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004454 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004455
Chris Lattnerf81d5882007-11-24 07:07:01 +00004456 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4457 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004458 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004459 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004460
Chris Lattnerf81d5882007-11-24 07:07:01 +00004461 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004462 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004463
Chris Lattnerf81d5882007-11-24 07:07:01 +00004464 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004465 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004466 DAG.getConstant(0, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004467 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004468 DAG.getConstant(1, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004469
Chris Lattnerf81d5882007-11-24 07:07:01 +00004470 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4471 // captures the result into a carry flag.
4472 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004473 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004474
Chris Lattnerf81d5882007-11-24 07:07:01 +00004475 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004476 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004477
Chris Lattnerf81d5882007-11-24 07:07:01 +00004478 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004479 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004480}
4481
Bob Wilson2e076c42009-06-22 23:27:02 +00004482static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4483 SDValue TmpOp0, TmpOp1;
4484 bool Invert = false;
4485 bool Swap = false;
4486 unsigned Opc = 0;
4487
4488 SDValue Op0 = Op.getOperand(0);
4489 SDValue Op1 = Op.getOperand(1);
4490 SDValue CC = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004491 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004492 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004493 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004494
Oliver Stannard51b1d462014-08-21 12:50:31 +00004495 if (Op1.getValueType().isFloatingPoint()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004496 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004497 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004498 case ISD::SETUNE:
4499 case ISD::SETNE: Invert = true; // Fallthrough
4500 case ISD::SETOEQ:
4501 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4502 case ISD::SETOLT:
4503 case ISD::SETLT: Swap = true; // Fallthrough
4504 case ISD::SETOGT:
4505 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4506 case ISD::SETOLE:
4507 case ISD::SETLE: Swap = true; // Fallthrough
4508 case ISD::SETOGE:
4509 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4510 case ISD::SETUGE: Swap = true; // Fallthrough
4511 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4512 case ISD::SETUGT: Swap = true; // Fallthrough
4513 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4514 case ISD::SETUEQ: Invert = true; // Fallthrough
4515 case ISD::SETONE:
4516 // Expand this to (OLT | OGT).
4517 TmpOp0 = Op0;
4518 TmpOp1 = Op1;
4519 Opc = ISD::OR;
4520 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4521 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
4522 break;
4523 case ISD::SETUO: Invert = true; // Fallthrough
4524 case ISD::SETO:
4525 // Expand this to (OLT | OGE).
4526 TmpOp0 = Op0;
4527 TmpOp1 = Op1;
4528 Opc = ISD::OR;
4529 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4530 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
4531 break;
4532 }
4533 } else {
4534 // Integer comparisons.
4535 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004536 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004537 case ISD::SETNE: Invert = true;
4538 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4539 case ISD::SETLT: Swap = true;
4540 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4541 case ISD::SETLE: Swap = true;
4542 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4543 case ISD::SETULT: Swap = true;
4544 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4545 case ISD::SETULE: Swap = true;
4546 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4547 }
4548
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004549 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004550 if (Opc == ARMISD::VCEQ) {
4551
4552 SDValue AndOp;
4553 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4554 AndOp = Op0;
4555 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4556 AndOp = Op1;
4557
4558 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004559 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004560 AndOp = AndOp.getOperand(0);
4561
4562 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4563 Opc = ARMISD::VTST;
Wesley Peck527da1b2010-11-23 03:31:01 +00004564 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
4565 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004566 Invert = !Invert;
4567 }
4568 }
4569 }
4570
4571 if (Swap)
4572 std::swap(Op0, Op1);
4573
Owen Andersonc7baee32010-11-08 23:21:22 +00004574 // If one of the operands is a constant vector zero, attempt to fold the
4575 // comparison to a specialized compare-against-zero form.
4576 SDValue SingleOp;
4577 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4578 SingleOp = Op0;
4579 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4580 if (Opc == ARMISD::VCGE)
4581 Opc = ARMISD::VCLEZ;
4582 else if (Opc == ARMISD::VCGT)
4583 Opc = ARMISD::VCLTZ;
4584 SingleOp = Op1;
4585 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004586
Owen Andersonc7baee32010-11-08 23:21:22 +00004587 SDValue Result;
4588 if (SingleOp.getNode()) {
4589 switch (Opc) {
4590 case ARMISD::VCEQ:
4591 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
4592 case ARMISD::VCGE:
4593 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
4594 case ARMISD::VCLEZ:
4595 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
4596 case ARMISD::VCGT:
4597 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
4598 case ARMISD::VCLTZ:
4599 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
4600 default:
4601 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4602 }
4603 } else {
4604 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4605 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004606
4607 if (Invert)
4608 Result = DAG.getNOT(dl, Result, VT);
4609
4610 return Result;
4611}
4612
Bob Wilson5b2b5042010-06-14 22:19:57 +00004613/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4614/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004615/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004616static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4617 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Andersona4076922010-11-05 21:57:54 +00004618 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004619 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004620
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004621 // SplatBitSize is set to the smallest size that splats the vector, so a
4622 // zero vector will always have SplatBitSize == 8. However, NEON modified
4623 // immediate instructions others than VMOV do not support the 8-bit encoding
4624 // of a zero vector, and the default encoding of zero is supposed to be the
4625 // 32-bit version.
4626 if (SplatBits == 0)
4627 SplatBitSize = 32;
4628
Bob Wilson2e076c42009-06-22 23:27:02 +00004629 switch (SplatBitSize) {
4630 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004631 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004632 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004633 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004634 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004635 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004636 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004637 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004638 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004639
4640 case 16:
4641 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004642 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004643 if ((SplatBits & ~0xff) == 0) {
4644 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004645 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004646 Imm = SplatBits;
4647 break;
4648 }
4649 if ((SplatBits & ~0xff00) == 0) {
4650 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004651 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004652 Imm = SplatBits >> 8;
4653 break;
4654 }
4655 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004656
4657 case 32:
4658 // NEON's 32-bit VMOV supports splat values where:
4659 // * only one byte is nonzero, or
4660 // * the least significant byte is 0xff and the second byte is nonzero, or
4661 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004662 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004663 if ((SplatBits & ~0xff) == 0) {
4664 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004665 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004666 Imm = SplatBits;
4667 break;
4668 }
4669 if ((SplatBits & ~0xff00) == 0) {
4670 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004671 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004672 Imm = SplatBits >> 8;
4673 break;
4674 }
4675 if ((SplatBits & ~0xff0000) == 0) {
4676 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004677 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004678 Imm = SplatBits >> 16;
4679 break;
4680 }
4681 if ((SplatBits & ~0xff000000) == 0) {
4682 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004683 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004684 Imm = SplatBits >> 24;
4685 break;
4686 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004687
Owen Andersona4076922010-11-05 21:57:54 +00004688 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4689 if (type == OtherModImm) return SDValue();
4690
Bob Wilson2e076c42009-06-22 23:27:02 +00004691 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004692 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4693 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004694 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004695 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004696 break;
4697 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004698
4699 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004700 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4701 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004702 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004703 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004704 break;
4705 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004706
4707 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4708 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4709 // VMOV.I32. A (very) minor optimization would be to replicate the value
4710 // and fall through here to test for a valid 64-bit splat. But, then the
4711 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004712 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004713
4714 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004715 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004716 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004717 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004718 uint64_t BitMask = 0xff;
4719 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004720 unsigned ImmMask = 1;
4721 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004722 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004723 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004724 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004725 Imm |= ImmMask;
4726 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004727 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004728 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004729 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004730 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004731 }
Christian Pirker6f81e752014-06-23 18:05:53 +00004732
4733 if (DAG.getTargetLoweringInfo().isBigEndian())
4734 // swap higher and lower 32 bit word
4735 Imm = ((Imm & 0xf) << 4) | ((Imm & 0xf0) >> 4);
4736
Bob Wilson6eae5202010-06-11 21:34:50 +00004737 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004738 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004739 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004740 break;
4741 }
4742
Bob Wilson6eae5202010-06-11 21:34:50 +00004743 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004744 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004745 }
4746
Bob Wilsona3f19012010-07-13 21:16:48 +00004747 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4748 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004749}
4750
Lang Hames591cdaf2012-03-29 21:56:11 +00004751SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4752 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004753 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004754 return SDValue();
4755
Tim Northoverf79c3a52013-08-20 08:57:11 +00004756 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004757 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004758
Oliver Stannard51b1d462014-08-21 12:50:31 +00004759 // Use the default (constant pool) lowering for double constants when we have
4760 // an SP-only FPU
4761 if (IsDouble && Subtarget->isFPOnlySP())
4762 return SDValue();
4763
Lang Hames591cdaf2012-03-29 21:56:11 +00004764 // Try splatting with a VMOV.f32...
4765 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004766 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4767
Lang Hames591cdaf2012-03-29 21:56:11 +00004768 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004769 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4770 // We have code in place to select a valid ConstantFP already, no need to
4771 // do any mangling.
4772 return Op;
4773 }
4774
4775 // It's a float and we are trying to use NEON operations where
4776 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004777 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004778 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4779 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4780 NewVal);
4781 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4782 DAG.getConstant(0, MVT::i32));
4783 }
4784
Tim Northoverf79c3a52013-08-20 08:57:11 +00004785 // The rest of our options are NEON only, make sure that's allowed before
4786 // proceeding..
4787 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4788 return SDValue();
4789
Lang Hames591cdaf2012-03-29 21:56:11 +00004790 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004791 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4792
4793 // It wouldn't really be worth bothering for doubles except for one very
4794 // important value, which does happen to match: 0.0. So make sure we don't do
4795 // anything stupid.
4796 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4797 return SDValue();
4798
4799 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
4800 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4801 false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004802 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004803 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004804 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4805 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004806 if (IsDouble)
4807 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4808
4809 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004810 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4811 VecConstant);
4812 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4813 DAG.getConstant(0, MVT::i32));
4814 }
4815
4816 // Finally, try a VMVN.i32
Tim Northoverf79c3a52013-08-20 08:57:11 +00004817 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4818 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004819 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004820 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004821 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004822
4823 if (IsDouble)
4824 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4825
4826 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004827 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4828 VecConstant);
4829 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4830 DAG.getConstant(0, MVT::i32));
4831 }
4832
4833 return SDValue();
4834}
4835
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004836// check if an VEXT instruction can handle the shuffle mask when the
4837// vector sources of the shuffle are the same.
4838static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4839 unsigned NumElts = VT.getVectorNumElements();
4840
4841 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4842 if (M[0] < 0)
4843 return false;
4844
4845 Imm = M[0];
4846
4847 // If this is a VEXT shuffle, the immediate value is the index of the first
4848 // element. The other shuffle indices must be the successive elements after
4849 // the first one.
4850 unsigned ExpectedElt = Imm;
4851 for (unsigned i = 1; i < NumElts; ++i) {
4852 // Increment the expected index. If it wraps around, just follow it
4853 // back to index zero and keep going.
4854 ++ExpectedElt;
4855 if (ExpectedElt == NumElts)
4856 ExpectedElt = 0;
4857
4858 if (M[i] < 0) continue; // ignore UNDEF indices
4859 if (ExpectedElt != static_cast<unsigned>(M[i]))
4860 return false;
4861 }
4862
4863 return true;
4864}
4865
Lang Hames591cdaf2012-03-29 21:56:11 +00004866
Benjamin Kramer339ced42012-01-15 13:16:05 +00004867static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004868 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004869 unsigned NumElts = VT.getVectorNumElements();
4870 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004871
4872 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4873 if (M[0] < 0)
4874 return false;
4875
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004876 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004877
4878 // If this is a VEXT shuffle, the immediate value is the index of the first
4879 // element. The other shuffle indices must be the successive elements after
4880 // the first one.
4881 unsigned ExpectedElt = Imm;
4882 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004883 // Increment the expected index. If it wraps around, it may still be
4884 // a VEXT but the source vectors must be swapped.
4885 ExpectedElt += 1;
4886 if (ExpectedElt == NumElts * 2) {
4887 ExpectedElt = 0;
4888 ReverseVEXT = true;
4889 }
4890
Bob Wilson411dfad2010-08-17 05:54:34 +00004891 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004892 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004893 return false;
4894 }
4895
4896 // Adjust the index value if the source operands will be swapped.
4897 if (ReverseVEXT)
4898 Imm -= NumElts;
4899
Bob Wilson32cd8552009-08-19 17:03:43 +00004900 return true;
4901}
4902
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004903/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4904/// instruction with the specified blocksize. (The order of the elements
4905/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00004906static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004907 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4908 "Only possible block sizes for VREV are: 16, 32, 64");
4909
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004910 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00004911 if (EltSz == 64)
4912 return false;
4913
4914 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004915 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00004916 // If the first shuffle index is UNDEF, be optimistic.
4917 if (M[0] < 0)
4918 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004919
4920 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4921 return false;
4922
4923 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004924 if (M[i] < 0) continue; // ignore UNDEF indices
4925 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004926 return false;
4927 }
4928
4929 return true;
4930}
4931
Benjamin Kramer339ced42012-01-15 13:16:05 +00004932static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00004933 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4934 // range, then 0 is placed into the resulting vector. So pretty much any mask
4935 // of 8 elements can work here.
4936 return VT == MVT::v8i8 && M.size() == 8;
4937}
4938
Benjamin Kramer339ced42012-01-15 13:16:05 +00004939static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004940 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4941 if (EltSz == 64)
4942 return false;
4943
Bob Wilsona7062312009-08-21 20:54:19 +00004944 unsigned NumElts = VT.getVectorNumElements();
4945 WhichResult = (M[0] == 0 ? 0 : 1);
4946 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004947 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4948 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsona7062312009-08-21 20:54:19 +00004949 return false;
4950 }
4951 return true;
4952}
4953
Bob Wilson0bbd3072009-12-03 06:40:55 +00004954/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4955/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4956/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004957static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004958 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4959 if (EltSz == 64)
4960 return false;
4961
4962 unsigned NumElts = VT.getVectorNumElements();
4963 WhichResult = (M[0] == 0 ? 0 : 1);
4964 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004965 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4966 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004967 return false;
4968 }
4969 return true;
4970}
4971
Benjamin Kramer339ced42012-01-15 13:16:05 +00004972static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004973 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4974 if (EltSz == 64)
4975 return false;
4976
Bob Wilsona7062312009-08-21 20:54:19 +00004977 unsigned NumElts = VT.getVectorNumElements();
4978 WhichResult = (M[0] == 0 ? 0 : 1);
4979 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004980 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsona7062312009-08-21 20:54:19 +00004981 if ((unsigned) M[i] != 2 * i + WhichResult)
4982 return false;
4983 }
4984
4985 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004986 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004987 return false;
4988
4989 return true;
4990}
4991
Bob Wilson0bbd3072009-12-03 06:40:55 +00004992/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4993/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4994/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00004995static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004996 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4997 if (EltSz == 64)
4998 return false;
4999
5000 unsigned Half = VT.getVectorNumElements() / 2;
5001 WhichResult = (M[0] == 0 ? 0 : 1);
5002 for (unsigned j = 0; j != 2; ++j) {
5003 unsigned Idx = WhichResult;
5004 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005005 int MIdx = M[i + j * Half];
5006 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson0bbd3072009-12-03 06:40:55 +00005007 return false;
5008 Idx += 2;
5009 }
5010 }
5011
5012 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5013 if (VT.is64BitVector() && EltSz == 32)
5014 return false;
5015
5016 return true;
5017}
5018
Benjamin Kramer339ced42012-01-15 13:16:05 +00005019static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00005020 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5021 if (EltSz == 64)
5022 return false;
5023
Bob Wilsona7062312009-08-21 20:54:19 +00005024 unsigned NumElts = VT.getVectorNumElements();
5025 WhichResult = (M[0] == 0 ? 0 : 1);
5026 unsigned Idx = WhichResult * NumElts / 2;
5027 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005028 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
5029 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsona7062312009-08-21 20:54:19 +00005030 return false;
5031 Idx += 1;
5032 }
5033
5034 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00005035 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00005036 return false;
5037
5038 return true;
5039}
5040
Bob Wilson0bbd3072009-12-03 06:40:55 +00005041/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
5042/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
5043/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005044static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00005045 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
5046 if (EltSz == 64)
5047 return false;
5048
5049 unsigned NumElts = VT.getVectorNumElements();
5050 WhichResult = (M[0] == 0 ? 0 : 1);
5051 unsigned Idx = WhichResult * NumElts / 2;
5052 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00005053 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
5054 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson0bbd3072009-12-03 06:40:55 +00005055 return false;
5056 Idx += 1;
5057 }
5058
5059 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
5060 if (VT.is64BitVector() && EltSz == 32)
5061 return false;
5062
5063 return true;
5064}
5065
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005066/// \return true if this is a reverse operation on an vector.
5067static bool isReverseMask(ArrayRef<int> M, EVT VT) {
5068 unsigned NumElts = VT.getVectorNumElements();
5069 // Make sure the mask has the right size.
5070 if (NumElts != M.size())
5071 return false;
5072
5073 // Look for <15, ..., 3, -1, 1, 0>.
5074 for (unsigned i = 0; i != NumElts; ++i)
5075 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
5076 return false;
5077
5078 return true;
5079}
5080
Dale Johannesen2bff5052010-07-29 20:10:08 +00005081// If N is an integer constant that can be moved into a register in one
5082// instruction, return an SDValue of such a constant (will become a MOV
5083// instruction). Otherwise return null.
5084static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005085 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00005086 uint64_t Val;
5087 if (!isa<ConstantSDNode>(N))
5088 return SDValue();
5089 Val = cast<ConstantSDNode>(N)->getZExtValue();
5090
5091 if (ST->isThumb1Only()) {
5092 if (Val <= 255 || ~Val <= 255)
5093 return DAG.getConstant(Val, MVT::i32);
5094 } else {
5095 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
5096 return DAG.getConstant(Val, MVT::i32);
5097 }
5098 return SDValue();
5099}
5100
Bob Wilson2e076c42009-06-22 23:27:02 +00005101// If this is a case we can't handle, return null and let the default
5102// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00005103SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
5104 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00005105 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005106 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005107 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00005108
5109 APInt SplatBits, SplatUndef;
5110 unsigned SplatBitSize;
5111 bool HasAnyUndefs;
5112 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005113 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00005114 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00005115 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00005116 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00005117 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00005118 DAG, VmovVT, VT.is128BitVector(),
5119 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00005120 if (Val.getNode()) {
5121 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005122 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00005123 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00005124
5125 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00005126 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00005127 Val = isNEONModifiedImm(NegatedImm,
5128 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00005129 DAG, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00005130 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005131 if (Val.getNode()) {
5132 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00005133 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00005134 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005135
5136 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00005137 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00005138 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00005139 if (ImmVal != -1) {
5140 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
5141 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
5142 }
5143 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00005144 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00005145 }
5146
Bob Wilson91fdf682010-05-22 00:23:12 +00005147 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00005148 //
5149 // As an optimisation, even if more than one value is used it may be more
5150 // profitable to splat with one value then change some lanes.
5151 //
5152 // Heuristically we decide to do this if the vector has a "dominant" value,
5153 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00005154 unsigned NumElts = VT.getVectorNumElements();
5155 bool isOnlyLowElement = true;
5156 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005157 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00005158 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00005159
5160 // Map of the number of times a particular SDValue appears in the
5161 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00005162 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00005163 SDValue Value;
5164 for (unsigned i = 0; i < NumElts; ++i) {
5165 SDValue V = Op.getOperand(i);
5166 if (V.getOpcode() == ISD::UNDEF)
5167 continue;
5168 if (i > 0)
5169 isOnlyLowElement = false;
5170 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
5171 isConstant = false;
5172
James Molloy49bdbce2012-09-06 09:55:02 +00005173 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00005174 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00005175
James Molloy49bdbce2012-09-06 09:55:02 +00005176 // Is this value dominant? (takes up more than half of the lanes)
5177 if (++Count > (NumElts / 2)) {
5178 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00005179 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00005180 }
Bob Wilson91fdf682010-05-22 00:23:12 +00005181 }
James Molloy49bdbce2012-09-06 09:55:02 +00005182 if (ValueCounts.size() != 1)
5183 usesOnlyOneValue = false;
5184 if (!Value.getNode() && ValueCounts.size() > 0)
5185 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00005186
James Molloy49bdbce2012-09-06 09:55:02 +00005187 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00005188 return DAG.getUNDEF(VT);
5189
Quentin Colombet0f2fe742013-07-23 22:34:47 +00005190 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5191 // Keep going if we are hitting this case.
5192 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005193 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5194
Dale Johannesen2bff5052010-07-29 20:10:08 +00005195 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5196
Dale Johannesen710a2d92010-10-19 20:00:17 +00005197 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5198 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005199 if (hasDominantValue && EltSize <= 32) {
5200 if (!isConstant) {
5201 SDValue N;
5202
5203 // If we are VDUPing a value that comes directly from a vector, that will
5204 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005205 // just use VDUPLANE. We can only do this if the lane being extracted
5206 // is at a constant index, as the VDUP from lane instructions only have
5207 // constant-index forms.
5208 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5209 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005210 // We need to create a new undef vector to use for the VDUPLANE if the
5211 // size of the vector from which we get the value is different than the
5212 // size of the vector that we need to create. We will insert the element
5213 // such that the register coalescer will remove unnecessary copies.
5214 if (VT != Value->getOperand(0).getValueType()) {
5215 ConstantSDNode *constIndex;
5216 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
5217 assert(constIndex && "The index is not a constant!");
5218 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5219 VT.getVectorNumElements();
5220 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5221 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
5222 Value, DAG.getConstant(index, MVT::i32)),
5223 DAG.getConstant(index, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005224 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005225 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005226 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005227 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005228 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5229
5230 if (!usesOnlyOneValue) {
5231 // The dominant value was splatted as 'N', but we now have to insert
5232 // all differing elements.
5233 for (unsigned I = 0; I < NumElts; ++I) {
5234 if (Op.getOperand(I) == Value)
5235 continue;
5236 SmallVector<SDValue, 3> Ops;
5237 Ops.push_back(N);
5238 Ops.push_back(Op.getOperand(I));
5239 Ops.push_back(DAG.getConstant(I, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005240 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005241 }
5242 }
5243 return N;
5244 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005245 if (VT.getVectorElementType().isFloatingPoint()) {
5246 SmallVector<SDValue, 8> Ops;
5247 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005248 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005249 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005250 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005251 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005252 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5253 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005254 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005255 }
James Molloy49bdbce2012-09-06 09:55:02 +00005256 if (usesOnlyOneValue) {
5257 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5258 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005259 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005260 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005261 }
5262
5263 // If all elements are constants and the case above didn't get hit, fall back
5264 // to the default expansion, which will generate a load from the constant
5265 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005266 if (isConstant)
5267 return SDValue();
5268
Bob Wilson6f2b8962011-01-07 21:37:30 +00005269 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5270 if (NumElts >= 4) {
5271 SDValue shuffle = ReconstructShuffle(Op, DAG);
5272 if (shuffle != SDValue())
5273 return shuffle;
5274 }
5275
Bob Wilson91fdf682010-05-22 00:23:12 +00005276 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005277 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5278 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005279 if (EltSize >= 32) {
5280 // Do the expansion with floating-point types, since that is what the VFP
5281 // registers are defined to use, and since i64 is not legal.
5282 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5283 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005284 SmallVector<SDValue, 8> Ops;
5285 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005286 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005287 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005288 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005289 }
5290
Jim Grosbach24e102a2013-07-08 18:18:52 +00005291 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5292 // know the default expansion would otherwise fall back on something even
5293 // worse. For a vector with one or two non-undef values, that's
5294 // scalar_to_vector for the elements followed by a shuffle (provided the
5295 // shuffle is valid for the target) and materialization element by element
5296 // on the stack followed by a load for everything else.
5297 if (!isConstant && !usesOnlyOneValue) {
5298 SDValue Vec = DAG.getUNDEF(VT);
5299 for (unsigned i = 0 ; i < NumElts; ++i) {
5300 SDValue V = Op.getOperand(i);
5301 if (V.getOpcode() == ISD::UNDEF)
5302 continue;
5303 SDValue LaneIdx = DAG.getConstant(i, MVT::i32);
5304 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5305 }
5306 return Vec;
5307 }
5308
Bob Wilson2e076c42009-06-22 23:27:02 +00005309 return SDValue();
5310}
5311
Bob Wilson6f2b8962011-01-07 21:37:30 +00005312// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005313// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005314SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5315 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005316 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005317 EVT VT = Op.getValueType();
5318 unsigned NumElts = VT.getVectorNumElements();
5319
5320 SmallVector<SDValue, 2> SourceVecs;
5321 SmallVector<unsigned, 2> MinElts;
5322 SmallVector<unsigned, 2> MaxElts;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005323
Bob Wilson6f2b8962011-01-07 21:37:30 +00005324 for (unsigned i = 0; i < NumElts; ++i) {
5325 SDValue V = Op.getOperand(i);
5326 if (V.getOpcode() == ISD::UNDEF)
5327 continue;
5328 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5329 // A shuffle can only come from building a vector from various
5330 // elements of other vectors.
5331 return SDValue();
Eli Friedman74d1da52011-10-14 23:58:49 +00005332 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
5333 VT.getVectorElementType()) {
5334 // This code doesn't know how to handle shuffles where the vector
5335 // element types do not match (this happens because type legalization
5336 // promotes the return type of EXTRACT_VECTOR_ELT).
5337 // FIXME: It might be appropriate to extend this code to handle
5338 // mismatched types.
5339 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005340 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005341
Bob Wilson6f2b8962011-01-07 21:37:30 +00005342 // Record this extraction against the appropriate vector if possible...
5343 SDValue SourceVec = V.getOperand(0);
Jim Grosbach6df755c2012-07-25 17:02:47 +00005344 // If the element number isn't a constant, we can't effectively
5345 // analyze what's going on.
5346 if (!isa<ConstantSDNode>(V.getOperand(1)))
5347 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005348 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5349 bool FoundSource = false;
5350 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
5351 if (SourceVecs[j] == SourceVec) {
5352 if (MinElts[j] > EltNo)
5353 MinElts[j] = EltNo;
5354 if (MaxElts[j] < EltNo)
5355 MaxElts[j] = EltNo;
5356 FoundSource = true;
5357 break;
5358 }
5359 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005360
Bob Wilson6f2b8962011-01-07 21:37:30 +00005361 // Or record a new source if not...
5362 if (!FoundSource) {
5363 SourceVecs.push_back(SourceVec);
5364 MinElts.push_back(EltNo);
5365 MaxElts.push_back(EltNo);
5366 }
5367 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005368
Bob Wilson6f2b8962011-01-07 21:37:30 +00005369 // Currently only do something sane when at most two source vectors
5370 // involved.
5371 if (SourceVecs.size() > 2)
5372 return SDValue();
5373
5374 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
5375 int VEXTOffsets[2] = {0, 0};
Andrew Trick5eb0a302011-01-19 02:26:13 +00005376
Bob Wilson6f2b8962011-01-07 21:37:30 +00005377 // This loop extracts the usage patterns of the source vectors
5378 // and prepares appropriate SDValues for a shuffle if possible.
5379 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
5380 if (SourceVecs[i].getValueType() == VT) {
5381 // No VEXT necessary
5382 ShuffleSrcs[i] = SourceVecs[i];
5383 VEXTOffsets[i] = 0;
5384 continue;
5385 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
5386 // It probably isn't worth padding out a smaller vector just to
5387 // break it down again in a shuffle.
5388 return SDValue();
5389 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005390
Bob Wilson6f2b8962011-01-07 21:37:30 +00005391 // Since only 64-bit and 128-bit vectors are legal on ARM and
5392 // we've eliminated the other cases...
Bob Wilson3fa9c062011-01-07 23:40:46 +00005393 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
5394 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick5eb0a302011-01-19 02:26:13 +00005395
Bob Wilson6f2b8962011-01-07 21:37:30 +00005396 if (MaxElts[i] - MinElts[i] >= NumElts) {
5397 // Span too large for a VEXT to cope
5398 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005399 }
5400
Bob Wilson6f2b8962011-01-07 21:37:30 +00005401 if (MinElts[i] >= NumElts) {
5402 // The extraction can just take the second half
5403 VEXTOffsets[i] = NumElts;
Eric Christopher2af95512011-01-14 23:50:53 +00005404 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5405 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005406 DAG.getIntPtrConstant(NumElts));
5407 } else if (MaxElts[i] < NumElts) {
5408 // The extraction can just take the first half
5409 VEXTOffsets[i] = 0;
Eric Christopher2af95512011-01-14 23:50:53 +00005410 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5411 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005412 DAG.getIntPtrConstant(0));
5413 } else {
5414 // An actual VEXT is needed
5415 VEXTOffsets[i] = MinElts[i];
Eric Christopher2af95512011-01-14 23:50:53 +00005416 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5417 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005418 DAG.getIntPtrConstant(0));
Eric Christopher2af95512011-01-14 23:50:53 +00005419 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5420 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005421 DAG.getIntPtrConstant(NumElts));
5422 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
5423 DAG.getConstant(VEXTOffsets[i], MVT::i32));
5424 }
5425 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005426
Bob Wilson6f2b8962011-01-07 21:37:30 +00005427 SmallVector<int, 8> Mask;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005428
Bob Wilson6f2b8962011-01-07 21:37:30 +00005429 for (unsigned i = 0; i < NumElts; ++i) {
5430 SDValue Entry = Op.getOperand(i);
5431 if (Entry.getOpcode() == ISD::UNDEF) {
5432 Mask.push_back(-1);
5433 continue;
5434 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005435
Bob Wilson6f2b8962011-01-07 21:37:30 +00005436 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher2af95512011-01-14 23:50:53 +00005437 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
5438 .getOperand(1))->getSExtValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005439 if (ExtractVec == SourceVecs[0]) {
5440 Mask.push_back(ExtractElt - VEXTOffsets[0]);
5441 } else {
5442 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
5443 }
5444 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005445
Bob Wilson6f2b8962011-01-07 21:37:30 +00005446 // Final check before we try to produce nonsense...
5447 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher2af95512011-01-14 23:50:53 +00005448 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
5449 &Mask[0]);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005450
Bob Wilson6f2b8962011-01-07 21:37:30 +00005451 return SDValue();
5452}
5453
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005454/// isShuffleMaskLegal - Targets can use this to indicate that they only
5455/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5456/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5457/// are assumed to be legal.
5458bool
5459ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5460 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005461 if (VT.getVectorNumElements() == 4 &&
5462 (VT.is128BitVector() || VT.is64BitVector())) {
5463 unsigned PFIndexes[4];
5464 for (unsigned i = 0; i != 4; ++i) {
5465 if (M[i] < 0)
5466 PFIndexes[i] = 8;
5467 else
5468 PFIndexes[i] = M[i];
5469 }
5470
5471 // Compute the index in the perfect shuffle table.
5472 unsigned PFTableIndex =
5473 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5474 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5475 unsigned Cost = (PFEntry >> 30);
5476
5477 if (Cost <= 4)
5478 return true;
5479 }
5480
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005481 bool ReverseVEXT;
Bob Wilsona7062312009-08-21 20:54:19 +00005482 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005483
Bob Wilson846bd792010-06-07 23:53:38 +00005484 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5485 return (EltSize >= 32 ||
5486 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005487 isVREVMask(M, VT, 64) ||
5488 isVREVMask(M, VT, 32) ||
5489 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005490 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005491 isVTBLMask(M, VT) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005492 isVTRNMask(M, VT, WhichResult) ||
5493 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson0bbd3072009-12-03 06:40:55 +00005494 isVZIPMask(M, VT, WhichResult) ||
5495 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
5496 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005497 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
5498 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005499}
5500
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005501/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5502/// the specified operations to build the shuffle.
5503static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5504 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005505 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005506 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5507 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5508 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5509
5510 enum {
5511 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5512 OP_VREV,
5513 OP_VDUP0,
5514 OP_VDUP1,
5515 OP_VDUP2,
5516 OP_VDUP3,
5517 OP_VEXT1,
5518 OP_VEXT2,
5519 OP_VEXT3,
5520 OP_VUZPL, // VUZP, left result
5521 OP_VUZPR, // VUZP, right result
5522 OP_VZIPL, // VZIP, left result
5523 OP_VZIPR, // VZIP, right result
5524 OP_VTRNL, // VTRN, left result
5525 OP_VTRNR // VTRN, right result
5526 };
5527
5528 if (OpNum == OP_COPY) {
5529 if (LHSID == (1*9+2)*9+3) return LHS;
5530 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5531 return RHS;
5532 }
5533
5534 SDValue OpLHS, OpRHS;
5535 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5536 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5537 EVT VT = OpLHS.getValueType();
5538
5539 switch (OpNum) {
5540 default: llvm_unreachable("Unknown shuffle opcode!");
5541 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005542 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005543 if (VT.getVectorElementType() == MVT::i32 ||
5544 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005545 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5546 // vrev <4 x i16> -> VREV32
5547 if (VT.getVectorElementType() == MVT::i16)
5548 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5549 // vrev <4 x i8> -> VREV16
5550 assert(VT.getVectorElementType() == MVT::i8);
5551 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005552 case OP_VDUP0:
5553 case OP_VDUP1:
5554 case OP_VDUP2:
5555 case OP_VDUP3:
5556 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005557 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005558 case OP_VEXT1:
5559 case OP_VEXT2:
5560 case OP_VEXT3:
5561 return DAG.getNode(ARMISD::VEXT, dl, VT,
5562 OpLHS, OpRHS,
5563 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
5564 case OP_VUZPL:
5565 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005566 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005567 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5568 case OP_VZIPL:
5569 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005570 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005571 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5572 case OP_VTRNL:
5573 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005574 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5575 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005576 }
5577}
5578
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005579static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005580 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005581 SelectionDAG &DAG) {
5582 // Check to see if we can use the VTBL instruction.
5583 SDValue V1 = Op.getOperand(0);
5584 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005585 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005586
5587 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005588 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005589 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
5590 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
5591
5592 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5593 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005594 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005595
Owen Anderson77aa2662011-04-05 21:48:57 +00005596 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005597 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005598}
5599
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005600static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5601 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005602 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005603 SDValue OpLHS = Op.getOperand(0);
5604 EVT VT = OpLHS.getValueType();
5605
5606 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5607 "Expect an v8i16/v16i8 type");
5608 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5609 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5610 // extract the first 8 bytes into the top double word and the last 8 bytes
5611 // into the bottom double word. The v8i16 case is similar.
5612 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5613 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
5614 DAG.getConstant(ExtractNum, MVT::i32));
5615}
5616
Bob Wilson2e076c42009-06-22 23:27:02 +00005617static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005618 SDValue V1 = Op.getOperand(0);
5619 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005620 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005621 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005622 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005623
Bob Wilsonc6800b52009-08-13 02:13:04 +00005624 // Convert shuffles that are directly supported on NEON to target-specific
5625 // DAG nodes, instead of keeping them as shuffles and matching them again
5626 // during code selection. This is more efficient and avoids the possibility
5627 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005628 // FIXME: floating-point vectors should be canonicalized to integer vectors
5629 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005630 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005631
Bob Wilson846bd792010-06-07 23:53:38 +00005632 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5633 if (EltSize <= 32) {
5634 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5635 int Lane = SVN->getSplatIndex();
5636 // If this is undef splat, generate it via "just" vdup, if possible.
5637 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005638
Dan Gohman198b7ff2011-11-03 21:49:52 +00005639 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005640 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5641 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5642 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005643 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5644 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5645 // reaches it).
5646 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5647 !isa<ConstantSDNode>(V1.getOperand(0))) {
5648 bool IsScalarToVector = true;
5649 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5650 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5651 IsScalarToVector = false;
5652 break;
5653 }
5654 if (IsScalarToVector)
5655 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5656 }
Bob Wilson846bd792010-06-07 23:53:38 +00005657 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
5658 DAG.getConstant(Lane, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005659 }
Bob Wilson846bd792010-06-07 23:53:38 +00005660
5661 bool ReverseVEXT;
5662 unsigned Imm;
5663 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5664 if (ReverseVEXT)
5665 std::swap(V1, V2);
5666 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
5667 DAG.getConstant(Imm, MVT::i32));
5668 }
5669
5670 if (isVREVMask(ShuffleMask, VT, 64))
5671 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5672 if (isVREVMask(ShuffleMask, VT, 32))
5673 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5674 if (isVREVMask(ShuffleMask, VT, 16))
5675 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5676
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005677 if (V2->getOpcode() == ISD::UNDEF &&
5678 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5679 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
5680 DAG.getConstant(Imm, MVT::i32));
5681 }
5682
Bob Wilson846bd792010-06-07 23:53:38 +00005683 // Check for Neon shuffles that modify both input vectors in place.
5684 // If both results are used, i.e., if there are two shuffles with the same
5685 // source operands and with masks corresponding to both results of one of
5686 // these operations, DAG memoization will ensure that a single node is
5687 // used for both shuffles.
5688 unsigned WhichResult;
5689 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5690 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5691 V1, V2).getValue(WhichResult);
5692 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5693 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5694 V1, V2).getValue(WhichResult);
5695 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5696 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5697 V1, V2).getValue(WhichResult);
5698
5699 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5700 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5701 V1, V1).getValue(WhichResult);
5702 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5703 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5704 V1, V1).getValue(WhichResult);
5705 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5706 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5707 V1, V1).getValue(WhichResult);
Bob Wilsoncce31f62009-08-14 05:08:32 +00005708 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005709
Bob Wilsona7062312009-08-21 20:54:19 +00005710 // If the shuffle is not directly supported and it has 4 elements, use
5711 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005712 unsigned NumElts = VT.getVectorNumElements();
5713 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005714 unsigned PFIndexes[4];
5715 for (unsigned i = 0; i != 4; ++i) {
5716 if (ShuffleMask[i] < 0)
5717 PFIndexes[i] = 8;
5718 else
5719 PFIndexes[i] = ShuffleMask[i];
5720 }
5721
5722 // Compute the index in the perfect shuffle table.
5723 unsigned PFTableIndex =
5724 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005725 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5726 unsigned Cost = (PFEntry >> 30);
5727
5728 if (Cost <= 4)
5729 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5730 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00005731
Bob Wilsond8a9a042010-06-04 00:04:02 +00005732 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00005733 if (EltSize >= 32) {
5734 // Do the expansion with floating-point types, since that is what the VFP
5735 // registers are defined to use, and since i64 is not legal.
5736 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5737 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005738 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5739 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005740 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00005741 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00005742 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00005743 Ops.push_back(DAG.getUNDEF(EltVT));
5744 else
5745 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5746 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5747 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
5748 MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00005749 }
Craig Topper48d114b2014-04-26 18:35:24 +00005750 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005751 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00005752 }
5753
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005754 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5755 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
5756
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005757 if (VT == MVT::v8i8) {
5758 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
5759 if (NewOp.getNode())
5760 return NewOp;
5761 }
5762
Bob Wilson6f34e272009-08-14 05:16:33 +00005763 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005764}
5765
Eli Friedmana5e244c2011-10-24 23:08:52 +00005766static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5767 // INSERT_VECTOR_ELT is legal only for immediate indexes.
5768 SDValue Lane = Op.getOperand(2);
5769 if (!isa<ConstantSDNode>(Lane))
5770 return SDValue();
5771
5772 return Op;
5773}
5774
Bob Wilson2e076c42009-06-22 23:27:02 +00005775static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00005776 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00005777 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00005778 if (!isa<ConstantSDNode>(Lane))
5779 return SDValue();
5780
5781 SDValue Vec = Op.getOperand(0);
5782 if (Op.getValueType() == MVT::i32 &&
5783 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005784 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00005785 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5786 }
5787
5788 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00005789}
5790
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005791static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5792 // The only time a CONCAT_VECTORS operation can have legal types is when
5793 // two 64-bit vectors are concatenated to a 128-bit vector.
5794 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5795 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005796 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005797 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005798 SDValue Op0 = Op.getOperand(0);
5799 SDValue Op1 = Op.getOperand(1);
5800 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005801 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005802 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005803 DAG.getIntPtrConstant(0));
5804 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005805 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005806 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005807 DAG.getIntPtrConstant(1));
Wesley Peck527da1b2010-11-23 03:31:01 +00005808 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005809}
5810
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005811/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5812/// element has been zero/sign-extended, depending on the isSigned parameter,
5813/// from an integer type half its size.
5814static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5815 bool isSigned) {
5816 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5817 EVT VT = N->getValueType(0);
5818 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5819 SDNode *BVN = N->getOperand(0).getNode();
5820 if (BVN->getValueType(0) != MVT::v4i32 ||
5821 BVN->getOpcode() != ISD::BUILD_VECTOR)
5822 return false;
5823 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5824 unsigned HiElt = 1 - LoElt;
5825 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5826 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5827 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5828 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5829 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5830 return false;
5831 if (isSigned) {
5832 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5833 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5834 return true;
5835 } else {
5836 if (Hi0->isNullValue() && Hi1->isNullValue())
5837 return true;
5838 }
5839 return false;
5840 }
5841
5842 if (N->getOpcode() != ISD::BUILD_VECTOR)
5843 return false;
5844
5845 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5846 SDNode *Elt = N->getOperand(i).getNode();
5847 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5848 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5849 unsigned HalfSize = EltSize / 2;
5850 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005851 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005852 return false;
5853 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005854 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005855 return false;
5856 }
5857 continue;
5858 }
5859 return false;
5860 }
5861
5862 return true;
5863}
5864
5865/// isSignExtended - Check if a node is a vector value that is sign-extended
5866/// or a constant BUILD_VECTOR with sign-extended elements.
5867static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5868 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5869 return true;
5870 if (isExtendedBUILD_VECTOR(N, DAG, true))
5871 return true;
5872 return false;
5873}
5874
5875/// isZeroExtended - Check if a node is a vector value that is zero-extended
5876/// or a constant BUILD_VECTOR with zero-extended elements.
5877static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5878 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5879 return true;
5880 if (isExtendedBUILD_VECTOR(N, DAG, false))
5881 return true;
5882 return false;
5883}
5884
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005885static EVT getExtensionTo64Bits(const EVT &OrigVT) {
5886 if (OrigVT.getSizeInBits() >= 64)
5887 return OrigVT;
5888
5889 assert(OrigVT.isSimple() && "Expecting a simple value type");
5890
5891 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
5892 switch (OrigSimpleTy) {
5893 default: llvm_unreachable("Unexpected Vector Type");
5894 case MVT::v2i8:
5895 case MVT::v2i16:
5896 return MVT::v2i32;
5897 case MVT::v4i8:
5898 return MVT::v4i16;
5899 }
5900}
5901
Sebastian Popa204f722012-11-30 19:08:04 +00005902/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5903/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5904/// We insert the required extension here to get the vector to fill a D register.
5905static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5906 const EVT &OrigTy,
5907 const EVT &ExtTy,
5908 unsigned ExtOpcode) {
5909 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5910 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5911 // 64-bits we need to insert a new extension so that it will be 64-bits.
5912 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5913 if (OrigTy.getSizeInBits() >= 64)
5914 return N;
5915
5916 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005917 EVT NewVT = getExtensionTo64Bits(OrigTy);
5918
Andrew Trickef9de2a2013-05-25 02:42:55 +00005919 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00005920}
5921
5922/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5923/// does not do any sign/zero extension. If the original vector is less
5924/// than 64 bits, an appropriate extension will be added after the load to
5925/// reach a total size of 64 bits. We have to add the extension separately
5926/// because ARM does not have a sign/zero extending load for vectors.
5927static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005928 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
5929
5930 // The load already has the right type.
5931 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005932 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00005933 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5934 LD->isNonTemporal(), LD->isInvariant(),
5935 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005936
5937 // We need to create a zextload/sextload. We cannot just create a load
5938 // followed by a zext/zext node because LowerMUL is also run during normal
5939 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005940 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005941 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00005942 LD->getMemoryVT(), LD->isVolatile(), LD->isInvariant(),
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005943 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00005944}
5945
5946/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5947/// extending load, or BUILD_VECTOR with extended elements, return the
5948/// unextended value. The unextended vector should be 64 bits so that it can
5949/// be used as an operand to a VMULL instruction. If the original vector size
5950/// before extension is less than 64 bits we add a an extension to resize
5951/// the vector to 64 bits.
5952static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00005953 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00005954 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5955 N->getOperand(0)->getValueType(0),
5956 N->getValueType(0),
5957 N->getOpcode());
5958
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005959 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00005960 return SkipLoadExtensionForVMULL(LD, DAG);
5961
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005962 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5963 // have been legalized as a BITCAST from v4i32.
5964 if (N->getOpcode() == ISD::BITCAST) {
5965 SDNode *BVN = N->getOperand(0).getNode();
5966 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5967 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5968 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00005969 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005970 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5971 }
5972 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5973 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5974 EVT VT = N->getValueType(0);
5975 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5976 unsigned NumElts = VT.getVectorNumElements();
5977 MVT TruncVT = MVT::getIntegerVT(EltSize);
5978 SmallVector<SDValue, 8> Ops;
5979 for (unsigned i = 0; i != NumElts; ++i) {
5980 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5981 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00005982 // Element types smaller than 32 bits are not legal, so use i32 elements.
5983 // The values are implicitly truncated so sext vs. zext doesn't matter.
5984 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005985 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005986 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topper48d114b2014-04-26 18:35:24 +00005987 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00005988}
5989
Evan Chenge2086e72011-03-29 01:56:09 +00005990static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5991 unsigned Opcode = N->getOpcode();
5992 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5993 SDNode *N0 = N->getOperand(0).getNode();
5994 SDNode *N1 = N->getOperand(1).getNode();
5995 return N0->hasOneUse() && N1->hasOneUse() &&
5996 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5997 }
5998 return false;
5999}
6000
6001static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
6002 unsigned Opcode = N->getOpcode();
6003 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
6004 SDNode *N0 = N->getOperand(0).getNode();
6005 SDNode *N1 = N->getOperand(1).getNode();
6006 return N0->hasOneUse() && N1->hasOneUse() &&
6007 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
6008 }
6009 return false;
6010}
6011
Bob Wilson38ab35a2010-09-01 23:50:19 +00006012static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
6013 // Multiplications are only custom-lowered for 128-bit vectors so that
6014 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
6015 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00006016 assert(VT.is128BitVector() && VT.isInteger() &&
6017 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00006018 SDNode *N0 = Op.getOperand(0).getNode();
6019 SDNode *N1 = Op.getOperand(1).getNode();
6020 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00006021 bool isMLA = false;
6022 bool isN0SExt = isSignExtended(N0, DAG);
6023 bool isN1SExt = isSignExtended(N1, DAG);
6024 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00006025 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00006026 else {
6027 bool isN0ZExt = isZeroExtended(N0, DAG);
6028 bool isN1ZExt = isZeroExtended(N1, DAG);
6029 if (isN0ZExt && isN1ZExt)
6030 NewOpc = ARMISD::VMULLu;
6031 else if (isN1SExt || isN1ZExt) {
6032 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
6033 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
6034 if (isN1SExt && isAddSubSExt(N0, DAG)) {
6035 NewOpc = ARMISD::VMULLs;
6036 isMLA = true;
6037 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
6038 NewOpc = ARMISD::VMULLu;
6039 isMLA = true;
6040 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
6041 std::swap(N0, N1);
6042 NewOpc = ARMISD::VMULLu;
6043 isMLA = true;
6044 }
6045 }
6046
6047 if (!NewOpc) {
6048 if (VT == MVT::v2i64)
6049 // Fall through to expand this. It is not legal.
6050 return SDValue();
6051 else
6052 // Other vector multiplications are legal.
6053 return Op;
6054 }
6055 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006056
6057 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006058 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00006059 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00006060 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006061 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00006062 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006063 assert(Op0.getValueType().is64BitVector() &&
6064 Op1.getValueType().is64BitVector() &&
6065 "unexpected types for extended operands to VMULL");
6066 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
6067 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00006068
Evan Chenge2086e72011-03-29 01:56:09 +00006069 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
6070 // isel lowering to take advantage of no-stall back to back vmul + vmla.
6071 // vmull q0, d4, d6
6072 // vmlal q0, d5, d6
6073 // is faster than
6074 // vaddl q0, d4, d5
6075 // vmovl q1, d6
6076 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00006077 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
6078 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00006079 EVT Op1VT = Op1.getValueType();
6080 return DAG.getNode(N0->getOpcode(), DL, VT,
6081 DAG.getNode(NewOpc, DL, VT,
6082 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
6083 DAG.getNode(NewOpc, DL, VT,
6084 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00006085}
6086
Owen Anderson77aa2662011-04-05 21:48:57 +00006087static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006088LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00006089 // Convert to float
6090 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
6091 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
6092 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
6093 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
6094 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
6095 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
6096 // Get reciprocal estimate.
6097 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00006098 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006099 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
6100 // Because char has a smaller range than uchar, we can actually get away
6101 // without any newton steps. This requires that we use a weird bias
6102 // of 0xb000, however (again, this has been exhaustively tested).
6103 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
6104 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
6105 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
6106 Y = DAG.getConstant(0xb000, MVT::i32);
6107 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
6108 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
6109 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
6110 // Convert back to short.
6111 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
6112 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
6113 return X;
6114}
6115
Owen Anderson77aa2662011-04-05 21:48:57 +00006116static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00006117LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00006118 SDValue N2;
6119 // Convert to float.
6120 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
6121 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
6122 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
6123 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
6124 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
6125 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006126
Nate Begemanfa62d502011-02-11 20:53:29 +00006127 // Use reciprocal estimate and one refinement step.
6128 // float4 recip = vrecpeq_f32(yf);
6129 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006130 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006131 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006132 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006133 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
6134 N1, N2);
6135 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6136 // Because short has a smaller range than ushort, we can actually get away
6137 // with only a single newton step. This requires that we use a weird bias
6138 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006139 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00006140 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6141 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006142 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00006143 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6144 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6145 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6146 // Convert back to integer and return.
6147 // return vmovn_s32(vcvt_s32_f32(result));
6148 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6149 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6150 return N0;
6151}
6152
6153static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
6154 EVT VT = Op.getValueType();
6155 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6156 "unexpected type for custom-lowering ISD::SDIV");
6157
Andrew Trickef9de2a2013-05-25 02:42:55 +00006158 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006159 SDValue N0 = Op.getOperand(0);
6160 SDValue N1 = Op.getOperand(1);
6161 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006162
Nate Begemanfa62d502011-02-11 20:53:29 +00006163 if (VT == MVT::v8i8) {
6164 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
6165 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006166
Nate Begemanfa62d502011-02-11 20:53:29 +00006167 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6168 DAG.getIntPtrConstant(4));
6169 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00006170 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00006171 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6172 DAG.getIntPtrConstant(0));
6173 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
6174 DAG.getIntPtrConstant(0));
6175
6176 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
6177 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
6178
6179 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6180 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006181
Nate Begemanfa62d502011-02-11 20:53:29 +00006182 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
6183 return N0;
6184 }
6185 return LowerSDIV_v4i16(N0, N1, dl, DAG);
6186}
6187
6188static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
6189 EVT VT = Op.getValueType();
6190 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6191 "unexpected type for custom-lowering ISD::UDIV");
6192
Andrew Trickef9de2a2013-05-25 02:42:55 +00006193 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006194 SDValue N0 = Op.getOperand(0);
6195 SDValue N1 = Op.getOperand(1);
6196 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006197
Nate Begemanfa62d502011-02-11 20:53:29 +00006198 if (VT == MVT::v8i8) {
6199 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6200 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006201
Nate Begemanfa62d502011-02-11 20:53:29 +00006202 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6203 DAG.getIntPtrConstant(4));
6204 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00006205 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00006206 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6207 DAG.getIntPtrConstant(0));
6208 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
6209 DAG.getIntPtrConstant(0));
Owen Anderson77aa2662011-04-05 21:48:57 +00006210
Nate Begemanfa62d502011-02-11 20:53:29 +00006211 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6212 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006213
Nate Begemanfa62d502011-02-11 20:53:29 +00006214 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6215 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006216
6217 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begemanfa62d502011-02-11 20:53:29 +00006218 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
6219 N0);
6220 return N0;
6221 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006222
Nate Begemanfa62d502011-02-11 20:53:29 +00006223 // v4i16 sdiv ... Convert to float.
6224 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6225 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6226 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6227 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6228 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006229 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006230
6231 // Use reciprocal estimate and two refinement steps.
6232 // float4 recip = vrecpeq_f32(yf);
6233 // recip *= vrecpsq_f32(yf, recip);
6234 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006235 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006236 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006237 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006238 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006239 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006240 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006241 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006242 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006243 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006244 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6245 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6246 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6247 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006248 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006249 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6250 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
6251 N1 = DAG.getConstant(2, MVT::i32);
6252 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6253 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6254 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6255 // Convert back to integer and return.
6256 // return vmovn_u32(vcvt_s32_f32(result));
6257 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6258 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6259 return N0;
6260}
6261
Evan Chenge8916542011-08-30 01:34:54 +00006262static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6263 EVT VT = Op.getNode()->getValueType(0);
6264 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6265
6266 unsigned Opc;
6267 bool ExtraOp = false;
6268 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006269 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006270 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6271 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6272 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6273 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6274 }
6275
6276 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006277 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006278 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006279 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006280 Op.getOperand(1), Op.getOperand(2));
6281}
6282
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006283SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6284 assert(Subtarget->isTargetDarwin());
6285
6286 // For iOS, we want to call an alternative entry point: __sincos_stret,
6287 // return values are passed via sret.
6288 SDLoc dl(Op);
6289 SDValue Arg = Op.getOperand(0);
6290 EVT ArgVT = Arg.getValueType();
6291 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
6292
6293 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
6294 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6295
6296 // Pair of floats / doubles used to pass the result.
Reid Kleckner343c3952014-11-20 23:51:47 +00006297 StructType *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006298
6299 // Create stack object for sret.
6300 const uint64_t ByteSize = TLI.getDataLayout()->getTypeAllocSize(RetTy);
6301 const unsigned StackAlign = TLI.getDataLayout()->getPrefTypeAlignment(RetTy);
6302 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6303 SDValue SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
6304
6305 ArgListTy Args;
6306 ArgListEntry Entry;
6307
6308 Entry.Node = SRet;
6309 Entry.Ty = RetTy->getPointerTo();
6310 Entry.isSExt = false;
6311 Entry.isZExt = false;
6312 Entry.isSRet = true;
6313 Args.push_back(Entry);
6314
6315 Entry.Node = Arg;
6316 Entry.Ty = ArgTy;
6317 Entry.isSExt = false;
6318 Entry.isZExt = false;
6319 Args.push_back(Entry);
6320
6321 const char *LibcallName = (ArgVT == MVT::f64)
6322 ? "__sincos_stret" : "__sincosf_stret";
6323 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
6324
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006325 TargetLowering::CallLoweringInfo CLI(DAG);
6326 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
6327 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), Callee,
Juergen Ributzka3bd03c72014-07-01 22:01:54 +00006328 std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006329 .setDiscardResult();
6330
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006331 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6332
6333 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6334 MachinePointerInfo(), false, false, false, 0);
6335
6336 // Address of cos field.
6337 SDValue Add = DAG.getNode(ISD::ADD, dl, getPointerTy(), SRet,
6338 DAG.getIntPtrConstant(ArgVT.getStoreSize()));
6339 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6340 MachinePointerInfo(), false, false, false, 0);
6341
6342 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6343 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6344 LoadSin.getValue(0), LoadCos.getValue(0));
6345}
6346
Eli Friedman10f9ce22011-09-15 22:26:18 +00006347static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006348 // Monotonic load/store is legal for all targets
6349 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6350 return Op;
6351
Alp Tokercb402912014-01-24 17:20:08 +00006352 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006353 // dmb or equivalent available.
6354 return SDValue();
6355}
6356
Tim Northoverbc933082013-05-23 19:11:20 +00006357static void ReplaceREADCYCLECOUNTER(SDNode *N,
6358 SmallVectorImpl<SDValue> &Results,
6359 SelectionDAG &DAG,
6360 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006361 SDLoc DL(N);
Tim Northoverbc933082013-05-23 19:11:20 +00006362 SDValue Cycles32, OutChain;
6363
6364 if (Subtarget->hasPerfMon()) {
6365 // Under Power Management extensions, the cycle-count is:
6366 // mrc p15, #0, <Rt>, c9, c13, #0
6367 SDValue Ops[] = { N->getOperand(0), // Chain
6368 DAG.getConstant(Intrinsic::arm_mrc, MVT::i32),
6369 DAG.getConstant(15, MVT::i32),
6370 DAG.getConstant(0, MVT::i32),
6371 DAG.getConstant(9, MVT::i32),
6372 DAG.getConstant(13, MVT::i32),
6373 DAG.getConstant(0, MVT::i32)
6374 };
6375
6376 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00006377 DAG.getVTList(MVT::i32, MVT::Other), Ops);
Tim Northoverbc933082013-05-23 19:11:20 +00006378 OutChain = Cycles32.getValue(1);
6379 } else {
6380 // Intrinsic is defined to return 0 on unsupported platforms. Technically
6381 // there are older ARM CPUs that have implementation-specific ways of
6382 // obtaining this information (FIXME!).
6383 Cycles32 = DAG.getConstant(0, MVT::i32);
6384 OutChain = DAG.getEntryNode();
6385 }
6386
6387
6388 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64,
6389 Cycles32, DAG.getConstant(0, MVT::i32));
6390 Results.push_back(Cycles64);
6391 Results.push_back(OutChain);
6392}
6393
Dan Gohman21cea8a2010-04-17 15:26:15 +00006394SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006395 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006396 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Cheng10043e22007-01-19 07:51:42 +00006397 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006398 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006399 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006400 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6401 default: llvm_unreachable("unknown object format");
6402 case Triple::COFF:
6403 return LowerGlobalAddressWindows(Op, DAG);
6404 case Triple::ELF:
6405 return LowerGlobalAddressELF(Op, DAG);
6406 case Triple::MachO:
6407 return LowerGlobalAddressDarwin(Op, DAG);
6408 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006409 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006410 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006411 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6412 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006413 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006414 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006415 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006416 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006417 case ISD::SINT_TO_FP:
6418 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6419 case ISD::FP_TO_SINT:
6420 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006421 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006422 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006423 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006424 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006425 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006426 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006427 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6428 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006429 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006430 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006431 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006432 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng15b80e42009-11-12 07:13:11 +00006433 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006434 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006435 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach8546ec92010-01-18 19:58:49 +00006436 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006437 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006438 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006439 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006440 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006441 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006442 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006443 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006444 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006445 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006446 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanfa62d502011-02-11 20:53:29 +00006447 case ISD::SDIV: return LowerSDIV(Op, DAG);
6448 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006449 case ISD::ADDC:
6450 case ISD::ADDE:
6451 case ISD::SUBC:
6452 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006453 case ISD::SADDO:
6454 case ISD::UADDO:
6455 case ISD::SSUBO:
6456 case ISD::USUBO:
6457 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006458 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006459 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006460 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006461 case ISD::SDIVREM:
6462 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00006463 case ISD::DYNAMIC_STACKALLOC:
6464 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
6465 return LowerDYNAMIC_STACKALLOC(Op, DAG);
6466 llvm_unreachable("Don't know how to custom lower this!");
Oliver Stannard51b1d462014-08-21 12:50:31 +00006467 case ISD::FP_ROUND: return LowerFP_ROUND(Op, DAG);
6468 case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006469 }
Evan Cheng10043e22007-01-19 07:51:42 +00006470}
6471
Duncan Sands6ed40142008-12-01 11:39:25 +00006472/// ReplaceNodeResults - Replace the results of node with an illegal result
6473/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006474void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
6475 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006476 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006477 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006478 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006479 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006480 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peck527da1b2010-11-23 03:31:01 +00006481 case ISD::BITCAST:
6482 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006483 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006484 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006485 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006486 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006487 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006488 case ISD::READCYCLECOUNTER:
6489 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6490 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00006491 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006492 if (Res.getNode())
6493 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006494}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006495
Evan Cheng10043e22007-01-19 07:51:42 +00006496//===----------------------------------------------------------------------===//
6497// ARM Scheduler Hooks
6498//===----------------------------------------------------------------------===//
6499
Bill Wendling030b58e2011-10-06 22:18:16 +00006500/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6501/// registers the function context.
6502void ARMTargetLowering::
6503SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6504 MachineBasicBlock *DispatchBB, int FI) const {
Eric Christopherd9134482014-08-04 21:25:23 +00006505 const TargetInstrInfo *TII =
6506 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Bill Wendling374ee192011-10-03 21:25:38 +00006507 DebugLoc dl = MI->getDebugLoc();
6508 MachineFunction *MF = MBB->getParent();
6509 MachineRegisterInfo *MRI = &MF->getRegInfo();
6510 MachineConstantPool *MCP = MF->getConstantPool();
6511 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6512 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006513
Bill Wendling374ee192011-10-03 21:25:38 +00006514 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006515 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006516
Bill Wendling374ee192011-10-03 21:25:38 +00006517 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006518 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006519 ARMConstantPoolValue *CPV =
6520 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6521 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6522
Craig Topper61e88f42014-11-21 05:58:21 +00006523 const TargetRegisterClass *TRC = isThumb ? &ARM::tGPRRegClass
6524 : &ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006525
Bill Wendling030b58e2011-10-06 22:18:16 +00006526 // Grab constant pool and fixed stack memory operands.
6527 MachineMemOperand *CPMMO =
6528 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6529 MachineMemOperand::MOLoad, 4, 4);
6530
6531 MachineMemOperand *FIMMOSt =
6532 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6533 MachineMemOperand::MOStore, 4, 4);
6534
6535 // Load the address of the dispatch MBB into the jump buffer.
6536 if (isThumb2) {
6537 // Incoming value: jbuf
6538 // ldr.n r5, LCPI1_1
6539 // orr r5, r5, #1
6540 // add r5, pc
6541 // str r5, [$jbuf, #+4] ; &jbuf[1]
6542 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6543 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6544 .addConstantPoolIndex(CPI)
6545 .addMemOperand(CPMMO));
6546 // Set the low bit because of thumb mode.
6547 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6548 AddDefaultCC(
6549 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6550 .addReg(NewVReg1, RegState::Kill)
6551 .addImm(0x01)));
6552 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6553 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6554 .addReg(NewVReg2, RegState::Kill)
6555 .addImm(PCLabelId);
6556 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6557 .addReg(NewVReg3, RegState::Kill)
6558 .addFrameIndex(FI)
6559 .addImm(36) // &jbuf[1] :: pc
6560 .addMemOperand(FIMMOSt));
6561 } else if (isThumb) {
6562 // Incoming value: jbuf
6563 // ldr.n r1, LCPI1_4
6564 // add r1, pc
6565 // mov r2, #1
6566 // orrs r1, r2
6567 // add r2, $jbuf, #+4 ; &jbuf[1]
6568 // str r1, [r2]
6569 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6570 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6571 .addConstantPoolIndex(CPI)
6572 .addMemOperand(CPMMO));
6573 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6574 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6575 .addReg(NewVReg1, RegState::Kill)
6576 .addImm(PCLabelId);
6577 // Set the low bit because of thumb mode.
6578 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6579 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6580 .addReg(ARM::CPSR, RegState::Define)
6581 .addImm(1));
6582 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6583 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6584 .addReg(ARM::CPSR, RegState::Define)
6585 .addReg(NewVReg2, RegState::Kill)
6586 .addReg(NewVReg3, RegState::Kill));
6587 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Tim Northover23075cc2014-10-20 21:28:41 +00006588 BuildMI(*MBB, MI, dl, TII->get(ARM::tADDframe), NewVReg5)
6589 .addFrameIndex(FI)
6590 .addImm(36); // &jbuf[1] :: pc
Bill Wendling030b58e2011-10-06 22:18:16 +00006591 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6592 .addReg(NewVReg4, RegState::Kill)
6593 .addReg(NewVReg5, RegState::Kill)
6594 .addImm(0)
6595 .addMemOperand(FIMMOSt));
6596 } else {
6597 // Incoming value: jbuf
6598 // ldr r1, LCPI1_1
6599 // add r1, pc, r1
6600 // str r1, [$jbuf, #+4] ; &jbuf[1]
6601 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6602 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6603 .addConstantPoolIndex(CPI)
6604 .addImm(0)
6605 .addMemOperand(CPMMO));
6606 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6607 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6608 .addReg(NewVReg1, RegState::Kill)
6609 .addImm(PCLabelId));
6610 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6611 .addReg(NewVReg2, RegState::Kill)
6612 .addFrameIndex(FI)
6613 .addImm(36) // &jbuf[1] :: pc
6614 .addMemOperand(FIMMOSt));
6615 }
6616}
6617
6618MachineBasicBlock *ARMTargetLowering::
6619EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
Eric Christopherd9134482014-08-04 21:25:23 +00006620 const TargetInstrInfo *TII =
6621 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Bill Wendling030b58e2011-10-06 22:18:16 +00006622 DebugLoc dl = MI->getDebugLoc();
6623 MachineFunction *MF = MBB->getParent();
6624 MachineRegisterInfo *MRI = &MF->getRegInfo();
6625 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6626 MachineFrameInfo *MFI = MF->getFrameInfo();
6627 int FI = MFI->getFunctionContextIndex();
6628
Craig Topper61e88f42014-11-21 05:58:21 +00006629 const TargetRegisterClass *TRC = Subtarget->isThumb() ? &ARM::tGPRRegClass
6630 : &ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00006631
Bill Wendling362c1b02011-10-06 21:29:56 +00006632 // Get a mapping of the call site numbers to all of the landing pads they're
6633 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00006634 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6635 unsigned MaxCSNum = 0;
6636 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00006637 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6638 ++BB) {
Bill Wendling202803e2011-10-05 00:02:33 +00006639 if (!BB->isLandingPad()) continue;
6640
6641 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6642 // pad.
6643 for (MachineBasicBlock::iterator
6644 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6645 if (!II->isEHLabel()) continue;
6646
6647 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006648 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006649
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006650 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6651 for (SmallVectorImpl<unsigned>::iterator
6652 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6653 CSI != CSE; ++CSI) {
6654 CallSiteNumToLPad[*CSI].push_back(BB);
6655 MaxCSNum = std::max(MaxCSNum, *CSI);
6656 }
Bill Wendling202803e2011-10-05 00:02:33 +00006657 break;
6658 }
6659 }
6660
6661 // Get an ordered list of the machine basic blocks for the jump table.
6662 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00006663 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00006664 LPadList.reserve(CallSiteNumToLPad.size());
6665 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6666 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6667 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006668 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00006669 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00006670 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6671 }
Bill Wendling202803e2011-10-05 00:02:33 +00006672 }
6673
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006674 assert(!LPadList.empty() &&
6675 "No landing pad destinations for the dispatch jump table!");
6676
Bill Wendling362c1b02011-10-06 21:29:56 +00006677 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00006678 MachineJumpTableInfo *JTI =
6679 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6680 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6681 unsigned UId = AFI->createJumpTableUId();
Chad Rosier96603432013-03-01 18:30:38 +00006682 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00006683
Bill Wendling362c1b02011-10-06 21:29:56 +00006684 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00006685
6686 // Shove the dispatch's address into the return slot in the function context.
6687 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6688 DispatchBB->setIsLandingPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00006689
Bill Wendling324be982011-10-05 00:39:32 +00006690 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006691 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00006692 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006693 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00006694 else
6695 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6696
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006697 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00006698 DispatchBB->addSuccessor(TrapBB);
6699
6700 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6701 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00006702
Bill Wendling510fbcd2011-10-17 21:32:56 +00006703 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00006704 MF->insert(MF->end(), DispatchBB);
6705 MF->insert(MF->end(), DispContBB);
6706 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00006707
Bill Wendling030b58e2011-10-06 22:18:16 +00006708 // Insert code into the entry block that creates and registers the function
6709 // context.
6710 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6711
Bill Wendling030b58e2011-10-06 22:18:16 +00006712 MachineMemOperand *FIMMOLd =
Bill Wendling362c1b02011-10-06 21:29:56 +00006713 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendlingb3d46782011-10-06 23:37:36 +00006714 MachineMemOperand::MOLoad |
6715 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00006716
Chad Rosier1ec8e402012-11-06 23:05:24 +00006717 MachineInstrBuilder MIB;
6718 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6719
6720 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6721 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6722
6723 // Add a register mask with no preserved registers. This results in all
6724 // registers being marked as clobbered.
6725 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00006726
Bill Wendling85833f72011-10-18 22:49:07 +00006727 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00006728 if (Subtarget->isThumb2()) {
6729 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6730 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6731 .addFrameIndex(FI)
6732 .addImm(4)
6733 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006734
Bill Wendling85833f72011-10-18 22:49:07 +00006735 if (NumLPads < 256) {
6736 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6737 .addReg(NewVReg1)
6738 .addImm(LPadList.size()));
6739 } else {
6740 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6741 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006742 .addImm(NumLPads & 0xFFFF));
6743
6744 unsigned VReg2 = VReg1;
6745 if ((NumLPads & 0xFFFF0000) != 0) {
6746 VReg2 = MRI->createVirtualRegister(TRC);
6747 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6748 .addReg(VReg1)
6749 .addImm(NumLPads >> 16));
6750 }
6751
Bill Wendling85833f72011-10-18 22:49:07 +00006752 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6753 .addReg(NewVReg1)
6754 .addReg(VReg2));
6755 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006756
Bill Wendling5626c662011-10-06 22:53:00 +00006757 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6758 .addMBB(TrapBB)
6759 .addImm(ARMCC::HI)
6760 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00006761
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006762 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6763 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006764 .addJumpTableIndex(MJTI)
6765 .addImm(UId));
Bill Wendling202803e2011-10-05 00:02:33 +00006766
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006767 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006768 AddDefaultCC(
6769 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006770 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6771 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00006772 .addReg(NewVReg1)
6773 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6774
6775 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006776 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00006777 .addReg(NewVReg1)
Bill Wendling5626c662011-10-06 22:53:00 +00006778 .addJumpTableIndex(MJTI)
6779 .addImm(UId);
6780 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00006781 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6782 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6783 .addFrameIndex(FI)
6784 .addImm(1)
6785 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00006786
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006787 if (NumLPads < 256) {
6788 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6789 .addReg(NewVReg1)
6790 .addImm(NumLPads));
6791 } else {
6792 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00006793 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6794 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6795
6796 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006797 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006798 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006799 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006800 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006801
6802 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6803 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6804 .addReg(VReg1, RegState::Define)
6805 .addConstantPoolIndex(Idx));
6806 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6807 .addReg(NewVReg1)
6808 .addReg(VReg1));
6809 }
6810
Bill Wendlingb3d46782011-10-06 23:37:36 +00006811 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6812 .addMBB(TrapBB)
6813 .addImm(ARMCC::HI)
6814 .addReg(ARM::CPSR);
6815
6816 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6817 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6818 .addReg(ARM::CPSR, RegState::Define)
6819 .addReg(NewVReg1)
6820 .addImm(2));
6821
6822 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00006823 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendlingb3d46782011-10-06 23:37:36 +00006824 .addJumpTableIndex(MJTI)
6825 .addImm(UId));
6826
6827 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6828 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6829 .addReg(ARM::CPSR, RegState::Define)
6830 .addReg(NewVReg2, RegState::Kill)
6831 .addReg(NewVReg3));
6832
6833 MachineMemOperand *JTMMOLd =
6834 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6835 MachineMemOperand::MOLoad, 4, 4);
6836
6837 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6838 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6839 .addReg(NewVReg4, RegState::Kill)
6840 .addImm(0)
6841 .addMemOperand(JTMMOLd));
6842
Chad Rosier96603432013-03-01 18:30:38 +00006843 unsigned NewVReg6 = NewVReg5;
6844 if (RelocM == Reloc::PIC_) {
6845 NewVReg6 = MRI->createVirtualRegister(TRC);
6846 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6847 .addReg(ARM::CPSR, RegState::Define)
6848 .addReg(NewVReg5, RegState::Kill)
6849 .addReg(NewVReg3));
6850 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00006851
6852 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6853 .addReg(NewVReg6, RegState::Kill)
6854 .addJumpTableIndex(MJTI)
6855 .addImm(UId);
Bill Wendling5626c662011-10-06 22:53:00 +00006856 } else {
6857 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6858 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6859 .addFrameIndex(FI)
6860 .addImm(4)
6861 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00006862
Bill Wendling4969dcd2011-10-18 22:52:20 +00006863 if (NumLPads < 256) {
6864 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6865 .addReg(NewVReg1)
6866 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00006867 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00006868 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6869 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006870 .addImm(NumLPads & 0xFFFF));
6871
6872 unsigned VReg2 = VReg1;
6873 if ((NumLPads & 0xFFFF0000) != 0) {
6874 VReg2 = MRI->createVirtualRegister(TRC);
6875 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6876 .addReg(VReg1)
6877 .addImm(NumLPads >> 16));
6878 }
6879
Bill Wendling4969dcd2011-10-18 22:52:20 +00006880 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6881 .addReg(NewVReg1)
6882 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00006883 } else {
6884 MachineConstantPool *ConstantPool = MF->getConstantPool();
6885 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6886 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6887
6888 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006889 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006890 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006891 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006892 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6893
6894 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6895 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6896 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00006897 .addConstantPoolIndex(Idx)
6898 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00006899 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6900 .addReg(NewVReg1)
6901 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00006902 }
6903
Bill Wendling5626c662011-10-06 22:53:00 +00006904 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6905 .addMBB(TrapBB)
6906 .addImm(ARMCC::HI)
6907 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00006908
Bill Wendling973c8172011-10-18 22:11:18 +00006909 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006910 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00006911 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006912 .addReg(NewVReg1)
6913 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00006914 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6915 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006916 .addJumpTableIndex(MJTI)
6917 .addImm(UId));
6918
6919 MachineMemOperand *JTMMOLd =
6920 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6921 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00006922 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006923 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00006924 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6925 .addReg(NewVReg3, RegState::Kill)
6926 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006927 .addImm(0)
6928 .addMemOperand(JTMMOLd));
6929
Chad Rosier96603432013-03-01 18:30:38 +00006930 if (RelocM == Reloc::PIC_) {
6931 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
6932 .addReg(NewVReg5, RegState::Kill)
6933 .addReg(NewVReg4)
6934 .addJumpTableIndex(MJTI)
6935 .addImm(UId);
6936 } else {
6937 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
6938 .addReg(NewVReg5, RegState::Kill)
6939 .addJumpTableIndex(MJTI)
6940 .addImm(UId);
6941 }
Bill Wendling5626c662011-10-06 22:53:00 +00006942 }
Bill Wendling202803e2011-10-05 00:02:33 +00006943
Bill Wendling324be982011-10-05 00:39:32 +00006944 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006945 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00006946 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006947 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6948 MachineBasicBlock *CurMBB = *I;
David Blaikie70573dc2014-11-19 07:49:26 +00006949 if (SeenMBBs.insert(CurMBB).second)
Bill Wendling883ec972011-10-07 23:18:02 +00006950 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006951 }
6952
Bill Wendling26d27802011-10-17 05:25:09 +00006953 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00006954 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00006955 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Craig Topper46276792014-08-24 23:23:06 +00006956 for (MachineBasicBlock *BB : InvokeBBs) {
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006957
6958 // Remove the landing pad successor from the invoke block and replace it
6959 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00006960 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6961 BB->succ_end());
6962 while (!Successors.empty()) {
6963 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling883ec972011-10-07 23:18:02 +00006964 if (SMBB->isLandingPad()) {
6965 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00006966 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006967 }
6968 }
6969
6970 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006971
6972 // Find the invoke call and mark all of the callee-saved registers as
6973 // 'implicit defined' so that they're spilled. This prevents code from
6974 // moving instructions to before the EH block, where they will never be
6975 // executed.
6976 for (MachineBasicBlock::reverse_iterator
6977 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00006978 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006979
6980 DenseMap<unsigned, bool> DefRegs;
6981 for (MachineInstr::mop_iterator
6982 OI = II->operands_begin(), OE = II->operands_end();
6983 OI != OE; ++OI) {
6984 if (!OI->isReg()) continue;
6985 DefRegs[OI->getReg()] = true;
6986 }
6987
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00006988 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006989
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006990 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00006991 unsigned Reg = SavedRegs[i];
6992 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00006993 !ARM::tGPRRegClass.contains(Reg) &&
6994 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006995 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006996 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006997 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006998 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006999 continue;
7000 if (!DefRegs[Reg])
7001 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00007002 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00007003
7004 break;
7005 }
Bill Wendling883ec972011-10-07 23:18:02 +00007006 }
Bill Wendling324be982011-10-05 00:39:32 +00007007
Bill Wendling617075f2011-10-18 18:30:49 +00007008 // Mark all former landing pads as non-landing pads. The dispatch is the only
7009 // landing pad now.
7010 for (SmallVectorImpl<MachineBasicBlock*>::iterator
7011 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
7012 (*I)->setIsLandingPad(false);
7013
Bill Wendling324be982011-10-05 00:39:32 +00007014 // The instruction is gone now.
7015 MI->eraseFromParent();
7016
Bill Wendling374ee192011-10-03 21:25:38 +00007017 return MBB;
7018}
7019
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007020static
7021MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
7022 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
7023 E = MBB->succ_end(); I != E; ++I)
7024 if (*I != Succ)
7025 return *I;
7026 llvm_unreachable("Expecting a BB with two successors!");
7027}
7028
Manman Renb504f492013-10-29 22:27:32 +00007029/// Return the load opcode for a given load size. If load size >= 8,
7030/// neon opcode will be returned.
7031static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
7032 if (LdSize >= 8)
7033 return LdSize == 16 ? ARM::VLD1q32wb_fixed
7034 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
7035 if (IsThumb1)
7036 return LdSize == 4 ? ARM::tLDRi
7037 : LdSize == 2 ? ARM::tLDRHi
7038 : LdSize == 1 ? ARM::tLDRBi : 0;
7039 if (IsThumb2)
7040 return LdSize == 4 ? ARM::t2LDR_POST
7041 : LdSize == 2 ? ARM::t2LDRH_POST
7042 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
7043 return LdSize == 4 ? ARM::LDR_POST_IMM
7044 : LdSize == 2 ? ARM::LDRH_POST
7045 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
7046}
7047
7048/// Return the store opcode for a given store size. If store size >= 8,
7049/// neon opcode will be returned.
7050static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
7051 if (StSize >= 8)
7052 return StSize == 16 ? ARM::VST1q32wb_fixed
7053 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
7054 if (IsThumb1)
7055 return StSize == 4 ? ARM::tSTRi
7056 : StSize == 2 ? ARM::tSTRHi
7057 : StSize == 1 ? ARM::tSTRBi : 0;
7058 if (IsThumb2)
7059 return StSize == 4 ? ARM::t2STR_POST
7060 : StSize == 2 ? ARM::t2STRH_POST
7061 : StSize == 1 ? ARM::t2STRB_POST : 0;
7062 return StSize == 4 ? ARM::STR_POST_IMM
7063 : StSize == 2 ? ARM::STRH_POST
7064 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
7065}
7066
7067/// Emit a post-increment load operation with given size. The instructions
7068/// will be added to BB at Pos.
7069static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
7070 const TargetInstrInfo *TII, DebugLoc dl,
7071 unsigned LdSize, unsigned Data, unsigned AddrIn,
7072 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7073 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
7074 assert(LdOpc != 0 && "Should have a load opcode");
7075 if (LdSize >= 8) {
7076 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7077 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7078 .addImm(0));
7079 } else if (IsThumb1) {
7080 // load + update AddrIn
7081 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7082 .addReg(AddrIn).addImm(0));
7083 MachineInstrBuilder MIB =
7084 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7085 MIB = AddDefaultT1CC(MIB);
7086 MIB.addReg(AddrIn).addImm(LdSize);
7087 AddDefaultPred(MIB);
7088 } else if (IsThumb2) {
7089 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7090 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7091 .addImm(LdSize));
7092 } else { // arm
7093 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
7094 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
7095 .addReg(0).addImm(LdSize));
7096 }
7097}
7098
7099/// Emit a post-increment store operation with given size. The instructions
7100/// will be added to BB at Pos.
7101static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
7102 const TargetInstrInfo *TII, DebugLoc dl,
7103 unsigned StSize, unsigned Data, unsigned AddrIn,
7104 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
7105 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
7106 assert(StOpc != 0 && "Should have a store opcode");
7107 if (StSize >= 8) {
7108 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7109 .addReg(AddrIn).addImm(0).addReg(Data));
7110 } else if (IsThumb1) {
7111 // store + update AddrIn
7112 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
7113 .addReg(AddrIn).addImm(0));
7114 MachineInstrBuilder MIB =
7115 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
7116 MIB = AddDefaultT1CC(MIB);
7117 MIB.addReg(AddrIn).addImm(StSize);
7118 AddDefaultPred(MIB);
7119 } else if (IsThumb2) {
7120 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7121 .addReg(Data).addReg(AddrIn).addImm(StSize));
7122 } else { // arm
7123 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
7124 .addReg(Data).addReg(AddrIn).addReg(0)
7125 .addImm(StSize));
7126 }
7127}
7128
David Peixottoc32e24a2013-10-17 19:49:22 +00007129MachineBasicBlock *
7130ARMTargetLowering::EmitStructByval(MachineInstr *MI,
7131 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00007132 // This pseudo instruction has 3 operands: dst, src, size
7133 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
7134 // Otherwise, we will generate unrolled scalar copies.
Eric Christopherd9134482014-08-04 21:25:23 +00007135 const TargetInstrInfo *TII =
7136 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Manman Rene8735522012-06-01 19:33:18 +00007137 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7138 MachineFunction::iterator It = BB;
7139 ++It;
7140
7141 unsigned dest = MI->getOperand(0).getReg();
7142 unsigned src = MI->getOperand(1).getReg();
7143 unsigned SizeVal = MI->getOperand(2).getImm();
7144 unsigned Align = MI->getOperand(3).getImm();
7145 DebugLoc dl = MI->getDebugLoc();
7146
Manman Rene8735522012-06-01 19:33:18 +00007147 MachineFunction *MF = BB->getParent();
7148 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00007149 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00007150 const TargetRegisterClass *TRC = nullptr;
7151 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007152
7153 bool IsThumb1 = Subtarget->isThumb1Only();
7154 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00007155
7156 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00007157 UnitSize = 1;
7158 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00007159 UnitSize = 2;
7160 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00007161 // Check whether we can use NEON instructions.
Bill Wendling698e84f2012-12-30 10:32:01 +00007162 if (!MF->getFunction()->getAttributes().
7163 hasAttribute(AttributeSet::FunctionIndex,
7164 Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00007165 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00007166 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00007167 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00007168 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00007169 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00007170 }
7171 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00007172 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00007173 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00007174 }
Manman Ren6e1fd462012-06-18 22:23:48 +00007175
David Peixottob0653e532013-10-24 16:39:36 +00007176 // Select the correct opcode and register class for unit size load/store
7177 bool IsNeon = UnitSize >= 8;
Craig Topper61e88f42014-11-21 05:58:21 +00007178 TRC = (IsThumb1 || IsThumb2) ? &ARM::tGPRRegClass : &ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00007179 if (IsNeon)
Craig Topper61e88f42014-11-21 05:58:21 +00007180 VecTRC = UnitSize == 16 ? &ARM::DPairRegClass
7181 : UnitSize == 8 ? &ARM::DPRRegClass
7182 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00007183
Manman Rene8735522012-06-01 19:33:18 +00007184 unsigned BytesLeft = SizeVal % UnitSize;
7185 unsigned LoopSize = SizeVal - BytesLeft;
7186
7187 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
7188 // Use LDR and STR to copy.
7189 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
7190 // [destOut] = STR_POST(scratch, destIn, UnitSize)
7191 unsigned srcIn = src;
7192 unsigned destIn = dest;
7193 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007194 unsigned srcOut = MRI.createVirtualRegister(TRC);
7195 unsigned destOut = MRI.createVirtualRegister(TRC);
7196 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007197 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7198 IsThumb1, IsThumb2);
7199 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7200 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007201 srcIn = srcOut;
7202 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007203 }
7204
7205 // Handle the leftover bytes with LDRB and STRB.
7206 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7207 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007208 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007209 unsigned srcOut = MRI.createVirtualRegister(TRC);
7210 unsigned destOut = MRI.createVirtualRegister(TRC);
7211 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007212 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7213 IsThumb1, IsThumb2);
7214 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7215 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007216 srcIn = srcOut;
7217 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007218 }
7219 MI->eraseFromParent(); // The instruction is gone now.
7220 return BB;
7221 }
7222
7223 // Expand the pseudo op to a loop.
7224 // thisMBB:
7225 // ...
7226 // movw varEnd, # --> with thumb2
7227 // movt varEnd, #
7228 // ldrcp varEnd, idx --> without thumb2
7229 // fallthrough --> loopMBB
7230 // loopMBB:
7231 // PHI varPhi, varEnd, varLoop
7232 // PHI srcPhi, src, srcLoop
7233 // PHI destPhi, dst, destLoop
7234 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7235 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7236 // subs varLoop, varPhi, #UnitSize
7237 // bne loopMBB
7238 // fallthrough --> exitMBB
7239 // exitMBB:
7240 // epilogue to handle left-over bytes
7241 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7242 // [destOut] = STRB_POST(scratch, destLoop, 1)
7243 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7244 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7245 MF->insert(It, loopMBB);
7246 MF->insert(It, exitMBB);
7247
7248 // Transfer the remainder of BB and its successor edges to exitMBB.
7249 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007250 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007251 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7252
7253 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007254 unsigned varEnd = MRI.createVirtualRegister(TRC);
7255 if (IsThumb2) {
7256 unsigned Vtmp = varEnd;
7257 if ((LoopSize & 0xFFFF0000) != 0)
7258 Vtmp = MRI.createVirtualRegister(TRC);
7259 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), Vtmp)
7260 .addImm(LoopSize & 0xFFFF));
7261
7262 if ((LoopSize & 0xFFFF0000) != 0)
7263 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
7264 .addReg(Vtmp).addImm(LoopSize >> 16));
7265 } else {
7266 MachineConstantPool *ConstantPool = MF->getConstantPool();
7267 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7268 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7269
7270 // MachineConstantPool wants an explicit alignment.
7271 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
7272 if (Align == 0)
7273 Align = getDataLayout()->getTypeAllocSize(C->getType());
7274 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7275
7276 if (IsThumb1)
7277 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7278 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7279 else
7280 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7281 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7282 }
Manman Rene8735522012-06-01 19:33:18 +00007283 BB->addSuccessor(loopMBB);
7284
7285 // Generate the loop body:
7286 // varPhi = PHI(varLoop, varEnd)
7287 // srcPhi = PHI(srcLoop, src)
7288 // destPhi = PHI(destLoop, dst)
7289 MachineBasicBlock *entryBB = BB;
7290 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007291 unsigned varLoop = MRI.createVirtualRegister(TRC);
7292 unsigned varPhi = MRI.createVirtualRegister(TRC);
7293 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7294 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7295 unsigned destLoop = MRI.createVirtualRegister(TRC);
7296 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007297
7298 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7299 .addReg(varLoop).addMBB(loopMBB)
7300 .addReg(varEnd).addMBB(entryBB);
7301 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7302 .addReg(srcLoop).addMBB(loopMBB)
7303 .addReg(src).addMBB(entryBB);
7304 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7305 .addReg(destLoop).addMBB(loopMBB)
7306 .addReg(dest).addMBB(entryBB);
7307
7308 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7309 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007310 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007311 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7312 IsThumb1, IsThumb2);
7313 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7314 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007315
7316 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007317 if (IsThumb1) {
7318 MachineInstrBuilder MIB =
7319 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7320 MIB = AddDefaultT1CC(MIB);
7321 MIB.addReg(varPhi).addImm(UnitSize);
7322 AddDefaultPred(MIB);
7323 } else {
7324 MachineInstrBuilder MIB =
7325 BuildMI(*BB, BB->end(), dl,
7326 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7327 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7328 MIB->getOperand(5).setReg(ARM::CPSR);
7329 MIB->getOperand(5).setIsDef(true);
7330 }
7331 BuildMI(*BB, BB->end(), dl,
7332 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7333 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007334
7335 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7336 BB->addSuccessor(loopMBB);
7337 BB->addSuccessor(exitMBB);
7338
7339 // Add epilogue to handle BytesLeft.
7340 BB = exitMBB;
7341 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007342
7343 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7344 // [destOut] = STRB_POST(scratch, destLoop, 1)
7345 unsigned srcIn = srcLoop;
7346 unsigned destIn = destLoop;
7347 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007348 unsigned srcOut = MRI.createVirtualRegister(TRC);
7349 unsigned destOut = MRI.createVirtualRegister(TRC);
7350 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007351 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7352 IsThumb1, IsThumb2);
7353 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7354 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007355 srcIn = srcOut;
7356 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007357 }
7358
7359 MI->eraseFromParent(); // The instruction is gone now.
7360 return BB;
7361}
7362
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007363MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007364ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
7365 MachineBasicBlock *MBB) const {
7366 const TargetMachine &TM = getTargetMachine();
Eric Christopherd9134482014-08-04 21:25:23 +00007367 const TargetInstrInfo &TII = *TM.getSubtargetImpl()->getInstrInfo();
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007368 DebugLoc DL = MI->getDebugLoc();
7369
7370 assert(Subtarget->isTargetWindows() &&
7371 "__chkstk is only supported on Windows");
7372 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
7373
7374 // __chkstk takes the number of words to allocate on the stack in R4, and
7375 // returns the stack adjustment in number of bytes in R4. This will not
7376 // clober any other registers (other than the obvious lr).
7377 //
7378 // Although, technically, IP should be considered a register which may be
7379 // clobbered, the call itself will not touch it. Windows on ARM is a pure
7380 // thumb-2 environment, so there is no interworking required. As a result, we
7381 // do not expect a veneer to be emitted by the linker, clobbering IP.
7382 //
Alp Toker1d099d92014-06-19 19:41:26 +00007383 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007384 // required, again, ensuring that IP is not clobbered.
7385 //
7386 // Finally, although some linkers may theoretically provide a trampoline for
7387 // out of range calls (which is quite common due to a 32M range limitation of
7388 // branches for Thumb), we can generate the long-call version via
7389 // -mcmodel=large, alleviating the need for the trampoline which may clobber
7390 // IP.
7391
7392 switch (TM.getCodeModel()) {
7393 case CodeModel::Small:
7394 case CodeModel::Medium:
7395 case CodeModel::Default:
7396 case CodeModel::Kernel:
7397 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
7398 .addImm((unsigned)ARMCC::AL).addReg(0)
7399 .addExternalSymbol("__chkstk")
7400 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7401 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7402 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7403 break;
7404 case CodeModel::Large:
7405 case CodeModel::JITDefault: {
7406 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7407 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
7408
7409 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
7410 .addExternalSymbol("__chkstk");
7411 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
7412 .addImm((unsigned)ARMCC::AL).addReg(0)
7413 .addReg(Reg, RegState::Kill)
7414 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7415 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7416 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7417 break;
7418 }
7419 }
7420
7421 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
7422 ARM::SP)
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +00007423 .addReg(ARM::SP).addReg(ARM::R4)));
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007424
7425 MI->eraseFromParent();
7426 return MBB;
7427}
7428
7429MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007430ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007431 MachineBasicBlock *BB) const {
Eric Christopherd9134482014-08-04 21:25:23 +00007432 const TargetInstrInfo *TII =
7433 getTargetMachine().getSubtargetImpl()->getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007434 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007435 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007436 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007437 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007438 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007439 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007440 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007441 // The Thumb2 pre-indexed stores have the same MI operands, they just
7442 // define them differently in the .td files from the isel patterns, so
7443 // they need pseudos.
7444 case ARM::t2STR_preidx:
7445 MI->setDesc(TII->get(ARM::t2STR_PRE));
7446 return BB;
7447 case ARM::t2STRB_preidx:
7448 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7449 return BB;
7450 case ARM::t2STRH_preidx:
7451 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7452 return BB;
7453
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007454 case ARM::STRi_preidx:
7455 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007456 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007457 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7458 // Decode the offset.
7459 unsigned Offset = MI->getOperand(4).getImm();
7460 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7461 Offset = ARM_AM::getAM2Offset(Offset);
7462 if (isSub)
7463 Offset = -Offset;
7464
Jim Grosbachf402f692011-08-12 21:02:34 +00007465 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007466 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007467 .addOperand(MI->getOperand(0)) // Rn_wb
7468 .addOperand(MI->getOperand(1)) // Rt
7469 .addOperand(MI->getOperand(2)) // Rn
7470 .addImm(Offset) // offset (skip GPR==zero_reg)
7471 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007472 .addOperand(MI->getOperand(6))
7473 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007474 MI->eraseFromParent();
7475 return BB;
7476 }
7477 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007478 case ARM::STRBr_preidx:
7479 case ARM::STRH_preidx: {
7480 unsigned NewOpc;
7481 switch (MI->getOpcode()) {
7482 default: llvm_unreachable("unexpected opcode!");
7483 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7484 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7485 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7486 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007487 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7488 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7489 MIB.addOperand(MI->getOperand(i));
7490 MI->eraseFromParent();
7491 return BB;
7492 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007493
Evan Chengbb2af352009-08-12 05:17:19 +00007494 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007495 // To "insert" a SELECT_CC instruction, we actually have to insert the
7496 // diamond control-flow pattern. The incoming instruction knows the
7497 // destination vreg to set, the condition code register to branch on, the
7498 // true/false values to select between, and a branch opcode to use.
7499 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007500 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007501 ++It;
7502
7503 // thisMBB:
7504 // ...
7505 // TrueVal = ...
7506 // cmpTY ccX, r1, r2
7507 // bCC copy1MBB
7508 // fallthrough --> copy0MBB
7509 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007510 MachineFunction *F = BB->getParent();
7511 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7512 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007513 F->insert(It, copy0MBB);
7514 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007515
7516 // Transfer the remainder of BB and its successor edges to sinkMBB.
7517 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007518 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007519 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7520
Dan Gohmanf4f04102010-07-06 15:49:48 +00007521 BB->addSuccessor(copy0MBB);
7522 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007523
Dan Gohman34396292010-07-06 20:24:04 +00007524 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7525 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7526
Evan Cheng10043e22007-01-19 07:51:42 +00007527 // copy0MBB:
7528 // %FalseValue = ...
7529 // # fallthrough to sinkMBB
7530 BB = copy0MBB;
7531
7532 // Update machine-CFG edges
7533 BB->addSuccessor(sinkMBB);
7534
7535 // sinkMBB:
7536 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7537 // ...
7538 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007539 BuildMI(*BB, BB->begin(), dl,
7540 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007541 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7542 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7543
Dan Gohman34396292010-07-06 20:24:04 +00007544 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007545 return BB;
7546 }
Evan Chengb972e562009-08-07 00:34:42 +00007547
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007548 case ARM::BCCi64:
7549 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007550 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007551 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007552
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007553 // Compare both parts that make up the double comparison separately for
7554 // equality.
7555 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7556
7557 unsigned LHS1 = MI->getOperand(1).getReg();
7558 unsigned LHS2 = MI->getOperand(2).getReg();
7559 if (RHSisZero) {
7560 AddDefaultPred(BuildMI(BB, dl,
7561 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7562 .addReg(LHS1).addImm(0));
7563 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7564 .addReg(LHS2).addImm(0)
7565 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7566 } else {
7567 unsigned RHS1 = MI->getOperand(3).getReg();
7568 unsigned RHS2 = MI->getOperand(4).getReg();
7569 AddDefaultPred(BuildMI(BB, dl,
7570 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7571 .addReg(LHS1).addReg(RHS1));
7572 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7573 .addReg(LHS2).addReg(RHS2)
7574 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7575 }
7576
7577 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7578 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7579 if (MI->getOperand(0).getImm() == ARMCC::NE)
7580 std::swap(destMBB, exitMBB);
7581
7582 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7583 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007584 if (isThumb2)
7585 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7586 else
7587 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007588
7589 MI->eraseFromParent(); // The pseudo instruction is gone now.
7590 return BB;
7591 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007592
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007593 case ARM::Int_eh_sjlj_setjmp:
7594 case ARM::Int_eh_sjlj_setjmp_nofp:
7595 case ARM::tInt_eh_sjlj_setjmp:
7596 case ARM::t2Int_eh_sjlj_setjmp:
7597 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7598 EmitSjLjDispatchBlock(MI, BB);
7599 return BB;
7600
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007601 case ARM::ABS:
7602 case ARM::t2ABS: {
7603 // To insert an ABS instruction, we have to insert the
7604 // diamond control-flow pattern. The incoming instruction knows the
7605 // source vreg to test against 0, the destination vreg to set,
7606 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007607 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007608 // It transforms
7609 // V1 = ABS V0
7610 // into
7611 // V2 = MOVS V0
7612 // BCC (branch to SinkBB if V0 >= 0)
7613 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007614 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007615 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7616 MachineFunction::iterator BBI = BB;
7617 ++BBI;
7618 MachineFunction *Fn = BB->getParent();
7619 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7620 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7621 Fn->insert(BBI, RSBBB);
7622 Fn->insert(BBI, SinkBB);
7623
7624 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7625 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7626 bool isThumb2 = Subtarget->isThumb2();
7627 MachineRegisterInfo &MRI = Fn->getRegInfo();
7628 // In Thumb mode S must not be specified if source register is the SP or
7629 // PC and if destination register is the SP, so restrict register class
Craig Topper61e88f42014-11-21 05:58:21 +00007630 unsigned NewRsbDstReg =
7631 MRI.createVirtualRegister(isThumb2 ? &ARM::rGPRRegClass : &ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007632
7633 // Transfer the remainder of BB and its successor edges to sinkMBB.
7634 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007635 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007636 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7637
7638 BB->addSuccessor(RSBBB);
7639 BB->addSuccessor(SinkBB);
7640
7641 // fall through to SinkMBB
7642 RSBBB->addSuccessor(SinkBB);
7643
Manman Rene0763c72012-06-15 21:32:12 +00007644 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00007645 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00007646 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7647 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007648
7649 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00007650 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007651 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7652 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7653
7654 // insert rsbri in RSBBB
7655 // Note: BCC and rsbri will be converted into predicated rsbmi
7656 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00007657 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007658 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Rene0763c72012-06-15 21:32:12 +00007659 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007660 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7661
Andrew Trick3f07c422011-10-18 18:40:53 +00007662 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007663 // reuse ABSDstReg to not change uses of ABS instruction
7664 BuildMI(*SinkBB, SinkBB->begin(), dl,
7665 TII->get(ARM::PHI), ABSDstReg)
7666 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00007667 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007668
7669 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00007670 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007671
7672 // return last added BB
7673 return SinkBB;
7674 }
Manman Rene8735522012-06-01 19:33:18 +00007675 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00007676 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00007677 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007678 case ARM::WIN__CHKSTK:
7679 return EmitLowered__chkstk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00007680 }
7681}
7682
Evan Chenge6fba772011-08-30 19:09:48 +00007683void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7684 SDNode *Node) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007685 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00007686 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7687 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7688 // operand is still set to noreg. If needed, set the optional operand's
7689 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00007690 //
Andrew Trick88b24502011-10-18 19:18:52 +00007691 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00007692
Andrew Trick924123a2011-09-21 02:20:46 +00007693 // Rename pseudo opcodes.
7694 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7695 if (NewOpc) {
Eric Christopherd9134482014-08-04 21:25:23 +00007696 const ARMBaseInstrInfo *TII = static_cast<const ARMBaseInstrInfo *>(
7697 getTargetMachine().getSubtargetImpl()->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: {
7801 EVT VT = N->getValueType(0);
7802 CC = N->getOperand(0);
7803 if (CC.getValueType() != MVT::i1)
7804 return false;
7805 Invert = !AllOnes;
7806 if (AllOnes)
7807 // When looking for an AllOnes constant, N is an sext, and the 'other'
7808 // value is 0.
7809 OtherOp = DAG.getConstant(0, VT);
7810 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7811 // When looking for a 0 constant, N can be zext or sext.
7812 OtherOp = DAG.getConstant(1, VT);
7813 else
7814 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7815 return true;
7816 }
7817 }
7818}
7819
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007820// Combine a constant select operand into its use:
7821//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007822// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7823// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7824// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7825// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7826// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007827//
7828// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007829// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007830//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007831// Also recognize sext/zext from i1:
7832//
7833// (add (zext cc), x) -> (select cc (add x, 1), x)
7834// (add (sext cc), x) -> (select cc (add x, -1), x)
7835//
7836// These transformations eventually create predicated instructions.
7837//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007838// @param N The node to transform.
7839// @param Slct The N operand that is a select.
7840// @param OtherOp The other N operand (x above).
7841// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007842// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007843// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00007844static
7845SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007846 TargetLowering::DAGCombinerInfo &DCI,
7847 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00007848 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007849 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007850 SDValue NonConstantVal;
7851 SDValue CCOp;
7852 bool SwapSelectOps;
7853 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7854 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007855 return SDValue();
7856
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007857 // Slct is now know to be the desired identity constant when CC is true.
7858 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007859 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007860 OtherOp, NonConstantVal);
7861 // Unless SwapSelectOps says CC should be false.
7862 if (SwapSelectOps)
7863 std::swap(TrueVal, FalseVal);
7864
Andrew Trickef9de2a2013-05-25 02:42:55 +00007865 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007866 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00007867}
7868
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007869// Attempt combineSelectAndUse on each operand of a commutative operator N.
7870static
7871SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7872 TargetLowering::DAGCombinerInfo &DCI) {
7873 SDValue N0 = N->getOperand(0);
7874 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007875 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007876 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7877 if (Result.getNode())
7878 return Result;
7879 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007880 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007881 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7882 if (Result.getNode())
7883 return Result;
7884 }
7885 return SDValue();
7886}
7887
Eric Christopher1b8b94192011-06-29 21:10:36 +00007888// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00007889// (only after legalization).
7890static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7891 TargetLowering::DAGCombinerInfo &DCI,
7892 const ARMSubtarget *Subtarget) {
7893
7894 // Only perform optimization if after legalize, and if NEON is available. We
7895 // also expected both operands to be BUILD_VECTORs.
7896 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7897 || N0.getOpcode() != ISD::BUILD_VECTOR
7898 || N1.getOpcode() != ISD::BUILD_VECTOR)
7899 return SDValue();
7900
7901 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7902 EVT VT = N->getValueType(0);
7903 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7904 return SDValue();
7905
7906 // Check that the vector operands are of the right form.
7907 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7908 // operands, where N is the size of the formed vector.
7909 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7910 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007911
7912 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00007913 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00007914 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00007915 SDValue Vec = N0->getOperand(0)->getOperand(0);
7916 SDNode *V = Vec.getNode();
7917 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00007918
Eric Christopher1b8b94192011-06-29 21:10:36 +00007919 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007920 // check to see if each of their operands are an EXTRACT_VECTOR with
7921 // the same vector and appropriate index.
7922 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7923 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7924 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00007925
Tanya Lattnere9e67052011-06-14 23:48:48 +00007926 SDValue ExtVec0 = N0->getOperand(i);
7927 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007928
Tanya Lattnere9e67052011-06-14 23:48:48 +00007929 // First operand is the vector, verify its the same.
7930 if (V != ExtVec0->getOperand(0).getNode() ||
7931 V != ExtVec1->getOperand(0).getNode())
7932 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00007933
Tanya Lattnere9e67052011-06-14 23:48:48 +00007934 // Second is the constant, verify its correct.
7935 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7936 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00007937
Tanya Lattnere9e67052011-06-14 23:48:48 +00007938 // For the constant, we want to see all the even or all the odd.
7939 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7940 || C1->getZExtValue() != nextIndex+1)
7941 return SDValue();
7942
7943 // Increment index.
7944 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007945 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00007946 return SDValue();
7947 }
7948
7949 // Create VPADDL node.
7950 SelectionDAG &DAG = DCI.DAG;
7951 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00007952
7953 // Build operand list.
7954 SmallVector<SDValue, 8> Ops;
7955 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7956 TLI.getPointerTy()));
7957
7958 // Input is the vector.
7959 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007960
Tanya Lattnere9e67052011-06-14 23:48:48 +00007961 // Get widened type and narrowed type.
7962 MVT widenType;
7963 unsigned numElem = VT.getVectorNumElements();
Silviu Barangaa3106e62014-04-03 10:44:27 +00007964
7965 EVT inputLaneType = Vec.getValueType().getVectorElementType();
7966 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00007967 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7968 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7969 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7970 default:
Craig Toppere55c5562012-02-07 02:50:20 +00007971 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00007972 }
7973
Craig Topper48d114b2014-04-26 18:35:24 +00007974 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00007975 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
7976 return DAG.getNode(ExtOp, SDLoc(N), VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00007977}
7978
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007979static SDValue findMUL_LOHI(SDValue V) {
7980 if (V->getOpcode() == ISD::UMUL_LOHI ||
7981 V->getOpcode() == ISD::SMUL_LOHI)
7982 return V;
7983 return SDValue();
7984}
7985
7986static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7987 TargetLowering::DAGCombinerInfo &DCI,
7988 const ARMSubtarget *Subtarget) {
7989
7990 if (Subtarget->isThumb1Only()) return SDValue();
7991
7992 // Only perform the checks after legalize when the pattern is available.
7993 if (DCI.isBeforeLegalize()) return SDValue();
7994
7995 // Look for multiply add opportunities.
7996 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7997 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7998 // a glue link from the first add to the second add.
7999 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
8000 // a S/UMLAL instruction.
8001 // loAdd UMUL_LOHI
8002 // \ / :lo \ :hi
8003 // \ / \ [no multiline comment]
8004 // ADDC | hiAdd
8005 // \ :glue / /
8006 // \ / /
8007 // ADDE
8008 //
8009 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
8010 SDValue AddcOp0 = AddcNode->getOperand(0);
8011 SDValue AddcOp1 = AddcNode->getOperand(1);
8012
8013 // Check if the two operands are from the same mul_lohi node.
8014 if (AddcOp0.getNode() == AddcOp1.getNode())
8015 return SDValue();
8016
8017 assert(AddcNode->getNumValues() == 2 &&
8018 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00008019 "Expect ADDC with two result values. First: i32");
8020
8021 // Check that we have a glued ADDC node.
8022 if (AddcNode->getValueType(1) != MVT::Glue)
8023 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008024
8025 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
8026 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
8027 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
8028 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
8029 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
8030 return SDValue();
8031
8032 // Look for the glued ADDE.
8033 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00008034 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008035 return SDValue();
8036
8037 // Make sure it is really an ADDE.
8038 if (AddeNode->getOpcode() != ISD::ADDE)
8039 return SDValue();
8040
8041 assert(AddeNode->getNumOperands() == 3 &&
8042 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
8043 "ADDE node has the wrong inputs");
8044
8045 // Check for the triangle shape.
8046 SDValue AddeOp0 = AddeNode->getOperand(0);
8047 SDValue AddeOp1 = AddeNode->getOperand(1);
8048
8049 // Make sure that the ADDE operands are not coming from the same node.
8050 if (AddeOp0.getNode() == AddeOp1.getNode())
8051 return SDValue();
8052
8053 // Find the MUL_LOHI node walking up ADDE's operands.
8054 bool IsLeftOperandMUL = false;
8055 SDValue MULOp = findMUL_LOHI(AddeOp0);
8056 if (MULOp == SDValue())
8057 MULOp = findMUL_LOHI(AddeOp1);
8058 else
8059 IsLeftOperandMUL = true;
8060 if (MULOp == SDValue())
Jyoti Allurf1d70502015-01-23 09:10:03 +00008061 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008062
8063 // Figure out the right opcode.
8064 unsigned Opc = MULOp->getOpcode();
8065 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
8066
8067 // Figure out the high and low input values to the MLAL node.
Craig Topper062a2ba2014-04-25 05:30:21 +00008068 SDValue* HiAdd = nullptr;
8069 SDValue* LoMul = nullptr;
8070 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008071
Jyoti Allurf1d70502015-01-23 09:10:03 +00008072 // Ensure that ADDE is from high result of ISD::SMUL_LOHI.
8073 if ((AddeOp0 != MULOp.getValue(1)) && (AddeOp1 != MULOp.getValue(1)))
8074 return SDValue();
8075
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008076 if (IsLeftOperandMUL)
8077 HiAdd = &AddeOp1;
8078 else
8079 HiAdd = &AddeOp0;
8080
8081
Jyoti Allurf1d70502015-01-23 09:10:03 +00008082 // Ensure that LoMul and LowAdd are taken from correct ISD::SMUL_LOHI node
8083 // whose low result is fed to the ADDC we are checking.
8084
8085 if (AddcOp0 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008086 LoMul = &AddcOp0;
8087 LowAdd = &AddcOp1;
8088 }
Jyoti Allurf1d70502015-01-23 09:10:03 +00008089 if (AddcOp1 == MULOp.getValue(0)) {
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008090 LoMul = &AddcOp1;
8091 LowAdd = &AddcOp0;
8092 }
8093
Craig Topper062a2ba2014-04-25 05:30:21 +00008094 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008095 return SDValue();
8096
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008097 // Create the merged node.
8098 SelectionDAG &DAG = DCI.DAG;
8099
8100 // Build operand list.
8101 SmallVector<SDValue, 8> Ops;
8102 Ops.push_back(LoMul->getOperand(0));
8103 Ops.push_back(LoMul->getOperand(1));
8104 Ops.push_back(*LowAdd);
8105 Ops.push_back(*HiAdd);
8106
Andrew Trickef9de2a2013-05-25 02:42:55 +00008107 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00008108 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00008109
8110 // Replace the ADDs' nodes uses by the MLA node's values.
8111 SDValue HiMLALResult(MLALNode.getNode(), 1);
8112 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
8113
8114 SDValue LoMLALResult(MLALNode.getNode(), 0);
8115 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
8116
8117 // Return original node to notify the driver to stop replacing.
8118 SDValue resNode(AddcNode, 0);
8119 return resNode;
8120}
8121
8122/// PerformADDCCombine - Target-specific dag combine transform from
8123/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
8124static SDValue PerformADDCCombine(SDNode *N,
8125 TargetLowering::DAGCombinerInfo &DCI,
8126 const ARMSubtarget *Subtarget) {
8127
8128 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
8129
8130}
8131
Bob Wilson728eb292010-07-29 20:34:14 +00008132/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
8133/// operands N0 and N1. This is a helper for PerformADDCombine that is
8134/// called with the default operands, and if that fails, with commuted
8135/// operands.
8136static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008137 TargetLowering::DAGCombinerInfo &DCI,
8138 const ARMSubtarget *Subtarget){
8139
8140 // Attempt to create vpaddl for this add.
8141 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
8142 if (Result.getNode())
8143 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00008144
Chris Lattner4147f082009-03-12 06:52:53 +00008145 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008146 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008147 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
8148 if (Result.getNode()) return Result;
8149 }
Chris Lattner4147f082009-03-12 06:52:53 +00008150 return SDValue();
8151}
8152
Bob Wilson728eb292010-07-29 20:34:14 +00008153/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
8154///
8155static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00008156 TargetLowering::DAGCombinerInfo &DCI,
8157 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00008158 SDValue N0 = N->getOperand(0);
8159 SDValue N1 = N->getOperand(1);
8160
8161 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008162 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008163 if (Result.getNode())
8164 return Result;
8165
8166 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00008167 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00008168}
8169
Chris Lattner4147f082009-03-12 06:52:53 +00008170/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00008171///
Chris Lattner4147f082009-03-12 06:52:53 +00008172static SDValue PerformSUBCombine(SDNode *N,
8173 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00008174 SDValue N0 = N->getOperand(0);
8175 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00008176
Chris Lattner4147f082009-03-12 06:52:53 +00008177 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00008178 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00008179 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
8180 if (Result.getNode()) return Result;
8181 }
Bob Wilson7117a912009-03-20 22:42:55 +00008182
Chris Lattner4147f082009-03-12 06:52:53 +00008183 return SDValue();
8184}
8185
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008186/// PerformVMULCombine
8187/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
8188/// special multiplier accumulator forwarding.
8189/// vmul d3, d0, d2
8190/// vmla d3, d1, d2
8191/// is faster than
8192/// vadd d3, d0, d1
8193/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00008194// However, for (A + B) * (A + B),
8195// vadd d2, d0, d1
8196// vmul d3, d0, d2
8197// vmla d3, d1, d2
8198// is slower than
8199// vadd d2, d0, d1
8200// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008201static SDValue PerformVMULCombine(SDNode *N,
8202 TargetLowering::DAGCombinerInfo &DCI,
8203 const ARMSubtarget *Subtarget) {
8204 if (!Subtarget->hasVMLxForwarding())
8205 return SDValue();
8206
8207 SelectionDAG &DAG = DCI.DAG;
8208 SDValue N0 = N->getOperand(0);
8209 SDValue N1 = N->getOperand(1);
8210 unsigned Opcode = N0.getOpcode();
8211 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8212 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00008213 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008214 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
8215 Opcode != ISD::FADD && Opcode != ISD::FSUB)
8216 return SDValue();
8217 std::swap(N0, N1);
8218 }
8219
Weiming Zhao2052f482013-09-25 23:12:06 +00008220 if (N0 == N1)
8221 return SDValue();
8222
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008223 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008224 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008225 SDValue N00 = N0->getOperand(0);
8226 SDValue N01 = N0->getOperand(1);
8227 return DAG.getNode(Opcode, DL, VT,
8228 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8229 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8230}
8231
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008232static SDValue PerformMULCombine(SDNode *N,
8233 TargetLowering::DAGCombinerInfo &DCI,
8234 const ARMSubtarget *Subtarget) {
8235 SelectionDAG &DAG = DCI.DAG;
8236
8237 if (Subtarget->isThumb1Only())
8238 return SDValue();
8239
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008240 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8241 return SDValue();
8242
8243 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008244 if (VT.is64BitVector() || VT.is128BitVector())
8245 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008246 if (VT != MVT::i32)
8247 return SDValue();
8248
8249 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8250 if (!C)
8251 return SDValue();
8252
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008253 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008254 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008255
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008256 ShiftAmt = ShiftAmt & (32 - 1);
8257 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008258 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008259
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008260 SDValue Res;
8261 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008262
8263 if (MulAmt >= 0) {
8264 if (isPowerOf2_32(MulAmt - 1)) {
8265 // (mul x, 2^N + 1) => (add (shl x, N), x)
8266 Res = DAG.getNode(ISD::ADD, DL, VT,
8267 V,
8268 DAG.getNode(ISD::SHL, DL, VT,
8269 V,
8270 DAG.getConstant(Log2_32(MulAmt - 1),
8271 MVT::i32)));
8272 } else if (isPowerOf2_32(MulAmt + 1)) {
8273 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8274 Res = DAG.getNode(ISD::SUB, DL, VT,
8275 DAG.getNode(ISD::SHL, DL, VT,
8276 V,
8277 DAG.getConstant(Log2_32(MulAmt + 1),
8278 MVT::i32)),
8279 V);
8280 } else
8281 return SDValue();
8282 } else {
8283 uint64_t MulAmtAbs = -MulAmt;
8284 if (isPowerOf2_32(MulAmtAbs + 1)) {
8285 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8286 Res = DAG.getNode(ISD::SUB, DL, VT,
8287 V,
8288 DAG.getNode(ISD::SHL, DL, VT,
8289 V,
8290 DAG.getConstant(Log2_32(MulAmtAbs + 1),
8291 MVT::i32)));
8292 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8293 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8294 Res = DAG.getNode(ISD::ADD, DL, VT,
8295 V,
8296 DAG.getNode(ISD::SHL, DL, VT,
8297 V,
8298 DAG.getConstant(Log2_32(MulAmtAbs-1),
8299 MVT::i32)));
8300 Res = DAG.getNode(ISD::SUB, DL, VT,
8301 DAG.getConstant(0, MVT::i32),Res);
8302
8303 } else
8304 return SDValue();
8305 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008306
8307 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008308 Res = DAG.getNode(ISD::SHL, DL, VT,
8309 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008310
8311 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008312 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008313 return SDValue();
8314}
8315
Owen Anderson30c48922010-11-05 19:27:46 +00008316static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008317 TargetLowering::DAGCombinerInfo &DCI,
8318 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008319
Owen Anderson30c48922010-11-05 19:27:46 +00008320 // Attempt to use immediate-form VBIC
8321 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008322 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008323 EVT VT = N->getValueType(0);
8324 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008325
Tanya Lattner266792a2011-04-07 15:24:20 +00008326 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8327 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008328
Owen Anderson30c48922010-11-05 19:27:46 +00008329 APInt SplatBits, SplatUndef;
8330 unsigned SplatBitSize;
8331 bool HasAnyUndefs;
8332 if (BVN &&
8333 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8334 if (SplatBitSize <= 64) {
8335 EVT VbicVT;
8336 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8337 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00008338 DAG, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008339 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008340 if (Val.getNode()) {
8341 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008342 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008343 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008344 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008345 }
8346 }
8347 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008348
Evan Chenge87681c2012-02-23 01:19:06 +00008349 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008350 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8351 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8352 if (Result.getNode())
8353 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008354 }
8355
Owen Anderson30c48922010-11-05 19:27:46 +00008356 return SDValue();
8357}
8358
Jim Grosbach11013ed2010-07-16 23:05:05 +00008359/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8360static SDValue PerformORCombine(SDNode *N,
8361 TargetLowering::DAGCombinerInfo &DCI,
8362 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008363 // Attempt to use immediate-form VORR
8364 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008365 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008366 EVT VT = N->getValueType(0);
8367 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008368
Tanya Lattner266792a2011-04-07 15:24:20 +00008369 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8370 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008371
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008372 APInt SplatBits, SplatUndef;
8373 unsigned SplatBitSize;
8374 bool HasAnyUndefs;
8375 if (BVN && Subtarget->hasNEON() &&
8376 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8377 if (SplatBitSize <= 64) {
8378 EVT VorrVT;
8379 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8380 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00008381 DAG, VorrVT, VT.is128BitVector(),
8382 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008383 if (Val.getNode()) {
8384 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008385 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008386 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008387 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008388 }
8389 }
8390 }
8391
Evan Chenge87681c2012-02-23 01:19:06 +00008392 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008393 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8394 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8395 if (Result.getNode())
8396 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008397 }
8398
Nadav Rotem3a94c542012-08-13 18:52:44 +00008399 // The code below optimizes (or (and X, Y), Z).
8400 // The AND operand needs to have a single user to make these optimizations
8401 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008402 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008403 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008404 return SDValue();
8405 SDValue N1 = N->getOperand(1);
8406
8407 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8408 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8409 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8410 APInt SplatUndef;
8411 unsigned SplatBitSize;
8412 bool HasAnyUndefs;
8413
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008414 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008415 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008416 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8417 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008418 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008419 HasAnyUndefs) && !HasAnyUndefs) {
8420 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8421 HasAnyUndefs) && !HasAnyUndefs) {
8422 // Ensure that the bit width of the constants are the same and that
8423 // the splat arguments are logical inverses as per the pattern we
8424 // are trying to simplify.
8425 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8426 SplatBits0 == ~SplatBits1) {
8427 // Canonicalize the vector type to make instruction selection
8428 // simpler.
8429 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8430 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8431 N0->getOperand(1),
8432 N0->getOperand(0),
8433 N1->getOperand(0));
8434 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8435 }
8436 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008437 }
8438 }
8439
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008440 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8441 // reasonable.
8442
Jim Grosbach11013ed2010-07-16 23:05:05 +00008443 // BFI is only available on V6T2+
8444 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8445 return SDValue();
8446
Andrew Trickef9de2a2013-05-25 02:42:55 +00008447 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008448 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008449 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008450 //
8451 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008452 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008453 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008454 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008455 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008456 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008457
Jim Grosbach11013ed2010-07-16 23:05:05 +00008458 if (VT != MVT::i32)
8459 return SDValue();
8460
Evan Cheng2e51bb42010-12-13 20:32:54 +00008461 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008462
Jim Grosbach11013ed2010-07-16 23:05:05 +00008463 // The value and the mask need to be constants so we can verify this is
8464 // actually a bitfield set. If the mask is 0xffff, we can do better
8465 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008466 SDValue MaskOp = N0.getOperand(1);
8467 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8468 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008469 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008470 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008471 if (Mask == 0xffff)
8472 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008473 SDValue Res;
8474 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008475 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8476 if (N1C) {
8477 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008478 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008479 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008480
Evan Cheng34345752010-12-11 04:11:38 +00008481 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008482 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008483
Evan Cheng2e51bb42010-12-13 20:32:54 +00008484 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Cheng34345752010-12-11 04:11:38 +00008485 DAG.getConstant(Val, MVT::i32),
8486 DAG.getConstant(Mask, MVT::i32));
8487
8488 // Do not add new nodes to DAG combiner worklist.
8489 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008490 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008491 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008492 } else if (N1.getOpcode() == ISD::AND) {
8493 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008494 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8495 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008496 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008497 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008498
Eric Christopherd5530962011-03-26 01:21:03 +00008499 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8500 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008501 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008502 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008503 // The pack halfword instruction works better for masks that fit it,
8504 // so use that when it's available.
8505 if (Subtarget->hasT2ExtractPack() &&
8506 (Mask == 0xffff || Mask == 0xffff0000))
8507 return SDValue();
8508 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008509 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008510 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopherd5530962011-03-26 01:21:03 +00008511 DAG.getConstant(amt, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008512 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008513 DAG.getConstant(Mask, MVT::i32));
8514 // Do not add new nodes to DAG combiner worklist.
8515 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008516 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008517 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008518 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008519 // The pack halfword instruction works better for masks that fit it,
8520 // so use that when it's available.
8521 if (Subtarget->hasT2ExtractPack() &&
8522 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8523 return SDValue();
8524 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008525 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008526 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008527 DAG.getConstant(lsb, MVT::i32));
8528 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopherd5530962011-03-26 01:21:03 +00008529 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008530 // Do not add new nodes to DAG combiner worklist.
8531 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008532 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008533 }
8534 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008535
Evan Cheng2e51bb42010-12-13 20:32:54 +00008536 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8537 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8538 ARM::isBitFieldInvertedMask(~Mask)) {
8539 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8540 // where lsb(mask) == #shamt and masked bits of B are known zero.
8541 SDValue ShAmt = N00.getOperand(1);
8542 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008543 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008544 if (ShAmtC != LSB)
8545 return SDValue();
8546
8547 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8548 DAG.getConstant(~Mask, MVT::i32));
8549
8550 // Do not add new nodes to DAG combiner worklist.
8551 DCI.CombineTo(N, Res, false);
8552 }
8553
Jim Grosbach11013ed2010-07-16 23:05:05 +00008554 return SDValue();
8555}
8556
Evan Chenge87681c2012-02-23 01:19:06 +00008557static SDValue PerformXORCombine(SDNode *N,
8558 TargetLowering::DAGCombinerInfo &DCI,
8559 const ARMSubtarget *Subtarget) {
8560 EVT VT = N->getValueType(0);
8561 SelectionDAG &DAG = DCI.DAG;
8562
8563 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8564 return SDValue();
8565
8566 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008567 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8568 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8569 if (Result.getNode())
8570 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008571 }
8572
8573 return SDValue();
8574}
8575
Evan Cheng6d02d902011-06-15 01:12:31 +00008576/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8577/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00008578static SDValue PerformBFICombine(SDNode *N,
8579 TargetLowering::DAGCombinerInfo &DCI) {
8580 SDValue N1 = N->getOperand(1);
8581 if (N1.getOpcode() == ISD::AND) {
8582 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8583 if (!N11C)
8584 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008585 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008586 unsigned LSB = countTrailingZeros(~InvMask);
8587 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Aaron Ballman0d6a0102014-12-16 14:04:11 +00008588 assert(Width <
8589 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +00008590 "undefined behavior");
8591 unsigned Mask = (1u << Width) - 1;
Evan Chengc1778132010-12-14 03:22:07 +00008592 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008593 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008594 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00008595 N->getOperand(0), N1.getOperand(0),
8596 N->getOperand(2));
8597 }
8598 return SDValue();
8599}
8600
Bob Wilson22806742010-09-22 22:09:21 +00008601/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8602/// ARMISD::VMOVRRD.
8603static SDValue PerformVMOVRRDCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008604 TargetLowering::DAGCombinerInfo &DCI,
8605 const ARMSubtarget *Subtarget) {
Bob Wilson22806742010-09-22 22:09:21 +00008606 // vmovrrd(vmovdrr x, y) -> x,y
8607 SDValue InDouble = N->getOperand(0);
Oliver Stannard51b1d462014-08-21 12:50:31 +00008608 if (InDouble.getOpcode() == ARMISD::VMOVDRR && !Subtarget->isFPOnlySP())
Bob Wilson22806742010-09-22 22:09:21 +00008609 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008610
8611 // vmovrrd(load f64) -> (load i32), (load i32)
8612 SDNode *InNode = InDouble.getNode();
8613 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8614 InNode->getValueType(0) == MVT::f64 &&
8615 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8616 !cast<LoadSDNode>(InNode)->isVolatile()) {
8617 // TODO: Should this be done for non-FrameIndex operands?
8618 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8619
8620 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008621 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008622 SDValue BasePtr = LD->getBasePtr();
8623 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8624 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008625 LD->isNonTemporal(), LD->isInvariant(),
8626 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008627
8628 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8629 DAG.getConstant(4, MVT::i32));
8630 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8631 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008632 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008633 std::min(4U, LD->getAlignment() / 2));
8634
8635 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Christian Pirker762b2c62014-06-01 09:30:52 +00008636 if (DCI.DAG.getTargetLoweringInfo().isBigEndian())
8637 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008638 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008639 return Result;
8640 }
8641
Bob Wilson22806742010-09-22 22:09:21 +00008642 return SDValue();
8643}
8644
8645/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8646/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8647static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8648 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8649 SDValue Op0 = N->getOperand(0);
8650 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00008651 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008652 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008653 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008654 Op1 = Op1.getOperand(0);
8655 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8656 Op0.getNode() == Op1.getNode() &&
8657 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008658 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00008659 N->getValueType(0), Op0.getOperand(0));
8660 return SDValue();
8661}
8662
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008663/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8664/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8665/// i64 vector to have f64 elements, since the value can then be loaded
8666/// directly into a VFP register.
8667static bool hasNormalLoadOperand(SDNode *N) {
8668 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8669 for (unsigned i = 0; i < NumElts; ++i) {
8670 SDNode *Elt = N->getOperand(i).getNode();
8671 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8672 return true;
8673 }
8674 return false;
8675}
8676
Bob Wilsoncb6db982010-09-17 22:59:05 +00008677/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8678/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008679static SDValue PerformBUILD_VECTORCombine(SDNode *N,
Oliver Stannard51b1d462014-08-21 12:50:31 +00008680 TargetLowering::DAGCombinerInfo &DCI,
8681 const ARMSubtarget *Subtarget) {
Bob Wilsoncb6db982010-09-17 22:59:05 +00008682 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8683 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8684 // into a pair of GPRs, which is fine when the value is used as a scalar,
8685 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008686 SelectionDAG &DAG = DCI.DAG;
8687 if (N->getNumOperands() == 2) {
8688 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8689 if (RV.getNode())
8690 return RV;
8691 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00008692
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008693 // Load i64 elements as f64 values so that type legalization does not split
8694 // them up into i32 values.
8695 EVT VT = N->getValueType(0);
8696 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8697 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008698 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008699 SmallVector<SDValue, 8> Ops;
8700 unsigned NumElts = VT.getVectorNumElements();
8701 for (unsigned i = 0; i < NumElts; ++i) {
8702 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8703 Ops.push_back(V);
8704 // Make the DAGCombiner fold the bitcast.
8705 DCI.AddToWorklist(V.getNode());
8706 }
8707 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00008708 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008709 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8710}
8711
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008712/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
8713static SDValue
8714PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8715 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
8716 // At that time, we may have inserted bitcasts from integer to float.
8717 // If these bitcasts have survived DAGCombine, change the lowering of this
8718 // BUILD_VECTOR in something more vector friendly, i.e., that does not
8719 // force to use floating point types.
8720
8721 // Make sure we can change the type of the vector.
8722 // This is possible iff:
8723 // 1. The vector is only used in a bitcast to a integer type. I.e.,
8724 // 1.1. Vector is used only once.
8725 // 1.2. Use is a bit convert to an integer type.
8726 // 2. The size of its operands are 32-bits (64-bits are not legal).
8727 EVT VT = N->getValueType(0);
8728 EVT EltVT = VT.getVectorElementType();
8729
8730 // Check 1.1. and 2.
8731 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
8732 return SDValue();
8733
8734 // By construction, the input type must be float.
8735 assert(EltVT == MVT::f32 && "Unexpected type!");
8736
8737 // Check 1.2.
8738 SDNode *Use = *N->use_begin();
8739 if (Use->getOpcode() != ISD::BITCAST ||
8740 Use->getValueType(0).isFloatingPoint())
8741 return SDValue();
8742
8743 // Check profitability.
8744 // Model is, if more than half of the relevant operands are bitcast from
8745 // i32, turn the build_vector into a sequence of insert_vector_elt.
8746 // Relevant operands are everything that is not statically
8747 // (i.e., at compile time) bitcasted.
8748 unsigned NumOfBitCastedElts = 0;
8749 unsigned NumElts = VT.getVectorNumElements();
8750 unsigned NumOfRelevantElts = NumElts;
8751 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
8752 SDValue Elt = N->getOperand(Idx);
8753 if (Elt->getOpcode() == ISD::BITCAST) {
8754 // Assume only bit cast to i32 will go away.
8755 if (Elt->getOperand(0).getValueType() == MVT::i32)
8756 ++NumOfBitCastedElts;
8757 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
8758 // Constants are statically casted, thus do not count them as
8759 // relevant operands.
8760 --NumOfRelevantElts;
8761 }
8762
8763 // Check if more than half of the elements require a non-free bitcast.
8764 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
8765 return SDValue();
8766
8767 SelectionDAG &DAG = DCI.DAG;
8768 // Create the new vector type.
8769 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
8770 // Check if the type is legal.
8771 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8772 if (!TLI.isTypeLegal(VecVT))
8773 return SDValue();
8774
8775 // Combine:
8776 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
8777 // => BITCAST INSERT_VECTOR_ELT
8778 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
8779 // (BITCAST EN), N.
8780 SDValue Vec = DAG.getUNDEF(VecVT);
8781 SDLoc dl(N);
8782 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
8783 SDValue V = N->getOperand(Idx);
8784 if (V.getOpcode() == ISD::UNDEF)
8785 continue;
8786 if (V.getOpcode() == ISD::BITCAST &&
8787 V->getOperand(0).getValueType() == MVT::i32)
8788 // Fold obvious case.
8789 V = V.getOperand(0);
8790 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00008791 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008792 // Make the DAGCombiner fold the bitcasts.
8793 DCI.AddToWorklist(V.getNode());
8794 }
8795 SDValue LaneIdx = DAG.getConstant(Idx, MVT::i32);
8796 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
8797 }
8798 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
8799 // Make the DAGCombiner fold the bitcasts.
8800 DCI.AddToWorklist(Vec.getNode());
8801 return Vec;
8802}
8803
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008804/// PerformInsertEltCombine - Target-specific dag combine xforms for
8805/// ISD::INSERT_VECTOR_ELT.
8806static SDValue PerformInsertEltCombine(SDNode *N,
8807 TargetLowering::DAGCombinerInfo &DCI) {
8808 // Bitcast an i64 load inserted into a vector to f64.
8809 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8810 EVT VT = N->getValueType(0);
8811 SDNode *Elt = N->getOperand(1).getNode();
8812 if (VT.getVectorElementType() != MVT::i64 ||
8813 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8814 return SDValue();
8815
8816 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008817 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008818 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8819 VT.getVectorNumElements());
8820 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8821 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8822 // Make the DAGCombiner fold the bitcasts.
8823 DCI.AddToWorklist(Vec.getNode());
8824 DCI.AddToWorklist(V.getNode());
8825 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8826 Vec, V, N->getOperand(2));
8827 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00008828}
8829
Bob Wilsonc7334a12010-10-27 20:38:28 +00008830/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8831/// ISD::VECTOR_SHUFFLE.
8832static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8833 // The LLVM shufflevector instruction does not require the shuffle mask
8834 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8835 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8836 // operands do not match the mask length, they are extended by concatenating
8837 // them with undef vectors. That is probably the right thing for other
8838 // targets, but for NEON it is better to concatenate two double-register
8839 // size vector operands into a single quad-register size vector. Do that
8840 // transformation here:
8841 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8842 // shuffle(concat(v1, v2), undef)
8843 SDValue Op0 = N->getOperand(0);
8844 SDValue Op1 = N->getOperand(1);
8845 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8846 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8847 Op0.getNumOperands() != 2 ||
8848 Op1.getNumOperands() != 2)
8849 return SDValue();
8850 SDValue Concat0Op1 = Op0.getOperand(1);
8851 SDValue Concat1Op1 = Op1.getOperand(1);
8852 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8853 Concat1Op1.getOpcode() != ISD::UNDEF)
8854 return SDValue();
8855 // Skip the transformation if any of the types are illegal.
8856 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8857 EVT VT = N->getValueType(0);
8858 if (!TLI.isTypeLegal(VT) ||
8859 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8860 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8861 return SDValue();
8862
Andrew Trickef9de2a2013-05-25 02:42:55 +00008863 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008864 Op0.getOperand(0), Op1.getOperand(0));
8865 // Translate the shuffle mask.
8866 SmallVector<int, 16> NewMask;
8867 unsigned NumElts = VT.getVectorNumElements();
8868 unsigned HalfElts = NumElts/2;
8869 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8870 for (unsigned n = 0; n < NumElts; ++n) {
8871 int MaskElt = SVN->getMaskElt(n);
8872 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00008873 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00008874 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00008875 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00008876 NewElt = HalfElts + MaskElt - NumElts;
8877 NewMask.push_back(NewElt);
8878 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00008879 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008880 DAG.getUNDEF(VT), NewMask.data());
8881}
8882
Ahmed Bougacha0cb86162014-12-13 23:22:12 +00008883/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP,
8884/// NEON load/store intrinsics, and generic vector load/stores, to merge
8885/// base address updates.
8886/// For generic load/stores, the memory type is assumed to be a vector.
Ahmed Bougachab31fba12014-12-09 21:30:00 +00008887/// The caller is assumed to have checked legality.
Bob Wilson06fce872011-02-07 17:43:21 +00008888static SDValue CombineBaseUpdate(SDNode *N,
8889 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson06fce872011-02-07 17:43:21 +00008890 SelectionDAG &DAG = DCI.DAG;
8891 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8892 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
Ahmed Bougacha0cb86162014-12-13 23:22:12 +00008893 bool isStore = N->getOpcode() == ISD::STORE;
8894 unsigned AddrOpIdx = ((isIntrinsic || isStore) ? 2 : 1);
Bob Wilson06fce872011-02-07 17:43:21 +00008895 SDValue Addr = N->getOperand(AddrOpIdx);
8896
8897 // Search for a use of the address operand that is an increment.
8898 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8899 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8900 SDNode *User = *UI;
8901 if (User->getOpcode() != ISD::ADD ||
8902 UI.getUse().getResNo() != Addr.getResNo())
8903 continue;
8904
8905 // Check that the add is independent of the load/store. Otherwise, folding
8906 // it would create a cycle.
8907 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8908 continue;
8909
8910 // Find the new opcode for the updating load/store.
8911 bool isLoad = true;
8912 bool isLaneOp = false;
8913 unsigned NewOpc = 0;
8914 unsigned NumVecs = 0;
8915 if (isIntrinsic) {
8916 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8917 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00008918 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008919 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8920 NumVecs = 1; break;
8921 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8922 NumVecs = 2; break;
8923 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8924 NumVecs = 3; break;
8925 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8926 NumVecs = 4; break;
8927 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8928 NumVecs = 2; isLaneOp = true; break;
8929 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8930 NumVecs = 3; isLaneOp = true; break;
8931 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8932 NumVecs = 4; isLaneOp = true; break;
8933 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8934 NumVecs = 1; isLoad = false; break;
8935 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8936 NumVecs = 2; isLoad = false; break;
8937 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8938 NumVecs = 3; isLoad = false; break;
8939 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8940 NumVecs = 4; isLoad = false; break;
8941 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8942 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8943 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8944 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8945 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8946 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8947 }
8948 } else {
8949 isLaneOp = true;
8950 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00008951 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008952 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8953 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8954 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
Ahmed Bougacha0cb86162014-12-13 23:22:12 +00008955 case ISD::LOAD: NewOpc = ARMISD::VLD1_UPD;
8956 NumVecs = 1; isLaneOp = false; break;
8957 case ISD::STORE: NewOpc = ARMISD::VST1_UPD;
8958 NumVecs = 1; isLoad = false; isLaneOp = false; break;
Bob Wilson06fce872011-02-07 17:43:21 +00008959 }
8960 }
8961
8962 // Find the size of memory referenced by the load/store.
8963 EVT VecTy;
8964 if (isLoad)
8965 VecTy = N->getValueType(0);
Ahmed Bougacha0cb86162014-12-13 23:22:12 +00008966 else if (isIntrinsic)
Renato Golindf8f9b62014-12-13 20:23:18 +00008967 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
Ahmed Bougacha0cb86162014-12-13 23:22:12 +00008968 else
8969 VecTy = N->getOperand(1).getValueType();
8970
Bob Wilson06fce872011-02-07 17:43:21 +00008971 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8972 if (isLaneOp)
8973 NumBytes /= VecTy.getVectorNumElements();
8974
8975 // If the increment is a constant, it must match the memory ref size.
8976 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8977 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8978 uint64_t IncVal = CInc->getZExtValue();
8979 if (IncVal != NumBytes)
8980 continue;
8981 } else if (NumBytes >= 3 * 16) {
8982 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8983 // separate instructions that make it harder to use a non-constant update.
8984 continue;
8985 }
8986
Ahmed Bougacha4553bff2014-12-23 06:07:31 +00008987 EVT AlignedVecTy = VecTy;
8988
8989 // If this is a less-than-standard-aligned load/store, change the type to
8990 // match the standard alignment.
8991 // The alignment is overlooked when selecting _UPD variants; and it's
8992 // easier to introduce bitcasts here than fix that.
8993 // There are 3 ways to get to this base-update combine:
8994 // - intrinsics: they are assumed to be properly aligned (to the standard
8995 // alignment of the memory type), so we don't need to do anything.
8996 // - ARMISD::VLDx nodes: they are only generated from the aforementioned
8997 // intrinsics, so, likewise, there's nothing to do.
8998 // - generic load/store instructions: the alignment is specified as an
8999 // explicit operand, rather than implicitly as the standard alignment
9000 // of the memory type (like the intrisics). We need to change the
9001 // memory type to match the explicit alignment. That way, we don't
9002 // generate non-standard-aligned ARMISD::VLDx nodes.
9003 if (LSBaseSDNode *LSN = dyn_cast<LSBaseSDNode>(N)) {
9004 unsigned Alignment = LSN->getAlignment();
9005 if (Alignment == 0)
9006 Alignment = 1;
9007 if (Alignment < VecTy.getScalarSizeInBits() / 8) {
9008 MVT EltTy = MVT::getIntegerVT(Alignment * 8);
9009 assert(NumVecs == 1 && "Unexpected multi-element generic load/store.");
9010 assert(!isLaneOp && "Unexpected generic load/store lane.");
9011 unsigned NumElts = NumBytes / (EltTy.getSizeInBits() / 8);
9012 AlignedVecTy = MVT::getVectorVT(EltTy, NumElts);
9013 }
9014 }
9015
Bob Wilson06fce872011-02-07 17:43:21 +00009016 // Create the new updating load/store node.
Ahmed Bougachab31fba12014-12-09 21:30:00 +00009017 // First, create an SDVTList for the new updating node's results.
Bob Wilson06fce872011-02-07 17:43:21 +00009018 EVT Tys[6];
9019 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
9020 unsigned n;
9021 for (n = 0; n < NumResultVecs; ++n)
Ahmed Bougacha4553bff2014-12-23 06:07:31 +00009022 Tys[n] = AlignedVecTy;
Bob Wilson06fce872011-02-07 17:43:21 +00009023 Tys[n++] = MVT::i32;
9024 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009025 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumResultVecs+2));
Ahmed Bougachab31fba12014-12-09 21:30:00 +00009026
9027 // Then, gather the new node's operands.
Bob Wilson06fce872011-02-07 17:43:21 +00009028 SmallVector<SDValue, 8> Ops;
9029 Ops.push_back(N->getOperand(0)); // incoming chain
9030 Ops.push_back(N->getOperand(AddrOpIdx));
9031 Ops.push_back(Inc);
Ahmed Bougacha0cb86162014-12-13 23:22:12 +00009032 if (StoreSDNode *StN = dyn_cast<StoreSDNode>(N)) {
9033 // Try to match the intrinsic's signature
9034 Ops.push_back(StN->getValue());
9035 Ops.push_back(DAG.getConstant(StN->getAlignment(), MVT::i32));
9036 } else {
9037 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i)
9038 Ops.push_back(N->getOperand(i));
Bob Wilson06fce872011-02-07 17:43:21 +00009039 }
Ahmed Bougacha4553bff2014-12-23 06:07:31 +00009040
9041 // If this is a non-standard-aligned store, the penultimate operand is the
9042 // stored value. Bitcast it to the aligned type.
9043 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::STORE) {
9044 SDValue &StVal = Ops[Ops.size()-2];
9045 StVal = DAG.getNode(ISD::BITCAST, SDLoc(N), AlignedVecTy, StVal);
9046 }
9047
Ahmed Bougachab31fba12014-12-09 21:30:00 +00009048 MemSDNode *MemInt = cast<MemSDNode>(N);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009049 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys,
Ahmed Bougacha4553bff2014-12-23 06:07:31 +00009050 Ops, AlignedVecTy,
Bob Wilson06fce872011-02-07 17:43:21 +00009051 MemInt->getMemOperand());
9052
9053 // Update the uses.
9054 std::vector<SDValue> NewResults;
9055 for (unsigned i = 0; i < NumResultVecs; ++i) {
9056 NewResults.push_back(SDValue(UpdN.getNode(), i));
9057 }
Ahmed Bougacha4553bff2014-12-23 06:07:31 +00009058
9059 // If this is an non-standard-aligned load, the first result is the loaded
9060 // value. Bitcast it to the expected result type.
9061 if (AlignedVecTy != VecTy && N->getOpcode() == ISD::LOAD) {
9062 SDValue &LdVal = NewResults[0];
9063 LdVal = DAG.getNode(ISD::BITCAST, SDLoc(N), VecTy, LdVal);
9064 }
9065
Bob Wilson06fce872011-02-07 17:43:21 +00009066 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
9067 DCI.CombineTo(N, NewResults);
9068 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
9069
9070 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00009071 }
Bob Wilson06fce872011-02-07 17:43:21 +00009072 return SDValue();
9073}
9074
Ahmed Bougachab31fba12014-12-09 21:30:00 +00009075static SDValue PerformVLDCombine(SDNode *N,
9076 TargetLowering::DAGCombinerInfo &DCI) {
9077 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9078 return SDValue();
9079
9080 return CombineBaseUpdate(N, DCI);
9081}
9082
Bob Wilson2d790df2010-11-28 06:51:26 +00009083/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
9084/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
9085/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
9086/// return true.
9087static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
9088 SelectionDAG &DAG = DCI.DAG;
9089 EVT VT = N->getValueType(0);
9090 // vldN-dup instructions only support 64-bit vectors for N > 1.
9091 if (!VT.is64BitVector())
9092 return false;
9093
9094 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
9095 SDNode *VLD = N->getOperand(0).getNode();
9096 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
9097 return false;
9098 unsigned NumVecs = 0;
9099 unsigned NewOpc = 0;
9100 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
9101 if (IntNo == Intrinsic::arm_neon_vld2lane) {
9102 NumVecs = 2;
9103 NewOpc = ARMISD::VLD2DUP;
9104 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
9105 NumVecs = 3;
9106 NewOpc = ARMISD::VLD3DUP;
9107 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
9108 NumVecs = 4;
9109 NewOpc = ARMISD::VLD4DUP;
9110 } else {
9111 return false;
9112 }
9113
9114 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
9115 // numbers match the load.
9116 unsigned VLDLaneNo =
9117 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
9118 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9119 UI != UE; ++UI) {
9120 // Ignore uses of the chain result.
9121 if (UI.getUse().getResNo() == NumVecs)
9122 continue;
9123 SDNode *User = *UI;
9124 if (User->getOpcode() != ARMISD::VDUPLANE ||
9125 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
9126 return false;
9127 }
9128
9129 // Create the vldN-dup node.
9130 EVT Tys[5];
9131 unsigned n;
9132 for (n = 0; n < NumVecs; ++n)
9133 Tys[n] = VT;
9134 Tys[n] = MVT::Other;
Craig Toppere1d12942014-08-27 05:25:25 +00009135 SDVTList SDTys = DAG.getVTList(makeArrayRef(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00009136 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
9137 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009138 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00009139 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00009140 VLDMemInt->getMemOperand());
9141
9142 // Update the uses.
9143 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
9144 UI != UE; ++UI) {
9145 unsigned ResNo = UI.getUse().getResNo();
9146 // Ignore uses of the chain result.
9147 if (ResNo == NumVecs)
9148 continue;
9149 SDNode *User = *UI;
9150 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
9151 }
9152
9153 // Now the vldN-lane intrinsic is dead except for its chain result.
9154 // Update uses of the chain.
9155 std::vector<SDValue> VLDDupResults;
9156 for (unsigned n = 0; n < NumVecs; ++n)
9157 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
9158 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
9159 DCI.CombineTo(VLD, VLDDupResults);
9160
9161 return true;
9162}
9163
Bob Wilson103a0dc2010-07-14 01:22:12 +00009164/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9165/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009166static SDValue PerformVDUPLANECombine(SDNode *N,
9167 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009168 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009169
Bob Wilson2d790df2010-11-28 06:51:26 +00009170 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9171 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9172 if (CombineVLDDUP(N, DCI))
9173 return SDValue(N, 0);
9174
9175 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9176 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009177 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009178 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009179 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009180 return SDValue();
9181
9182 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9183 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9184 // The canonical VMOV for a zero vector uses a 32-bit element size.
9185 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9186 unsigned EltBits;
9187 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9188 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00009189 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009190 if (EltSize > VT.getVectorElementType().getSizeInBits())
9191 return SDValue();
9192
Andrew Trickef9de2a2013-05-25 02:42:55 +00009193 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009194}
9195
Ahmed Bougacha0cb86162014-12-13 23:22:12 +00009196static SDValue PerformLOADCombine(SDNode *N,
9197 TargetLowering::DAGCombinerInfo &DCI) {
9198 EVT VT = N->getValueType(0);
9199
9200 // If this is a legal vector load, try to combine it into a VLD1_UPD.
9201 if (ISD::isNormalLoad(N) && VT.isVector() &&
9202 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9203 return CombineBaseUpdate(N, DCI);
9204
9205 return SDValue();
9206}
9207
Ahmed Bougacha23167462014-12-09 21:26:53 +00009208/// PerformSTORECombine - Target-specific dag combine xforms for
9209/// ISD::STORE.
9210static SDValue PerformSTORECombine(SDNode *N,
9211 TargetLowering::DAGCombinerInfo &DCI) {
9212 StoreSDNode *St = cast<StoreSDNode>(N);
9213 if (St->isVolatile())
9214 return SDValue();
9215
9216 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
9217 // pack all of the elements in one place. Next, store to memory in fewer
9218 // chunks.
9219 SDValue StVal = St->getValue();
9220 EVT VT = StVal.getValueType();
9221 if (St->isTruncatingStore() && VT.isVector()) {
9222 SelectionDAG &DAG = DCI.DAG;
9223 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9224 EVT StVT = St->getMemoryVT();
9225 unsigned NumElems = VT.getVectorNumElements();
9226 assert(StVT != VT && "Cannot truncate to the same type");
9227 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
9228 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
9229
9230 // From, To sizes and ElemCount must be pow of two
9231 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
9232
9233 // We are going to use the original vector elt for storing.
9234 // Accumulated smaller vector elements must be a multiple of the store size.
9235 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
9236
9237 unsigned SizeRatio = FromEltSz / ToEltSz;
9238 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
9239
9240 // Create a type on which we perform the shuffle.
9241 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
9242 NumElems*SizeRatio);
9243 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
9244
9245 SDLoc DL(St);
9246 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
9247 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
9248 for (unsigned i = 0; i < NumElems; ++i)
9249 ShuffleVec[i] = TLI.isBigEndian() ? (i+1) * SizeRatio - 1 : i * SizeRatio;
9250
9251 // Can't shuffle using an illegal type.
9252 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
9253
9254 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
9255 DAG.getUNDEF(WideVec.getValueType()),
9256 ShuffleVec.data());
9257 // At this point all of the data is stored at the bottom of the
9258 // register. We now need to save it to mem.
9259
9260 // Find the largest store unit
9261 MVT StoreType = MVT::i8;
Ahmed Bougacha67dd2d22015-01-07 21:27:10 +00009262 for (MVT Tp : MVT::integer_valuetypes()) {
Ahmed Bougacha23167462014-12-09 21:26:53 +00009263 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
9264 StoreType = Tp;
9265 }
9266 // Didn't find a legal store type.
9267 if (!TLI.isTypeLegal(StoreType))
9268 return SDValue();
9269
9270 // Bitcast the original vector into a vector of store-size units
9271 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
9272 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
9273 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
9274 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
9275 SmallVector<SDValue, 8> Chains;
9276 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
9277 TLI.getPointerTy());
9278 SDValue BasePtr = St->getBasePtr();
9279
9280 // Perform one or more big stores into memory.
9281 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
9282 for (unsigned I = 0; I < E; I++) {
9283 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
9284 StoreType, ShuffWide,
9285 DAG.getIntPtrConstant(I));
9286 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
9287 St->getPointerInfo(), St->isVolatile(),
9288 St->isNonTemporal(), St->getAlignment());
9289 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
9290 Increment);
9291 Chains.push_back(Ch);
9292 }
9293 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
9294 }
9295
9296 if (!ISD::isNormalStore(St))
9297 return SDValue();
9298
9299 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
9300 // ARM stores of arguments in the same cache line.
9301 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
9302 StVal.getNode()->hasOneUse()) {
9303 SelectionDAG &DAG = DCI.DAG;
9304 bool isBigEndian = DAG.getTargetLoweringInfo().isBigEndian();
9305 SDLoc DL(St);
9306 SDValue BasePtr = St->getBasePtr();
9307 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
9308 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
9309 BasePtr, St->getPointerInfo(), St->isVolatile(),
9310 St->isNonTemporal(), St->getAlignment());
9311
9312 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
9313 DAG.getConstant(4, MVT::i32));
9314 return DAG.getStore(NewST1.getValue(0), DL,
9315 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
9316 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
9317 St->isNonTemporal(),
9318 std::min(4U, St->getAlignment() / 2));
9319 }
9320
9321 if (StVal.getValueType() == MVT::i64 &&
9322 StVal.getNode()->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9323
9324 // Bitcast an i64 store extracted from a vector to f64.
9325 // Otherwise, the i64 value will be legalized to a pair of i32 values.
9326 SelectionDAG &DAG = DCI.DAG;
9327 SDLoc dl(StVal);
9328 SDValue IntVec = StVal.getOperand(0);
9329 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
9330 IntVec.getValueType().getVectorNumElements());
9331 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
9332 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
9333 Vec, StVal.getOperand(1));
9334 dl = SDLoc(N);
9335 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
9336 // Make the DAGCombiner fold the bitcasts.
9337 DCI.AddToWorklist(Vec.getNode());
9338 DCI.AddToWorklist(ExtElt.getNode());
9339 DCI.AddToWorklist(V.getNode());
9340 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
9341 St->getPointerInfo(), St->isVolatile(),
9342 St->isNonTemporal(), St->getAlignment(),
9343 St->getAAInfo());
9344 }
9345
Ahmed Bougacha0cb86162014-12-13 23:22:12 +00009346 // If this is a legal vector store, try to combine it into a VST1_UPD.
9347 if (ISD::isNormalStore(N) && VT.isVector() &&
9348 DCI.DAG.getTargetLoweringInfo().isTypeLegal(VT))
9349 return CombineBaseUpdate(N, DCI);
9350
Ahmed Bougacha23167462014-12-09 21:26:53 +00009351 return SDValue();
9352}
9353
Eric Christopher1b8b94192011-06-29 21:10:36 +00009354// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00009355// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
9356static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
9357{
Chad Rosier6b610b32011-06-28 17:26:57 +00009358 integerPart cN;
9359 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009360 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
9361 I != E; I++) {
9362 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
9363 if (!C)
9364 return false;
9365
Eric Christopher1b8b94192011-06-29 21:10:36 +00009366 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009367 APFloat APF = C->getValueAPF();
9368 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
9369 != APFloat::opOK || !isExact)
9370 return false;
9371
9372 c0 = (I == 0) ? cN : c0;
9373 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
9374 return false;
9375 }
9376 C = c0;
9377 return true;
9378}
9379
9380/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
9381/// can replace combinations of VMUL and VCVT (floating-point to integer)
9382/// when the VMUL has a constant operand that is a power of 2.
9383///
9384/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9385/// vmul.f32 d16, d17, d16
9386/// vcvt.s32.f32 d16, d16
9387/// becomes:
9388/// vcvt.s32.f32 d16, d16, #3
9389static SDValue PerformVCVTCombine(SDNode *N,
9390 TargetLowering::DAGCombinerInfo &DCI,
9391 const ARMSubtarget *Subtarget) {
9392 SelectionDAG &DAG = DCI.DAG;
9393 SDValue Op = N->getOperand(0);
9394
9395 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
9396 Op.getOpcode() != ISD::FMUL)
9397 return SDValue();
9398
9399 uint64_t C;
9400 SDValue N0 = Op->getOperand(0);
9401 SDValue ConstVec = Op->getOperand(1);
9402 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
9403
Eric Christopher1b8b94192011-06-29 21:10:36 +00009404 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00009405 !isConstVecPow2(ConstVec, isSigned, C))
9406 return SDValue();
9407
Tim Northover7cbc2152013-06-28 15:29:25 +00009408 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
9409 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
Bradley Smithececb7f2014-12-16 10:59:27 +00009410 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9411 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32 ||
9412 NumLanes > 4) {
Tim Northover7cbc2152013-06-28 15:29:25 +00009413 // These instructions only exist converting from f32 to i32. We can handle
9414 // smaller integers by generating an extra truncate, but larger ones would
Bradley Smithececb7f2014-12-16 10:59:27 +00009415 // be lossy. We also can't handle more then 4 lanes, since these intructions
9416 // only support v2i32/v4i32 types.
Tim Northover7cbc2152013-06-28 15:29:25 +00009417 return SDValue();
9418 }
9419
Chad Rosierfa8d8932011-06-24 19:23:04 +00009420 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9421 Intrinsic::arm_neon_vcvtfp2fxu;
Tim Northover7cbc2152013-06-28 15:29:25 +00009422 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
9423 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9424 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
9425 DAG.getConstant(Log2_64(C), MVT::i32));
9426
9427 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9428 FixConv = DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), FixConv);
9429
9430 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009431}
9432
9433/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9434/// can replace combinations of VCVT (integer to floating-point) and VDIV
9435/// when the VDIV has a constant operand that is a power of 2.
9436///
9437/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9438/// vcvt.f32.s32 d16, d16
9439/// vdiv.f32 d16, d17, d16
9440/// becomes:
9441/// vcvt.f32.s32 d16, d16, #3
9442static SDValue PerformVDIVCombine(SDNode *N,
9443 TargetLowering::DAGCombinerInfo &DCI,
9444 const ARMSubtarget *Subtarget) {
9445 SelectionDAG &DAG = DCI.DAG;
9446 SDValue Op = N->getOperand(0);
9447 unsigned OpOpcode = Op.getNode()->getOpcode();
9448
9449 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
9450 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9451 return SDValue();
9452
9453 uint64_t C;
9454 SDValue ConstVec = N->getOperand(1);
9455 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
9456
9457 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9458 !isConstVecPow2(ConstVec, isSigned, C))
9459 return SDValue();
9460
Tim Northover7cbc2152013-06-28 15:29:25 +00009461 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
9462 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
9463 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9464 // These instructions only exist converting from i32 to f32. We can handle
9465 // smaller integers by generating an extra extend, but larger ones would
9466 // be lossy.
9467 return SDValue();
9468 }
9469
9470 SDValue ConvInput = Op.getOperand(0);
9471 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9472 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9473 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
9474 SDLoc(N), NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9475 ConvInput);
9476
Eric Christopher1b8b94192011-06-29 21:10:36 +00009477 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00009478 Intrinsic::arm_neon_vcvtfxu2fp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009479 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
Chad Rosierfa8d8932011-06-24 19:23:04 +00009480 Op.getValueType(),
Eric Christopher1b8b94192011-06-29 21:10:36 +00009481 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Tim Northover7cbc2152013-06-28 15:29:25 +00009482 ConvInput, DAG.getConstant(Log2_64(C), MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00009483}
9484
9485/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00009486/// operand of a vector shift operation, where all the elements of the
9487/// build_vector must have the same constant integer value.
9488static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9489 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00009490 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00009491 Op = Op.getOperand(0);
9492 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9493 APInt SplatBits, SplatUndef;
9494 unsigned SplatBitSize;
9495 bool HasAnyUndefs;
9496 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9497 HasAnyUndefs, ElementBits) ||
9498 SplatBitSize > ElementBits)
9499 return false;
9500 Cnt = SplatBits.getSExtValue();
9501 return true;
9502}
9503
9504/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9505/// operand of a vector shift left operation. That value must be in the range:
9506/// 0 <= Value < ElementBits for a left shift; or
9507/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009508static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009509 assert(VT.isVector() && "vector shift count is not a vector type");
9510 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9511 if (! getVShiftImm(Op, ElementBits, Cnt))
9512 return false;
9513 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9514}
9515
9516/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9517/// operand of a vector shift right operation. For a shift opcode, the value
9518/// is positive, but for an intrinsic the value count must be negative. The
9519/// absolute value must be in the range:
9520/// 1 <= |Value| <= ElementBits for a right shift; or
9521/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009522static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00009523 int64_t &Cnt) {
9524 assert(VT.isVector() && "vector shift count is not a vector type");
9525 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9526 if (! getVShiftImm(Op, ElementBits, Cnt))
9527 return false;
9528 if (isIntrinsic)
9529 Cnt = -Cnt;
9530 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9531}
9532
9533/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9534static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9535 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9536 switch (IntNo) {
9537 default:
9538 // Don't do anything for most intrinsics.
9539 break;
9540
9541 // Vector shifts: check for immediate versions and lower them.
9542 // Note: This is done during DAG combining instead of DAG legalizing because
9543 // the build_vectors for 64-bit vector element shift counts are generally
9544 // not legal, and it is hard to see their values after they get legalized to
9545 // loads from a constant pool.
9546 case Intrinsic::arm_neon_vshifts:
9547 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009548 case Intrinsic::arm_neon_vrshifts:
9549 case Intrinsic::arm_neon_vrshiftu:
9550 case Intrinsic::arm_neon_vrshiftn:
9551 case Intrinsic::arm_neon_vqshifts:
9552 case Intrinsic::arm_neon_vqshiftu:
9553 case Intrinsic::arm_neon_vqshiftsu:
9554 case Intrinsic::arm_neon_vqshiftns:
9555 case Intrinsic::arm_neon_vqshiftnu:
9556 case Intrinsic::arm_neon_vqshiftnsu:
9557 case Intrinsic::arm_neon_vqrshiftns:
9558 case Intrinsic::arm_neon_vqrshiftnu:
9559 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009560 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009561 int64_t Cnt;
9562 unsigned VShiftOpc = 0;
9563
9564 switch (IntNo) {
9565 case Intrinsic::arm_neon_vshifts:
9566 case Intrinsic::arm_neon_vshiftu:
9567 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9568 VShiftOpc = ARMISD::VSHL;
9569 break;
9570 }
9571 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9572 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9573 ARMISD::VSHRs : ARMISD::VSHRu);
9574 break;
9575 }
9576 return SDValue();
9577
Bob Wilson2e076c42009-06-22 23:27:02 +00009578 case Intrinsic::arm_neon_vrshifts:
9579 case Intrinsic::arm_neon_vrshiftu:
9580 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9581 break;
9582 return SDValue();
9583
9584 case Intrinsic::arm_neon_vqshifts:
9585 case Intrinsic::arm_neon_vqshiftu:
9586 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9587 break;
9588 return SDValue();
9589
9590 case Intrinsic::arm_neon_vqshiftsu:
9591 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9592 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +00009593 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009594
Bob Wilson2e076c42009-06-22 23:27:02 +00009595 case Intrinsic::arm_neon_vrshiftn:
9596 case Intrinsic::arm_neon_vqshiftns:
9597 case Intrinsic::arm_neon_vqshiftnu:
9598 case Intrinsic::arm_neon_vqshiftnsu:
9599 case Intrinsic::arm_neon_vqrshiftns:
9600 case Intrinsic::arm_neon_vqrshiftnu:
9601 case Intrinsic::arm_neon_vqrshiftnsu:
9602 // Narrowing shifts require an immediate right shift.
9603 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9604 break;
Jim Grosbach84511e12010-06-02 21:53:11 +00009605 llvm_unreachable("invalid shift count for narrowing vector shift "
9606 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009607
9608 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00009609 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00009610 }
9611
9612 switch (IntNo) {
9613 case Intrinsic::arm_neon_vshifts:
9614 case Intrinsic::arm_neon_vshiftu:
9615 // Opcode already set above.
9616 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00009617 case Intrinsic::arm_neon_vrshifts:
9618 VShiftOpc = ARMISD::VRSHRs; break;
9619 case Intrinsic::arm_neon_vrshiftu:
9620 VShiftOpc = ARMISD::VRSHRu; break;
9621 case Intrinsic::arm_neon_vrshiftn:
9622 VShiftOpc = ARMISD::VRSHRN; break;
9623 case Intrinsic::arm_neon_vqshifts:
9624 VShiftOpc = ARMISD::VQSHLs; break;
9625 case Intrinsic::arm_neon_vqshiftu:
9626 VShiftOpc = ARMISD::VQSHLu; break;
9627 case Intrinsic::arm_neon_vqshiftsu:
9628 VShiftOpc = ARMISD::VQSHLsu; break;
9629 case Intrinsic::arm_neon_vqshiftns:
9630 VShiftOpc = ARMISD::VQSHRNs; break;
9631 case Intrinsic::arm_neon_vqshiftnu:
9632 VShiftOpc = ARMISD::VQSHRNu; break;
9633 case Intrinsic::arm_neon_vqshiftnsu:
9634 VShiftOpc = ARMISD::VQSHRNsu; break;
9635 case Intrinsic::arm_neon_vqrshiftns:
9636 VShiftOpc = ARMISD::VQRSHRNs; break;
9637 case Intrinsic::arm_neon_vqrshiftnu:
9638 VShiftOpc = ARMISD::VQRSHRNu; break;
9639 case Intrinsic::arm_neon_vqrshiftnsu:
9640 VShiftOpc = ARMISD::VQRSHRNsu; break;
9641 }
9642
Andrew Trickef9de2a2013-05-25 02:42:55 +00009643 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009644 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009645 }
9646
9647 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009648 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009649 int64_t Cnt;
9650 unsigned VShiftOpc = 0;
9651
9652 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9653 VShiftOpc = ARMISD::VSLI;
9654 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9655 VShiftOpc = ARMISD::VSRI;
9656 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009657 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009658 }
9659
Andrew Trickef9de2a2013-05-25 02:42:55 +00009660 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +00009661 N->getOperand(1), N->getOperand(2),
Owen Anderson9f944592009-08-11 20:47:22 +00009662 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009663 }
9664
9665 case Intrinsic::arm_neon_vqrshifts:
9666 case Intrinsic::arm_neon_vqrshiftu:
9667 // No immediate versions of these to check for.
9668 break;
9669 }
9670
9671 return SDValue();
9672}
9673
9674/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9675/// lowers them. As with the vector shift intrinsics, this is done during DAG
9676/// combining instead of DAG legalizing because the build_vectors for 64-bit
9677/// vector element shift counts are generally not legal, and it is hard to see
9678/// their values after they get legalized to loads from a constant pool.
9679static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9680 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009681 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +00009682 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9683 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9684 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9685 SDValue N1 = N->getOperand(1);
9686 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9687 SDValue N0 = N->getOperand(0);
9688 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9689 DAG.MaskedValueIsZero(N0.getOperand(0),
9690 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009691 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +00009692 }
9693 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009694
9695 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +00009696 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9697 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +00009698 return SDValue();
9699
9700 assert(ST->hasNEON() && "unexpected vector shift");
9701 int64_t Cnt;
9702
9703 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009704 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009705
9706 case ISD::SHL:
9707 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009708 return DAG.getNode(ARMISD::VSHL, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009709 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009710 break;
9711
9712 case ISD::SRA:
9713 case ISD::SRL:
9714 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9715 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9716 ARMISD::VSHRs : ARMISD::VSHRu);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009717 return DAG.getNode(VShiftOpc, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009718 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009719 }
9720 }
9721 return SDValue();
9722}
9723
9724/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9725/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9726static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9727 const ARMSubtarget *ST) {
9728 SDValue N0 = N->getOperand(0);
9729
9730 // Check for sign- and zero-extensions of vector extract operations of 8-
9731 // and 16-bit vector elements. NEON supports these directly. They are
9732 // handled during DAG combining because type legalization will promote them
9733 // to 32-bit types and it is messy to recognize the operations after that.
9734 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9735 SDValue Vec = N0.getOperand(0);
9736 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009737 EVT VT = N->getValueType(0);
9738 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009739 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9740
Owen Anderson9f944592009-08-11 20:47:22 +00009741 if (VT == MVT::i32 &&
9742 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +00009743 TLI.isTypeLegal(Vec.getValueType()) &&
9744 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009745
9746 unsigned Opc = 0;
9747 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009748 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009749 case ISD::SIGN_EXTEND:
9750 Opc = ARMISD::VGETLANEs;
9751 break;
9752 case ISD::ZERO_EXTEND:
9753 case ISD::ANY_EXTEND:
9754 Opc = ARMISD::VGETLANEu;
9755 break;
9756 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009757 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +00009758 }
9759 }
9760
9761 return SDValue();
9762}
9763
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009764/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9765/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9766static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9767 const ARMSubtarget *ST) {
9768 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng55f0c6b2010-07-15 22:07:12 +00009769 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009770 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9771 // a NaN; only do the transformation when it matches that behavior.
9772
9773 // For now only do this when using NEON for FP operations; if using VFP, it
9774 // is not obvious that the benefit outweighs the cost of switching to the
9775 // NEON pipeline.
9776 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9777 N->getValueType(0) != MVT::f32)
9778 return SDValue();
9779
9780 SDValue CondLHS = N->getOperand(0);
9781 SDValue CondRHS = N->getOperand(1);
9782 SDValue LHS = N->getOperand(2);
9783 SDValue RHS = N->getOperand(3);
9784 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9785
9786 unsigned Opcode = 0;
9787 bool IsReversed;
Bob Wilsonba8ac742010-02-24 22:15:53 +00009788 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009789 IsReversed = false; // x CC y ? x : y
Bob Wilsonba8ac742010-02-24 22:15:53 +00009790 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009791 IsReversed = true ; // x CC y ? y : x
9792 } else {
9793 return SDValue();
9794 }
9795
Bob Wilsonba8ac742010-02-24 22:15:53 +00009796 bool IsUnordered;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009797 switch (CC) {
9798 default: break;
9799 case ISD::SETOLT:
9800 case ISD::SETOLE:
9801 case ISD::SETLT:
9802 case ISD::SETLE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009803 case ISD::SETULT:
9804 case ISD::SETULE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009805 // If LHS is NaN, an ordered comparison will be false and the result will
9806 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9807 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9808 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9809 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9810 break;
9811 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9812 // will return -0, so vmin can only be used for unsafe math or if one of
9813 // the operands is known to be nonzero.
9814 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009815 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009816 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9817 break;
9818 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009819 break;
9820
9821 case ISD::SETOGT:
9822 case ISD::SETOGE:
9823 case ISD::SETGT:
9824 case ISD::SETGE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009825 case ISD::SETUGT:
9826 case ISD::SETUGE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009827 // If LHS is NaN, an ordered comparison will be false and the result will
9828 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9829 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9830 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9831 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9832 break;
9833 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9834 // will return +0, so vmax can only be used for unsafe math or if one of
9835 // the operands is known to be nonzero.
9836 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009837 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009838 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9839 break;
9840 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009841 break;
9842 }
9843
9844 if (!Opcode)
9845 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009846 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009847}
9848
Evan Chengf863e3f2011-07-13 00:42:17 +00009849/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9850SDValue
9851ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9852 SDValue Cmp = N->getOperand(4);
9853 if (Cmp.getOpcode() != ARMISD::CMPZ)
9854 // Only looking at EQ and NE cases.
9855 return SDValue();
9856
9857 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009858 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +00009859 SDValue LHS = Cmp.getOperand(0);
9860 SDValue RHS = Cmp.getOperand(1);
9861 SDValue FalseVal = N->getOperand(0);
9862 SDValue TrueVal = N->getOperand(1);
9863 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +00009864 ARMCC::CondCodes CC =
9865 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +00009866
9867 // Simplify
9868 // mov r1, r0
9869 // cmp r1, x
9870 // mov r0, y
9871 // moveq r0, x
9872 // to
9873 // cmp r0, x
9874 // movne r0, y
9875 //
9876 // mov r1, r0
9877 // cmp r1, x
9878 // mov r0, x
9879 // movne r0, y
9880 // to
9881 // cmp r0, x
9882 // movne r0, y
9883 /// FIXME: Turn this into a target neutral optimization?
9884 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +00009885 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +00009886 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9887 N->getOperand(3), Cmp);
9888 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9889 SDValue ARMcc;
9890 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9891 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9892 N->getOperand(3), NewCmp);
9893 }
9894
9895 if (Res.getNode()) {
9896 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00009897 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +00009898 // Capture demanded bits information that would be otherwise lost.
9899 if (KnownZero == 0xfffffffe)
9900 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9901 DAG.getValueType(MVT::i1));
9902 else if (KnownZero == 0xffffff00)
9903 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9904 DAG.getValueType(MVT::i8));
9905 else if (KnownZero == 0xffff0000)
9906 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9907 DAG.getValueType(MVT::i16));
9908 }
9909
9910 return Res;
9911}
9912
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009913SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +00009914 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009915 switch (N->getOpcode()) {
9916 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009917 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +00009918 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009919 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009920 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009921 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +00009922 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9923 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +00009924 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009925 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI, Subtarget);
Bob Wilson22806742010-09-22 22:09:21 +00009926 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009927 case ISD::STORE: return PerformSTORECombine(N, DCI);
Oliver Stannard51b1d462014-08-21 12:50:31 +00009928 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI, Subtarget);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009929 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +00009930 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +00009931 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009932 case ISD::FP_TO_SINT:
9933 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9934 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009935 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00009936 case ISD::SHL:
9937 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009938 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00009939 case ISD::SIGN_EXTEND:
9940 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009941 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9942 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +00009943 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Ahmed Bougacha0cb86162014-12-13 23:22:12 +00009944 case ISD::LOAD: return PerformLOADCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009945 case ARMISD::VLD2DUP:
9946 case ARMISD::VLD3DUP:
9947 case ARMISD::VLD4DUP:
Ahmed Bougachab31fba12014-12-09 21:30:00 +00009948 return PerformVLDCombine(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009949 case ARMISD::BUILD_VECTOR:
9950 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009951 case ISD::INTRINSIC_VOID:
9952 case ISD::INTRINSIC_W_CHAIN:
9953 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9954 case Intrinsic::arm_neon_vld1:
9955 case Intrinsic::arm_neon_vld2:
9956 case Intrinsic::arm_neon_vld3:
9957 case Intrinsic::arm_neon_vld4:
9958 case Intrinsic::arm_neon_vld2lane:
9959 case Intrinsic::arm_neon_vld3lane:
9960 case Intrinsic::arm_neon_vld4lane:
9961 case Intrinsic::arm_neon_vst1:
9962 case Intrinsic::arm_neon_vst2:
9963 case Intrinsic::arm_neon_vst3:
9964 case Intrinsic::arm_neon_vst4:
9965 case Intrinsic::arm_neon_vst2lane:
9966 case Intrinsic::arm_neon_vst3lane:
9967 case Intrinsic::arm_neon_vst4lane:
Ahmed Bougachab31fba12014-12-09 21:30:00 +00009968 return PerformVLDCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009969 default: break;
9970 }
9971 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009972 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009973 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009974}
9975
Evan Chengd42641c2011-02-02 01:06:55 +00009976bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9977 EVT VT) const {
9978 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9979}
9980
Matt Arsenault6f2a5262014-07-27 17:46:40 +00009981bool ARMTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
9982 unsigned,
9983 unsigned,
9984 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009985 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +00009986 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009987
9988 switch (VT.getSimpleVT().SimpleTy) {
9989 default:
9990 return false;
9991 case MVT::i8:
9992 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009993 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009994 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +00009995 if (AllowsUnaligned) {
9996 if (Fast)
9997 *Fast = Subtarget->hasV7Ops();
9998 return true;
9999 }
10000 return false;
10001 }
Evan Chengeec6bc62012-08-15 17:44:53 +000010002 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +000010003 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +000010004 // For any little-endian targets with neon, we can support unaligned ld/st
10005 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +000010006 // A big-endian target may also explicitly support unaligned accesses
Evan Cheng79e2ca92012-12-10 23:21:26 +000010007 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
10008 if (Fast)
10009 *Fast = true;
10010 return true;
10011 }
10012 return false;
10013 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +000010014 }
10015}
10016
Lang Hames9929c422011-11-02 22:52:45 +000010017static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
10018 unsigned AlignCheck) {
10019 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
10020 (DstAlign == 0 || DstAlign % AlignCheck == 0));
10021}
10022
10023EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
10024 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +000010025 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +000010026 bool MemcpyStrSrc,
10027 MachineFunction &MF) const {
10028 const Function *F = MF.getFunction();
10029
10030 // See if we can use NEON instructions for this...
Evan Cheng962711e2012-12-12 02:34:41 +000010031 if ((!IsMemset || ZeroMemset) &&
Evan Cheng79e2ca92012-12-10 23:21:26 +000010032 Subtarget->hasNEON() &&
Bill Wendling698e84f2012-12-30 10:32:01 +000010033 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
10034 Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010035 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +000010036 if (Size >= 16 &&
10037 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010038 (allowsMisalignedMemoryAccesses(MVT::v2f64, 0, 1, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010039 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +000010040 } else if (Size >= 8 &&
10041 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault6f2a5262014-07-27 17:46:40 +000010042 (allowsMisalignedMemoryAccesses(MVT::f64, 0, 1, &Fast) &&
10043 Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +000010044 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +000010045 }
10046 }
10047
Lang Hamesb85fcd02011-11-08 18:56:23 +000010048 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +000010049 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010050 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +000010051 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +000010052 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +000010053
Lang Hames9929c422011-11-02 22:52:45 +000010054 // Let the target-independent logic figure it out.
10055 return MVT::Other;
10056}
10057
Evan Cheng9ec512d2012-12-06 19:13:27 +000010058bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
10059 if (Val.getOpcode() != ISD::LOAD)
10060 return false;
10061
10062 EVT VT1 = Val.getValueType();
10063 if (!VT1.isSimple() || !VT1.isInteger() ||
10064 !VT2.isSimple() || !VT2.isInteger())
10065 return false;
10066
10067 switch (VT1.getSimpleVT().SimpleTy) {
10068 default: break;
10069 case MVT::i1:
10070 case MVT::i8:
10071 case MVT::i16:
10072 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
10073 return true;
10074 }
10075
10076 return false;
10077}
10078
Tim Northovercc2e9032013-08-06 13:58:03 +000010079bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
10080 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
10081 return false;
10082
10083 if (!isTypeLegal(EVT::getEVT(Ty1)))
10084 return false;
10085
10086 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
10087
10088 // Assuming the caller doesn't have a zeroext or signext return parameter,
10089 // truncation all the way down to i1 is valid.
10090 return true;
10091}
10092
10093
Evan Chengdc49a8d2009-08-14 20:09:37 +000010094static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
10095 if (V < 0)
10096 return false;
10097
10098 unsigned Scale = 1;
10099 switch (VT.getSimpleVT().SimpleTy) {
10100 default: return false;
10101 case MVT::i1:
10102 case MVT::i8:
10103 // Scale == 1;
10104 break;
10105 case MVT::i16:
10106 // Scale == 2;
10107 Scale = 2;
10108 break;
10109 case MVT::i32:
10110 // Scale == 4;
10111 Scale = 4;
10112 break;
10113 }
10114
10115 if ((V & (Scale - 1)) != 0)
10116 return false;
10117 V /= Scale;
10118 return V == (V & ((1LL << 5) - 1));
10119}
10120
10121static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
10122 const ARMSubtarget *Subtarget) {
10123 bool isNeg = false;
10124 if (V < 0) {
10125 isNeg = true;
10126 V = - V;
10127 }
10128
10129 switch (VT.getSimpleVT().SimpleTy) {
10130 default: return false;
10131 case MVT::i1:
10132 case MVT::i8:
10133 case MVT::i16:
10134 case MVT::i32:
10135 // + imm12 or - imm8
10136 if (isNeg)
10137 return V == (V & ((1LL << 8) - 1));
10138 return V == (V & ((1LL << 12) - 1));
10139 case MVT::f32:
10140 case MVT::f64:
10141 // Same as ARM mode. FIXME: NEON?
10142 if (!Subtarget->hasVFP2())
10143 return false;
10144 if ((V & 3) != 0)
10145 return false;
10146 V >>= 2;
10147 return V == (V & ((1LL << 8) - 1));
10148 }
10149}
10150
Evan Cheng2150b922007-03-12 23:30:29 +000010151/// isLegalAddressImmediate - Return true if the integer value can be used
10152/// as the offset of the target addressing mode for load / store of the
10153/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010154static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010155 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +000010156 if (V == 0)
10157 return true;
10158
Evan Chengce5dfb62009-03-09 19:15:00 +000010159 if (!VT.isSimple())
10160 return false;
10161
Evan Chengdc49a8d2009-08-14 20:09:37 +000010162 if (Subtarget->isThumb1Only())
10163 return isLegalT1AddressImmediate(V, VT);
10164 else if (Subtarget->isThumb2())
10165 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +000010166
Evan Chengdc49a8d2009-08-14 20:09:37 +000010167 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +000010168 if (V < 0)
10169 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +000010170 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +000010171 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010172 case MVT::i1:
10173 case MVT::i8:
10174 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +000010175 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010176 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010177 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +000010178 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010179 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +000010180 case MVT::f32:
10181 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010182 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +000010183 return false;
Evan Chengbef131de2007-05-03 02:00:18 +000010184 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +000010185 return false;
10186 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +000010187 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +000010188 }
Evan Cheng10043e22007-01-19 07:51:42 +000010189}
10190
Evan Chengdc49a8d2009-08-14 20:09:37 +000010191bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
10192 EVT VT) const {
10193 int Scale = AM.Scale;
10194 if (Scale < 0)
10195 return false;
10196
10197 switch (VT.getSimpleVT().SimpleTy) {
10198 default: return false;
10199 case MVT::i1:
10200 case MVT::i8:
10201 case MVT::i16:
10202 case MVT::i32:
10203 if (Scale == 1)
10204 return true;
10205 // r + r << imm
10206 Scale = Scale & ~1;
10207 return Scale == 2 || Scale == 4 || Scale == 8;
10208 case MVT::i64:
10209 // r + r
10210 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
10211 return true;
10212 return false;
10213 case MVT::isVoid:
10214 // Note, we allow "void" uses (basically, uses that aren't loads or
10215 // stores), because arm allows folding a scale into many arithmetic
10216 // operations. This should be made more precise and revisited later.
10217
10218 // Allow r << imm, but the imm has to be a multiple of two.
10219 if (Scale & 1) return false;
10220 return isPowerOf2_32(Scale);
10221 }
10222}
10223
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010224/// isLegalAddressingMode - Return true if the addressing mode represented
10225/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson7117a912009-03-20 22:42:55 +000010226bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +000010227 Type *Ty) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010228 EVT VT = getValueType(Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +000010229 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +000010230 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010231
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010232 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +000010233 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010234 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010235
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010236 switch (AM.Scale) {
10237 case 0: // no scale reg, must be "r+i" or "r", or "i".
10238 break;
10239 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010240 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010241 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +000010242 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010243 default:
Chris Lattner502c3f42007-04-13 06:50:55 +000010244 // ARM doesn't support any R+R*scale+imm addr modes.
10245 if (AM.BaseOffs)
10246 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010247
Bob Wilson866c1742009-04-08 17:55:28 +000010248 if (!VT.isSimple())
10249 return false;
10250
Evan Chengdc49a8d2009-08-14 20:09:37 +000010251 if (Subtarget->isThumb2())
10252 return isLegalT2ScaledAddressingMode(AM, VT);
10253
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010254 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +000010255 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010256 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +000010257 case MVT::i1:
10258 case MVT::i8:
10259 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010260 if (Scale < 0) Scale = -Scale;
10261 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010262 return true;
10263 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +000010264 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +000010265 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +000010266 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010267 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +000010268 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010269 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +000010270 return false;
Bob Wilson7117a912009-03-20 22:42:55 +000010271
Owen Anderson9f944592009-08-11 20:47:22 +000010272 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010273 // Note, we allow "void" uses (basically, uses that aren't loads or
10274 // stores), because arm allows folding a scale into many arithmetic
10275 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +000010276
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010277 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +000010278 if (Scale & 1) return false;
10279 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010280 }
Evan Cheng2150b922007-03-12 23:30:29 +000010281 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +000010282 return true;
Evan Cheng2150b922007-03-12 23:30:29 +000010283}
10284
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010285/// isLegalICmpImmediate - Return true if the specified immediate is legal
10286/// icmp immediate, that is the target has icmp instructions which can compare
10287/// a register against the immediate without having to materialize the
10288/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +000010289bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010290 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010291 if (!Subtarget->isThumb())
Chandler Carruth8a102c22012-04-06 20:10:52 +000010292 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010293 if (Subtarget->isThumb2())
Chandler Carruth8a102c22012-04-06 20:10:52 +000010294 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +000010295 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +000010296 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +000010297}
10298
Andrew Tricka22cdb72012-07-18 18:34:27 +000010299/// isLegalAddImmediate - Return true if the specified immediate is a legal add
10300/// *or sub* immediate, that is the target has add or sub instructions which can
10301/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +000010302/// immediate into a register.
10303bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +000010304 // Same encoding for add/sub, just flip the sign.
10305 int64_t AbsImm = llvm::abs64(Imm);
10306 if (!Subtarget->isThumb())
10307 return ARM_AM::getSOImmVal(AbsImm) != -1;
10308 if (Subtarget->isThumb2())
10309 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
10310 // Thumb1 only has 8-bit unsigned immediate.
10311 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +000010312}
10313
Owen Anderson53aa7a92009-08-10 22:56:29 +000010314static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010315 bool isSEXTLoad, SDValue &Base,
10316 SDValue &Offset, bool &isInc,
10317 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +000010318 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10319 return false;
10320
Owen Anderson9f944592009-08-11 20:47:22 +000010321 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +000010322 // AddressingMode 3
10323 Base = Ptr->getOperand(0);
10324 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010325 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010326 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010327 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010328 isInc = false;
10329 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10330 return true;
10331 }
10332 }
10333 isInc = (Ptr->getOpcode() == ISD::ADD);
10334 Offset = Ptr->getOperand(1);
10335 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000010336 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000010337 // AddressingMode 2
10338 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010339 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010340 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010341 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010342 isInc = false;
10343 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10344 Base = Ptr->getOperand(0);
10345 return true;
10346 }
10347 }
10348
10349 if (Ptr->getOpcode() == ISD::ADD) {
10350 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000010351 ARM_AM::ShiftOpc ShOpcVal=
10352 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000010353 if (ShOpcVal != ARM_AM::no_shift) {
10354 Base = Ptr->getOperand(1);
10355 Offset = Ptr->getOperand(0);
10356 } else {
10357 Base = Ptr->getOperand(0);
10358 Offset = Ptr->getOperand(1);
10359 }
10360 return true;
10361 }
10362
10363 isInc = (Ptr->getOpcode() == ISD::ADD);
10364 Base = Ptr->getOperand(0);
10365 Offset = Ptr->getOperand(1);
10366 return true;
10367 }
10368
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000010369 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000010370 return false;
10371}
10372
Owen Anderson53aa7a92009-08-10 22:56:29 +000010373static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010374 bool isSEXTLoad, SDValue &Base,
10375 SDValue &Offset, bool &isInc,
10376 SelectionDAG &DAG) {
10377 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10378 return false;
10379
10380 Base = Ptr->getOperand(0);
10381 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
10382 int RHSC = (int)RHS->getZExtValue();
10383 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
10384 assert(Ptr->getOpcode() == ISD::ADD);
10385 isInc = false;
10386 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10387 return true;
10388 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
10389 isInc = Ptr->getOpcode() == ISD::ADD;
10390 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
10391 return true;
10392 }
10393 }
10394
10395 return false;
10396}
10397
Evan Cheng10043e22007-01-19 07:51:42 +000010398/// getPreIndexedAddressParts - returns true by value, base pointer and
10399/// offset pointer and addressing mode by reference if the node's address
10400/// can be legally represented as pre-indexed load / store address.
10401bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010402ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
10403 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010404 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010405 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010406 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010407 return false;
10408
Owen Anderson53aa7a92009-08-10 22:56:29 +000010409 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010410 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010411 bool isSEXTLoad = false;
10412 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10413 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010414 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010415 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10416 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10417 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010418 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010419 } else
10420 return false;
10421
10422 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010423 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010424 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010425 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10426 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010427 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010428 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000010429 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000010430 if (!isLegal)
10431 return false;
10432
10433 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10434 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010435}
10436
10437/// getPostIndexedAddressParts - returns true by value, base pointer and
10438/// offset pointer and addressing mode by reference if this node can be
10439/// combined with a load / store to form a post-indexed load / store.
10440bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010441 SDValue &Base,
10442 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010443 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010444 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010445 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010446 return false;
10447
Owen Anderson53aa7a92009-08-10 22:56:29 +000010448 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010449 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010450 bool isSEXTLoad = false;
10451 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010452 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010453 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010454 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10455 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010456 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010457 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010458 } else
10459 return false;
10460
10461 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010462 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010463 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010464 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000010465 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010466 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010467 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10468 isInc, DAG);
10469 if (!isLegal)
10470 return false;
10471
Evan Chengf19384d2010-05-18 21:31:17 +000010472 if (Ptr != Base) {
10473 // Swap base ptr and offset to catch more post-index load / store when
10474 // it's legal. In Thumb2 mode, offset must be an immediate.
10475 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10476 !Subtarget->isThumb2())
10477 std::swap(Base, Offset);
10478
10479 // Post-indexed load / store update the base pointer.
10480 if (Ptr != Base)
10481 return false;
10482 }
10483
Evan Cheng84c6cda2009-07-02 07:28:31 +000010484 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10485 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010486}
10487
Jay Foada0653a32014-05-14 21:14:37 +000010488void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10489 APInt &KnownZero,
10490 APInt &KnownOne,
10491 const SelectionDAG &DAG,
10492 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000010493 unsigned BitWidth = KnownOne.getBitWidth();
10494 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000010495 switch (Op.getOpcode()) {
10496 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000010497 case ARMISD::ADDC:
10498 case ARMISD::ADDE:
10499 case ARMISD::SUBC:
10500 case ARMISD::SUBE:
10501 // These nodes' second result is a boolean
10502 if (Op.getResNo() == 0)
10503 break;
10504 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10505 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010506 case ARMISD::CMOV: {
10507 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000010508 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010509 if (KnownZero == 0 && KnownOne == 0) return;
10510
Dan Gohmanf990faf2008-02-13 00:35:47 +000010511 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000010512 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010513 KnownZero &= KnownZeroRHS;
10514 KnownOne &= KnownOneRHS;
10515 return;
10516 }
Tim Northover01b4aa92014-04-03 15:10:35 +000010517 case ISD::INTRINSIC_W_CHAIN: {
10518 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
10519 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
10520 switch (IntID) {
10521 default: return;
10522 case Intrinsic::arm_ldaex:
10523 case Intrinsic::arm_ldrex: {
10524 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
10525 unsigned MemBits = VT.getScalarType().getSizeInBits();
10526 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
10527 return;
10528 }
10529 }
10530 }
Evan Cheng10043e22007-01-19 07:51:42 +000010531 }
10532}
10533
10534//===----------------------------------------------------------------------===//
10535// ARM Inline Assembly Support
10536//===----------------------------------------------------------------------===//
10537
Evan Cheng078b0b02011-01-08 01:24:27 +000010538bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10539 // Looking for "rev" which is V6+.
10540 if (!Subtarget->hasV6Ops())
10541 return false;
10542
10543 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10544 std::string AsmStr = IA->getAsmString();
10545 SmallVector<StringRef, 4> AsmPieces;
10546 SplitString(AsmStr, AsmPieces, ";\n");
10547
10548 switch (AsmPieces.size()) {
10549 default: return false;
10550 case 1:
10551 AsmStr = AsmPieces[0];
10552 AsmPieces.clear();
10553 SplitString(AsmStr, AsmPieces, " \t,");
10554
10555 // rev $0, $1
10556 if (AsmPieces.size() == 3 &&
10557 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10558 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010559 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010560 if (Ty && Ty->getBitWidth() == 32)
10561 return IntrinsicLowering::LowerToByteSwap(CI);
10562 }
10563 break;
10564 }
10565
10566 return false;
10567}
10568
Evan Cheng10043e22007-01-19 07:51:42 +000010569/// getConstraintType - Given a constraint letter, return the type of
10570/// constraint it is for this target.
10571ARMTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +000010572ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
10573 if (Constraint.size() == 1) {
10574 switch (Constraint[0]) {
10575 default: break;
10576 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010577 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010578 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010579 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010580 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010581 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010582 // An address with a single base register. Due to the way we
10583 // currently handle addresses it is the same as an 'r' memory constraint.
10584 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010585 }
Eric Christophere256cd02011-06-21 22:10:57 +000010586 } else if (Constraint.size() == 2) {
10587 switch (Constraint[0]) {
10588 default: break;
10589 // All 'U+' constraints are addresses.
10590 case 'U': return C_Memory;
10591 }
Evan Cheng10043e22007-01-19 07:51:42 +000010592 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010593 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010594}
10595
John Thompsone8360b72010-10-29 17:29:13 +000010596/// Examine constraint type and operand type and determine a weight value.
10597/// This object must already have been set up with the operand type
10598/// and the current alternative constraint selected.
10599TargetLowering::ConstraintWeight
10600ARMTargetLowering::getSingleConstraintMatchWeight(
10601 AsmOperandInfo &info, const char *constraint) const {
10602 ConstraintWeight weight = CW_Invalid;
10603 Value *CallOperandVal = info.CallOperandVal;
10604 // If we don't have a value, we can't do a match,
10605 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010606 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010607 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010608 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010609 // Look at the constraint type.
10610 switch (*constraint) {
10611 default:
10612 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10613 break;
10614 case 'l':
10615 if (type->isIntegerTy()) {
10616 if (Subtarget->isThumb())
10617 weight = CW_SpecificReg;
10618 else
10619 weight = CW_Register;
10620 }
10621 break;
10622 case 'w':
10623 if (type->isFloatingPointTy())
10624 weight = CW_Register;
10625 break;
10626 }
10627 return weight;
10628}
10629
Eric Christophercf2007c2011-06-30 23:50:52 +000010630typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10631RCPair
Evan Cheng10043e22007-01-19 07:51:42 +000010632ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000010633 MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000010634 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010635 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000010636 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000010637 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010638 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010639 return RCPair(0U, &ARM::tGPRRegClass);
10640 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000010641 case 'h': // High regs or no regs.
10642 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010643 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000010644 break;
Chris Lattner6223e832007-04-02 17:24:08 +000010645 case 'r':
Akira Hatanakab9615342014-11-03 20:37:04 +000010646 if (Subtarget->isThumb1Only())
10647 return RCPair(0U, &ARM::tGPRRegClass);
Craig Topperc7242e02012-04-20 07:30:17 +000010648 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010649 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000010650 if (VT == MVT::Other)
10651 break;
Owen Anderson9f944592009-08-11 20:47:22 +000010652 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010653 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000010654 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010655 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000010656 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010657 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010658 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010659 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000010660 if (VT == MVT::Other)
10661 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010662 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010663 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010664 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010665 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010666 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010667 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010668 break;
Eric Christopherc011d312011-07-01 00:30:46 +000010669 case 't':
10670 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010671 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000010672 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010673 }
10674 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000010675 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000010676 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000010677
Evan Cheng10043e22007-01-19 07:51:42 +000010678 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10679}
10680
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010681/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10682/// vector. If it is invalid, don't add anything to Ops.
10683void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010684 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010685 std::vector<SDValue>&Ops,
10686 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000010687 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010688
Eric Christopherde9399b2011-06-02 23:16:42 +000010689 // Currently only support length 1 constraints.
10690 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010691
Eric Christopherde9399b2011-06-02 23:16:42 +000010692 char ConstraintLetter = Constraint[0];
10693 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010694 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000010695 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010696 case 'I': case 'J': case 'K': case 'L':
10697 case 'M': case 'N': case 'O':
10698 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10699 if (!C)
10700 return;
10701
10702 int64_t CVal64 = C->getSExtValue();
10703 int CVal = (int) CVal64;
10704 // None of these constraints allow values larger than 32 bits. Check
10705 // that the value fits in an int.
10706 if (CVal != CVal64)
10707 return;
10708
Eric Christopherde9399b2011-06-02 23:16:42 +000010709 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000010710 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000010711 // Constant suitable for movw, must be between 0 and
10712 // 65535.
10713 if (Subtarget->hasV6T2Ops())
10714 if (CVal >= 0 && CVal <= 65535)
10715 break;
10716 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010717 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000010718 if (Subtarget->isThumb1Only()) {
10719 // This must be a constant between 0 and 255, for ADD
10720 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010721 if (CVal >= 0 && CVal <= 255)
10722 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010723 } else if (Subtarget->isThumb2()) {
10724 // A constant that can be used as an immediate value in a
10725 // data-processing instruction.
10726 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10727 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010728 } else {
10729 // A constant that can be used as an immediate value in a
10730 // data-processing instruction.
10731 if (ARM_AM::getSOImmVal(CVal) != -1)
10732 break;
10733 }
10734 return;
10735
10736 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000010737 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010738 // This must be a constant between -255 and -1, for negated ADD
10739 // immediates. This can be used in GCC with an "n" modifier that
10740 // prints the negated value, for use with SUB instructions. It is
10741 // not useful otherwise but is implemented for compatibility.
10742 if (CVal >= -255 && CVal <= -1)
10743 break;
10744 } else {
10745 // This must be a constant between -4095 and 4095. It is not clear
10746 // what this constraint is intended for. Implemented for
10747 // compatibility with GCC.
10748 if (CVal >= -4095 && CVal <= 4095)
10749 break;
10750 }
10751 return;
10752
10753 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000010754 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010755 // A 32-bit value where only one byte has a nonzero value. Exclude
10756 // zero to match GCC. This constraint is used by GCC internally for
10757 // constants that can be loaded with a move/shift combination.
10758 // It is not useful otherwise but is implemented for compatibility.
10759 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10760 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010761 } else if (Subtarget->isThumb2()) {
10762 // A constant whose bitwise inverse can be used as an immediate
10763 // value in a data-processing instruction. This can be used in GCC
10764 // with a "B" modifier that prints the inverted value, for use with
10765 // BIC and MVN instructions. It is not useful otherwise but is
10766 // implemented for compatibility.
10767 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10768 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010769 } else {
10770 // A constant whose bitwise inverse can be used as an immediate
10771 // value in a data-processing instruction. This can be used in GCC
10772 // with a "B" modifier that prints the inverted value, for use with
10773 // BIC and MVN instructions. It is not useful otherwise but is
10774 // implemented for compatibility.
10775 if (ARM_AM::getSOImmVal(~CVal) != -1)
10776 break;
10777 }
10778 return;
10779
10780 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000010781 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010782 // This must be a constant between -7 and 7,
10783 // for 3-operand ADD/SUB immediate instructions.
10784 if (CVal >= -7 && CVal < 7)
10785 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010786 } else if (Subtarget->isThumb2()) {
10787 // A constant whose negation can be used as an immediate value in a
10788 // data-processing instruction. This can be used in GCC with an "n"
10789 // modifier that prints the negated value, for use with SUB
10790 // instructions. It is not useful otherwise but is implemented for
10791 // compatibility.
10792 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10793 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010794 } else {
10795 // A constant whose negation can be used as an immediate value in a
10796 // data-processing instruction. This can be used in GCC with an "n"
10797 // modifier that prints the negated value, for use with SUB
10798 // instructions. It is not useful otherwise but is implemented for
10799 // compatibility.
10800 if (ARM_AM::getSOImmVal(-CVal) != -1)
10801 break;
10802 }
10803 return;
10804
10805 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000010806 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010807 // This must be a multiple of 4 between 0 and 1020, for
10808 // ADD sp + immediate.
10809 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10810 break;
10811 } else {
10812 // A power of two or a constant between 0 and 32. This is used in
10813 // GCC for the shift amount on shifted register operands, but it is
10814 // useful in general for any shift amounts.
10815 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10816 break;
10817 }
10818 return;
10819
10820 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000010821 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010822 // This must be a constant between 0 and 31, for shift amounts.
10823 if (CVal >= 0 && CVal <= 31)
10824 break;
10825 }
10826 return;
10827
10828 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000010829 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010830 // This must be a multiple of 4 between -508 and 508, for
10831 // ADD/SUB sp = sp + immediate.
10832 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10833 break;
10834 }
10835 return;
10836 }
10837 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10838 break;
10839 }
10840
10841 if (Result.getNode()) {
10842 Ops.push_back(Result);
10843 return;
10844 }
Dale Johannesence97d552010-06-25 21:55:36 +000010845 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010846}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010847
Renato Golin87610692013-07-16 09:32:17 +000010848SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
10849 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only");
10850 unsigned Opcode = Op->getOpcode();
10851 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
Saleem Abdulrasool740be892014-08-17 22:50:59 +000010852 "Invalid opcode for Div/Rem lowering");
Renato Golin87610692013-07-16 09:32:17 +000010853 bool isSigned = (Opcode == ISD::SDIVREM);
10854 EVT VT = Op->getValueType(0);
10855 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
10856
10857 RTLIB::Libcall LC;
10858 switch (VT.getSimpleVT().SimpleTy) {
10859 default: llvm_unreachable("Unexpected request for libcall!");
Saleem Abdulrasool740be892014-08-17 22:50:59 +000010860 case MVT::i8: LC = isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
10861 case MVT::i16: LC = isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
10862 case MVT::i32: LC = isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
10863 case MVT::i64: LC = isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
Renato Golin87610692013-07-16 09:32:17 +000010864 }
10865
10866 SDValue InChain = DAG.getEntryNode();
10867
10868 TargetLowering::ArgListTy Args;
10869 TargetLowering::ArgListEntry Entry;
10870 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
10871 EVT ArgVT = Op->getOperand(i).getValueType();
10872 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
10873 Entry.Node = Op->getOperand(i);
10874 Entry.Ty = ArgTy;
10875 Entry.isSExt = isSigned;
10876 Entry.isZExt = !isSigned;
10877 Args.push_back(Entry);
10878 }
10879
10880 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
10881 getPointerTy());
10882
Reid Kleckner343c3952014-11-20 23:51:47 +000010883 Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
Renato Golin87610692013-07-16 09:32:17 +000010884
10885 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010886 TargetLowering::CallLoweringInfo CLI(DAG);
10887 CLI.setDebugLoc(dl).setChain(InChain)
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000010888 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, std::move(Args), 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010889 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000010890
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010891 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000010892 return CallInfo.first;
10893}
10894
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000010895SDValue
10896ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
10897 assert(Subtarget->isTargetWindows() && "unsupported target platform");
10898 SDLoc DL(Op);
10899
10900 // Get the inputs.
10901 SDValue Chain = Op.getOperand(0);
10902 SDValue Size = Op.getOperand(1);
10903
10904 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
10905 DAG.getConstant(2, MVT::i32));
10906
10907 SDValue Flag;
10908 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
10909 Flag = Chain.getValue(1);
10910
Saleem Abdulrasoolc4e00282014-07-19 01:29:51 +000010911 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000010912 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
10913
10914 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
10915 Chain = NewSP.getValue(1);
10916
10917 SDValue Ops[2] = { NewSP, Chain };
10918 return DAG.getMergeValues(Ops, DL);
10919}
10920
Oliver Stannard51b1d462014-08-21 12:50:31 +000010921SDValue ARMTargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const {
10922 assert(Op.getValueType() == MVT::f64 && Subtarget->isFPOnlySP() &&
10923 "Unexpected type for custom-lowering FP_EXTEND");
10924
10925 RTLIB::Libcall LC;
10926 LC = RTLIB::getFPEXT(Op.getOperand(0).getValueType(), Op.getValueType());
10927
10928 SDValue SrcVal = Op.getOperand(0);
10929 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
10930 /*isSigned*/ false, SDLoc(Op)).first;
10931}
10932
10933SDValue ARMTargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
10934 assert(Op.getOperand(0).getValueType() == MVT::f64 &&
10935 Subtarget->isFPOnlySP() &&
10936 "Unexpected type for custom-lowering FP_ROUND");
10937
10938 RTLIB::Libcall LC;
10939 LC = RTLIB::getFPROUND(Op.getOperand(0).getValueType(), Op.getValueType());
10940
10941 SDValue SrcVal = Op.getOperand(0);
10942 return makeLibCall(DAG, LC, Op.getValueType(), &SrcVal, 1,
10943 /*isSigned*/ false, SDLoc(Op)).first;
10944}
10945
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010946bool
10947ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10948 // The ARM target isn't yet aware of offsets.
10949 return false;
10950}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010951
Jim Grosbach11013ed2010-07-16 23:05:05 +000010952bool ARM::isBitFieldInvertedMask(unsigned v) {
10953 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010954 return false;
10955
Jim Grosbach11013ed2010-07-16 23:05:05 +000010956 // there can be 1's on either or both "outsides", all the "inside"
10957 // bits must be 0's
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010958 unsigned TO = CountTrailingOnes_32(v);
10959 unsigned LO = CountLeadingOnes_32(v);
10960 v = (v >> TO) << TO;
10961 v = (v << LO) >> LO;
10962 return v == 0;
Jim Grosbach11013ed2010-07-16 23:05:05 +000010963}
10964
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010965/// isFPImmLegal - Returns true if the target can instruction select the
10966/// specified FP immediate natively. If false, the legalizer will
10967/// materialize the FP immediate as a load from a constant pool.
10968bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10969 if (!Subtarget->hasVFP3())
10970 return false;
10971 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010972 return ARM_AM::getFP32Imm(Imm) != -1;
Oliver Stannard51b1d462014-08-21 12:50:31 +000010973 if (VT == MVT::f64 && !Subtarget->isFPOnlySP())
Jim Grosbachefc761a2011-09-30 00:50:06 +000010974 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010975 return false;
10976}
Bob Wilson5549d492010-09-21 17:56:22 +000010977
Wesley Peck527da1b2010-11-23 03:31:01 +000010978/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000010979/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10980/// specified in the intrinsic calls.
10981bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10982 const CallInst &I,
10983 unsigned Intrinsic) const {
10984 switch (Intrinsic) {
10985 case Intrinsic::arm_neon_vld1:
10986 case Intrinsic::arm_neon_vld2:
10987 case Intrinsic::arm_neon_vld3:
10988 case Intrinsic::arm_neon_vld4:
10989 case Intrinsic::arm_neon_vld2lane:
10990 case Intrinsic::arm_neon_vld3lane:
10991 case Intrinsic::arm_neon_vld4lane: {
10992 Info.opc = ISD::INTRINSIC_W_CHAIN;
10993 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010994 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010995 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10996 Info.ptrVal = I.getArgOperand(0);
10997 Info.offset = 0;
10998 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10999 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11000 Info.vol = false; // volatile loads with NEON intrinsics not supported
11001 Info.readMem = true;
11002 Info.writeMem = false;
11003 return true;
11004 }
11005 case Intrinsic::arm_neon_vst1:
11006 case Intrinsic::arm_neon_vst2:
11007 case Intrinsic::arm_neon_vst3:
11008 case Intrinsic::arm_neon_vst4:
11009 case Intrinsic::arm_neon_vst2lane:
11010 case Intrinsic::arm_neon_vst3lane:
11011 case Intrinsic::arm_neon_vst4lane: {
11012 Info.opc = ISD::INTRINSIC_VOID;
11013 // Conservatively set memVT to the entire set of vectors stored.
11014 unsigned NumElts = 0;
11015 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000011016 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000011017 if (!ArgTy->isVectorTy())
11018 break;
Micah Villmowcdfe20b2012-10-08 16:38:25 +000011019 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000011020 }
11021 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
11022 Info.ptrVal = I.getArgOperand(0);
11023 Info.offset = 0;
11024 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
11025 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
11026 Info.vol = false; // volatile stores with NEON intrinsics not supported
11027 Info.readMem = false;
11028 Info.writeMem = true;
11029 return true;
11030 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011031 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011032 case Intrinsic::arm_ldrex: {
11033 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
11034 Info.opc = ISD::INTRINSIC_W_CHAIN;
11035 Info.memVT = MVT::getVT(PtrTy->getElementType());
11036 Info.ptrVal = I.getArgOperand(0);
11037 Info.offset = 0;
11038 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
11039 Info.vol = true;
11040 Info.readMem = true;
11041 Info.writeMem = false;
11042 return true;
11043 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011044 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000011045 case Intrinsic::arm_strex: {
11046 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
11047 Info.opc = ISD::INTRINSIC_W_CHAIN;
11048 Info.memVT = MVT::getVT(PtrTy->getElementType());
11049 Info.ptrVal = I.getArgOperand(1);
11050 Info.offset = 0;
11051 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
11052 Info.vol = true;
11053 Info.readMem = false;
11054 Info.writeMem = true;
11055 return true;
11056 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011057 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011058 case Intrinsic::arm_strexd: {
11059 Info.opc = ISD::INTRINSIC_W_CHAIN;
11060 Info.memVT = MVT::i64;
11061 Info.ptrVal = I.getArgOperand(2);
11062 Info.offset = 0;
11063 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011064 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011065 Info.readMem = false;
11066 Info.writeMem = true;
11067 return true;
11068 }
Tim Northover1ff5f292014-03-26 14:39:31 +000011069 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011070 case Intrinsic::arm_ldrexd: {
11071 Info.opc = ISD::INTRINSIC_W_CHAIN;
11072 Info.memVT = MVT::i64;
11073 Info.ptrVal = I.getArgOperand(0);
11074 Info.offset = 0;
11075 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000011076 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000011077 Info.readMem = true;
11078 Info.writeMem = false;
11079 return true;
11080 }
Bob Wilson5549d492010-09-21 17:56:22 +000011081 default:
11082 break;
11083 }
11084
11085 return false;
11086}
Juergen Ributzka659ce002014-01-28 01:20:14 +000011087
11088/// \brief Returns true if it is beneficial to convert a load of a constant
11089/// to just the constant itself.
11090bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
11091 Type *Ty) const {
11092 assert(Ty->isIntegerTy());
11093
11094 unsigned Bits = Ty->getPrimitiveSizeInBits();
11095 if (Bits == 0 || Bits > 32)
11096 return false;
11097 return true;
11098}
Tim Northover037f26f22014-04-17 18:22:47 +000011099
Robin Morisset25c8e312014-09-17 00:06:58 +000011100bool ARMTargetLowering::hasLoadLinkedStoreConditional() const { return true; }
11101
Robin Morisset5349e8e2014-09-18 18:56:04 +000011102Instruction* ARMTargetLowering::makeDMB(IRBuilder<> &Builder,
11103 ARM_MB::MemBOpt Domain) const {
Robin Morisseta47cb412014-09-03 21:01:03 +000011104 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morisset5349e8e2014-09-18 18:56:04 +000011105
11106 // First, if the target has no DMB, see what fallback we can use.
11107 if (!Subtarget->hasDataBarrier()) {
11108 // Some ARMv6 cpus can support data barriers with an mcr instruction.
11109 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
11110 // here.
11111 if (Subtarget->hasV6Ops() && !Subtarget->isThumb()) {
11112 Function *MCR = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_mcr);
11113 Value* args[6] = {Builder.getInt32(15), Builder.getInt32(0),
11114 Builder.getInt32(0), Builder.getInt32(7),
11115 Builder.getInt32(10), Builder.getInt32(5)};
11116 return Builder.CreateCall(MCR, args);
11117 } else {
11118 // Instead of using barriers, atomic accesses on these subtargets use
11119 // libcalls.
11120 llvm_unreachable("makeDMB on a target so old that it has no barriers");
11121 }
11122 } else {
11123 Function *DMB = llvm::Intrinsic::getDeclaration(M, Intrinsic::arm_dmb);
11124 // Only a full system barrier exists in the M-class architectures.
11125 Domain = Subtarget->isMClass() ? ARM_MB::SY : Domain;
11126 Constant *CDomain = Builder.getInt32(Domain);
11127 return Builder.CreateCall(DMB, CDomain);
11128 }
Robin Morisseta47cb412014-09-03 21:01:03 +000011129}
11130
11131// Based on http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
Robin Morissetdedef332014-09-23 20:31:14 +000011132Instruction* ARMTargetLowering::emitLeadingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011133 AtomicOrdering Ord, bool IsStore,
11134 bool IsLoad) const {
11135 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011136 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011137
11138 switch (Ord) {
11139 case NotAtomic:
11140 case Unordered:
11141 llvm_unreachable("Invalid fence: unordered/non-atomic");
11142 case Monotonic:
11143 case Acquire:
Robin Morissetdedef332014-09-23 20:31:14 +000011144 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011145 case SequentiallyConsistent:
11146 if (!IsStore)
Robin Morissetdedef332014-09-23 20:31:14 +000011147 return nullptr; // Nothing to do
11148 /*FALLTHROUGH*/
Robin Morisseta47cb412014-09-03 21:01:03 +000011149 case Release:
11150 case AcquireRelease:
11151 if (Subtarget->isSwift())
Robin Morissetdedef332014-09-23 20:31:14 +000011152 return makeDMB(Builder, ARM_MB::ISHST);
Robin Morisseta47cb412014-09-03 21:01:03 +000011153 // FIXME: add a comment with a link to documentation justifying this.
11154 else
Robin Morissetdedef332014-09-23 20:31:14 +000011155 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011156 }
Robin Morissetdedef332014-09-23 20:31:14 +000011157 llvm_unreachable("Unknown fence ordering in emitLeadingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011158}
11159
Robin Morissetdedef332014-09-23 20:31:14 +000011160Instruction* ARMTargetLowering::emitTrailingFence(IRBuilder<> &Builder,
Robin Morisseta47cb412014-09-03 21:01:03 +000011161 AtomicOrdering Ord, bool IsStore,
11162 bool IsLoad) const {
11163 if (!getInsertFencesForAtomic())
Robin Morissetdedef332014-09-23 20:31:14 +000011164 return nullptr;
Robin Morisseta47cb412014-09-03 21:01:03 +000011165
11166 switch (Ord) {
11167 case NotAtomic:
11168 case Unordered:
11169 llvm_unreachable("Invalid fence: unordered/not-atomic");
11170 case Monotonic:
11171 case Release:
Robin Morissetdedef332014-09-23 20:31:14 +000011172 return nullptr; // Nothing to do
Robin Morisseta47cb412014-09-03 21:01:03 +000011173 case Acquire:
11174 case AcquireRelease:
Robin Morissetdedef332014-09-23 20:31:14 +000011175 case SequentiallyConsistent:
11176 return makeDMB(Builder, ARM_MB::ISH);
Robin Morisseta47cb412014-09-03 21:01:03 +000011177 }
Robin Morissetdedef332014-09-23 20:31:14 +000011178 llvm_unreachable("Unknown fence ordering in emitTrailingFence");
Robin Morisseta47cb412014-09-03 21:01:03 +000011179}
11180
Robin Morisseted3d48f2014-09-03 21:29:59 +000011181// Loads and stores less than 64-bits are already atomic; ones above that
11182// are doomed anyway, so defer to the default libcall and blame the OS when
11183// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11184// anything for those.
11185bool ARMTargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
11186 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
11187 return (Size == 64) && !Subtarget->isMClass();
11188}
Tim Northover037f26f22014-04-17 18:22:47 +000011189
Robin Morisseted3d48f2014-09-03 21:29:59 +000011190// Loads and stores less than 64-bits are already atomic; ones above that
11191// are doomed anyway, so defer to the default libcall and blame the OS when
11192// things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
11193// anything for those.
Robin Morisseta7b357f2014-09-23 18:33:21 +000011194// FIXME: ldrd and strd are atomic if the CPU has LPAE (e.g. A15 has that
11195// guarantee, see DDI0406C ARM architecture reference manual,
11196// sections A8.8.72-74 LDRD)
Robin Morisseted3d48f2014-09-03 21:29:59 +000011197bool ARMTargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
11198 unsigned Size = LI->getType()->getPrimitiveSizeInBits();
11199 return (Size == 64) && !Subtarget->isMClass();
11200}
11201
11202// For the real atomic operations, we have ldrex/strex up to 32 bits,
11203// and up to 64 bits on the non-M profiles
11204bool ARMTargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
11205 unsigned Size = AI->getType()->getPrimitiveSizeInBits();
Aaron Ballman169eeb912014-09-04 11:52:24 +000011206 return Size <= (Subtarget->isMClass() ? 32U : 64U);
Tim Northover037f26f22014-04-17 18:22:47 +000011207}
11208
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011209// This has so far only been implemented for MachO.
11210bool ARMTargetLowering::useLoadStackGuardNode() const {
Eric Christopher66322e82014-12-05 00:22:35 +000011211 return Subtarget->isTargetMachO();
Akira Hatanakae5b6e0d2014-07-25 19:31:34 +000011212}
11213
Quentin Colombetc32615d2014-10-31 17:52:53 +000011214bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
11215 unsigned &Cost) const {
11216 // If we do not have NEON, vector types are not natively supported.
11217 if (!Subtarget->hasNEON())
11218 return false;
11219
11220 // Floating point values and vector values map to the same register file.
11221 // Therefore, althought we could do a store extract of a vector type, this is
11222 // better to leave at float as we have more freedom in the addressing mode for
11223 // those.
11224 if (VectorTy->isFPOrFPVectorTy())
11225 return false;
11226
11227 // If the index is unknown at compile time, this is very expensive to lower
11228 // and it is not possible to combine the store with the extract.
11229 if (!isa<ConstantInt>(Idx))
11230 return false;
11231
11232 assert(VectorTy->isVectorTy() && "VectorTy is not a vector type");
11233 unsigned BitWidth = cast<VectorType>(VectorTy)->getBitWidth();
11234 // We can do a store + vector extract on any vector that fits perfectly in a D
11235 // or Q register.
11236 if (BitWidth == 64 || BitWidth == 128) {
11237 Cost = 0;
11238 return true;
11239 }
11240 return false;
11241}
11242
Tim Northover037f26f22014-04-17 18:22:47 +000011243Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
11244 AtomicOrdering Ord) const {
11245 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
11246 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
Robin Morissetb155f522014-08-18 16:48:58 +000011247 bool IsAcquire = isAtLeastAcquire(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011248
11249 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
11250 // intrinsic must return {i32, i32} and we have to recombine them into a
11251 // single i64 here.
11252 if (ValTy->getPrimitiveSizeInBits() == 64) {
11253 Intrinsic::ID Int =
11254 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
11255 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
11256
11257 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11258 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
11259
11260 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
11261 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011262 if (!Subtarget->isLittle())
11263 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011264 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
11265 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
11266 return Builder.CreateOr(
11267 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
11268 }
11269
11270 Type *Tys[] = { Addr->getType() };
11271 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
11272 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
11273
11274 return Builder.CreateTruncOrBitCast(
11275 Builder.CreateCall(Ldrex, Addr),
11276 cast<PointerType>(Addr->getType())->getElementType());
11277}
11278
11279Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
11280 Value *Addr,
11281 AtomicOrdering Ord) const {
11282 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
Robin Morissetb155f522014-08-18 16:48:58 +000011283 bool IsRelease = isAtLeastRelease(Ord);
Tim Northover037f26f22014-04-17 18:22:47 +000011284
11285 // Since the intrinsics must have legal type, the i64 intrinsics take two
11286 // parameters: "i32, i32". We must marshal Val into the appropriate form
11287 // before the call.
11288 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
11289 Intrinsic::ID Int =
11290 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
11291 Function *Strex = Intrinsic::getDeclaration(M, Int);
11292 Type *Int32Ty = Type::getInt32Ty(M->getContext());
11293
11294 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
11295 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000011296 if (!Subtarget->isLittle())
11297 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000011298 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
11299 return Builder.CreateCall3(Strex, Lo, Hi, Addr);
11300 }
11301
11302 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
11303 Type *Tys[] = { Addr->getType() };
11304 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
11305
11306 return Builder.CreateCall2(
11307 Strex, Builder.CreateZExtOrBitCast(
11308 Val, Strex->getFunctionType()->getParamType(0)),
11309 Addr);
11310}
Oliver Stannardc24f2172014-05-09 14:01:47 +000011311
11312enum HABaseType {
11313 HA_UNKNOWN = 0,
11314 HA_FLOAT,
11315 HA_DOUBLE,
11316 HA_VECT64,
11317 HA_VECT128
11318};
11319
11320static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
11321 uint64_t &Members) {
11322 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
11323 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
11324 uint64_t SubMembers = 0;
11325 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
11326 return false;
11327 Members += SubMembers;
11328 }
11329 } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
11330 uint64_t SubMembers = 0;
11331 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
11332 return false;
11333 Members += SubMembers * AT->getNumElements();
11334 } else if (Ty->isFloatTy()) {
11335 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
11336 return false;
11337 Members = 1;
11338 Base = HA_FLOAT;
11339 } else if (Ty->isDoubleTy()) {
11340 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
11341 return false;
11342 Members = 1;
11343 Base = HA_DOUBLE;
11344 } else if (const VectorType *VT = dyn_cast<VectorType>(Ty)) {
11345 Members = 1;
11346 switch (Base) {
11347 case HA_FLOAT:
11348 case HA_DOUBLE:
11349 return false;
11350 case HA_VECT64:
11351 return VT->getBitWidth() == 64;
11352 case HA_VECT128:
11353 return VT->getBitWidth() == 128;
11354 case HA_UNKNOWN:
11355 switch (VT->getBitWidth()) {
11356 case 64:
11357 Base = HA_VECT64;
11358 return true;
11359 case 128:
11360 Base = HA_VECT128;
11361 return true;
11362 default:
11363 return false;
11364 }
11365 }
11366 }
11367
11368 return (Members > 0 && Members <= 4);
11369}
11370
11371/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate.
11372bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
11373 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000011374 if (getEffectiveCallingConv(CallConv, isVarArg) !=
11375 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000011376 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000011377
11378 HABaseType Base = HA_UNKNOWN;
11379 uint64_t Members = 0;
11380 bool result = isHomogeneousAggregate(Ty, Base, Members);
Justin Bognerc0087f32014-08-12 03:24:59 +000011381 DEBUG(dbgs() << "isHA: " << result << " "; Ty->dump());
Tim Northover4f1909f2014-05-27 10:43:38 +000011382 return result;
Oliver Stannardc24f2172014-05-09 14:01:47 +000011383}