blob: c2d9b7a1248fede2a9687093fb4a870afc988b4c [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"
Bill Wendling202803e2011-10-05 00:02:33 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng10043e22007-01-19 07:51:42 +000034#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/CallingConv.h"
36#include "llvm/IR/Constants.h"
37#include "llvm/IR/Function.h"
38#include "llvm/IR/GlobalValue.h"
Tim Northover037f26f22014-04-17 18:22:47 +000039#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/Instruction.h"
41#include "llvm/IR/Instructions.h"
42#include "llvm/IR/Intrinsics.h"
43#include "llvm/IR/Type.h"
Bill Wendling46ffefc2010-03-09 02:46:12 +000044#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach32bb3622010-04-14 22:28:31 +000045#include "llvm/Support/CommandLine.h"
Oliver Stannardc24f2172014-05-09 14:01:47 +000046#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000047#include "llvm/Support/ErrorHandling.h"
Evan Cheng2150b922007-03-12 23:30:29 +000048#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000049#include "llvm/Target/TargetOptions.h"
David Peixottoc32e24a2013-10-17 19:49:22 +000050#include <utility>
Evan Cheng10043e22007-01-19 07:51:42 +000051using namespace llvm;
52
Chandler Carruth84e68b22014-04-22 02:41:26 +000053#define DEBUG_TYPE "arm-isel"
54
Dale Johannesend679ff72010-06-03 21:09:53 +000055STATISTIC(NumTailCalls, "Number of tail calls");
Evan Cheng68aec142011-01-19 02:16:49 +000056STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren9f911162012-06-01 02:44:42 +000057STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesend679ff72010-06-03 21:09:53 +000058
Eric Christopher347f4c32010-12-15 23:47:29 +000059cl::opt<bool>
Jim Grosbach32bb3622010-04-14 22:28:31 +000060EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng25f93642010-07-08 02:08:50 +000061 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbach32bb3622010-04-14 22:28:31 +000062 cl::init(false));
63
Evan Chengf128bdc2010-06-16 07:35:02 +000064static cl::opt<bool>
65ARMInterworking("arm-interworking", cl::Hidden,
66 cl::desc("Enable / disable ARM interworking (for debugging only)"),
67 cl::init(true));
68
Benjamin Kramer7ba71be2011-11-26 23:01:57 +000069namespace {
Cameron Zwarich89019782011-06-10 20:59:24 +000070 class ARMCCState : public CCState {
71 public:
72 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Craig Topperb94011f2013-07-14 04:42:23 +000073 const TargetMachine &TM, SmallVectorImpl<CCValAssign> &locs,
Cameron Zwarich89019782011-06-10 20:59:24 +000074 LLVMContext &C, ParmContext PC)
75 : CCState(CC, isVarArg, MF, TM, locs, C) {
76 assert(((PC == Call) || (PC == Prologue)) &&
77 "ARMCCState users must specify whether their context is call"
78 "or prologue generation.");
79 CallOrPrologue = PC;
80 }
81 };
82}
83
Stuart Hastings45fe3c32011-04-20 16:47:52 +000084// The APCS parameter registers.
Craig Topper840beec2014-04-04 05:16:06 +000085static const MCPhysReg GPRArgRegs[] = {
Stuart Hastings45fe3c32011-04-20 16:47:52 +000086 ARM::R0, ARM::R1, ARM::R2, ARM::R3
87};
88
Craig Topper4fa625f2012-08-12 03:16:37 +000089void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
90 MVT PromotedBitwiseVT) {
Bob Wilson2e076c42009-06-22 23:27:02 +000091 if (VT != PromotedLdStVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +000092 setOperationAction(ISD::LOAD, VT, Promote);
93 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000094
Craig Topper4fa625f2012-08-12 03:16:37 +000095 setOperationAction(ISD::STORE, VT, Promote);
96 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000097 }
98
Craig Topper4fa625f2012-08-12 03:16:37 +000099 MVT ElemTy = VT.getVectorElementType();
Owen Anderson9f944592009-08-11 20:47:22 +0000100 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Craig Topper4fa625f2012-08-12 03:16:37 +0000101 setOperationAction(ISD::SETCC, VT, Custom);
102 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
103 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000104 if (ElemTy == MVT::i32) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000105 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
106 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
107 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
108 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000109 } else {
Craig Topper4fa625f2012-08-12 03:16:37 +0000110 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
111 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
112 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
113 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson5d8cfb22009-09-16 20:20:44 +0000114 }
Craig Topper4fa625f2012-08-12 03:16:37 +0000115 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
116 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
117 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
118 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
119 setOperationAction(ISD::SELECT, VT, Expand);
120 setOperationAction(ISD::SELECT_CC, VT, Expand);
Jim Grosbach30af4422012-10-12 22:59:21 +0000121 setOperationAction(ISD::VSELECT, VT, Expand);
Craig Topper4fa625f2012-08-12 03:16:37 +0000122 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000123 if (VT.isInteger()) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000124 setOperationAction(ISD::SHL, VT, Custom);
125 setOperationAction(ISD::SRA, VT, Custom);
126 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson2e076c42009-06-22 23:27:02 +0000127 }
128
129 // Promote all bit-wise operations.
130 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000131 setOperationAction(ISD::AND, VT, Promote);
132 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
133 setOperationAction(ISD::OR, VT, Promote);
134 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
135 setOperationAction(ISD::XOR, VT, Promote);
136 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000137 }
Bob Wilson4ed397c2009-09-16 00:17:28 +0000138
139 // Neon does not support vector divide/remainder operations.
Craig Topper4fa625f2012-08-12 03:16:37 +0000140 setOperationAction(ISD::SDIV, VT, Expand);
141 setOperationAction(ISD::UDIV, VT, Expand);
142 setOperationAction(ISD::FDIV, VT, Expand);
143 setOperationAction(ISD::SREM, VT, Expand);
144 setOperationAction(ISD::UREM, VT, Expand);
145 setOperationAction(ISD::FREM, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000146}
147
Craig Topper4fa625f2012-08-12 03:16:37 +0000148void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000149 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000150 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000151}
152
Craig Topper4fa625f2012-08-12 03:16:37 +0000153void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000154 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000155 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000156}
157
Chris Lattner5e693ed2009-07-28 03:13:23 +0000158static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
Tim Northoverd6a729b2014-01-06 14:28:05 +0000159 if (TM.getSubtarget<ARMSubtarget>().isTargetMachO())
Bill Wendlingbbcaa402010-03-15 21:09:38 +0000160 return new TargetLoweringObjectFileMachO();
Bill Wendling46ffefc2010-03-09 02:46:12 +0000161
Chris Lattner4e7dfaf2009-08-02 00:34:36 +0000162 return new ARMElfTargetObjectFile();
Chris Lattner5e693ed2009-07-28 03:13:23 +0000163}
164
Evan Cheng10043e22007-01-19 07:51:42 +0000165ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Evan Cheng408aa562009-11-06 22:24:13 +0000166 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng10043e22007-01-19 07:51:42 +0000167 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Chengdf907f42010-07-23 22:39:59 +0000168 RegInfo = TM.getRegisterInfo();
Evan Chengbf407072010-09-10 01:29:16 +0000169 Itins = TM.getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000170
Duncan Sandsf2641e12011-09-06 19:07:46 +0000171 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
172
Tim Northoverd6a729b2014-01-06 14:28:05 +0000173 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000174 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000175 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
Tim Northover978d25f2014-04-22 10:10:09 +0000176 Subtarget->hasARMOps() && !TM.Options.UseSoftFloat) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000177 // Single-precision floating-point arithmetic.
178 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
179 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
180 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
181 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000182
Evan Chengc9f22fd12007-04-27 08:15:43 +0000183 // Double-precision floating-point arithmetic.
184 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
185 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
186 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
187 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng143576d2007-01-31 09:30:58 +0000188
Evan Chengc9f22fd12007-04-27 08:15:43 +0000189 // Single-precision comparisons.
190 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
191 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
192 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
193 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
194 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
195 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
196 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
197 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000198
Evan Chengc9f22fd12007-04-27 08:15:43 +0000199 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
200 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
201 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
205 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
206 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng143576d2007-01-31 09:30:58 +0000207
Evan Chengc9f22fd12007-04-27 08:15:43 +0000208 // Double-precision comparisons.
209 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
210 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
211 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
212 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
213 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
214 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
215 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
216 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000217
Evan Chengc9f22fd12007-04-27 08:15:43 +0000218 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
219 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
220 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
221 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
222 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
223 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
224 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
225 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Cheng10043e22007-01-19 07:51:42 +0000226
Evan Chengc9f22fd12007-04-27 08:15:43 +0000227 // Floating-point to integer conversions.
228 // i64 conversions are done via library routines even when generating VFP
229 // instructions, so use the same ones.
230 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
231 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
232 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
233 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000234
Evan Chengc9f22fd12007-04-27 08:15:43 +0000235 // Conversions between floating types.
236 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
237 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
238
239 // Integer to floating-point conversions.
240 // i64 conversions are done via library routines even when generating VFP
241 // instructions, so use the same ones.
Bob Wilsondc40d5a2009-03-20 23:16:43 +0000242 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
243 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengc9f22fd12007-04-27 08:15:43 +0000244 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
245 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
246 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
247 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
248 }
Evan Cheng10043e22007-01-19 07:51:42 +0000249 }
250
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000251 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000252 setLibcallName(RTLIB::SHL_I128, nullptr);
253 setLibcallName(RTLIB::SRL_I128, nullptr);
254 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000255
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000256 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetMachO() &&
257 !Subtarget->isTargetWindows()) {
Wesley Peck527da1b2010-11-23 03:31:01 +0000258 // Double-precision floating-point arithmetic helper functions
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000259 // RTABI chapter 4.1.2, Table 2
260 setLibcallName(RTLIB::ADD_F64, "__aeabi_dadd");
261 setLibcallName(RTLIB::DIV_F64, "__aeabi_ddiv");
262 setLibcallName(RTLIB::MUL_F64, "__aeabi_dmul");
263 setLibcallName(RTLIB::SUB_F64, "__aeabi_dsub");
264 setLibcallCallingConv(RTLIB::ADD_F64, CallingConv::ARM_AAPCS);
265 setLibcallCallingConv(RTLIB::DIV_F64, CallingConv::ARM_AAPCS);
266 setLibcallCallingConv(RTLIB::MUL_F64, CallingConv::ARM_AAPCS);
267 setLibcallCallingConv(RTLIB::SUB_F64, CallingConv::ARM_AAPCS);
268
269 // Double-precision floating-point comparison helper functions
270 // RTABI chapter 4.1.2, Table 3
271 setLibcallName(RTLIB::OEQ_F64, "__aeabi_dcmpeq");
272 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
273 setLibcallName(RTLIB::UNE_F64, "__aeabi_dcmpeq");
274 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETEQ);
275 setLibcallName(RTLIB::OLT_F64, "__aeabi_dcmplt");
276 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
277 setLibcallName(RTLIB::OLE_F64, "__aeabi_dcmple");
278 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
279 setLibcallName(RTLIB::OGE_F64, "__aeabi_dcmpge");
280 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
281 setLibcallName(RTLIB::OGT_F64, "__aeabi_dcmpgt");
282 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
283 setLibcallName(RTLIB::UO_F64, "__aeabi_dcmpun");
284 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
285 setLibcallName(RTLIB::O_F64, "__aeabi_dcmpun");
286 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
287 setLibcallCallingConv(RTLIB::OEQ_F64, CallingConv::ARM_AAPCS);
288 setLibcallCallingConv(RTLIB::UNE_F64, CallingConv::ARM_AAPCS);
289 setLibcallCallingConv(RTLIB::OLT_F64, CallingConv::ARM_AAPCS);
290 setLibcallCallingConv(RTLIB::OLE_F64, CallingConv::ARM_AAPCS);
291 setLibcallCallingConv(RTLIB::OGE_F64, CallingConv::ARM_AAPCS);
292 setLibcallCallingConv(RTLIB::OGT_F64, CallingConv::ARM_AAPCS);
293 setLibcallCallingConv(RTLIB::UO_F64, CallingConv::ARM_AAPCS);
294 setLibcallCallingConv(RTLIB::O_F64, CallingConv::ARM_AAPCS);
295
296 // Single-precision floating-point arithmetic helper functions
297 // RTABI chapter 4.1.2, Table 4
298 setLibcallName(RTLIB::ADD_F32, "__aeabi_fadd");
299 setLibcallName(RTLIB::DIV_F32, "__aeabi_fdiv");
300 setLibcallName(RTLIB::MUL_F32, "__aeabi_fmul");
301 setLibcallName(RTLIB::SUB_F32, "__aeabi_fsub");
302 setLibcallCallingConv(RTLIB::ADD_F32, CallingConv::ARM_AAPCS);
303 setLibcallCallingConv(RTLIB::DIV_F32, CallingConv::ARM_AAPCS);
304 setLibcallCallingConv(RTLIB::MUL_F32, CallingConv::ARM_AAPCS);
305 setLibcallCallingConv(RTLIB::SUB_F32, CallingConv::ARM_AAPCS);
306
307 // Single-precision floating-point comparison helper functions
308 // RTABI chapter 4.1.2, Table 5
309 setLibcallName(RTLIB::OEQ_F32, "__aeabi_fcmpeq");
310 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
311 setLibcallName(RTLIB::UNE_F32, "__aeabi_fcmpeq");
312 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETEQ);
313 setLibcallName(RTLIB::OLT_F32, "__aeabi_fcmplt");
314 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
315 setLibcallName(RTLIB::OLE_F32, "__aeabi_fcmple");
316 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
317 setLibcallName(RTLIB::OGE_F32, "__aeabi_fcmpge");
318 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
319 setLibcallName(RTLIB::OGT_F32, "__aeabi_fcmpgt");
320 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
321 setLibcallName(RTLIB::UO_F32, "__aeabi_fcmpun");
322 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
323 setLibcallName(RTLIB::O_F32, "__aeabi_fcmpun");
324 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
325 setLibcallCallingConv(RTLIB::OEQ_F32, CallingConv::ARM_AAPCS);
326 setLibcallCallingConv(RTLIB::UNE_F32, CallingConv::ARM_AAPCS);
327 setLibcallCallingConv(RTLIB::OLT_F32, CallingConv::ARM_AAPCS);
328 setLibcallCallingConv(RTLIB::OLE_F32, CallingConv::ARM_AAPCS);
329 setLibcallCallingConv(RTLIB::OGE_F32, CallingConv::ARM_AAPCS);
330 setLibcallCallingConv(RTLIB::OGT_F32, CallingConv::ARM_AAPCS);
331 setLibcallCallingConv(RTLIB::UO_F32, CallingConv::ARM_AAPCS);
332 setLibcallCallingConv(RTLIB::O_F32, CallingConv::ARM_AAPCS);
333
334 // Floating-point to integer conversions.
335 // RTABI chapter 4.1.2, Table 6
336 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz");
337 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz");
338 setLibcallName(RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz");
339 setLibcallName(RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz");
340 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz");
341 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz");
342 setLibcallName(RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz");
343 setLibcallName(RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz");
344 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I32, CallingConv::ARM_AAPCS);
345 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I32, CallingConv::ARM_AAPCS);
346 setLibcallCallingConv(RTLIB::FPTOSINT_F64_I64, CallingConv::ARM_AAPCS);
347 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::ARM_AAPCS);
348 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I32, CallingConv::ARM_AAPCS);
349 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I32, CallingConv::ARM_AAPCS);
350 setLibcallCallingConv(RTLIB::FPTOSINT_F32_I64, CallingConv::ARM_AAPCS);
351 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::ARM_AAPCS);
352
353 // Conversions between floating types.
354 // RTABI chapter 4.1.2, Table 7
355 setLibcallName(RTLIB::FPROUND_F64_F32, "__aeabi_d2f");
356 setLibcallName(RTLIB::FPEXT_F32_F64, "__aeabi_f2d");
357 setLibcallCallingConv(RTLIB::FPROUND_F64_F32, CallingConv::ARM_AAPCS);
Wesley Peck527da1b2010-11-23 03:31:01 +0000358 setLibcallCallingConv(RTLIB::FPEXT_F32_F64, CallingConv::ARM_AAPCS);
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000359
360 // Integer to floating-point conversions.
361 // RTABI chapter 4.1.2, Table 8
362 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d");
363 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d");
364 setLibcallName(RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d");
365 setLibcallName(RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d");
366 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f");
367 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f");
368 setLibcallName(RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f");
369 setLibcallName(RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f");
370 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
371 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F64, CallingConv::ARM_AAPCS);
372 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
373 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F64, CallingConv::ARM_AAPCS);
374 setLibcallCallingConv(RTLIB::SINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
375 setLibcallCallingConv(RTLIB::UINTTOFP_I32_F32, CallingConv::ARM_AAPCS);
376 setLibcallCallingConv(RTLIB::SINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
377 setLibcallCallingConv(RTLIB::UINTTOFP_I64_F32, CallingConv::ARM_AAPCS);
378
379 // Long long helper functions
380 // RTABI chapter 4.2, Table 9
381 setLibcallName(RTLIB::MUL_I64, "__aeabi_lmul");
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000382 setLibcallName(RTLIB::SHL_I64, "__aeabi_llsl");
383 setLibcallName(RTLIB::SRL_I64, "__aeabi_llsr");
384 setLibcallName(RTLIB::SRA_I64, "__aeabi_lasr");
385 setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::ARM_AAPCS);
386 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
387 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
388 setLibcallCallingConv(RTLIB::SHL_I64, CallingConv::ARM_AAPCS);
389 setLibcallCallingConv(RTLIB::SRL_I64, CallingConv::ARM_AAPCS);
390 setLibcallCallingConv(RTLIB::SRA_I64, CallingConv::ARM_AAPCS);
391
392 // Integer division functions
393 // RTABI chapter 4.3.1
394 setLibcallName(RTLIB::SDIV_I8, "__aeabi_idiv");
395 setLibcallName(RTLIB::SDIV_I16, "__aeabi_idiv");
396 setLibcallName(RTLIB::SDIV_I32, "__aeabi_idiv");
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000397 setLibcallName(RTLIB::SDIV_I64, "__aeabi_ldivmod");
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000398 setLibcallName(RTLIB::UDIV_I8, "__aeabi_uidiv");
399 setLibcallName(RTLIB::UDIV_I16, "__aeabi_uidiv");
400 setLibcallName(RTLIB::UDIV_I32, "__aeabi_uidiv");
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000401 setLibcallName(RTLIB::UDIV_I64, "__aeabi_uldivmod");
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000402 setLibcallCallingConv(RTLIB::SDIV_I8, CallingConv::ARM_AAPCS);
403 setLibcallCallingConv(RTLIB::SDIV_I16, CallingConv::ARM_AAPCS);
404 setLibcallCallingConv(RTLIB::SDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000405 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::ARM_AAPCS);
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000406 setLibcallCallingConv(RTLIB::UDIV_I8, CallingConv::ARM_AAPCS);
407 setLibcallCallingConv(RTLIB::UDIV_I16, CallingConv::ARM_AAPCS);
Wesley Peck527da1b2010-11-23 03:31:01 +0000408 setLibcallCallingConv(RTLIB::UDIV_I32, CallingConv::ARM_AAPCS);
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000409 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::ARM_AAPCS);
Renato Golin4cd51872011-05-22 21:41:23 +0000410
411 // Memory operations
412 // RTABI chapter 4.3.4
413 setLibcallName(RTLIB::MEMCPY, "__aeabi_memcpy");
414 setLibcallName(RTLIB::MEMMOVE, "__aeabi_memmove");
415 setLibcallName(RTLIB::MEMSET, "__aeabi_memset");
Anton Korobeynikovd0c46552012-01-29 09:11:50 +0000416 setLibcallCallingConv(RTLIB::MEMCPY, CallingConv::ARM_AAPCS);
417 setLibcallCallingConv(RTLIB::MEMMOVE, CallingConv::ARM_AAPCS);
418 setLibcallCallingConv(RTLIB::MEMSET, CallingConv::ARM_AAPCS);
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000419 }
420
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000421 if (Subtarget->isTargetWindows()) {
422 static const struct {
423 const RTLIB::Libcall Op;
424 const char * const Name;
425 const CallingConv::ID CC;
426 } LibraryCalls[] = {
427 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
428 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
429 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
430 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
431 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
432 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
433 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
434 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
435 };
436
437 for (const auto &LC : LibraryCalls) {
438 setLibcallName(LC.Op, LC.Name);
439 setLibcallCallingConv(LC.Op, LC.CC);
440 }
441 }
442
Bob Wilsonbc158992011-10-07 16:59:21 +0000443 // Use divmod compiler-rt calls for iOS 5.0 and later.
Cameron Esfahani943908b2013-08-29 20:23:14 +0000444 if (Subtarget->getTargetTriple().isiOS() &&
Bob Wilsonbc158992011-10-07 16:59:21 +0000445 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
446 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
447 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
448 }
449
David Goodwin22c2fba2009-07-08 23:10:31 +0000450 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000451 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000452 else
Craig Topperc7242e02012-04-20 07:30:17 +0000453 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000454 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
455 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000456 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Jim Grosbach4d5dc3e2010-08-11 15:44:15 +0000457 if (!Subtarget->isFPOnlySP())
Craig Topperc7242e02012-04-20 07:30:17 +0000458 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Bob Wilson7117a912009-03-20 22:42:55 +0000459
Owen Anderson9f944592009-08-11 20:47:22 +0000460 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000461 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000462
Eli Friedman6f84fed2011-11-08 01:43:53 +0000463 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
464 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
465 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
466 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
467 setTruncStoreAction((MVT::SimpleValueType)VT,
468 (MVT::SimpleValueType)InnerVT, Expand);
469 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
470 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
471 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000472
473 setOperationAction(ISD::MULHS, (MVT::SimpleValueType)VT, Expand);
474 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
475 setOperationAction(ISD::MULHU, (MVT::SimpleValueType)VT, Expand);
476 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000477 }
478
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000479 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000480 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000481
Bob Wilson2e076c42009-06-22 23:27:02 +0000482 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000483 addDRTypeForNEON(MVT::v2f32);
484 addDRTypeForNEON(MVT::v8i8);
485 addDRTypeForNEON(MVT::v4i16);
486 addDRTypeForNEON(MVT::v2i32);
487 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000488
Owen Anderson9f944592009-08-11 20:47:22 +0000489 addQRTypeForNEON(MVT::v4f32);
490 addQRTypeForNEON(MVT::v2f64);
491 addQRTypeForNEON(MVT::v16i8);
492 addQRTypeForNEON(MVT::v8i16);
493 addQRTypeForNEON(MVT::v4i32);
494 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000495
Bob Wilson194a2512009-09-15 23:55:57 +0000496 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
497 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000498 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
499 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000500 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
501 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
502 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000503 // FIXME: Code duplication: FDIV and FREM are expanded always, see
504 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000505 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
506 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000507 // FIXME: Create unittest.
508 // In another words, find a way when "copysign" appears in DAG with vector
509 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000510 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000511 // FIXME: Code duplication: SETCC has custom operation action, see
512 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000513 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000514 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000515 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
516 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
517 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
518 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
519 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
520 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
521 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
522 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
523 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
524 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
525 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
526 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000527 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000528 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
529 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
530 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
531 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
532 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000533 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000534
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000535 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
536 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
537 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
538 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
539 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
540 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
541 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
542 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
543 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
544 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000545 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
546 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
547 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
548 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000549 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000550
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000551 // Mark v2f32 intrinsics.
552 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
553 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
554 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
555 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
556 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
557 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
558 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
559 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
560 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
561 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
562 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
563 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
564 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
565 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
566 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
567
Bob Wilson6cc46572009-09-16 00:32:15 +0000568 // Neon does not support some operations on v1i64 and v2i64 types.
569 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000570 // Custom handling for some quad-vector types to detect VMULL.
571 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
572 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
573 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000574 // Custom handling for some vector types to avoid expensive expansions
575 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
576 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
577 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
578 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000579 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
580 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000581 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000582 // a destination type that is wider than the source, and nor does
583 // it have a FP_TO_[SU]INT instruction with a narrower destination than
584 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000585 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
586 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000587 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
588 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000589
Eli Friedmane6385e62012-11-15 22:44:27 +0000590 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000591 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000592
Evan Chengb4eae132012-12-04 22:41:50 +0000593 // NEON does not have single instruction CTPOP for vectors with element
594 // types wider than 8-bits. However, custom lowering can leverage the
595 // v8i8/v16i8 vcnt instruction.
596 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
597 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
598 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
599 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
600
Jim Grosbach5f215872013-02-27 21:31:12 +0000601 // NEON only has FMA instructions as of VFP4.
602 if (!Subtarget->hasVFP4()) {
603 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
604 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
605 }
606
Bob Wilson06fce872011-02-07 17:43:21 +0000607 setTargetDAGCombine(ISD::INTRINSIC_VOID);
608 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000609 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
610 setTargetDAGCombine(ISD::SHL);
611 setTargetDAGCombine(ISD::SRL);
612 setTargetDAGCombine(ISD::SRA);
613 setTargetDAGCombine(ISD::SIGN_EXTEND);
614 setTargetDAGCombine(ISD::ZERO_EXTEND);
615 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsonc6c13a32010-02-18 06:05:53 +0000616 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000617 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000618 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000619 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
620 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000621 setTargetDAGCombine(ISD::FP_TO_SINT);
622 setTargetDAGCombine(ISD::FP_TO_UINT);
623 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem097106b2011-10-15 20:03:12 +0000624
James Molloy547d4c02012-02-20 09:24:05 +0000625 // It is legal to extload from v4i8 to v4i16 or v4i32.
626 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
627 MVT::v4i16, MVT::v2i16,
628 MVT::v2i32};
629 for (unsigned i = 0; i < 6; ++i) {
630 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
631 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
632 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
633 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000634 }
635
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000636 // ARM and Thumb2 support UMLAL/SMLAL.
637 if (!Subtarget->isThumb1Only())
638 setTargetDAGCombine(ISD::ADDC);
639
640
Evan Cheng6addd652007-05-18 00:19:34 +0000641 computeRegisterProperties();
Evan Cheng10043e22007-01-19 07:51:42 +0000642
643 // ARM does not have f32 extending load.
Owen Anderson9f944592009-08-11 20:47:22 +0000644 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000645
Duncan Sands95d46ef2008-01-23 20:39:46 +0000646 // ARM does not have i1 sign extending load.
Owen Anderson9f944592009-08-11 20:47:22 +0000647 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000648
Evan Cheng10043e22007-01-19 07:51:42 +0000649 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000650 if (!Subtarget->isThumb1Only()) {
651 for (unsigned im = (unsigned)ISD::PRE_INC;
652 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000653 setIndexedLoadAction(im, MVT::i1, Legal);
654 setIndexedLoadAction(im, MVT::i8, Legal);
655 setIndexedLoadAction(im, MVT::i16, Legal);
656 setIndexedLoadAction(im, MVT::i32, Legal);
657 setIndexedStoreAction(im, MVT::i1, Legal);
658 setIndexedStoreAction(im, MVT::i8, Legal);
659 setIndexedStoreAction(im, MVT::i16, Legal);
660 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000661 }
Evan Cheng10043e22007-01-19 07:51:42 +0000662 }
663
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000664 setOperationAction(ISD::SADDO, MVT::i32, Custom);
665 setOperationAction(ISD::UADDO, MVT::i32, Custom);
666 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
667 setOperationAction(ISD::USUBO, MVT::i32, Custom);
668
Evan Cheng10043e22007-01-19 07:51:42 +0000669 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000670 setOperationAction(ISD::MUL, MVT::i64, Expand);
671 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000672 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000673 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
674 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000675 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000676 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
677 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000678 setOperationAction(ISD::MULHS, MVT::i32, Expand);
679
Jim Grosbach5d994042009-10-31 19:38:01 +0000680 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000681 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000682 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000683 setOperationAction(ISD::SRL, MVT::i64, Custom);
684 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000685
Evan Chenge8916542011-08-30 01:34:54 +0000686 if (!Subtarget->isThumb1Only()) {
687 // FIXME: We should do this for Thumb1 as well.
688 setOperationAction(ISD::ADDC, MVT::i32, Custom);
689 setOperationAction(ISD::ADDE, MVT::i32, Custom);
690 setOperationAction(ISD::SUBC, MVT::i32, Custom);
691 setOperationAction(ISD::SUBE, MVT::i32, Custom);
692 }
693
Evan Cheng10043e22007-01-19 07:51:42 +0000694 // ARM does not have ROTL.
Owen Anderson9f944592009-08-11 20:47:22 +0000695 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach8546ec92010-01-18 19:58:49 +0000696 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000697 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000698 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000699 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000700
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000701 // These just redirect to CTTZ and CTLZ on ARM.
702 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
703 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
704
Tim Northoverbc933082013-05-23 19:11:20 +0000705 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
706
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000707 // Only ARMv6 has BSWAP.
708 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000709 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000710
Bob Wilsone8a549c2012-09-29 21:43:49 +0000711 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
712 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
713 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbach92d999002010-05-05 20:44:35 +0000714 setOperationAction(ISD::SDIV, MVT::i32, Expand);
715 setOperationAction(ISD::UDIV, MVT::i32, Expand);
716 }
Renato Golin87610692013-07-16 09:32:17 +0000717
718 // FIXME: Also set divmod for SREM on EABI
Owen Anderson9f944592009-08-11 20:47:22 +0000719 setOperationAction(ISD::SREM, MVT::i32, Expand);
720 setOperationAction(ISD::UREM, MVT::i32, Expand);
Renato Golin87610692013-07-16 09:32:17 +0000721 // Register based DivRem for AEABI (RTABI 4.2)
722 if (Subtarget->isTargetAEABI()) {
723 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
724 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
725 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
726 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
727 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
728 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
729 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
730 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
731
732 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
733 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
734 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
735 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
736 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
737 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
738 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
739 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
740
741 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
742 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
743 } else {
744 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
745 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
746 }
Bob Wilson7117a912009-03-20 22:42:55 +0000747
Owen Anderson9f944592009-08-11 20:47:22 +0000748 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
749 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
750 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
751 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000752 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000753
Evan Cheng74d92c12011-04-08 21:37:21 +0000754 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000755
Evan Cheng10043e22007-01-19 07:51:42 +0000756 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000757 setOperationAction(ISD::VASTART, MVT::Other, Custom);
758 setOperationAction(ISD::VAARG, MVT::Other, Expand);
759 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
760 setOperationAction(ISD::VAEND, MVT::Other, Expand);
761 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
762 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000763
Tim Northoverd6a729b2014-01-06 14:28:05 +0000764 if (!Subtarget->isTargetMachO()) {
765 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000766 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000767 setExceptionPointerRegister(ARM::R0);
768 setExceptionSelectorRegister(ARM::R1);
769 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000770
Evan Chengf7f97b42010-04-15 22:20:34 +0000771 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng6e809de2010-08-11 06:22:01 +0000772 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
773 // the default expansion.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000774 if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000775 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
776 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000777 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000778
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000779 // On v8, we have particularly efficient implementations of atomic fences
780 // if they can be combined with nearby atomic loads and stores.
781 if (!Subtarget->hasV8Ops()) {
782 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
783 setInsertFencesForAtomic(true);
784 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000785 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000786 // If there's anything we can use as a barrier, go through custom lowering
787 // for ATOMIC_FENCE.
788 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
789 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
790
Jim Grosbach6860bb72010-06-18 22:35:32 +0000791 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000792 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000793 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000794 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000795 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000796 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000797 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000798 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000799 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000800 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000801 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000802 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000803 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000804 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
805 // Unordered/Monotonic case.
806 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
807 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000808 }
Evan Cheng10043e22007-01-19 07:51:42 +0000809
Evan Cheng21acf9f2010-11-04 05:19:35 +0000810 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000811
Eli Friedman8cfa7712010-06-26 04:36:50 +0000812 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
813 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000814 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
815 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000816 }
Owen Anderson9f944592009-08-11 20:47:22 +0000817 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000818
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000819 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
820 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000821 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000822 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000823 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000824 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
825 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000826
827 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000828 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbach31984832010-07-07 00:07:57 +0000829 if (Subtarget->isTargetDarwin()) {
830 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
831 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall7d84ece2011-05-29 19:50:32 +0000832 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbach31984832010-07-07 00:07:57 +0000833 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000834
Owen Anderson9f944592009-08-11 20:47:22 +0000835 setOperationAction(ISD::SETCC, MVT::i32, Expand);
836 setOperationAction(ISD::SETCC, MVT::f32, Expand);
837 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000838 setOperationAction(ISD::SELECT, MVT::i32, Custom);
839 setOperationAction(ISD::SELECT, MVT::f32, Custom);
840 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000841 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
842 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
843 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000844
Owen Anderson9f944592009-08-11 20:47:22 +0000845 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
846 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
847 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
848 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
849 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000850
Dan Gohman482732a2007-10-11 23:21:31 +0000851 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000852 setOperationAction(ISD::FSIN, MVT::f64, Expand);
853 setOperationAction(ISD::FSIN, MVT::f32, Expand);
854 setOperationAction(ISD::FCOS, MVT::f32, Expand);
855 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000856 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
857 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000858 setOperationAction(ISD::FREM, MVT::f64, Expand);
859 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000860 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
861 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000862 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
863 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000864 }
Owen Anderson9f944592009-08-11 20:47:22 +0000865 setOperationAction(ISD::FPOW, MVT::f64, Expand);
866 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000867
Evan Chengd0007f32012-04-10 21:40:28 +0000868 if (!Subtarget->hasVFP4()) {
869 setOperationAction(ISD::FMA, MVT::f64, Expand);
870 setOperationAction(ISD::FMA, MVT::f32, Expand);
871 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000872
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000873 // Various VFP goodness
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000874 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilsone4191e72010-03-19 22:51:32 +0000875 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
876 if (Subtarget->hasVFP2()) {
877 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
878 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
879 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
880 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
881 }
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000882 // Special handling for half-precision FP.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000883 if (!Subtarget->hasFP16()) {
884 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
885 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000886 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000887 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000888
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000889 // Combine sin / cos into one node or libcall if possible.
890 if (Subtarget->hasSinCos()) {
891 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
892 setLibcallName(RTLIB::SINCOS_F64, "sincos");
893 if (Subtarget->getTargetTriple().getOS() == Triple::IOS) {
894 // For iOS, we don't want to the normal expansion of a libcall to
895 // sincos. We want to issue a libcall to __sincos_stret.
896 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
897 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
898 }
899 }
Evan Cheng10043e22007-01-19 07:51:42 +0000900
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000901 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000902 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000903 setTargetDAGCombine(ISD::ADD);
904 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000905 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000906 setTargetDAGCombine(ISD::AND);
907 setTargetDAGCombine(ISD::OR);
908 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000909
Evan Chengf258a152012-02-23 02:58:19 +0000910 if (Subtarget->hasV6Ops())
911 setTargetDAGCombine(ISD::SRL);
912
Evan Cheng10043e22007-01-19 07:51:42 +0000913 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +0000914
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000915 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
916 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +0000917 setSchedulingPreference(Sched::RegPressure);
918 else
919 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +0000920
Evan Cheng3ae2b792011-01-06 06:52:41 +0000921 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000922 MaxStoresPerMemset = 8;
923 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
924 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
925 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
926 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
927 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengb71233f2010-06-26 01:52:05 +0000928
Rafael Espindolaa76eccf2010-07-11 04:01:49 +0000929 // On ARM arguments smaller than 4 bytes are extended, so all arguments
930 // are at least 4 bytes aligned.
931 setMinStackArgumentAlignment(4);
932
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000933 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000934 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000935
Eli Friedman2518f832011-05-06 20:34:06 +0000936 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +0000937}
938
Andrew Trick43f25632011-01-19 02:35:27 +0000939// FIXME: It might make sense to define the representative register class as the
940// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
941// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
942// SPR's representative would be DPR_VFP2. This should work well if register
943// pressure tracking were modified such that a register use would increment the
944// pressure of the register class's representative and all of it's super
945// classes' representatives transitively. We have not implemented this because
946// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000947// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +0000948// and extractions.
Evan Chenga77f3d32010-07-21 06:09:07 +0000949std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000950ARMTargetLowering::findRepresentativeClass(MVT VT) const{
Craig Topper062a2ba2014-04-25 05:30:21 +0000951 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +0000952 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000953 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +0000954 default:
Evan Chenga77f3d32010-07-21 06:09:07 +0000955 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng28590382010-07-21 23:53:58 +0000956 // Use DPR as representative register class for all floating point
957 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
958 // the cost is 1 for both f32 and f64.
959 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +0000960 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +0000961 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +0000962 // When NEON is used for SP, only half of the register file is available
963 // because operations that define both SP and DP results will be constrained
964 // to the VFP2 class (D0-D15). We currently model this constraint prior to
965 // coalescing by double-counting the SP regs. See the FIXME above.
966 if (Subtarget->useNEONForSinglePrecisionFP())
967 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000968 break;
969 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
970 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +0000971 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000972 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000973 break;
974 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000975 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000976 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +0000977 break;
978 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000979 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000980 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +0000981 break;
Evan Cheng10f99a32010-07-19 22:15:08 +0000982 }
Evan Chenga77f3d32010-07-21 06:09:07 +0000983 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +0000984}
985
Evan Cheng10043e22007-01-19 07:51:42 +0000986const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
987 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000988 default: return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +0000989 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +0000990 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +0000991 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
992 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +0000993 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +0000994 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
995 case ARMISD::tCALL: return "ARMISD::tCALL";
996 case ARMISD::BRCOND: return "ARMISD::BRCOND";
997 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +0000998 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +0000999 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +00001000 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +00001001 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
1002 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +00001003 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +00001004 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +00001005 case ARMISD::CMPFP: return "ARMISD::CMPFP";
1006 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +00001007 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +00001008 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +00001009
Evan Cheng10043e22007-01-19 07:51:42 +00001010 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +00001011
Jim Grosbach8546ec92010-01-18 19:58:49 +00001012 case ARMISD::RBIT: return "ARMISD::RBIT";
1013
Bob Wilsone4191e72010-03-19 22:51:32 +00001014 case ARMISD::FTOSI: return "ARMISD::FTOSI";
1015 case ARMISD::FTOUI: return "ARMISD::FTOUI";
1016 case ARMISD::SITOF: return "ARMISD::SITOF";
1017 case ARMISD::UITOF: return "ARMISD::UITOF";
1018
Evan Cheng10043e22007-01-19 07:51:42 +00001019 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
1020 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
1021 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +00001022
Evan Chenge8916542011-08-30 01:34:54 +00001023 case ARMISD::ADDC: return "ARMISD::ADDC";
1024 case ARMISD::ADDE: return "ARMISD::ADDE";
1025 case ARMISD::SUBC: return "ARMISD::SUBC";
1026 case ARMISD::SUBE: return "ARMISD::SUBE";
1027
Bob Wilson22806742010-09-22 22:09:21 +00001028 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
1029 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001030
Evan Chengec6d7c92009-10-28 06:55:03 +00001031 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
1032 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
1033
Dale Johannesend679ff72010-06-03 21:09:53 +00001034 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +00001035
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00001036 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +00001037
Evan Chengb972e562009-08-07 00:34:42 +00001038 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
1039
Bob Wilson7ed59712010-10-30 00:54:37 +00001040 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +00001041
Evan Cheng8740ee32010-11-03 06:34:55 +00001042 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
1043
Bob Wilson2e076c42009-06-22 23:27:02 +00001044 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +00001045 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001046 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +00001047 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
1048 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001049 case ARMISD::VCGEU: return "ARMISD::VCGEU";
1050 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +00001051 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1052 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001053 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1054 case ARMISD::VTST: return "ARMISD::VTST";
1055
1056 case ARMISD::VSHL: return "ARMISD::VSHL";
1057 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1058 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001059 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1060 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1061 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1062 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1063 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1064 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1065 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1066 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1067 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1068 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1069 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1070 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
1071 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1072 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001073 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001074 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001075 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001076 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001077 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001078 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001079 case ARMISD::VREV64: return "ARMISD::VREV64";
1080 case ARMISD::VREV32: return "ARMISD::VREV32";
1081 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001082 case ARMISD::VZIP: return "ARMISD::VZIP";
1083 case ARMISD::VUZP: return "ARMISD::VUZP";
1084 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001085 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1086 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001087 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1088 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001089 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1090 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001091 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilsonc6c13a32010-02-18 06:05:53 +00001092 case ARMISD::FMAX: return "ARMISD::FMAX";
1093 case ARMISD::FMIN: return "ARMISD::FMIN";
Joey Goulye3dd6842013-08-23 12:01:13 +00001094 case ARMISD::VMAXNM: return "ARMISD::VMAX";
1095 case ARMISD::VMINNM: return "ARMISD::VMIN";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001096 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001097 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1098 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001099 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilson2d790df2010-11-28 06:51:26 +00001100 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1101 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1102 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001103 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1104 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1105 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1106 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1107 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1108 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1109 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1110 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1111 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1112 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1113 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1114 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1115 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1116 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1117 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1118 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1119 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001120 }
1121}
1122
Matt Arsenault758659232013-05-18 00:21:46 +00001123EVT ARMTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Duncan Sandsf2641e12011-09-06 19:07:46 +00001124 if (!VT.isVector()) return getPointerTy();
1125 return VT.changeVectorElementTypeToInteger();
1126}
1127
Evan Cheng4cad68e2010-05-15 02:18:07 +00001128/// getRegClassFor - Return the register class that should be used for the
1129/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001130const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001131 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1132 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1133 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001134 if (Subtarget->hasNEON()) {
1135 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001136 return &ARM::QQPRRegClass;
1137 if (VT == MVT::v8i64)
1138 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001139 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001140 return TargetLowering::getRegClassFor(VT);
1141}
1142
Eric Christopher84bdfd82010-07-21 22:26:11 +00001143// Create a fast isel object.
1144FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001145ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1146 const TargetLibraryInfo *libInfo) const {
1147 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001148}
1149
Anton Korobeynikov19edda02010-07-24 21:52:08 +00001150/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1151/// be used for loads / stores from the global.
1152unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1153 return (Subtarget->isThumb1Only() ? 127 : 4095);
1154}
1155
Evan Cheng4401f882010-05-20 23:26:43 +00001156Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001157 unsigned NumVals = N->getNumValues();
1158 if (!NumVals)
1159 return Sched::RegPressure;
1160
1161 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001162 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001163 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001164 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001165 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001166 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001167 }
Evan Chengbf914992010-05-28 23:25:23 +00001168
1169 if (!N->isMachineOpcode())
1170 return Sched::RegPressure;
1171
1172 // Load are scheduled for latency even if there instruction itinerary
1173 // is not available.
1174 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001175 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001176
Evan Cheng6cc775f2011-06-28 19:10:37 +00001177 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001178 return Sched::RegPressure;
1179 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001180 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001181 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001182
Evan Cheng4401f882010-05-20 23:26:43 +00001183 return Sched::RegPressure;
1184}
1185
Evan Cheng10043e22007-01-19 07:51:42 +00001186//===----------------------------------------------------------------------===//
1187// Lowering Code
1188//===----------------------------------------------------------------------===//
1189
Evan Cheng10043e22007-01-19 07:51:42 +00001190/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1191static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1192 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001193 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001194 case ISD::SETNE: return ARMCC::NE;
1195 case ISD::SETEQ: return ARMCC::EQ;
1196 case ISD::SETGT: return ARMCC::GT;
1197 case ISD::SETGE: return ARMCC::GE;
1198 case ISD::SETLT: return ARMCC::LT;
1199 case ISD::SETLE: return ARMCC::LE;
1200 case ISD::SETUGT: return ARMCC::HI;
1201 case ISD::SETUGE: return ARMCC::HS;
1202 case ISD::SETULT: return ARMCC::LO;
1203 case ISD::SETULE: return ARMCC::LS;
1204 }
1205}
1206
Bob Wilsona2e83332009-09-09 23:14:54 +00001207/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1208static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001209 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001210 CondCode2 = ARMCC::AL;
1211 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001212 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001213 case ISD::SETEQ:
1214 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1215 case ISD::SETGT:
1216 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1217 case ISD::SETGE:
1218 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1219 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001220 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001221 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1222 case ISD::SETO: CondCode = ARMCC::VC; break;
1223 case ISD::SETUO: CondCode = ARMCC::VS; break;
1224 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1225 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1226 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1227 case ISD::SETLT:
1228 case ISD::SETULT: CondCode = ARMCC::LT; break;
1229 case ISD::SETLE:
1230 case ISD::SETULE: CondCode = ARMCC::LE; break;
1231 case ISD::SETNE:
1232 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1233 }
Evan Cheng10043e22007-01-19 07:51:42 +00001234}
1235
Bob Wilsona4c22902009-04-17 19:07:39 +00001236//===----------------------------------------------------------------------===//
1237// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001238//===----------------------------------------------------------------------===//
1239
1240#include "ARMGenCallingConv.inc"
1241
Oliver Stannardc24f2172014-05-09 14:01:47 +00001242/// getEffectiveCallingConv - Get the effective calling convention, taking into
1243/// account presence of floating point hardware and calling convention
1244/// limitations, such as support for variadic functions.
1245CallingConv::ID
1246ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1247 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001248 switch (CC) {
1249 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001250 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001251 case CallingConv::ARM_AAPCS:
1252 case CallingConv::ARM_APCS:
1253 case CallingConv::GHC:
1254 return CC;
1255 case CallingConv::ARM_AAPCS_VFP:
1256 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1257 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001258 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001259 return CallingConv::ARM_APCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001260 else if (Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001261 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1262 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001263 return CallingConv::ARM_AAPCS_VFP;
1264 else
1265 return CallingConv::ARM_AAPCS;
1266 case CallingConv::Fast:
1267 if (!Subtarget->isAAPCS_ABI()) {
1268 if (Subtarget->hasVFP2() && !isVarArg)
1269 return CallingConv::Fast;
1270 return CallingConv::ARM_APCS;
1271 } else if (Subtarget->hasVFP2() && !isVarArg)
1272 return CallingConv::ARM_AAPCS_VFP;
1273 else
1274 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001275 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001276}
1277
1278/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1279/// CallingConvention.
1280CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1281 bool Return,
1282 bool isVarArg) const {
1283 switch (getEffectiveCallingConv(CC, isVarArg)) {
1284 default:
1285 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001286 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001287 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001288 case CallingConv::ARM_AAPCS:
1289 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1290 case CallingConv::ARM_AAPCS_VFP:
1291 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1292 case CallingConv::Fast:
1293 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001294 case CallingConv::GHC:
1295 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001296 }
1297}
1298
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001299/// LowerCallResult - Lower the result values of a call into the
1300/// appropriate copies out of appropriate physical registers.
1301SDValue
1302ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001303 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001304 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001305 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001306 SmallVectorImpl<SDValue> &InVals,
1307 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001308
Bob Wilsona4c22902009-04-17 19:07:39 +00001309 // Assign locations to each value returned by this call.
1310 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001311 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1312 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001313 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001314 CCAssignFnForNode(CallConv, /* Return*/ true,
1315 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001316
1317 // Copy all of the result registers out of their specified physreg.
1318 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1319 CCValAssign VA = RVLocs[i];
1320
Stephen Linb8bd2322013-04-20 05:14:40 +00001321 // Pass 'this' value directly from the argument to return value, to avoid
1322 // reg unit interference
1323 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001324 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1325 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001326 InVals.push_back(ThisVal);
1327 continue;
1328 }
1329
Bob Wilson0041bd32009-04-25 00:33:20 +00001330 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001331 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001332 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001333 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001334 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001335 Chain = Lo.getValue(1);
1336 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001337 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001338 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001339 InFlag);
1340 Chain = Hi.getValue(1);
1341 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001342 if (!Subtarget->isLittle())
1343 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001344 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001345
Owen Anderson9f944592009-08-11 20:47:22 +00001346 if (VA.getLocVT() == MVT::v2f64) {
1347 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1348 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1349 DAG.getConstant(0, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001350
1351 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001352 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001353 Chain = Lo.getValue(1);
1354 InFlag = Lo.getValue(2);
1355 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001356 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001357 Chain = Hi.getValue(1);
1358 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001359 if (!Subtarget->isLittle())
1360 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001361 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001362 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1363 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001364 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001365 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001366 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1367 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001368 Chain = Val.getValue(1);
1369 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001370 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001371
1372 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001373 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001374 case CCValAssign::Full: break;
1375 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001376 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001377 break;
1378 }
1379
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001380 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001381 }
1382
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001383 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001384}
1385
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001386/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001387SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001388ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1389 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001390 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001391 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001392 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001393 unsigned LocMemOffset = VA.getLocMemOffset();
1394 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1395 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilsona4c22902009-04-17 19:07:39 +00001396 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner886250c2010-09-21 18:51:21 +00001397 MachinePointerInfo::getStack(LocMemOffset),
David Greene0d0149f2010-02-15 16:55:24 +00001398 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001399}
1400
Andrew Trickef9de2a2013-05-25 02:42:55 +00001401void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001402 SDValue Chain, SDValue &Arg,
1403 RegsToPassVector &RegsToPass,
1404 CCValAssign &VA, CCValAssign &NextVA,
1405 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001406 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001407 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001408
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001409 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001410 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001411 unsigned id = Subtarget->isLittle() ? 0 : 1;
1412 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001413
1414 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001415 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001416 else {
1417 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001418 if (!StackPtr.getNode())
Bob Wilson2e076c42009-06-22 23:27:02 +00001419 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1420
Christian Pirkerb5728192014-05-08 14:06:24 +00001421 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001422 dl, DAG, NextVA,
1423 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001424 }
1425}
1426
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001427/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001428/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1429/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001430SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001431ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001432 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001433 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001434 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001435 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1436 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1437 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001438 SDValue Chain = CLI.Chain;
1439 SDValue Callee = CLI.Callee;
1440 bool &isTailCall = CLI.IsTailCall;
1441 CallingConv::ID CallConv = CLI.CallConv;
1442 bool doesNotRet = CLI.DoesNotReturn;
1443 bool isVarArg = CLI.IsVarArg;
1444
Dale Johannesend679ff72010-06-03 21:09:53 +00001445 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001446 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1447 bool isThisReturn = false;
1448 bool isSibCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001449
Bob Wilson8decdc42011-10-07 17:17:49 +00001450 // Disable tail calls if they're not supported.
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001451 if (!Subtarget->supportsTailCall() || MF.getTarget().Options.DisableTailCalls)
Bob Wilson3c9ed762010-08-13 22:43:33 +00001452 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001453
Dale Johannesend679ff72010-06-03 21:09:53 +00001454 if (isTailCall) {
1455 // Check if it's really possible to do a tail call.
1456 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001457 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001458 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001459 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1460 report_fatal_error("failed to perform tail call elimination on a call "
1461 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001462 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1463 // detected sibcalls.
1464 if (isTailCall) {
1465 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001466 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001467 }
1468 }
Evan Cheng10043e22007-01-19 07:51:42 +00001469
Bob Wilsona4c22902009-04-17 19:07:39 +00001470 // Analyze operands of the call, assigning locations to each operand.
1471 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001472 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1473 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001474 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001475 CCAssignFnForNode(CallConv, /* Return*/ false,
1476 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001477
Bob Wilsona4c22902009-04-17 19:07:39 +00001478 // Get a count of how many bytes are to be pushed on the stack.
1479 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001480
Dale Johannesend679ff72010-06-03 21:09:53 +00001481 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001482 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001483 NumBytes = 0;
1484
Evan Cheng10043e22007-01-19 07:51:42 +00001485 // Adjust the stack pointer for the new arguments...
1486 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001487 if (!isSibCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00001488 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
1489 dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001490
Jim Grosbach6ad4bcb2010-02-24 01:43:03 +00001491 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +00001492
Bob Wilson2e076c42009-06-22 23:27:02 +00001493 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001494 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001495
Bob Wilsona4c22902009-04-17 19:07:39 +00001496 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001497 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001498 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1499 i != e;
1500 ++i, ++realArgIdx) {
1501 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001502 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001503 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001504 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001505
Bob Wilsona4c22902009-04-17 19:07:39 +00001506 // Promote the value if needed.
1507 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001508 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001509 case CCValAssign::Full: break;
1510 case CCValAssign::SExt:
1511 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1512 break;
1513 case CCValAssign::ZExt:
1514 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1515 break;
1516 case CCValAssign::AExt:
1517 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1518 break;
1519 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001520 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001521 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001522 }
1523
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001524 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001525 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001526 if (VA.getLocVT() == MVT::v2f64) {
1527 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1528 DAG.getConstant(0, MVT::i32));
1529 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1530 DAG.getConstant(1, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001531
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001532 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001533 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1534
1535 VA = ArgLocs[++i]; // skip ahead to next loc
1536 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001537 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001538 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1539 } else {
1540 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001541
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001542 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1543 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001544 }
1545 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001546 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001547 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001548 }
1549 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001550 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1551 assert(VA.getLocVT() == MVT::i32 &&
1552 "unexpected calling convention register assignment");
1553 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001554 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001555 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001556 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001557 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001558 } else if (isByVal) {
1559 assert(VA.isMemLoc());
1560 unsigned offset = 0;
1561
1562 // True if this byval aggregate will be split between registers
1563 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001564 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
1565 unsigned CurByValIdx = CCInfo.getInRegsParamsProceed();
1566
1567 if (CurByValIdx < ByValArgsCount) {
1568
1569 unsigned RegBegin, RegEnd;
1570 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1571
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001572 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1573 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001574 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001575 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1576 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1577 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1578 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001579 false, false, false,
1580 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001581 MemOpChains.push_back(Load.getValue(1));
1582 RegsToPass.push_back(std::make_pair(j, Load));
1583 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001584
1585 // If parameter size outsides register area, "offset" value
1586 // helps us to calculate stack slot for remained part properly.
1587 offset = RegEnd - RegBegin;
1588
1589 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001590 }
1591
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001592 if (Flags.getByValSize() > 4*offset) {
Manman Ren9f911162012-06-01 02:44:42 +00001593 unsigned LocMemOffset = VA.getLocMemOffset();
1594 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1595 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1596 StkPtrOff);
1597 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1598 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1599 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1600 MVT::i32);
Manman Rene8735522012-06-01 19:33:18 +00001601 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001602
Manman Ren9f911162012-06-01 02:44:42 +00001603 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001604 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001605 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001606 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001607 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001608 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001609 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001610
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001611 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1612 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001613 }
Evan Cheng10043e22007-01-19 07:51:42 +00001614 }
1615
1616 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001617 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001618
1619 // Build a sequence of copy-to-reg nodes chained together with token chain
1620 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001621 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001622 // Tail call byval lowering might overwrite argument registers so in case of
1623 // tail call optimization the copies to registers are lowered later.
1624 if (!isTailCall)
1625 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1626 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1627 RegsToPass[i].second, InFlag);
1628 InFlag = Chain.getValue(1);
1629 }
Evan Cheng10043e22007-01-19 07:51:42 +00001630
Dale Johannesend679ff72010-06-03 21:09:53 +00001631 // For tail calls lower the arguments to the 'real' stack slot.
1632 if (isTailCall) {
1633 // Force all the incoming stack arguments to be loaded from the stack
1634 // before any new outgoing arguments are stored to the stack, because the
1635 // outgoing stack slots may alias the incoming argument stack slots, and
1636 // the alias isn't otherwise explicit. This is slightly more conservative
1637 // than necessary, because it means that each store effectively depends
1638 // on every argument instead of just those arguments it would clobber.
1639
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001640 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001641 InFlag = SDValue();
1642 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1643 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1644 RegsToPass[i].second, InFlag);
1645 InFlag = Chain.getValue(1);
1646 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001647 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001648 }
1649
Bill Wendling24c79f22008-09-16 21:48:12 +00001650 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1651 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1652 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001653 bool isDirect = false;
1654 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001655 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001656 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001657
1658 if (EnableARMLongCalls) {
1659 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1660 && "long-calls with non-static relocation model!");
1661 // Handle a global address or an external symbol. If it's not one of
1662 // those, the target's already in a register, so we don't need to do
1663 // anything extra.
1664 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001665 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001666 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001667 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001668 ARMConstantPoolValue *CPV =
1669 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1670
Jim Grosbach32bb3622010-04-14 22:28:31 +00001671 // Get the address of the callee into a register
1672 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1673 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1674 Callee = DAG.getLoad(getPointerTy(), dl,
1675 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001676 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001677 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001678 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1679 const char *Sym = S->getSymbol();
1680
1681 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001682 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001683 ARMConstantPoolValue *CPV =
1684 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1685 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001686 // Get the address of the callee into a register
1687 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1688 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1689 Callee = DAG.getLoad(getPointerTy(), dl,
1690 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001691 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001692 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001693 }
1694 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001695 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001696 isDirect = true;
Chris Lattner55452c22009-07-15 04:12:33 +00001697 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Tim Northoverd6a729b2014-01-06 14:28:05 +00001698 bool isStub = (isExt && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001699 getTargetMachine().getRelocationModel() != Reloc::Static;
1700 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc3c949b42007-06-19 21:05:09 +00001701 // ARM call to a local ARM function is predicable.
Evan Chengf128bdc2010-06-16 07:35:02 +00001702 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001703 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001704 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001705 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Tim Northover72360d22013-12-02 10:35:41 +00001706 Callee = DAG.getNode(ARMISD::WrapperPIC, dl, getPointerTy(),
1707 DAG.getTargetGlobalAddress(GV, dl, getPointerTy()));
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001708 } else {
1709 // On ELF targets for PIC code, direct calls should go through the PLT
1710 unsigned OpFlags = 0;
1711 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001712 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001713 OpFlags = ARMII::MO_PLT;
1714 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1715 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001716 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001717 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001718 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001719 getTargetMachine().getRelocationModel() != Reloc::Static;
1720 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng83f35172007-01-30 20:37:08 +00001721 // tBX takes a register source operand.
1722 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001723 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001724 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001725 ARMConstantPoolValue *CPV =
1726 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1727 ARMPCLabelIndex, 4);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001728 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001729 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen021052a2009-02-04 20:06:27 +00001730 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Chengcdbb70c2009-10-31 03:39:36 +00001731 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001732 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001733 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00001734 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson7117a912009-03-20 22:42:55 +00001735 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen021052a2009-02-04 20:06:27 +00001736 getPointerTy(), Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001737 } else {
1738 unsigned OpFlags = 0;
1739 // On ELF targets for PIC code, direct calls should go through the PLT
1740 if (Subtarget->isTargetELF() &&
1741 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1742 OpFlags = ARMII::MO_PLT;
1743 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1744 }
Evan Cheng10043e22007-01-19 07:51:42 +00001745 }
1746
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001747 // FIXME: handle tail calls differently.
1748 unsigned CallOpc;
Tim Northoverdee86042013-12-02 14:46:26 +00001749 bool HasMinSizeAttr = Subtarget->isMinSize();
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001750 if (Subtarget->isThumb()) {
1751 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001752 CallOpc = ARMISD::CALL_NOLINK;
1753 else
1754 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1755 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001756 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001757 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001758 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet8e1fe842012-11-02 21:32:17 +00001759 // Emit regular call when code size is the priority
1760 !HasMinSizeAttr)
Evan Cheng65f9d192012-02-28 18:51:51 +00001761 // "mov lr, pc; b _foo" to avoid confusing the RSP
1762 CallOpc = ARMISD::CALL_NOLINK;
1763 else
1764 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001765 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001766
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001767 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001768 Ops.push_back(Chain);
1769 Ops.push_back(Callee);
1770
1771 // Add argument registers to the end of the list so that they are known live
1772 // into the call.
1773 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1774 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1775 RegsToPass[i].second.getValueType()));
1776
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001777 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001778 if (!isTailCall) {
1779 const uint32_t *Mask;
1780 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1781 const ARMBaseRegisterInfo *ARI = static_cast<const ARMBaseRegisterInfo*>(TRI);
1782 if (isThisReturn) {
1783 // For 'this' returns, use the R0-preserving mask if applicable
1784 Mask = ARI->getThisReturnPreservedMask(CallConv);
1785 if (!Mask) {
1786 // Set isThisReturn to false if the calling convention is not one that
1787 // allows 'returned' to be modeled in this way, so LowerCallResult does
1788 // not try to pass 'this' straight through
1789 isThisReturn = false;
1790 Mask = ARI->getCallPreservedMask(CallConv);
1791 }
1792 } else
Stephen Linff7fcee2013-06-26 21:42:14 +00001793 Mask = ARI->getCallPreservedMask(CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001794
Matthias Braunc22630e2013-10-04 16:52:54 +00001795 assert(Mask && "Missing call preserved mask for calling convention");
1796 Ops.push_back(DAG.getRegisterMask(Mask));
1797 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001798
Gabor Greiff304a7a2008-08-28 21:40:38 +00001799 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001800 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001801
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001802 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001803 if (isTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001804 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Dale Johannesend679ff72010-06-03 21:09:53 +00001805
Duncan Sands739a0542008-07-02 17:40:58 +00001806 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001807 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001808 InFlag = Chain.getValue(1);
1809
Chris Lattner27539552008-10-11 22:08:30 +00001810 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001811 DAG.getIntPtrConstant(0, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001812 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001813 InFlag = Chain.getValue(1);
1814
Bob Wilsona4c22902009-04-17 19:07:39 +00001815 // Handle result values, copying them out of physregs into vregs that we
1816 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001817 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001818 InVals, isThisReturn,
1819 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001820}
1821
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001822/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001823/// on the stack. Remember the next parameter register to allocate,
1824/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001825/// this.
1826void
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001827ARMTargetLowering::HandleByVal(
1828 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001829 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1830 assert((State->getCallOrPrologue() == Prologue ||
1831 State->getCallOrPrologue() == Call) &&
1832 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001833
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001834 if ((ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001835 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1836 unsigned AlignInRegs = Align / 4;
1837 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1838 for (unsigned i = 0; i < Waste; ++i)
1839 reg = State->AllocateReg(GPRArgRegs, 4);
1840 }
1841 if (reg != 0) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001842 unsigned excess = 4 * (ARM::R4 - reg);
1843
1844 // Special case when NSAA != SP and parameter size greater than size of
1845 // all remained GPR regs. In that case we can't split parameter, we must
1846 // send it to stack. We also must set NCRN to R4, so waste all
1847 // remained registers.
Oliver Stannardd55e1152014-03-05 15:25:27 +00001848 const unsigned NSAAOffset = State->getNextStackOffset();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001849 if (Subtarget->isAAPCS_ABI() && NSAAOffset != 0 && size > excess) {
1850 while (State->AllocateReg(GPRArgRegs, 4))
1851 ;
1852 return;
1853 }
1854
1855 // First register for byval parameter is the first register that wasn't
1856 // allocated before this method call, so it would be "reg".
1857 // If parameter is small enough to be saved in range [reg, r4), then
1858 // the end (first after last) register would be reg + param-size-in-regs,
1859 // else parameter would be splitted between registers and stack,
1860 // end register would be r4 in this case.
1861 unsigned ByValRegBegin = reg;
Stepan Dyatkovskiy2703bca2013-05-08 14:51:27 +00001862 unsigned ByValRegEnd = (size < excess) ? reg + size/4 : (unsigned)ARM::R4;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001863 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1864 // Note, first register is allocated in the beginning of function already,
1865 // allocate remained amount of registers we need.
1866 for (unsigned i = reg+1; i != ByValRegEnd; ++i)
1867 State->AllocateReg(GPRArgRegs, 4);
Oliver Stannardd55e1152014-03-05 15:25:27 +00001868 // A byval parameter that is split between registers and memory needs its
1869 // size truncated here.
1870 // In the case where the entire structure fits in registers, we set the
1871 // size in memory to zero.
1872 if (size < excess)
1873 size = 0;
1874 else
1875 size -= excess;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001876 }
1877 }
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001878}
1879
Dale Johannesend679ff72010-06-03 21:09:53 +00001880/// MatchingStackOffset - Return true if the given stack call argument is
1881/// already available in the same position (relatively) of the caller's
1882/// incoming argument stack.
1883static
1884bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1885 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00001886 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001887 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1888 int FI = INT_MAX;
1889 if (Arg.getOpcode() == ISD::CopyFromReg) {
1890 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001891 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00001892 return false;
1893 MachineInstr *Def = MRI->getVRegDef(VR);
1894 if (!Def)
1895 return false;
1896 if (!Flags.isByVal()) {
1897 if (!TII->isLoadFromStackSlot(Def, FI))
1898 return false;
1899 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00001900 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00001901 }
1902 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1903 if (Flags.isByVal())
1904 // ByVal argument is passed in as a pointer but it's now being
1905 // dereferenced. e.g.
1906 // define @foo(%struct.X* %A) {
1907 // tail call @bar(%struct.X* byval %A)
1908 // }
1909 return false;
1910 SDValue Ptr = Ld->getBasePtr();
1911 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1912 if (!FINode)
1913 return false;
1914 FI = FINode->getIndex();
1915 } else
1916 return false;
1917
1918 assert(FI != INT_MAX);
1919 if (!MFI->isFixedObjectIndex(FI))
1920 return false;
1921 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1922}
1923
1924/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1925/// for tail call optimization. Targets which want to do tail call
1926/// optimization should implement this function.
1927bool
1928ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1929 CallingConv::ID CalleeCC,
1930 bool isVarArg,
1931 bool isCalleeStructRet,
1932 bool isCallerStructRet,
1933 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001934 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00001935 const SmallVectorImpl<ISD::InputArg> &Ins,
1936 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00001937 const Function *CallerF = DAG.getMachineFunction().getFunction();
1938 CallingConv::ID CallerCC = CallerF->getCallingConv();
1939 bool CCMatch = CallerCC == CalleeCC;
1940
1941 // Look for obvious safe cases to perform tail call optimization that do not
1942 // require ABI changes. This is what gcc calls sibcall.
1943
Jim Grosbache3864cc2010-06-16 23:45:49 +00001944 // Do not sibcall optimize vararg calls unless the call site is not passing
1945 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00001946 if (isVarArg && !Outs.empty())
1947 return false;
1948
Tim Northoverd8407452013-10-01 14:33:28 +00001949 // Exception-handling functions need a special set of instructions to indicate
1950 // a return to the hardware. Tail-calling another function would probably
1951 // break this.
1952 if (CallerF->hasFnAttribute("interrupt"))
1953 return false;
1954
Dale Johannesend679ff72010-06-03 21:09:53 +00001955 // Also avoid sibcall optimization if either caller or callee uses struct
1956 // return semantics.
1957 if (isCalleeStructRet || isCallerStructRet)
1958 return false;
1959
Dale Johannesend24c66b2010-06-23 18:52:34 +00001960 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach3840c902011-07-08 20:18:11 +00001961 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1962 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1963 // support in the assembler and linker to be used. This would need to be
1964 // fixed to fully support tail calls in Thumb1.
1965 //
Dale Johannesene2289282010-07-08 01:18:23 +00001966 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1967 // LR. This means if we need to reload LR, it takes an extra instructions,
1968 // which outweighs the value of the tail call; but here we don't know yet
1969 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach535d3b42010-09-08 03:54:02 +00001970 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesene2289282010-07-08 01:18:23 +00001971 // emitEpilogue if LR is used.
Dale Johannesene2289282010-07-08 01:18:23 +00001972
1973 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1974 // but we need to make sure there are enough registers; the only valid
1975 // registers are the 4 used for parameters. We don't currently do this
1976 // case.
Evan Chengd4b08732010-11-30 23:55:39 +00001977 if (Subtarget->isThumb1Only())
1978 return false;
Dale Johannesen3ac52b32010-06-18 18:13:11 +00001979
Dale Johannesend679ff72010-06-03 21:09:53 +00001980 // If the calling conventions do not match, then we'd better make sure the
1981 // results are returned in the same way as what the caller expects.
1982 if (!CCMatch) {
1983 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwarich89019782011-06-10 20:59:24 +00001984 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1985 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001986 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1987
1988 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwarich89019782011-06-10 20:59:24 +00001989 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1990 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001991 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1992
1993 if (RVLocs1.size() != RVLocs2.size())
1994 return false;
1995 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1996 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1997 return false;
1998 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1999 return false;
2000 if (RVLocs1[i].isRegLoc()) {
2001 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2002 return false;
2003 } else {
2004 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2005 return false;
2006 }
2007 }
2008 }
2009
Manman Ren7e48b252012-10-12 23:39:43 +00002010 // If Caller's vararg or byval argument has been split between registers and
2011 // stack, do not perform tail call, since part of the argument is in caller's
2012 // local frame.
2013 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
2014 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002015 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00002016 return false;
2017
Dale Johannesend679ff72010-06-03 21:09:53 +00002018 // If the callee takes no arguments then go on to check the results of the
2019 // call.
2020 if (!Outs.empty()) {
2021 // Check if stack adjustment is needed. For now, do not do this if any
2022 // argument is passed on the stack.
2023 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00002024 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
2025 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00002026 CCInfo.AnalyzeCallOperands(Outs,
2027 CCAssignFnForNode(CalleeCC, false, isVarArg));
2028 if (CCInfo.getNextStackOffset()) {
2029 MachineFunction &MF = DAG.getMachineFunction();
2030
2031 // Check if the arguments are already laid out in the right way as
2032 // the caller's fixed stack objects.
2033 MachineFrameInfo *MFI = MF.getFrameInfo();
2034 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topper07720d82012-03-25 23:49:58 +00002035 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002036 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
2037 i != e;
2038 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002039 CCValAssign &VA = ArgLocs[i];
2040 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002041 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002042 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00002043 if (VA.getLocInfo() == CCValAssign::Indirect)
2044 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002045 if (VA.needsCustom()) {
2046 // f64 and vector types are split into multiple registers or
2047 // register/stack-slot combinations. The types will not match
2048 // the registers; give up on memory f64 refs until we figure
2049 // out what to do about this.
2050 if (!VA.isRegLoc())
2051 return false;
2052 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002053 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002054 if (RegVT == MVT::v2f64) {
2055 if (!ArgLocs[++i].isRegLoc())
2056 return false;
2057 if (!ArgLocs[++i].isRegLoc())
2058 return false;
2059 }
2060 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002061 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2062 MFI, MRI, TII))
2063 return false;
2064 }
2065 }
2066 }
2067 }
2068
2069 return true;
2070}
2071
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002072bool
2073ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2074 MachineFunction &MF, bool isVarArg,
2075 const SmallVectorImpl<ISD::OutputArg> &Outs,
2076 LLVMContext &Context) const {
2077 SmallVector<CCValAssign, 16> RVLocs;
2078 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context);
2079 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2080 isVarArg));
2081}
2082
Tim Northoverd8407452013-10-01 14:33:28 +00002083static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2084 SDLoc DL, SelectionDAG &DAG) {
2085 const MachineFunction &MF = DAG.getMachineFunction();
2086 const Function *F = MF.getFunction();
2087
2088 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2089
2090 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2091 // version of the "preferred return address". These offsets affect the return
2092 // instruction if this is a return from PL1 without hypervisor extensions.
2093 // IRQ/FIQ: +4 "subs pc, lr, #4"
2094 // SWI: 0 "subs pc, lr, #0"
2095 // ABORT: +4 "subs pc, lr, #4"
2096 // UNDEF: +4/+2 "subs pc, lr, #0"
2097 // UNDEF varies depending on where the exception came from ARM or Thumb
2098 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2099
2100 int64_t LROffset;
2101 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2102 IntKind == "ABORT")
2103 LROffset = 4;
2104 else if (IntKind == "SWI" || IntKind == "UNDEF")
2105 LROffset = 0;
2106 else
2107 report_fatal_error("Unsupported interrupt attribute. If present, value "
2108 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2109
2110 RetOps.insert(RetOps.begin() + 1, DAG.getConstant(LROffset, MVT::i32, false));
2111
Craig Topper48d114b2014-04-26 18:35:24 +00002112 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002113}
2114
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002115SDValue
2116ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002117 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002118 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002119 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002120 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002121
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002122 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002123 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002124
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002125 // CCState - Info about the registers and stack slots.
Cameron Zwarich89019782011-06-10 20:59:24 +00002126 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2127 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002128
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002129 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002130 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2131 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002132
Bob Wilsona4c22902009-04-17 19:07:39 +00002133 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002134 SmallVector<SDValue, 4> RetOps;
2135 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002136 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002137
2138 // Copy the result values into the output registers.
2139 for (unsigned i = 0, realRVLocIdx = 0;
2140 i != RVLocs.size();
2141 ++i, ++realRVLocIdx) {
2142 CCValAssign &VA = RVLocs[i];
2143 assert(VA.isRegLoc() && "Can only return in registers!");
2144
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002145 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002146
2147 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002148 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002149 case CCValAssign::Full: break;
2150 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002151 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002152 break;
2153 }
2154
Bob Wilsona4c22902009-04-17 19:07:39 +00002155 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002156 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002157 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002158 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2159 DAG.getConstant(0, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002160 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002161 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002162
Christian Pirkerb5728192014-05-08 14:06:24 +00002163 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2164 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2165 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002166 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002167 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002168 VA = RVLocs[++i]; // skip ahead to next loc
2169 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002170 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2171 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002172 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002173 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002174 VA = RVLocs[++i]; // skip ahead to next loc
2175
2176 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002177 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2178 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002179 }
2180 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2181 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002182 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002183 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002184 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2185 fmrrd.getValue(isLittleEndian ? 0 : 1),
2186 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002187 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002188 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002189 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002190 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2191 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002192 Flag);
2193 } else
2194 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2195
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002196 // Guarantee that all emitted copies are
2197 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002198 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002199 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002200 }
2201
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002202 // Update chain and glue.
2203 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002204 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002205 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002206
Tim Northoverd8407452013-10-01 14:33:28 +00002207 // CPUs which aren't M-class use a special sequence to return from
2208 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2209 // though we use "subs pc, lr, #N").
2210 //
2211 // M-class CPUs actually use a normal return sequence with a special
2212 // (hardware-provided) value in LR, so the normal code path works.
2213 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2214 !Subtarget->isMClass()) {
2215 if (Subtarget->isThumb1Only())
2216 report_fatal_error("interrupt attribute is not supported in Thumb1");
2217 return LowerInterruptReturn(RetOps, dl, DAG);
2218 }
2219
Craig Topper48d114b2014-04-26 18:35:24 +00002220 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002221}
2222
Evan Chengf8bad082012-04-10 01:51:00 +00002223bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002224 if (N->getNumValues() != 1)
2225 return false;
2226 if (!N->hasNUsesOfValue(1, 0))
2227 return false;
2228
Evan Chengf8bad082012-04-10 01:51:00 +00002229 SDValue TCChain = Chain;
2230 SDNode *Copy = *N->use_begin();
2231 if (Copy->getOpcode() == ISD::CopyToReg) {
2232 // If the copy has a glue operand, we conservatively assume it isn't safe to
2233 // perform a tail call.
2234 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2235 return false;
2236 TCChain = Copy->getOperand(0);
2237 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2238 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002239 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002240 SmallPtrSet<SDNode*, 2> Copies;
2241 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002242 UI != UE; ++UI) {
2243 if (UI->getOpcode() != ISD::CopyToReg)
2244 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002245 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002246 }
Evan Chengf8bad082012-04-10 01:51:00 +00002247 if (Copies.size() > 2)
2248 return false;
2249
2250 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2251 UI != UE; ++UI) {
2252 SDValue UseChain = UI->getOperand(0);
2253 if (Copies.count(UseChain.getNode()))
2254 // Second CopyToReg
2255 Copy = *UI;
2256 else
2257 // First CopyToReg
2258 TCChain = UseChain;
2259 }
2260 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002261 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002262 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002263 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002264 Copy = *Copy->use_begin();
2265 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002266 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002267 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002268 } else {
2269 return false;
2270 }
2271
Evan Cheng419ea282010-12-01 22:59:46 +00002272 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002273 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2274 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002275 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2276 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002277 return false;
2278 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002279 }
2280
Evan Chengf8bad082012-04-10 01:51:00 +00002281 if (!HasRet)
2282 return false;
2283
2284 Chain = TCChain;
2285 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002286}
2287
Evan Cheng0663f232011-03-21 01:19:09 +00002288bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002289 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002290 return false;
2291
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00002292 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng0663f232011-03-21 01:19:09 +00002293 return false;
2294
2295 return !Subtarget->isThumb1Only();
2296}
2297
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002298// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2299// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2300// one of the above mentioned nodes. It has to be wrapped because otherwise
2301// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2302// be used to form addressing mode. These wrapped nodes will be selected
2303// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002304static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002305 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002306 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002307 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002308 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002309 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002310 if (CP->isMachineConstantPoolEntry())
2311 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2312 CP->getAlignment());
2313 else
2314 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2315 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002316 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002317}
2318
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002319unsigned ARMTargetLowering::getJumpTableEncoding() const {
2320 return MachineJumpTableInfo::EK_Inline;
2321}
2322
Dan Gohman21cea8a2010-04-17 15:26:15 +00002323SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2324 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002325 MachineFunction &MF = DAG.getMachineFunction();
2326 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2327 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002328 SDLoc DL(Op);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002329 EVT PtrVT = getPointerTy();
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002330 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002331 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2332 SDValue CPAddr;
2333 if (RelocM == Reloc::Static) {
2334 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2335 } else {
2336 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002337 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002338 ARMConstantPoolValue *CPV =
2339 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2340 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002341 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2342 }
2343 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2344 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002345 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002346 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002347 if (RelocM == Reloc::Static)
2348 return Result;
Evan Cheng408aa562009-11-06 22:24:13 +00002349 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002350 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002351}
2352
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002353// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002354SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002355ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002356 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002357 SDLoc dl(GA);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002358 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002359 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002360 MachineFunction &MF = DAG.getMachineFunction();
2361 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002362 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002363 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002364 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2365 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002366 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002367 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002368 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattner7727d052010-09-21 06:44:06 +00002369 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002370 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002371 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002372
Evan Cheng408aa562009-11-06 22:24:13 +00002373 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002374 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002375
2376 // call __tls_get_addr.
2377 ArgListTy Args;
2378 ArgListEntry Entry;
2379 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002380 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002381 Args.push_back(Entry);
Dale Johannesen555a3752009-01-30 23:10:59 +00002382 // FIXME: is there useful debug info available here?
Justin Holewinskiaa583972012-05-25 16:35:28 +00002383 TargetLowering::CallLoweringInfo CLI(Chain,
2384 (Type *) Type::getInt32Ty(*DAG.getContext()),
Evan Cheng09c070f2009-08-14 19:11:20 +00002385 false, false, false, false,
Evan Cheng65f9d192012-02-28 18:51:51 +00002386 0, CallingConv::C, /*isTailCall=*/false,
2387 /*doesNotRet=*/false, /*isReturnValueUsed=*/true,
Bill Wendling78c5b7a2010-03-02 01:55:18 +00002388 DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
Justin Holewinskiaa583972012-05-25 16:35:28 +00002389 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002390 return CallResult.first;
2391}
2392
2393// Lower ISD::GlobalTLSAddress using the "initial exec" or
2394// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002395SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002396ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002397 SelectionDAG &DAG,
2398 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002399 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002400 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002401 SDValue Offset;
2402 SDValue Chain = DAG.getEntryNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002403 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002404 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002405 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002406
Hans Wennborgaea41202012-05-04 09:40:39 +00002407 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002408 MachineFunction &MF = DAG.getMachineFunction();
2409 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002410 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002411 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002412 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2413 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002414 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2415 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2416 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002417 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002418 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002419 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002420 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002421 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002422 Chain = Offset.getValue(1);
2423
Evan Cheng408aa562009-11-06 22:24:13 +00002424 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002425 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002426
Evan Chengcdbb70c2009-10-31 03:39:36 +00002427 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002428 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002429 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002430 } else {
2431 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002432 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002433 ARMConstantPoolValue *CPV =
2434 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002435 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002436 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002437 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002438 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002439 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002440 }
2441
2442 // The address of the thread local variable is the add of the thread
2443 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002444 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002445}
2446
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002447SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002448ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002449 // TODO: implement the "local dynamic" model
2450 assert(Subtarget->isTargetELF() &&
2451 "TLS not implemented for non-ELF targets");
2452 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgaea41202012-05-04 09:40:39 +00002453
2454 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2455
2456 switch (model) {
2457 case TLSModel::GeneralDynamic:
2458 case TLSModel::LocalDynamic:
2459 return LowerToTLSGeneralDynamicModel(GA, DAG);
2460 case TLSModel::InitialExec:
2461 case TLSModel::LocalExec:
2462 return LowerToTLSExecModels(GA, DAG, model);
2463 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002464 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002465}
2466
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002467SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002468 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002469 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002470 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002471 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002472 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002473 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002474 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002475 ARMConstantPoolConstant::Create(GV,
2476 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002477 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002478 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson7117a912009-03-20 22:42:55 +00002479 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002480 CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002481 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002482 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002483 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002484 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002485 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002486 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002487 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002488 MachinePointerInfo::getGOT(),
2489 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002490 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002491 }
2492
2493 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002494 // pair. This is always cheaper.
2495 if (Subtarget->useMovt()) {
Evan Cheng68aec142011-01-19 02:16:49 +00002496 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002497 // FIXME: Once remat is capable of dealing with instructions with register
2498 // operands, expand this into two nodes.
2499 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2500 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002501 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002502 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2503 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2504 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2505 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002506 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002507 }
2508}
2509
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002510SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002511 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002512 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002513 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002514 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002515 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002516
Tim Northover72360d22013-12-02 10:35:41 +00002517 if (Subtarget->useMovt())
Evan Cheng68aec142011-01-19 02:16:49 +00002518 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002519
Tim Northover72360d22013-12-02 10:35:41 +00002520 // FIXME: Once remat is capable of dealing with instructions with register
2521 // operands, expand this into multiple nodes
2522 unsigned Wrapper =
2523 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002524
Tim Northover72360d22013-12-02 10:35:41 +00002525 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2526 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002527
Evan Cheng1b389522009-09-03 07:04:02 +00002528 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002529 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2530 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002531 return Result;
2532}
2533
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002534SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2535 SelectionDAG &DAG) const {
2536 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
2537 assert(Subtarget->useMovt() && "Windows on ARM expects to use movw/movt");
2538
2539 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2540 EVT PtrVT = getPointerTy();
2541 SDLoc DL(Op);
2542
2543 ++NumMovwMovt;
2544
2545 // FIXME: Once remat is capable of dealing with instructions with register
2546 // operands, expand this into two nodes.
2547 return DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2548 DAG.getTargetGlobalAddress(GV, DL, PtrVT));
2549}
2550
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002551SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002552 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002553 assert(Subtarget->isTargetELF() &&
2554 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002555 MachineFunction &MF = DAG.getMachineFunction();
2556 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002557 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002558 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002559 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002560 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002561 ARMConstantPoolValue *CPV =
2562 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2563 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002564 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002565 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002566 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002567 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002568 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00002569 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002570 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002571}
2572
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002573SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002574ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002575 SDLoc dl(Op);
Jim Grosbachfaa3abb2010-05-27 23:49:24 +00002576 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002577 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2578 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002579 Op.getOperand(1), Val);
2580}
2581
2582SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002583ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002584 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002585 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2586 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2587}
2588
2589SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002590ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002591 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002592 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002593 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002594 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002595 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson17f88782009-08-04 00:25:01 +00002596 case Intrinsic::arm_thread_pointer: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002597 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson17f88782009-08-04 00:25:01 +00002598 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2599 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002600 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002601 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002602 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002603 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002604 EVT PtrVT = getPointerTy();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002605 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2606 SDValue CPAddr;
2607 unsigned PCAdj = (RelocM != Reloc::PIC_)
2608 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002609 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002610 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2611 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002612 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002613 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002614 SDValue Result =
Evan Chengcdbb70c2009-10-31 03:39:36 +00002615 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002616 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002617 false, false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002618
2619 if (RelocM == Reloc::PIC_) {
Evan Cheng408aa562009-11-06 22:24:13 +00002620 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002621 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2622 }
2623 return Result;
2624 }
Evan Cheng18381b42011-03-29 23:06:19 +00002625 case Intrinsic::arm_neon_vmulls:
2626 case Intrinsic::arm_neon_vmullu: {
2627 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2628 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002629 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002630 Op.getOperand(1), Op.getOperand(2));
2631 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002632 }
2633}
2634
Eli Friedman30a49e92011-08-03 21:06:02 +00002635static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2636 const ARMSubtarget *Subtarget) {
2637 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002638 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002639 if (!Subtarget->hasDataBarrier()) {
2640 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2641 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2642 // here.
2643 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002644 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002645 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002646 DAG.getConstant(0, MVT::i32));
2647 }
2648
Tim Northover36b24172013-07-03 09:20:36 +00002649 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2650 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
2651 unsigned Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002652 if (Subtarget->isMClass()) {
2653 // Only a full system barrier exists in the M-class architectures.
2654 Domain = ARM_MB::SY;
2655 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002656 // Swift happens to implement ISHST barriers in a way that's compatible with
2657 // Release semantics but weaker than ISH so we'd be fools not to use
2658 // it. Beware: other processors probably don't!
2659 Domain = ARM_MB::ISHST;
2660 }
2661
Joey Gouly926d3f52013-09-05 15:35:24 +00002662 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
2663 DAG.getConstant(Intrinsic::arm_dmb, MVT::i32),
Tim Northover36b24172013-07-03 09:20:36 +00002664 DAG.getConstant(Domain, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002665}
2666
Evan Cheng8740ee32010-11-03 06:34:55 +00002667static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2668 const ARMSubtarget *Subtarget) {
2669 // ARM pre v5TE and Thumb1 does not have preload instructions.
2670 if (!(Subtarget->isThumb2() ||
2671 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2672 // Just preserve the chain.
2673 return Op.getOperand(0);
2674
Andrew Trickef9de2a2013-05-25 02:42:55 +00002675 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002676 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2677 if (!isRead &&
2678 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2679 // ARMv7 with MP extension has PLDW.
2680 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002681
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002682 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2683 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002684 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002685 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002686 isData = ~isData & 1;
2687 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002688
2689 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng21acf9f2010-11-04 05:19:35 +00002690 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2691 DAG.getConstant(isData, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002692}
2693
Dan Gohman31ae5862010-04-17 14:41:14 +00002694static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2695 MachineFunction &MF = DAG.getMachineFunction();
2696 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2697
Evan Cheng10043e22007-01-19 07:51:42 +00002698 // vastart just stores the address of the VarArgsFrameIndex slot into the
2699 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002700 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002701 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002702 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002703 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002704 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2705 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002706}
2707
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002708SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002709ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2710 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002711 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002712 MachineFunction &MF = DAG.getMachineFunction();
2713 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2714
Craig Topper760b1342012-02-22 05:59:10 +00002715 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002716 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002717 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002718 else
Craig Topperc7242e02012-04-20 07:30:17 +00002719 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002720
2721 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002722 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002723 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002724
2725 SDValue ArgValue2;
2726 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002727 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002728 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002729
2730 // Create load node to retrieve arguments from the stack.
2731 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Chengcdbb70c2009-10-31 03:39:36 +00002732 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002733 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002734 false, false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002735 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002736 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002737 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002738 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002739 if (!Subtarget->isLittle())
2740 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002741 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002742}
2743
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002744void
2745ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002746 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002747 unsigned ArgSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002748 unsigned &ArgRegsSize,
2749 unsigned &ArgRegsSaveSize)
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002750 const {
2751 unsigned NumGPRs;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002752 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2753 unsigned RBegin, REnd;
2754 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2755 NumGPRs = REnd - RBegin;
2756 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002757 unsigned int firstUnalloced;
2758 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2759 sizeof(GPRArgRegs) /
2760 sizeof(GPRArgRegs[0]));
2761 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2762 }
2763
2764 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002765 ArgRegsSize = NumGPRs * 4;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002766
2767 // If parameter is split between stack and GPRs...
Mark Seabornbe266aa2014-02-16 18:59:48 +00002768 if (NumGPRs && Align > 4 &&
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002769 (ArgRegsSize < ArgSize ||
2770 InRegsParamRecordIdx >= CCInfo.getInRegsParamsCount())) {
Mark Seabornbe266aa2014-02-16 18:59:48 +00002771 // Add padding for part of param recovered from GPRs. For example,
2772 // if Align == 8, its last byte must be at address K*8 - 1.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002773 // We need to do it, since remained (stack) part of parameter has
2774 // stack alignment, and we need to "attach" "GPRs head" without gaps
2775 // to it:
2776 // Stack:
2777 // |---- 8 bytes block ----| |---- 8 bytes block ----| |---- 8 bytes...
2778 // [ [padding] [GPRs head] ] [ Tail passed via stack ....
2779 //
2780 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2781 unsigned Padding =
Mark Seabornbe266aa2014-02-16 18:59:48 +00002782 OffsetToAlignment(ArgRegsSize + AFI->getArgRegsSaveSize(), Align);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002783 ArgRegsSaveSize = ArgRegsSize + Padding;
2784 } else
2785 // We don't need to extend regs save size for byval parameters if they
2786 // are passed via GPRs only.
2787 ArgRegsSaveSize = ArgRegsSize;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002788}
2789
2790// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002791// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002792// byval). Either way, we allocate stack slots adjacent to the data
2793// provided by our caller, and store the unallocated registers there.
2794// If this is a variadic function, the va_list pointer will begin with
2795// these values; otherwise, this reassembles a (byval) structure that
2796// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002797// Return: The frame index registers were stored into.
2798int
2799ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002800 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002801 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002802 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002803 unsigned OffsetFromOrigArg,
2804 unsigned ArgOffset,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002805 unsigned ArgSize,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002806 bool ForceMutable,
2807 unsigned ByValStoreOffset,
2808 unsigned TotalArgRegsSaveSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002809
2810 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002811 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002812 // Setup first unallocated register as first byval register;
2813 // eat all remained registers
2814 // (these two actions are performed by HandleByVal method).
2815 // Then, here, we initialize stack frame with
2816 // "store-reg" instructions.
2817 // Case #2. Var-args function, that doesn't contain byval parameters.
2818 // The same: eat all remained unallocated registers,
2819 // initialize stack frame.
2820
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002821 MachineFunction &MF = DAG.getMachineFunction();
2822 MachineFrameInfo *MFI = MF.getFrameInfo();
2823 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002824 unsigned firstRegToSaveIndex, lastRegToSaveIndex;
2825 unsigned RBegin, REnd;
2826 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2827 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2828 firstRegToSaveIndex = RBegin - ARM::R0;
2829 lastRegToSaveIndex = REnd - ARM::R0;
2830 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002831 firstRegToSaveIndex = CCInfo.getFirstUnallocated
Craig Topper58713212013-07-15 04:27:47 +00002832 (GPRArgRegs, array_lengthof(GPRArgRegs));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002833 lastRegToSaveIndex = 4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002834 }
2835
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002836 unsigned ArgRegsSize, ArgRegsSaveSize;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002837 computeRegArea(CCInfo, MF, InRegsParamRecordIdx, ArgSize,
2838 ArgRegsSize, ArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002839
2840 // Store any by-val regs to their spots on the stack so that they may be
2841 // loaded by deferencing the result of formal parameter pointer or va_next.
2842 // Note: once stack area for byval/varargs registers
2843 // was initialized, it can't be initialized again.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002844 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002845 unsigned Padding = ArgRegsSaveSize - ArgRegsSize;
2846
2847 if (Padding) {
2848 assert(AFI->getStoredByValParamsPadding() == 0 &&
2849 "The only parameter may be padded.");
2850 AFI->setStoredByValParamsPadding(Padding);
2851 }
2852
Oliver Stannardd55e1152014-03-05 15:25:27 +00002853 int FrameIndex = MFI->CreateFixedObject(ArgRegsSaveSize,
2854 Padding +
2855 ByValStoreOffset -
2856 (int64_t)TotalArgRegsSaveSize,
2857 false);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002858 SDValue FIN = DAG.getFrameIndex(FrameIndex, getPointerTy());
Oliver Stannardd55e1152014-03-05 15:25:27 +00002859 if (Padding) {
2860 MFI->CreateFixedObject(Padding,
2861 ArgOffset + ByValStoreOffset -
2862 (int64_t)ArgRegsSaveSize,
2863 false);
2864 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002865
2866 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002867 for (unsigned i = 0; firstRegToSaveIndex < lastRegToSaveIndex;
2868 ++firstRegToSaveIndex, ++i) {
Craig Topper760b1342012-02-22 05:59:10 +00002869 const TargetRegisterClass *RC;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002870 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002871 RC = &ARM::tGPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002872 else
Craig Topperc7242e02012-04-20 07:30:17 +00002873 RC = &ARM::GPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002874
2875 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2876 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2877 SDValue Store =
2878 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002879 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002880 false, false, 0);
2881 MemOps.push_back(Store);
2882 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2883 DAG.getConstant(4, getPointerTy()));
2884 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002885
2886 AFI->setArgRegsSaveSize(ArgRegsSaveSize + AFI->getArgRegsSaveSize());
2887
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002888 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002889 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002890 return FrameIndex;
Oliver Stannardd55e1152014-03-05 15:25:27 +00002891 } else {
2892 if (ArgSize == 0) {
2893 // We cannot allocate a zero-byte object for the first variadic argument,
2894 // so just make up a size.
2895 ArgSize = 4;
2896 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002897 // This will point to the next argument passed via stack.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002898 return MFI->CreateFixedObject(
Oliver Stannardd55e1152014-03-05 15:25:27 +00002899 ArgSize, ArgOffset, !ForceMutable);
2900 }
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002901}
2902
2903// Setup stack frame, the va_list pointer will start from.
2904void
2905ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002906 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002907 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002908 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002909 bool ForceMutable) const {
2910 MachineFunction &MF = DAG.getMachineFunction();
2911 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2912
2913 // Try to store any remaining integer argument regs
2914 // to their spots on the stack so that they may be loaded by deferencing
2915 // the result of va_next.
2916 // If there is no regs to be stored, just point address after last
2917 // argument passed via stack.
2918 int FrameIndex =
Craig Topper062a2ba2014-04-25 05:30:21 +00002919 StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
2920 CCInfo.getInRegsParamsCount(), 0, ArgOffset, 0, ForceMutable,
2921 0, TotalArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002922
2923 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002924}
2925
Bob Wilson2e076c42009-06-22 23:27:02 +00002926SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002927ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002928 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002929 const SmallVectorImpl<ISD::InputArg>
2930 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002931 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002932 SmallVectorImpl<SDValue> &InVals)
2933 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00002934 MachineFunction &MF = DAG.getMachineFunction();
2935 MachineFrameInfo *MFI = MF.getFrameInfo();
2936
Bob Wilsona4c22902009-04-17 19:07:39 +00002937 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2938
2939 // Assign locations to all of the incoming arguments.
2940 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00002941 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2942 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002943 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002944 CCAssignFnForNode(CallConv, /* Return*/ false,
2945 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00002946
Bob Wilsona4c22902009-04-17 19:07:39 +00002947 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002948 int lastInsIndex = -1;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002949 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002950 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2951 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002952
2953 // Initially ArgRegsSaveSize is zero.
2954 // Then we increase this value each time we meet byval parameter.
2955 // We also increase this value in case of varargs function.
2956 AFI->setArgRegsSaveSize(0);
2957
Oliver Stannardd55e1152014-03-05 15:25:27 +00002958 unsigned ByValStoreOffset = 0;
2959 unsigned TotalArgRegsSaveSize = 0;
2960 unsigned ArgRegsSaveSizeMaxAlign = 4;
2961
2962 // Calculate the amount of stack space that we need to allocate to store
2963 // byval and variadic arguments that are passed in registers.
2964 // We need to know this before we allocate the first byval or variadic
2965 // argument, as they will be allocated a stack slot below the CFA (Canonical
2966 // Frame Address, the stack pointer at entry to the function).
2967 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2968 CCValAssign &VA = ArgLocs[i];
2969 if (VA.isMemLoc()) {
2970 int index = VA.getValNo();
2971 if (index != lastInsIndex) {
2972 ISD::ArgFlagsTy Flags = Ins[index].Flags;
2973 if (Flags.isByVal()) {
2974 unsigned ExtraArgRegsSize;
2975 unsigned ExtraArgRegsSaveSize;
2976 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsProceed(),
2977 Flags.getByValSize(),
2978 ExtraArgRegsSize, ExtraArgRegsSaveSize);
2979
2980 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
2981 if (Flags.getByValAlign() > ArgRegsSaveSizeMaxAlign)
2982 ArgRegsSaveSizeMaxAlign = Flags.getByValAlign();
2983 CCInfo.nextInRegsParam();
2984 }
2985 lastInsIndex = index;
2986 }
2987 }
2988 }
2989 CCInfo.rewindByValRegsInfo();
2990 lastInsIndex = -1;
2991 if (isVarArg) {
2992 unsigned ExtraArgRegsSize;
2993 unsigned ExtraArgRegsSaveSize;
2994 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsCount(), 0,
2995 ExtraArgRegsSize, ExtraArgRegsSaveSize);
2996 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
2997 }
2998 // If the arg regs save area contains N-byte aligned values, the
2999 // bottom of it must be at least N-byte aligned.
3000 TotalArgRegsSaveSize = RoundUpToAlignment(TotalArgRegsSaveSize, ArgRegsSaveSizeMaxAlign);
3001 TotalArgRegsSaveSize = std::min(TotalArgRegsSaveSize, 16U);
3002
Bob Wilsona4c22902009-04-17 19:07:39 +00003003 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3004 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00003005 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
3006 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsonea09d4a2009-04-17 20:35:10 +00003007 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00003008 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003009 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00003010
Bob Wilsona4c22902009-04-17 19:07:39 +00003011 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003012 // f64 and vector types are split up into multiple registers or
3013 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00003014 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003015 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003016 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00003017 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00003018 SDValue ArgValue2;
3019 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00003020 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson699bdf72010-04-13 22:03:22 +00003021 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3022 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00003023 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003024 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00003025 } else {
3026 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
3027 Chain, DAG, dl);
3028 }
Owen Anderson9f944592009-08-11 20:47:22 +00003029 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
3030 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00003031 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson9f944592009-08-11 20:47:22 +00003032 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00003033 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
3034 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003035 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00003036
Bob Wilson2e076c42009-06-22 23:27:02 +00003037 } else {
Craig Topper760b1342012-02-22 05:59:10 +00003038 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00003039
Owen Anderson9f944592009-08-11 20:47:22 +00003040 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00003041 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003042 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003043 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003044 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00003045 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003046 else if (RegVT == MVT::i32)
Craig Topperc7242e02012-04-20 07:30:17 +00003047 RC = AFI->isThumb1OnlyFunction() ?
3048 (const TargetRegisterClass*)&ARM::tGPRRegClass :
3049 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003050 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003051 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003052
3053 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003054 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003055 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003056 }
3057
3058 // If this is an 8 or 16-bit value, it is really passed promoted
3059 // to 32 bits. Insert an assert[sz]ext to capture this, then
3060 // truncate to the right size.
3061 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003062 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003063 case CCValAssign::Full: break;
3064 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003065 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003066 break;
3067 case CCValAssign::SExt:
3068 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3069 DAG.getValueType(VA.getValVT()));
3070 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3071 break;
3072 case CCValAssign::ZExt:
3073 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3074 DAG.getValueType(VA.getValVT()));
3075 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3076 break;
3077 }
3078
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003079 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003080
3081 } else { // VA.isRegLoc()
3082
3083 // sanity check
3084 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003085 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003086
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003087 int index = ArgLocs[i].getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003088
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003089 // Some Ins[] entries become multiple ArgLoc[] entries.
3090 // Process them only once.
3091 if (index != lastInsIndex)
3092 {
3093 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003094 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003095 // This can be changed with more analysis.
3096 // In case of tail call optimization mark all arguments mutable.
3097 // Since they could be overwritten by lowering of arguments in case of
3098 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003099 if (Flags.isByVal()) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003100 unsigned CurByValIndex = CCInfo.getInRegsParamsProceed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003101
3102 ByValStoreOffset = RoundUpToAlignment(ByValStoreOffset, Flags.getByValAlign());
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003103 int FrameIndex = StoreByValRegs(
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003104 CCInfo, DAG, dl, Chain, CurOrigArg,
3105 CurByValIndex,
3106 Ins[VA.getValNo()].PartOffset,
3107 VA.getLocMemOffset(),
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003108 Flags.getByValSize(),
Oliver Stannardd55e1152014-03-05 15:25:27 +00003109 true /*force mutable frames*/,
3110 ByValStoreOffset,
3111 TotalArgRegsSaveSize);
3112 ByValStoreOffset += Flags.getByValSize();
3113 ByValStoreOffset = std::min(ByValStoreOffset, 16U);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003114 InVals.push_back(DAG.getFrameIndex(FrameIndex, getPointerTy()));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003115 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003116 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003117 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003118 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003119 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003120
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003121 // Create load nodes to retrieve arguments from the stack.
3122 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3123 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3124 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003125 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003126 }
3127 lastInsIndex = index;
3128 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003129 }
3130 }
3131
3132 // varargs
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003133 if (isVarArg)
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003134 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003135 CCInfo.getNextStackOffset(),
3136 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003137
Oliver Stannardb14c6252014-04-02 16:10:33 +00003138 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3139
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003140 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003141}
3142
3143/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003144static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003145 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003146 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003147 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003148 // Maybe this has already been legalized into the constant pool?
3149 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003150 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003151 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003152 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003153 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003154 }
3155 }
3156 return false;
3157}
3158
Evan Cheng10043e22007-01-19 07:51:42 +00003159/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3160/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003161SDValue
3162ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003163 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003164 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003165 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003166 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003167 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003168 // Constant does not fit, try adjusting it by one?
3169 switch (CC) {
3170 default: break;
3171 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003172 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003173 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003174 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003175 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003176 }
3177 break;
3178 case ISD::SETULT:
3179 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003180 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003181 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003182 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003183 }
3184 break;
3185 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003186 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003187 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003188 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003189 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003190 }
3191 break;
3192 case ISD::SETULE:
3193 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003194 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003195 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003196 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003197 }
3198 break;
3199 }
3200 }
3201 }
3202
3203 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003204 ARMISD::NodeType CompareType;
3205 switch (CondCode) {
3206 default:
3207 CompareType = ARMISD::CMP;
3208 break;
3209 case ARMCC::EQ:
3210 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003211 // Uses only Z Flag
3212 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003213 break;
3214 }
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003215 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003216 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003217}
3218
3219/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003220SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003221ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003222 SDLoc dl) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003223 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003224 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003225 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003226 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003227 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3228 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003229}
3230
Bob Wilson45acbd02011-03-08 01:17:20 +00003231/// duplicateCmp - Glue values can have only one use, so this function
3232/// duplicates a comparison node.
3233SDValue
3234ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3235 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003236 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003237 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3238 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3239
3240 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3241 Cmp = Cmp.getOperand(0);
3242 Opc = Cmp.getOpcode();
3243 if (Opc == ARMISD::CMPFP)
3244 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3245 else {
3246 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3247 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3248 }
3249 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3250}
3251
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003252std::pair<SDValue, SDValue>
3253ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3254 SDValue &ARMcc) const {
3255 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3256
3257 SDValue Value, OverflowCmp;
3258 SDValue LHS = Op.getOperand(0);
3259 SDValue RHS = Op.getOperand(1);
3260
3261
3262 // FIXME: We are currently always generating CMPs because we don't support
3263 // generating CMN through the backend. This is not as good as the natural
3264 // CMP case because it causes a register dependency and cannot be folded
3265 // later.
3266
3267 switch (Op.getOpcode()) {
3268 default:
3269 llvm_unreachable("Unknown overflow instruction!");
3270 case ISD::SADDO:
3271 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3272 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3273 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3274 break;
3275 case ISD::UADDO:
3276 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3277 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3278 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3279 break;
3280 case ISD::SSUBO:
3281 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3282 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3283 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3284 break;
3285 case ISD::USUBO:
3286 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3287 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3288 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3289 break;
3290 } // switch (...)
3291
3292 return std::make_pair(Value, OverflowCmp);
3293}
3294
3295
3296SDValue
3297ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3298 // Let legalize expand this if it isn't a legal type yet.
3299 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3300 return SDValue();
3301
3302 SDValue Value, OverflowCmp;
3303 SDValue ARMcc;
3304 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3305 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3306 // We use 0 and 1 as false and true values.
3307 SDValue TVal = DAG.getConstant(1, MVT::i32);
3308 SDValue FVal = DAG.getConstant(0, MVT::i32);
3309 EVT VT = Op.getValueType();
3310
3311 SDValue Overflow = DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, TVal, FVal,
3312 ARMcc, CCR, OverflowCmp);
3313
3314 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
3315 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), VTs, Value, Overflow);
3316}
3317
3318
Bill Wendling6a981312010-08-11 08:43:16 +00003319SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3320 SDValue Cond = Op.getOperand(0);
3321 SDValue SelectTrue = Op.getOperand(1);
3322 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003323 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003324 unsigned Opc = Cond.getOpcode();
3325
3326 if (Cond.getResNo() == 1 &&
3327 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3328 Opc == ISD::USUBO)) {
3329 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3330 return SDValue();
3331
3332 SDValue Value, OverflowCmp;
3333 SDValue ARMcc;
3334 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3335 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3336 EVT VT = Op.getValueType();
3337
3338 return DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, SelectTrue, SelectFalse,
3339 ARMcc, CCR, OverflowCmp);
3340
3341 }
Bill Wendling6a981312010-08-11 08:43:16 +00003342
3343 // Convert:
3344 //
3345 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3346 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3347 //
3348 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3349 const ConstantSDNode *CMOVTrue =
3350 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3351 const ConstantSDNode *CMOVFalse =
3352 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3353
3354 if (CMOVTrue && CMOVFalse) {
3355 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3356 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3357
3358 SDValue True;
3359 SDValue False;
3360 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3361 True = SelectTrue;
3362 False = SelectFalse;
3363 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3364 True = SelectFalse;
3365 False = SelectTrue;
3366 }
3367
3368 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003369 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003370 SDValue ARMcc = Cond.getOperand(2);
3371 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003372 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003373 assert(True.getValueType() == VT);
3374 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendling6a981312010-08-11 08:43:16 +00003375 }
3376 }
3377 }
3378
Dan Gohmand4a77c42012-02-24 00:09:36 +00003379 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3380 // undefined bits before doing a full-word comparison with zero.
3381 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
3382 DAG.getConstant(1, Cond.getValueType()));
3383
Bill Wendling6a981312010-08-11 08:43:16 +00003384 return DAG.getSelectCC(dl, Cond,
3385 DAG.getConstant(0, Cond.getValueType()),
3386 SelectTrue, SelectFalse, ISD::SETNE);
3387}
3388
Joey Gouly881eab52013-08-22 15:29:11 +00003389static ISD::CondCode getInverseCCForVSEL(ISD::CondCode CC) {
3390 if (CC == ISD::SETNE)
3391 return ISD::SETEQ;
Weiming Zhao63871d22013-12-18 22:25:17 +00003392 return ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003393}
3394
3395static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3396 bool &swpCmpOps, bool &swpVselOps) {
3397 // Start by selecting the GE condition code for opcodes that return true for
3398 // 'equality'
3399 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3400 CC == ISD::SETULE)
3401 CondCode = ARMCC::GE;
3402
3403 // and GT for opcodes that return false for 'equality'.
3404 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3405 CC == ISD::SETULT)
3406 CondCode = ARMCC::GT;
3407
3408 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3409 // to swap the compare operands.
3410 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3411 CC == ISD::SETULT)
3412 swpCmpOps = true;
3413
3414 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3415 // If we have an unordered opcode, we need to swap the operands to the VSEL
3416 // instruction (effectively negating the condition).
3417 //
3418 // This also has the effect of swapping which one of 'less' or 'greater'
3419 // returns true, so we also swap the compare operands. It also switches
3420 // whether we return true for 'equality', so we compensate by picking the
3421 // opposite condition code to our original choice.
3422 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3423 CC == ISD::SETUGT) {
3424 swpCmpOps = !swpCmpOps;
3425 swpVselOps = !swpVselOps;
3426 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3427 }
3428
3429 // 'ordered' is 'anything but unordered', so use the VS condition code and
3430 // swap the VSEL operands.
3431 if (CC == ISD::SETO) {
3432 CondCode = ARMCC::VS;
3433 swpVselOps = true;
3434 }
3435
3436 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3437 // code and swap the VSEL operands.
3438 if (CC == ISD::SETUNE) {
3439 CondCode = ARMCC::EQ;
3440 swpVselOps = true;
3441 }
3442}
3443
Dan Gohman21cea8a2010-04-17 15:26:15 +00003444SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003445 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003446 SDValue LHS = Op.getOperand(0);
3447 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003448 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003449 SDValue TrueVal = Op.getOperand(2);
3450 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003451 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003452
Owen Anderson9f944592009-08-11 20:47:22 +00003453 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003454 // Try to generate VSEL on ARMv8.
3455 // The VSEL instruction can't use all the usual ARM condition
3456 // codes: it only has two bits to select the condition code, so it's
3457 // constrained to use only GE, GT, VS and EQ.
3458 //
3459 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3460 // swap the operands of the previous compare instruction (effectively
3461 // inverting the compare condition, swapping 'less' and 'greater') and
3462 // sometimes need to swap the operands to the VSEL (which inverts the
3463 // condition in the sense of firing whenever the previous condition didn't)
Joey Goulyccd04892013-09-13 13:46:57 +00003464 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003465 TrueVal.getValueType() == MVT::f64)) {
3466 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3467 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3468 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
3469 CC = getInverseCCForVSEL(CC);
3470 std::swap(TrueVal, FalseVal);
3471 }
3472 }
3473
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003474 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003475 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003476 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Joey Gouly881eab52013-08-22 15:29:11 +00003477 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3478 Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003479 }
3480
3481 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003482 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003483
Joey Gouly881eab52013-08-22 15:29:11 +00003484 // Try to generate VSEL on ARMv8.
Joey Goulyccd04892013-09-13 13:46:57 +00003485 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003486 TrueVal.getValueType() == MVT::f64)) {
Joey Goulye3dd6842013-08-23 12:01:13 +00003487 // We can select VMAXNM/VMINNM from a compare followed by a select with the
3488 // same operands, as follows:
3489 // c = fcmp [ogt, olt, ugt, ult] a, b
3490 // select c, a, b
3491 // We only do this in unsafe-fp-math, because signed zeros and NaNs are
3492 // handled differently than the original code sequence.
3493 if (getTargetMachine().Options.UnsafeFPMath && LHS == TrueVal &&
3494 RHS == FalseVal) {
3495 if (CC == ISD::SETOGT || CC == ISD::SETUGT)
3496 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal);
3497 if (CC == ISD::SETOLT || CC == ISD::SETULT)
3498 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal);
3499 }
3500
Joey Gouly881eab52013-08-22 15:29:11 +00003501 bool swpCmpOps = false;
3502 bool swpVselOps = false;
3503 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3504
3505 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3506 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3507 if (swpCmpOps)
3508 std::swap(LHS, RHS);
3509 if (swpVselOps)
3510 std::swap(TrueVal, FalseVal);
3511 }
3512 }
3513
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003514 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3515 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003516 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00003517 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003518 ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003519 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003520 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003521 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003522 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson7117a912009-03-20 22:42:55 +00003523 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003524 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Cheng10043e22007-01-19 07:51:42 +00003525 }
3526 return Result;
3527}
3528
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003529/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3530/// to morph to an integer compare sequence.
3531static bool canChangeToInt(SDValue Op, bool &SeenZero,
3532 const ARMSubtarget *Subtarget) {
3533 SDNode *N = Op.getNode();
3534 if (!N->hasOneUse())
3535 // Otherwise it requires moving the value from fp to integer registers.
3536 return false;
3537 if (!N->getNumValues())
3538 return false;
3539 EVT VT = Op.getValueType();
3540 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3541 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3542 // vmrs are very slow, e.g. cortex-a8.
3543 return false;
3544
3545 if (isFloatingPointZero(Op)) {
3546 SeenZero = true;
3547 return true;
3548 }
3549 return ISD::isNormalLoad(N);
3550}
3551
3552static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3553 if (isFloatingPointZero(Op))
3554 return DAG.getConstant(0, MVT::i32);
3555
3556 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003557 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003558 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003559 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003560 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003561
3562 llvm_unreachable("Unknown VFP cmp argument!");
3563}
3564
3565static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3566 SDValue &RetVal1, SDValue &RetVal2) {
3567 if (isFloatingPointZero(Op)) {
3568 RetVal1 = DAG.getConstant(0, MVT::i32);
3569 RetVal2 = DAG.getConstant(0, MVT::i32);
3570 return;
3571 }
3572
3573 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3574 SDValue Ptr = Ld->getBasePtr();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003575 RetVal1 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003576 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003577 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003578 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003579 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003580
3581 EVT PtrType = Ptr.getValueType();
3582 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003583 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003584 PtrType, Ptr, DAG.getConstant(4, PtrType));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003585 RetVal2 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003586 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003587 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003588 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003589 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003590 return;
3591 }
3592
3593 llvm_unreachable("Unknown VFP cmp argument!");
3594}
3595
3596/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3597/// f32 and even f64 comparisons to integer ones.
3598SDValue
3599ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3600 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003601 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003602 SDValue LHS = Op.getOperand(2);
3603 SDValue RHS = Op.getOperand(3);
3604 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003605 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003606
Evan Chengd12af5d2012-03-01 23:27:13 +00003607 bool LHSSeenZero = false;
3608 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3609 bool RHSSeenZero = false;
3610 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3611 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003612 // If unsafe fp math optimization is enabled and there are no other uses of
3613 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003614 // to an integer comparison.
3615 if (CC == ISD::SETOEQ)
3616 CC = ISD::SETEQ;
3617 else if (CC == ISD::SETUNE)
3618 CC = ISD::SETNE;
3619
Evan Chengd12af5d2012-03-01 23:27:13 +00003620 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003621 SDValue ARMcc;
3622 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003623 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3624 bitcastf32Toi32(LHS, DAG), Mask);
3625 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3626 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003627 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3628 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3629 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3630 Chain, Dest, ARMcc, CCR, Cmp);
3631 }
3632
3633 SDValue LHS1, LHS2;
3634 SDValue RHS1, RHS2;
3635 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3636 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003637 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3638 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003639 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3640 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003641 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003642 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003643 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003644 }
3645
3646 return SDValue();
3647}
3648
3649SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3650 SDValue Chain = Op.getOperand(0);
3651 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3652 SDValue LHS = Op.getOperand(2);
3653 SDValue RHS = Op.getOperand(3);
3654 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003655 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003656
Owen Anderson9f944592009-08-11 20:47:22 +00003657 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003658 SDValue ARMcc;
3659 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003660 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003661 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003662 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003663 }
3664
Owen Anderson9f944592009-08-11 20:47:22 +00003665 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003666
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003667 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003668 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3669 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3670 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3671 if (Result.getNode())
3672 return Result;
3673 }
3674
Evan Cheng10043e22007-01-19 07:51:42 +00003675 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003676 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003677
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003678 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3679 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003680 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003681 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003682 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003683 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003684 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003685 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3686 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003687 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003688 }
3689 return Res;
3690}
3691
Dan Gohman21cea8a2010-04-17 15:26:15 +00003692SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003693 SDValue Chain = Op.getOperand(0);
3694 SDValue Table = Op.getOperand(1);
3695 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003696 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003697
Owen Anderson53aa7a92009-08-10 22:56:29 +00003698 EVT PTy = getPointerTy();
Evan Cheng10043e22007-01-19 07:51:42 +00003699 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3700 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3f17aee2009-07-14 18:44:34 +00003701 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003702 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson9f944592009-08-11 20:47:22 +00003703 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chengc8bed032009-07-28 20:53:24 +00003704 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3705 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003706 if (Subtarget->isThumb2()) {
3707 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3708 // which does another jump to the destination. This also makes it easier
3709 // to translate it to TBB / TBH later.
3710 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003711 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Chengc6d70ae2009-07-29 02:18:14 +00003712 Addr, Op.getOperand(2), JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003713 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003714 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003715 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattner7727d052010-09-21 06:44:06 +00003716 MachinePointerInfo::getJumpTable(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003717 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003718 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003719 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson9f944592009-08-11 20:47:22 +00003720 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003721 } else {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003722 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003723 MachinePointerInfo::getJumpTable(),
3724 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003725 Chain = Addr.getValue(1);
Owen Anderson9f944592009-08-11 20:47:22 +00003726 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003727 }
Evan Cheng10043e22007-01-19 07:51:42 +00003728}
3729
Eli Friedman2d4055b2011-11-09 23:36:02 +00003730static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003731 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003732 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003733
James Molloy547d4c02012-02-20 09:24:05 +00003734 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3735 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3736 return Op;
3737 return DAG.UnrollVectorOp(Op.getNode());
3738 }
3739
3740 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3741 "Invalid type for custom lowering!");
3742 if (VT != MVT::v4i16)
3743 return DAG.UnrollVectorOp(Op.getNode());
3744
3745 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3746 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003747}
3748
Bob Wilsone4191e72010-03-19 22:51:32 +00003749static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003750 EVT VT = Op.getValueType();
3751 if (VT.isVector())
3752 return LowerVectorFP_TO_INT(Op, DAG);
3753
Andrew Trickef9de2a2013-05-25 02:42:55 +00003754 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003755 unsigned Opc;
3756
3757 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003758 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003759 case ISD::FP_TO_SINT:
3760 Opc = ARMISD::FTOSI;
3761 break;
3762 case ISD::FP_TO_UINT:
3763 Opc = ARMISD::FTOUI;
3764 break;
3765 }
3766 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peck527da1b2010-11-23 03:31:01 +00003767 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003768}
3769
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003770static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3771 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003772 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003773
Eli Friedman2d4055b2011-11-09 23:36:02 +00003774 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3775 if (VT.getVectorElementType() == MVT::f32)
3776 return Op;
3777 return DAG.UnrollVectorOp(Op.getNode());
3778 }
3779
Duncan Sandsa41634e2011-08-12 14:54:45 +00003780 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3781 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003782 if (VT != MVT::v4f32)
3783 return DAG.UnrollVectorOp(Op.getNode());
3784
3785 unsigned CastOpc;
3786 unsigned Opc;
3787 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003788 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003789 case ISD::SINT_TO_FP:
3790 CastOpc = ISD::SIGN_EXTEND;
3791 Opc = ISD::SINT_TO_FP;
3792 break;
3793 case ISD::UINT_TO_FP:
3794 CastOpc = ISD::ZERO_EXTEND;
3795 Opc = ISD::UINT_TO_FP;
3796 break;
3797 }
3798
3799 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3800 return DAG.getNode(Opc, dl, VT, Op);
3801}
3802
Bob Wilsone4191e72010-03-19 22:51:32 +00003803static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3804 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003805 if (VT.isVector())
3806 return LowerVectorINT_TO_FP(Op, DAG);
3807
Andrew Trickef9de2a2013-05-25 02:42:55 +00003808 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003809 unsigned Opc;
3810
3811 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003812 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003813 case ISD::SINT_TO_FP:
3814 Opc = ARMISD::SITOF;
3815 break;
3816 case ISD::UINT_TO_FP:
3817 Opc = ARMISD::UITOF;
3818 break;
3819 }
3820
Wesley Peck527da1b2010-11-23 03:31:01 +00003821 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilsone4191e72010-03-19 22:51:32 +00003822 return DAG.getNode(Opc, dl, VT, Op);
3823}
3824
Evan Cheng25f93642010-07-08 02:08:50 +00003825SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00003826 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003827 SDValue Tmp0 = Op.getOperand(0);
3828 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003829 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003830 EVT VT = Op.getValueType();
3831 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00003832 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3833 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3834 bool UseNEON = !InGPR && Subtarget->hasNEON();
3835
3836 if (UseNEON) {
3837 // Use VBSL to copy the sign bit.
3838 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3839 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3840 DAG.getTargetConstant(EncodedVal, MVT::i32));
3841 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3842 if (VT == MVT::f64)
3843 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3844 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3845 DAG.getConstant(32, MVT::i32));
3846 else /*if (VT == MVT::f32)*/
3847 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3848 if (SrcVT == MVT::f32) {
3849 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3850 if (VT == MVT::f64)
3851 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3852 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3853 DAG.getConstant(32, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00003854 } else if (VT == MVT::f32)
3855 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3856 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3857 DAG.getConstant(32, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00003858 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3859 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3860
3861 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3862 MVT::i32);
3863 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3864 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3865 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00003866
Evan Chengd6b641e2011-02-23 02:24:55 +00003867 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3868 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3869 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00003870 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00003871 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3872 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3873 DAG.getConstant(0, MVT::i32));
3874 } else {
3875 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3876 }
3877
3878 return Res;
3879 }
Evan Cheng2da1c952011-02-11 02:28:55 +00003880
3881 // Bitcast operand 1 to i32.
3882 if (SrcVT == MVT::f64)
3883 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00003884 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00003885 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3886
Evan Chengd6b641e2011-02-23 02:24:55 +00003887 // Or in the signbit with integer operations.
3888 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3889 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3890 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3891 if (VT == MVT::f32) {
3892 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3893 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3894 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3895 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00003896 }
3897
Evan Chengd6b641e2011-02-23 02:24:55 +00003898 // f64: Or the high part with signbit and then combine two parts.
3899 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00003900 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00003901 SDValue Lo = Tmp0.getValue(0);
3902 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3903 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3904 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00003905}
3906
Evan Cheng168ced92010-05-22 01:47:14 +00003907SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3908 MachineFunction &MF = DAG.getMachineFunction();
3909 MachineFrameInfo *MFI = MF.getFrameInfo();
3910 MFI->setReturnAddressIsTaken(true);
3911
Bill Wendling908bf812014-01-06 00:43:20 +00003912 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003913 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003914
Evan Cheng168ced92010-05-22 01:47:14 +00003915 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003916 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00003917 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3918 if (Depth) {
3919 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3920 SDValue Offset = DAG.getConstant(4, MVT::i32);
3921 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3922 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003923 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00003924 }
3925
3926 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00003927 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00003928 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3929}
3930
Dan Gohman21cea8a2010-04-17 15:26:15 +00003931SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003932 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3933 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00003934
Owen Anderson53aa7a92009-08-10 22:56:29 +00003935 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003936 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003937 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Tim Northoverd6a729b2014-01-06 14:28:05 +00003938 unsigned FrameReg = (Subtarget->isThumb() || Subtarget->isTargetMachO())
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003939 ? ARM::R7 : ARM::R11;
3940 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3941 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00003942 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3943 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003944 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003945 return FrameAddr;
3946}
3947
Renato Golinc7aea402014-05-06 16:51:25 +00003948// FIXME? Maybe this could be a TableGen attribute on some registers and
3949// this table could be generated automatically from RegInfo.
Hal Finkelf0e086a2014-05-11 19:29:07 +00003950unsigned ARMTargetLowering::getRegisterByName(const char* RegName,
3951 EVT VT) const {
Renato Golinc7aea402014-05-06 16:51:25 +00003952 unsigned Reg = StringSwitch<unsigned>(RegName)
3953 .Case("sp", ARM::SP)
3954 .Default(0);
3955 if (Reg)
3956 return Reg;
3957 report_fatal_error("Invalid register name global variable");
3958}
3959
Wesley Peck527da1b2010-11-23 03:31:01 +00003960/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00003961/// expand a bit convert where either the source or destination type is i64 to
3962/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3963/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3964/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00003965static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00003966 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003967 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003968 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00003969
Bob Wilson59b70ea2010-04-17 05:30:19 +00003970 // This function is only supposed to be called for i64 types, either as the
3971 // source or destination of the bit convert.
3972 EVT SrcVT = Op.getValueType();
3973 EVT DstVT = N->getValueType(0);
3974 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00003975 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00003976
Bob Wilson59b70ea2010-04-17 05:30:19 +00003977 // Turn i64->f64 into VMOVDRR.
3978 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00003979 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3980 DAG.getConstant(0, MVT::i32));
3981 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3982 DAG.getConstant(1, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00003983 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00003984 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00003985 }
Bob Wilson7117a912009-03-20 22:42:55 +00003986
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00003987 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00003988 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00003989 SDValue Cvt;
Christian Pirker6692e7c2014-05-14 16:59:44 +00003990 if (TLI.isBigEndian() && SrcVT.isVector() &&
3991 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00003992 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3993 DAG.getVTList(MVT::i32, MVT::i32),
3994 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
3995 else
3996 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3997 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00003998 // Merge the pieces into a single i64 value.
3999 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
4000 }
Bob Wilson7117a912009-03-20 22:42:55 +00004001
Bob Wilson59b70ea2010-04-17 05:30:19 +00004002 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00004003}
4004
Bob Wilson2e076c42009-06-22 23:27:02 +00004005/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00004006/// Zero vectors are used to represent vector negation and in those cases
4007/// will be implemented with the NEON VNEG instruction. However, VNEG does
4008/// not support i64 elements, so sometimes the zero vectors will need to be
4009/// explicitly constructed. Regardless, use a canonical VMOV to create the
4010/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004011static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004012 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00004013 // The canonical modified immediate encoding of a zero vector is....0!
4014 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
4015 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
4016 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00004017 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00004018}
4019
Jim Grosbach624fcb22009-10-31 21:00:56 +00004020/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
4021/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004022SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
4023 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00004024 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4025 EVT VT = Op.getValueType();
4026 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004027 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004028 SDValue ShOpLo = Op.getOperand(0);
4029 SDValue ShOpHi = Op.getOperand(1);
4030 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004031 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004032 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00004033
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004034 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
4035
Jim Grosbach624fcb22009-10-31 21:00:56 +00004036 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4037 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4038 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
4039 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4040 DAG.getConstant(VTBits, MVT::i32));
4041 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
4042 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004043 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004044
4045 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4046 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004047 ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004048 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004049 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004050 CCR, Cmp);
4051
4052 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004053 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004054}
4055
Jim Grosbach5d994042009-10-31 19:38:01 +00004056/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4057/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004058SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4059 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004060 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4061 EVT VT = Op.getValueType();
4062 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004063 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004064 SDValue ShOpLo = Op.getOperand(0);
4065 SDValue ShOpHi = Op.getOperand(1);
4066 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004067 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004068
4069 assert(Op.getOpcode() == ISD::SHL_PARTS);
4070 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4071 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4072 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4073 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4074 DAG.getConstant(VTBits, MVT::i32));
4075 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4076 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4077
4078 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4079 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4080 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004081 ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004082 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004083 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004084 CCR, Cmp);
4085
4086 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004087 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004088}
4089
Jim Grosbach535d3b42010-09-08 03:54:02 +00004090SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004091 SelectionDAG &DAG) const {
4092 // The rounding mode is in bits 23:22 of the FPSCR.
4093 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4094 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4095 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004096 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004097 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
4098 DAG.getConstant(Intrinsic::arm_get_fpscr,
4099 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004100 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemanb69b1822010-08-03 21:31:55 +00004101 DAG.getConstant(1U << 22, MVT::i32));
4102 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
4103 DAG.getConstant(22, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004104 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemanb69b1822010-08-03 21:31:55 +00004105 DAG.getConstant(3, MVT::i32));
4106}
4107
Jim Grosbach8546ec92010-01-18 19:58:49 +00004108static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4109 const ARMSubtarget *ST) {
4110 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004111 SDLoc dl(N);
Jim Grosbach8546ec92010-01-18 19:58:49 +00004112
4113 if (!ST->hasV6T2Ops())
4114 return SDValue();
4115
4116 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
4117 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4118}
4119
Evan Chengb4eae132012-12-04 22:41:50 +00004120/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4121/// for each 16-bit element from operand, repeated. The basic idea is to
4122/// leverage vcnt to get the 8-bit counts, gather and add the results.
4123///
4124/// Trace for v4i16:
4125/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4126/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4127/// 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 +00004128/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004129/// [b0 b1 b2 b3 b4 b5 b6 b7]
4130/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4131/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4132/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4133static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4134 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004135 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004136
4137 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4138 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4139 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4140 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4141 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4142 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4143}
4144
4145/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4146/// bit-count for each 16-bit element from the operand. We need slightly
4147/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4148/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004149///
Evan Chengb4eae132012-12-04 22:41:50 +00004150/// Trace for v4i16:
4151/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4152/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4153/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4154/// v4i16:Extracted = [k0 k1 k2 k3 ]
4155static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4156 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004157 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004158
4159 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4160 if (VT.is64BitVector()) {
4161 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4162 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
4163 DAG.getIntPtrConstant(0));
4164 } else {
4165 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
4166 BitCounts, DAG.getIntPtrConstant(0));
4167 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4168 }
4169}
4170
4171/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4172/// bit-count for each 32-bit element from the operand. The idea here is
4173/// to split the vector into 16-bit elements, leverage the 16-bit count
4174/// routine, and then combine the results.
4175///
4176/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4177/// input = [v0 v1 ] (vi: 32-bit elements)
4178/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4179/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004180/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004181/// [k0 k1 k2 k3 ]
4182/// N1 =+[k1 k0 k3 k2 ]
4183/// [k0 k2 k1 k3 ]
4184/// N2 =+[k1 k3 k0 k2 ]
4185/// [k0 k2 k1 k3 ]
4186/// Extended =+[k1 k3 k0 k2 ]
4187/// [k0 k2 ]
4188/// Extracted=+[k1 k3 ]
4189///
4190static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4191 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004192 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004193
4194 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4195
4196 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4197 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4198 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4199 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4200 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4201
4202 if (VT.is64BitVector()) {
4203 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4204 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
4205 DAG.getIntPtrConstant(0));
4206 } else {
4207 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
4208 DAG.getIntPtrConstant(0));
4209 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4210 }
4211}
4212
4213static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4214 const ARMSubtarget *ST) {
4215 EVT VT = N->getValueType(0);
4216
4217 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004218 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4219 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004220 "Unexpected type for custom ctpop lowering");
4221
4222 if (VT.getVectorElementType() == MVT::i32)
4223 return lowerCTPOP32BitElements(N, DAG);
4224 else
4225 return lowerCTPOP16BitElements(N, DAG);
4226}
4227
Bob Wilson2e076c42009-06-22 23:27:02 +00004228static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4229 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004230 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004231 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004232
Bob Wilson7d471332010-11-18 21:16:28 +00004233 if (!VT.isVector())
4234 return SDValue();
4235
Bob Wilson2e076c42009-06-22 23:27:02 +00004236 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004237 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004238
Bob Wilson7d471332010-11-18 21:16:28 +00004239 // Left shifts translate directly to the vshiftu intrinsic.
4240 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004241 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilson7d471332010-11-18 21:16:28 +00004242 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
4243 N->getOperand(0), N->getOperand(1));
4244
4245 assert((N->getOpcode() == ISD::SRA ||
4246 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4247
4248 // NEON uses the same intrinsics for both left and right shifts. For
4249 // right shifts, the shift amounts are negative, so negate the vector of
4250 // shift amounts.
4251 EVT ShiftVT = N->getOperand(1).getValueType();
4252 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4253 getZeroVector(ShiftVT, DAG, dl),
4254 N->getOperand(1));
4255 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4256 Intrinsic::arm_neon_vshifts :
4257 Intrinsic::arm_neon_vshiftu);
4258 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4259 DAG.getConstant(vshiftInt, MVT::i32),
4260 N->getOperand(0), NegatedCount);
4261}
4262
4263static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4264 const ARMSubtarget *ST) {
4265 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004266 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004267
Eli Friedman682d8c12009-08-22 03:13:10 +00004268 // We can get here for a node like i32 = ISD::SHL i32, i64
4269 if (VT != MVT::i64)
4270 return SDValue();
4271
4272 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004273 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004274
Chris Lattnerf81d5882007-11-24 07:07:01 +00004275 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4276 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004277 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004278 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004279
Chris Lattnerf81d5882007-11-24 07:07:01 +00004280 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004281 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004282
Chris Lattnerf81d5882007-11-24 07:07:01 +00004283 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004284 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004285 DAG.getConstant(0, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004286 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004287 DAG.getConstant(1, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004288
Chris Lattnerf81d5882007-11-24 07:07:01 +00004289 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4290 // captures the result into a carry flag.
4291 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004292 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004293
Chris Lattnerf81d5882007-11-24 07:07:01 +00004294 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004295 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004296
Chris Lattnerf81d5882007-11-24 07:07:01 +00004297 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004298 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004299}
4300
Bob Wilson2e076c42009-06-22 23:27:02 +00004301static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4302 SDValue TmpOp0, TmpOp1;
4303 bool Invert = false;
4304 bool Swap = false;
4305 unsigned Opc = 0;
4306
4307 SDValue Op0 = Op.getOperand(0);
4308 SDValue Op1 = Op.getOperand(1);
4309 SDValue CC = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004310 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004311 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004312 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004313
4314 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
4315 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004316 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004317 case ISD::SETUNE:
4318 case ISD::SETNE: Invert = true; // Fallthrough
4319 case ISD::SETOEQ:
4320 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4321 case ISD::SETOLT:
4322 case ISD::SETLT: Swap = true; // Fallthrough
4323 case ISD::SETOGT:
4324 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4325 case ISD::SETOLE:
4326 case ISD::SETLE: Swap = true; // Fallthrough
4327 case ISD::SETOGE:
4328 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4329 case ISD::SETUGE: Swap = true; // Fallthrough
4330 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4331 case ISD::SETUGT: Swap = true; // Fallthrough
4332 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4333 case ISD::SETUEQ: Invert = true; // Fallthrough
4334 case ISD::SETONE:
4335 // Expand this to (OLT | OGT).
4336 TmpOp0 = Op0;
4337 TmpOp1 = Op1;
4338 Opc = ISD::OR;
4339 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4340 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
4341 break;
4342 case ISD::SETUO: Invert = true; // Fallthrough
4343 case ISD::SETO:
4344 // Expand this to (OLT | OGE).
4345 TmpOp0 = Op0;
4346 TmpOp1 = Op1;
4347 Opc = ISD::OR;
4348 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4349 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
4350 break;
4351 }
4352 } else {
4353 // Integer comparisons.
4354 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004355 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004356 case ISD::SETNE: Invert = true;
4357 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4358 case ISD::SETLT: Swap = true;
4359 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4360 case ISD::SETLE: Swap = true;
4361 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4362 case ISD::SETULT: Swap = true;
4363 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4364 case ISD::SETULE: Swap = true;
4365 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4366 }
4367
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004368 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004369 if (Opc == ARMISD::VCEQ) {
4370
4371 SDValue AndOp;
4372 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4373 AndOp = Op0;
4374 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4375 AndOp = Op1;
4376
4377 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004378 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004379 AndOp = AndOp.getOperand(0);
4380
4381 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4382 Opc = ARMISD::VTST;
Wesley Peck527da1b2010-11-23 03:31:01 +00004383 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
4384 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004385 Invert = !Invert;
4386 }
4387 }
4388 }
4389
4390 if (Swap)
4391 std::swap(Op0, Op1);
4392
Owen Andersonc7baee32010-11-08 23:21:22 +00004393 // If one of the operands is a constant vector zero, attempt to fold the
4394 // comparison to a specialized compare-against-zero form.
4395 SDValue SingleOp;
4396 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4397 SingleOp = Op0;
4398 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4399 if (Opc == ARMISD::VCGE)
4400 Opc = ARMISD::VCLEZ;
4401 else if (Opc == ARMISD::VCGT)
4402 Opc = ARMISD::VCLTZ;
4403 SingleOp = Op1;
4404 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004405
Owen Andersonc7baee32010-11-08 23:21:22 +00004406 SDValue Result;
4407 if (SingleOp.getNode()) {
4408 switch (Opc) {
4409 case ARMISD::VCEQ:
4410 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
4411 case ARMISD::VCGE:
4412 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
4413 case ARMISD::VCLEZ:
4414 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
4415 case ARMISD::VCGT:
4416 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
4417 case ARMISD::VCLTZ:
4418 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
4419 default:
4420 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4421 }
4422 } else {
4423 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4424 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004425
4426 if (Invert)
4427 Result = DAG.getNOT(dl, Result, VT);
4428
4429 return Result;
4430}
4431
Bob Wilson5b2b5042010-06-14 22:19:57 +00004432/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4433/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004434/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004435static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4436 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Andersona4076922010-11-05 21:57:54 +00004437 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004438 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004439
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004440 // SplatBitSize is set to the smallest size that splats the vector, so a
4441 // zero vector will always have SplatBitSize == 8. However, NEON modified
4442 // immediate instructions others than VMOV do not support the 8-bit encoding
4443 // of a zero vector, and the default encoding of zero is supposed to be the
4444 // 32-bit version.
4445 if (SplatBits == 0)
4446 SplatBitSize = 32;
4447
Bob Wilson2e076c42009-06-22 23:27:02 +00004448 switch (SplatBitSize) {
4449 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004450 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004451 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004452 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004453 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004454 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004455 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004456 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004457 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004458
4459 case 16:
4460 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004461 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004462 if ((SplatBits & ~0xff) == 0) {
4463 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004464 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004465 Imm = SplatBits;
4466 break;
4467 }
4468 if ((SplatBits & ~0xff00) == 0) {
4469 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004470 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004471 Imm = SplatBits >> 8;
4472 break;
4473 }
4474 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004475
4476 case 32:
4477 // NEON's 32-bit VMOV supports splat values where:
4478 // * only one byte is nonzero, or
4479 // * the least significant byte is 0xff and the second byte is nonzero, or
4480 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004481 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004482 if ((SplatBits & ~0xff) == 0) {
4483 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004484 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004485 Imm = SplatBits;
4486 break;
4487 }
4488 if ((SplatBits & ~0xff00) == 0) {
4489 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004490 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004491 Imm = SplatBits >> 8;
4492 break;
4493 }
4494 if ((SplatBits & ~0xff0000) == 0) {
4495 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004496 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004497 Imm = SplatBits >> 16;
4498 break;
4499 }
4500 if ((SplatBits & ~0xff000000) == 0) {
4501 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004502 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004503 Imm = SplatBits >> 24;
4504 break;
4505 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004506
Owen Andersona4076922010-11-05 21:57:54 +00004507 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4508 if (type == OtherModImm) return SDValue();
4509
Bob Wilson2e076c42009-06-22 23:27:02 +00004510 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004511 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4512 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004513 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004514 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004515 break;
4516 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004517
4518 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004519 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4520 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004521 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004522 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004523 break;
4524 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004525
4526 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4527 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4528 // VMOV.I32. A (very) minor optimization would be to replicate the value
4529 // and fall through here to test for a valid 64-bit splat. But, then the
4530 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004531 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004532
4533 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004534 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004535 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004536 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004537 uint64_t BitMask = 0xff;
4538 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004539 unsigned ImmMask = 1;
4540 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004541 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004542 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004543 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004544 Imm |= ImmMask;
4545 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004546 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004547 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004548 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004549 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004550 }
Bob Wilson6eae5202010-06-11 21:34:50 +00004551 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004552 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004553 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004554 break;
4555 }
4556
Bob Wilson6eae5202010-06-11 21:34:50 +00004557 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004558 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004559 }
4560
Bob Wilsona3f19012010-07-13 21:16:48 +00004561 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4562 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004563}
4564
Lang Hames591cdaf2012-03-29 21:56:11 +00004565SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4566 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004567 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004568 return SDValue();
4569
Tim Northoverf79c3a52013-08-20 08:57:11 +00004570 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004571 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004572
4573 // Try splatting with a VMOV.f32...
4574 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004575 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4576
Lang Hames591cdaf2012-03-29 21:56:11 +00004577 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004578 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4579 // We have code in place to select a valid ConstantFP already, no need to
4580 // do any mangling.
4581 return Op;
4582 }
4583
4584 // It's a float and we are trying to use NEON operations where
4585 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004586 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004587 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4588 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4589 NewVal);
4590 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4591 DAG.getConstant(0, MVT::i32));
4592 }
4593
Tim Northoverf79c3a52013-08-20 08:57:11 +00004594 // The rest of our options are NEON only, make sure that's allowed before
4595 // proceeding..
4596 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4597 return SDValue();
4598
Lang Hames591cdaf2012-03-29 21:56:11 +00004599 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004600 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4601
4602 // It wouldn't really be worth bothering for doubles except for one very
4603 // important value, which does happen to match: 0.0. So make sure we don't do
4604 // anything stupid.
4605 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4606 return SDValue();
4607
4608 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
4609 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4610 false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004611 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004612 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004613 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4614 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004615 if (IsDouble)
4616 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4617
4618 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004619 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4620 VecConstant);
4621 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4622 DAG.getConstant(0, MVT::i32));
4623 }
4624
4625 // Finally, try a VMVN.i32
Tim Northoverf79c3a52013-08-20 08:57:11 +00004626 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4627 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004628 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004629 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004630 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004631
4632 if (IsDouble)
4633 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4634
4635 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004636 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4637 VecConstant);
4638 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4639 DAG.getConstant(0, MVT::i32));
4640 }
4641
4642 return SDValue();
4643}
4644
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004645// check if an VEXT instruction can handle the shuffle mask when the
4646// vector sources of the shuffle are the same.
4647static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4648 unsigned NumElts = VT.getVectorNumElements();
4649
4650 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4651 if (M[0] < 0)
4652 return false;
4653
4654 Imm = M[0];
4655
4656 // If this is a VEXT shuffle, the immediate value is the index of the first
4657 // element. The other shuffle indices must be the successive elements after
4658 // the first one.
4659 unsigned ExpectedElt = Imm;
4660 for (unsigned i = 1; i < NumElts; ++i) {
4661 // Increment the expected index. If it wraps around, just follow it
4662 // back to index zero and keep going.
4663 ++ExpectedElt;
4664 if (ExpectedElt == NumElts)
4665 ExpectedElt = 0;
4666
4667 if (M[i] < 0) continue; // ignore UNDEF indices
4668 if (ExpectedElt != static_cast<unsigned>(M[i]))
4669 return false;
4670 }
4671
4672 return true;
4673}
4674
Lang Hames591cdaf2012-03-29 21:56:11 +00004675
Benjamin Kramer339ced42012-01-15 13:16:05 +00004676static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004677 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004678 unsigned NumElts = VT.getVectorNumElements();
4679 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004680
4681 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4682 if (M[0] < 0)
4683 return false;
4684
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004685 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004686
4687 // If this is a VEXT shuffle, the immediate value is the index of the first
4688 // element. The other shuffle indices must be the successive elements after
4689 // the first one.
4690 unsigned ExpectedElt = Imm;
4691 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004692 // Increment the expected index. If it wraps around, it may still be
4693 // a VEXT but the source vectors must be swapped.
4694 ExpectedElt += 1;
4695 if (ExpectedElt == NumElts * 2) {
4696 ExpectedElt = 0;
4697 ReverseVEXT = true;
4698 }
4699
Bob Wilson411dfad2010-08-17 05:54:34 +00004700 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004701 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004702 return false;
4703 }
4704
4705 // Adjust the index value if the source operands will be swapped.
4706 if (ReverseVEXT)
4707 Imm -= NumElts;
4708
Bob Wilson32cd8552009-08-19 17:03:43 +00004709 return true;
4710}
4711
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004712/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4713/// instruction with the specified blocksize. (The order of the elements
4714/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00004715static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004716 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4717 "Only possible block sizes for VREV are: 16, 32, 64");
4718
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004719 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00004720 if (EltSz == 64)
4721 return false;
4722
4723 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004724 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00004725 // If the first shuffle index is UNDEF, be optimistic.
4726 if (M[0] < 0)
4727 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004728
4729 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4730 return false;
4731
4732 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004733 if (M[i] < 0) continue; // ignore UNDEF indices
4734 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004735 return false;
4736 }
4737
4738 return true;
4739}
4740
Benjamin Kramer339ced42012-01-15 13:16:05 +00004741static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00004742 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4743 // range, then 0 is placed into the resulting vector. So pretty much any mask
4744 // of 8 elements can work here.
4745 return VT == MVT::v8i8 && M.size() == 8;
4746}
4747
Benjamin Kramer339ced42012-01-15 13:16:05 +00004748static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004749 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4750 if (EltSz == 64)
4751 return false;
4752
Bob Wilsona7062312009-08-21 20:54:19 +00004753 unsigned NumElts = VT.getVectorNumElements();
4754 WhichResult = (M[0] == 0 ? 0 : 1);
4755 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004756 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4757 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsona7062312009-08-21 20:54:19 +00004758 return false;
4759 }
4760 return true;
4761}
4762
Bob Wilson0bbd3072009-12-03 06:40:55 +00004763/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4764/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4765/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004766static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004767 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4768 if (EltSz == 64)
4769 return false;
4770
4771 unsigned NumElts = VT.getVectorNumElements();
4772 WhichResult = (M[0] == 0 ? 0 : 1);
4773 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004774 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4775 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004776 return false;
4777 }
4778 return true;
4779}
4780
Benjamin Kramer339ced42012-01-15 13:16:05 +00004781static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004782 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4783 if (EltSz == 64)
4784 return false;
4785
Bob Wilsona7062312009-08-21 20:54:19 +00004786 unsigned NumElts = VT.getVectorNumElements();
4787 WhichResult = (M[0] == 0 ? 0 : 1);
4788 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004789 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsona7062312009-08-21 20:54:19 +00004790 if ((unsigned) M[i] != 2 * i + WhichResult)
4791 return false;
4792 }
4793
4794 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004795 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004796 return false;
4797
4798 return true;
4799}
4800
Bob Wilson0bbd3072009-12-03 06:40:55 +00004801/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4802/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4803/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00004804static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004805 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4806 if (EltSz == 64)
4807 return false;
4808
4809 unsigned Half = VT.getVectorNumElements() / 2;
4810 WhichResult = (M[0] == 0 ? 0 : 1);
4811 for (unsigned j = 0; j != 2; ++j) {
4812 unsigned Idx = WhichResult;
4813 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004814 int MIdx = M[i + j * Half];
4815 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson0bbd3072009-12-03 06:40:55 +00004816 return false;
4817 Idx += 2;
4818 }
4819 }
4820
4821 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4822 if (VT.is64BitVector() && EltSz == 32)
4823 return false;
4824
4825 return true;
4826}
4827
Benjamin Kramer339ced42012-01-15 13:16:05 +00004828static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004829 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4830 if (EltSz == 64)
4831 return false;
4832
Bob Wilsona7062312009-08-21 20:54:19 +00004833 unsigned NumElts = VT.getVectorNumElements();
4834 WhichResult = (M[0] == 0 ? 0 : 1);
4835 unsigned Idx = WhichResult * NumElts / 2;
4836 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004837 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4838 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsona7062312009-08-21 20:54:19 +00004839 return false;
4840 Idx += 1;
4841 }
4842
4843 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004844 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004845 return false;
4846
4847 return true;
4848}
4849
Bob Wilson0bbd3072009-12-03 06:40:55 +00004850/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4851/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4852/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004853static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004854 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4855 if (EltSz == 64)
4856 return false;
4857
4858 unsigned NumElts = VT.getVectorNumElements();
4859 WhichResult = (M[0] == 0 ? 0 : 1);
4860 unsigned Idx = WhichResult * NumElts / 2;
4861 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004862 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4863 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004864 return false;
4865 Idx += 1;
4866 }
4867
4868 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4869 if (VT.is64BitVector() && EltSz == 32)
4870 return false;
4871
4872 return true;
4873}
4874
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00004875/// \return true if this is a reverse operation on an vector.
4876static bool isReverseMask(ArrayRef<int> M, EVT VT) {
4877 unsigned NumElts = VT.getVectorNumElements();
4878 // Make sure the mask has the right size.
4879 if (NumElts != M.size())
4880 return false;
4881
4882 // Look for <15, ..., 3, -1, 1, 0>.
4883 for (unsigned i = 0; i != NumElts; ++i)
4884 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
4885 return false;
4886
4887 return true;
4888}
4889
Dale Johannesen2bff5052010-07-29 20:10:08 +00004890// If N is an integer constant that can be moved into a register in one
4891// instruction, return an SDValue of such a constant (will become a MOV
4892// instruction). Otherwise return null.
4893static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004894 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00004895 uint64_t Val;
4896 if (!isa<ConstantSDNode>(N))
4897 return SDValue();
4898 Val = cast<ConstantSDNode>(N)->getZExtValue();
4899
4900 if (ST->isThumb1Only()) {
4901 if (Val <= 255 || ~Val <= 255)
4902 return DAG.getConstant(Val, MVT::i32);
4903 } else {
4904 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4905 return DAG.getConstant(Val, MVT::i32);
4906 }
4907 return SDValue();
4908}
4909
Bob Wilson2e076c42009-06-22 23:27:02 +00004910// If this is a case we can't handle, return null and let the default
4911// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00004912SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4913 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00004914 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004915 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004916 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004917
4918 APInt SplatBits, SplatUndef;
4919 unsigned SplatBitSize;
4920 bool HasAnyUndefs;
4921 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00004922 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00004923 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00004924 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00004925 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00004926 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00004927 DAG, VmovVT, VT.is128BitVector(),
4928 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00004929 if (Val.getNode()) {
4930 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00004931 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00004932 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00004933
4934 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00004935 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004936 Val = isNEONModifiedImm(NegatedImm,
4937 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00004938 DAG, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00004939 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00004940 if (Val.getNode()) {
4941 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00004942 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00004943 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00004944
4945 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00004946 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00004947 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00004948 if (ImmVal != -1) {
4949 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4950 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4951 }
4952 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00004953 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00004954 }
4955
Bob Wilson91fdf682010-05-22 00:23:12 +00004956 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00004957 //
4958 // As an optimisation, even if more than one value is used it may be more
4959 // profitable to splat with one value then change some lanes.
4960 //
4961 // Heuristically we decide to do this if the vector has a "dominant" value,
4962 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00004963 unsigned NumElts = VT.getVectorNumElements();
4964 bool isOnlyLowElement = true;
4965 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00004966 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00004967 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00004968
4969 // Map of the number of times a particular SDValue appears in the
4970 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00004971 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00004972 SDValue Value;
4973 for (unsigned i = 0; i < NumElts; ++i) {
4974 SDValue V = Op.getOperand(i);
4975 if (V.getOpcode() == ISD::UNDEF)
4976 continue;
4977 if (i > 0)
4978 isOnlyLowElement = false;
4979 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4980 isConstant = false;
4981
James Molloy49bdbce2012-09-06 09:55:02 +00004982 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00004983 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00004984
James Molloy49bdbce2012-09-06 09:55:02 +00004985 // Is this value dominant? (takes up more than half of the lanes)
4986 if (++Count > (NumElts / 2)) {
4987 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00004988 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00004989 }
Bob Wilson91fdf682010-05-22 00:23:12 +00004990 }
James Molloy49bdbce2012-09-06 09:55:02 +00004991 if (ValueCounts.size() != 1)
4992 usesOnlyOneValue = false;
4993 if (!Value.getNode() && ValueCounts.size() > 0)
4994 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00004995
James Molloy49bdbce2012-09-06 09:55:02 +00004996 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00004997 return DAG.getUNDEF(VT);
4998
Quentin Colombet0f2fe742013-07-23 22:34:47 +00004999 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
5000 // Keep going if we are hitting this case.
5001 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00005002 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
5003
Dale Johannesen2bff5052010-07-29 20:10:08 +00005004 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5005
Dale Johannesen710a2d92010-10-19 20:00:17 +00005006 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
5007 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00005008 if (hasDominantValue && EltSize <= 32) {
5009 if (!isConstant) {
5010 SDValue N;
5011
5012 // If we are VDUPing a value that comes directly from a vector, that will
5013 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00005014 // just use VDUPLANE. We can only do this if the lane being extracted
5015 // is at a constant index, as the VDUP from lane instructions only have
5016 // constant-index forms.
5017 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5018 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00005019 // We need to create a new undef vector to use for the VDUPLANE if the
5020 // size of the vector from which we get the value is different than the
5021 // size of the vector that we need to create. We will insert the element
5022 // such that the register coalescer will remove unnecessary copies.
5023 if (VT != Value->getOperand(0).getValueType()) {
5024 ConstantSDNode *constIndex;
5025 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
5026 assert(constIndex && "The index is not a constant!");
5027 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
5028 VT.getVectorNumElements();
5029 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
5030 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
5031 Value, DAG.getConstant(index, MVT::i32)),
5032 DAG.getConstant(index, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005033 } else
Silviu Barangab1409702012-10-15 09:41:32 +00005034 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00005035 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00005036 } else
James Molloy49bdbce2012-09-06 09:55:02 +00005037 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
5038
5039 if (!usesOnlyOneValue) {
5040 // The dominant value was splatted as 'N', but we now have to insert
5041 // all differing elements.
5042 for (unsigned I = 0; I < NumElts; ++I) {
5043 if (Op.getOperand(I) == Value)
5044 continue;
5045 SmallVector<SDValue, 3> Ops;
5046 Ops.push_back(N);
5047 Ops.push_back(Op.getOperand(I));
5048 Ops.push_back(DAG.getConstant(I, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005049 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005050 }
5051 }
5052 return N;
5053 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005054 if (VT.getVectorElementType().isFloatingPoint()) {
5055 SmallVector<SDValue, 8> Ops;
5056 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005057 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005058 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005059 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005060 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005061 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5062 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005063 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005064 }
James Molloy49bdbce2012-09-06 09:55:02 +00005065 if (usesOnlyOneValue) {
5066 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5067 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005068 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005069 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005070 }
5071
5072 // If all elements are constants and the case above didn't get hit, fall back
5073 // to the default expansion, which will generate a load from the constant
5074 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005075 if (isConstant)
5076 return SDValue();
5077
Bob Wilson6f2b8962011-01-07 21:37:30 +00005078 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5079 if (NumElts >= 4) {
5080 SDValue shuffle = ReconstructShuffle(Op, DAG);
5081 if (shuffle != SDValue())
5082 return shuffle;
5083 }
5084
Bob Wilson91fdf682010-05-22 00:23:12 +00005085 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005086 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5087 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005088 if (EltSize >= 32) {
5089 // Do the expansion with floating-point types, since that is what the VFP
5090 // registers are defined to use, and since i64 is not legal.
5091 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5092 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005093 SmallVector<SDValue, 8> Ops;
5094 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005095 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005096 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005097 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005098 }
5099
Jim Grosbach24e102a2013-07-08 18:18:52 +00005100 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5101 // know the default expansion would otherwise fall back on something even
5102 // worse. For a vector with one or two non-undef values, that's
5103 // scalar_to_vector for the elements followed by a shuffle (provided the
5104 // shuffle is valid for the target) and materialization element by element
5105 // on the stack followed by a load for everything else.
5106 if (!isConstant && !usesOnlyOneValue) {
5107 SDValue Vec = DAG.getUNDEF(VT);
5108 for (unsigned i = 0 ; i < NumElts; ++i) {
5109 SDValue V = Op.getOperand(i);
5110 if (V.getOpcode() == ISD::UNDEF)
5111 continue;
5112 SDValue LaneIdx = DAG.getConstant(i, MVT::i32);
5113 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5114 }
5115 return Vec;
5116 }
5117
Bob Wilson2e076c42009-06-22 23:27:02 +00005118 return SDValue();
5119}
5120
Bob Wilson6f2b8962011-01-07 21:37:30 +00005121// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005122// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005123SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5124 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005125 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005126 EVT VT = Op.getValueType();
5127 unsigned NumElts = VT.getVectorNumElements();
5128
5129 SmallVector<SDValue, 2> SourceVecs;
5130 SmallVector<unsigned, 2> MinElts;
5131 SmallVector<unsigned, 2> MaxElts;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005132
Bob Wilson6f2b8962011-01-07 21:37:30 +00005133 for (unsigned i = 0; i < NumElts; ++i) {
5134 SDValue V = Op.getOperand(i);
5135 if (V.getOpcode() == ISD::UNDEF)
5136 continue;
5137 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5138 // A shuffle can only come from building a vector from various
5139 // elements of other vectors.
5140 return SDValue();
Eli Friedman74d1da52011-10-14 23:58:49 +00005141 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
5142 VT.getVectorElementType()) {
5143 // This code doesn't know how to handle shuffles where the vector
5144 // element types do not match (this happens because type legalization
5145 // promotes the return type of EXTRACT_VECTOR_ELT).
5146 // FIXME: It might be appropriate to extend this code to handle
5147 // mismatched types.
5148 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005149 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005150
Bob Wilson6f2b8962011-01-07 21:37:30 +00005151 // Record this extraction against the appropriate vector if possible...
5152 SDValue SourceVec = V.getOperand(0);
Jim Grosbach6df755c2012-07-25 17:02:47 +00005153 // If the element number isn't a constant, we can't effectively
5154 // analyze what's going on.
5155 if (!isa<ConstantSDNode>(V.getOperand(1)))
5156 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005157 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5158 bool FoundSource = false;
5159 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
5160 if (SourceVecs[j] == SourceVec) {
5161 if (MinElts[j] > EltNo)
5162 MinElts[j] = EltNo;
5163 if (MaxElts[j] < EltNo)
5164 MaxElts[j] = EltNo;
5165 FoundSource = true;
5166 break;
5167 }
5168 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005169
Bob Wilson6f2b8962011-01-07 21:37:30 +00005170 // Or record a new source if not...
5171 if (!FoundSource) {
5172 SourceVecs.push_back(SourceVec);
5173 MinElts.push_back(EltNo);
5174 MaxElts.push_back(EltNo);
5175 }
5176 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005177
Bob Wilson6f2b8962011-01-07 21:37:30 +00005178 // Currently only do something sane when at most two source vectors
5179 // involved.
5180 if (SourceVecs.size() > 2)
5181 return SDValue();
5182
5183 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
5184 int VEXTOffsets[2] = {0, 0};
Andrew Trick5eb0a302011-01-19 02:26:13 +00005185
Bob Wilson6f2b8962011-01-07 21:37:30 +00005186 // This loop extracts the usage patterns of the source vectors
5187 // and prepares appropriate SDValues for a shuffle if possible.
5188 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
5189 if (SourceVecs[i].getValueType() == VT) {
5190 // No VEXT necessary
5191 ShuffleSrcs[i] = SourceVecs[i];
5192 VEXTOffsets[i] = 0;
5193 continue;
5194 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
5195 // It probably isn't worth padding out a smaller vector just to
5196 // break it down again in a shuffle.
5197 return SDValue();
5198 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005199
Bob Wilson6f2b8962011-01-07 21:37:30 +00005200 // Since only 64-bit and 128-bit vectors are legal on ARM and
5201 // we've eliminated the other cases...
Bob Wilson3fa9c062011-01-07 23:40:46 +00005202 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
5203 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick5eb0a302011-01-19 02:26:13 +00005204
Bob Wilson6f2b8962011-01-07 21:37:30 +00005205 if (MaxElts[i] - MinElts[i] >= NumElts) {
5206 // Span too large for a VEXT to cope
5207 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005208 }
5209
Bob Wilson6f2b8962011-01-07 21:37:30 +00005210 if (MinElts[i] >= NumElts) {
5211 // The extraction can just take the second half
5212 VEXTOffsets[i] = NumElts;
Eric Christopher2af95512011-01-14 23:50:53 +00005213 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5214 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005215 DAG.getIntPtrConstant(NumElts));
5216 } else if (MaxElts[i] < NumElts) {
5217 // The extraction can just take the first half
5218 VEXTOffsets[i] = 0;
Eric Christopher2af95512011-01-14 23:50:53 +00005219 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5220 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005221 DAG.getIntPtrConstant(0));
5222 } else {
5223 // An actual VEXT is needed
5224 VEXTOffsets[i] = MinElts[i];
Eric Christopher2af95512011-01-14 23:50:53 +00005225 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5226 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005227 DAG.getIntPtrConstant(0));
Eric Christopher2af95512011-01-14 23:50:53 +00005228 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5229 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005230 DAG.getIntPtrConstant(NumElts));
5231 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
5232 DAG.getConstant(VEXTOffsets[i], MVT::i32));
5233 }
5234 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005235
Bob Wilson6f2b8962011-01-07 21:37:30 +00005236 SmallVector<int, 8> Mask;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005237
Bob Wilson6f2b8962011-01-07 21:37:30 +00005238 for (unsigned i = 0; i < NumElts; ++i) {
5239 SDValue Entry = Op.getOperand(i);
5240 if (Entry.getOpcode() == ISD::UNDEF) {
5241 Mask.push_back(-1);
5242 continue;
5243 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005244
Bob Wilson6f2b8962011-01-07 21:37:30 +00005245 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher2af95512011-01-14 23:50:53 +00005246 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
5247 .getOperand(1))->getSExtValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005248 if (ExtractVec == SourceVecs[0]) {
5249 Mask.push_back(ExtractElt - VEXTOffsets[0]);
5250 } else {
5251 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
5252 }
5253 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005254
Bob Wilson6f2b8962011-01-07 21:37:30 +00005255 // Final check before we try to produce nonsense...
5256 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher2af95512011-01-14 23:50:53 +00005257 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
5258 &Mask[0]);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005259
Bob Wilson6f2b8962011-01-07 21:37:30 +00005260 return SDValue();
5261}
5262
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005263/// isShuffleMaskLegal - Targets can use this to indicate that they only
5264/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5265/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5266/// are assumed to be legal.
5267bool
5268ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5269 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005270 if (VT.getVectorNumElements() == 4 &&
5271 (VT.is128BitVector() || VT.is64BitVector())) {
5272 unsigned PFIndexes[4];
5273 for (unsigned i = 0; i != 4; ++i) {
5274 if (M[i] < 0)
5275 PFIndexes[i] = 8;
5276 else
5277 PFIndexes[i] = M[i];
5278 }
5279
5280 // Compute the index in the perfect shuffle table.
5281 unsigned PFTableIndex =
5282 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5283 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5284 unsigned Cost = (PFEntry >> 30);
5285
5286 if (Cost <= 4)
5287 return true;
5288 }
5289
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005290 bool ReverseVEXT;
Bob Wilsona7062312009-08-21 20:54:19 +00005291 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005292
Bob Wilson846bd792010-06-07 23:53:38 +00005293 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5294 return (EltSize >= 32 ||
5295 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005296 isVREVMask(M, VT, 64) ||
5297 isVREVMask(M, VT, 32) ||
5298 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005299 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005300 isVTBLMask(M, VT) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005301 isVTRNMask(M, VT, WhichResult) ||
5302 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson0bbd3072009-12-03 06:40:55 +00005303 isVZIPMask(M, VT, WhichResult) ||
5304 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
5305 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005306 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
5307 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005308}
5309
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005310/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5311/// the specified operations to build the shuffle.
5312static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5313 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005314 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005315 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5316 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5317 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5318
5319 enum {
5320 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5321 OP_VREV,
5322 OP_VDUP0,
5323 OP_VDUP1,
5324 OP_VDUP2,
5325 OP_VDUP3,
5326 OP_VEXT1,
5327 OP_VEXT2,
5328 OP_VEXT3,
5329 OP_VUZPL, // VUZP, left result
5330 OP_VUZPR, // VUZP, right result
5331 OP_VZIPL, // VZIP, left result
5332 OP_VZIPR, // VZIP, right result
5333 OP_VTRNL, // VTRN, left result
5334 OP_VTRNR // VTRN, right result
5335 };
5336
5337 if (OpNum == OP_COPY) {
5338 if (LHSID == (1*9+2)*9+3) return LHS;
5339 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5340 return RHS;
5341 }
5342
5343 SDValue OpLHS, OpRHS;
5344 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5345 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5346 EVT VT = OpLHS.getValueType();
5347
5348 switch (OpNum) {
5349 default: llvm_unreachable("Unknown shuffle opcode!");
5350 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005351 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005352 if (VT.getVectorElementType() == MVT::i32 ||
5353 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005354 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5355 // vrev <4 x i16> -> VREV32
5356 if (VT.getVectorElementType() == MVT::i16)
5357 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5358 // vrev <4 x i8> -> VREV16
5359 assert(VT.getVectorElementType() == MVT::i8);
5360 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005361 case OP_VDUP0:
5362 case OP_VDUP1:
5363 case OP_VDUP2:
5364 case OP_VDUP3:
5365 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005366 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005367 case OP_VEXT1:
5368 case OP_VEXT2:
5369 case OP_VEXT3:
5370 return DAG.getNode(ARMISD::VEXT, dl, VT,
5371 OpLHS, OpRHS,
5372 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
5373 case OP_VUZPL:
5374 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005375 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005376 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5377 case OP_VZIPL:
5378 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005379 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005380 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5381 case OP_VTRNL:
5382 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005383 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5384 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005385 }
5386}
5387
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005388static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005389 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005390 SelectionDAG &DAG) {
5391 // Check to see if we can use the VTBL instruction.
5392 SDValue V1 = Op.getOperand(0);
5393 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005394 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005395
5396 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005397 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005398 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
5399 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
5400
5401 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5402 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005403 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005404
Owen Anderson77aa2662011-04-05 21:48:57 +00005405 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005406 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005407}
5408
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005409static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5410 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005411 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005412 SDValue OpLHS = Op.getOperand(0);
5413 EVT VT = OpLHS.getValueType();
5414
5415 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5416 "Expect an v8i16/v16i8 type");
5417 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5418 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5419 // extract the first 8 bytes into the top double word and the last 8 bytes
5420 // into the bottom double word. The v8i16 case is similar.
5421 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5422 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
5423 DAG.getConstant(ExtractNum, MVT::i32));
5424}
5425
Bob Wilson2e076c42009-06-22 23:27:02 +00005426static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005427 SDValue V1 = Op.getOperand(0);
5428 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005429 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005430 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005431 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005432
Bob Wilsonc6800b52009-08-13 02:13:04 +00005433 // Convert shuffles that are directly supported on NEON to target-specific
5434 // DAG nodes, instead of keeping them as shuffles and matching them again
5435 // during code selection. This is more efficient and avoids the possibility
5436 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005437 // FIXME: floating-point vectors should be canonicalized to integer vectors
5438 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005439 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005440
Bob Wilson846bd792010-06-07 23:53:38 +00005441 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5442 if (EltSize <= 32) {
5443 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5444 int Lane = SVN->getSplatIndex();
5445 // If this is undef splat, generate it via "just" vdup, if possible.
5446 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005447
Dan Gohman198b7ff2011-11-03 21:49:52 +00005448 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005449 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5450 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5451 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005452 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5453 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5454 // reaches it).
5455 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5456 !isa<ConstantSDNode>(V1.getOperand(0))) {
5457 bool IsScalarToVector = true;
5458 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5459 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5460 IsScalarToVector = false;
5461 break;
5462 }
5463 if (IsScalarToVector)
5464 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5465 }
Bob Wilson846bd792010-06-07 23:53:38 +00005466 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
5467 DAG.getConstant(Lane, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005468 }
Bob Wilson846bd792010-06-07 23:53:38 +00005469
5470 bool ReverseVEXT;
5471 unsigned Imm;
5472 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5473 if (ReverseVEXT)
5474 std::swap(V1, V2);
5475 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
5476 DAG.getConstant(Imm, MVT::i32));
5477 }
5478
5479 if (isVREVMask(ShuffleMask, VT, 64))
5480 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5481 if (isVREVMask(ShuffleMask, VT, 32))
5482 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5483 if (isVREVMask(ShuffleMask, VT, 16))
5484 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5485
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005486 if (V2->getOpcode() == ISD::UNDEF &&
5487 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5488 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
5489 DAG.getConstant(Imm, MVT::i32));
5490 }
5491
Bob Wilson846bd792010-06-07 23:53:38 +00005492 // Check for Neon shuffles that modify both input vectors in place.
5493 // If both results are used, i.e., if there are two shuffles with the same
5494 // source operands and with masks corresponding to both results of one of
5495 // these operations, DAG memoization will ensure that a single node is
5496 // used for both shuffles.
5497 unsigned WhichResult;
5498 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5499 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5500 V1, V2).getValue(WhichResult);
5501 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5502 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5503 V1, V2).getValue(WhichResult);
5504 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5505 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5506 V1, V2).getValue(WhichResult);
5507
5508 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5509 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5510 V1, V1).getValue(WhichResult);
5511 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5512 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5513 V1, V1).getValue(WhichResult);
5514 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5515 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5516 V1, V1).getValue(WhichResult);
Bob Wilsoncce31f62009-08-14 05:08:32 +00005517 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005518
Bob Wilsona7062312009-08-21 20:54:19 +00005519 // If the shuffle is not directly supported and it has 4 elements, use
5520 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005521 unsigned NumElts = VT.getVectorNumElements();
5522 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005523 unsigned PFIndexes[4];
5524 for (unsigned i = 0; i != 4; ++i) {
5525 if (ShuffleMask[i] < 0)
5526 PFIndexes[i] = 8;
5527 else
5528 PFIndexes[i] = ShuffleMask[i];
5529 }
5530
5531 // Compute the index in the perfect shuffle table.
5532 unsigned PFTableIndex =
5533 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005534 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5535 unsigned Cost = (PFEntry >> 30);
5536
5537 if (Cost <= 4)
5538 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5539 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00005540
Bob Wilsond8a9a042010-06-04 00:04:02 +00005541 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00005542 if (EltSize >= 32) {
5543 // Do the expansion with floating-point types, since that is what the VFP
5544 // registers are defined to use, and since i64 is not legal.
5545 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5546 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005547 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5548 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005549 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00005550 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00005551 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00005552 Ops.push_back(DAG.getUNDEF(EltVT));
5553 else
5554 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5555 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5556 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
5557 MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00005558 }
Craig Topper48d114b2014-04-26 18:35:24 +00005559 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005560 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00005561 }
5562
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005563 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5564 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
5565
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005566 if (VT == MVT::v8i8) {
5567 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
5568 if (NewOp.getNode())
5569 return NewOp;
5570 }
5571
Bob Wilson6f34e272009-08-14 05:16:33 +00005572 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005573}
5574
Eli Friedmana5e244c2011-10-24 23:08:52 +00005575static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5576 // INSERT_VECTOR_ELT is legal only for immediate indexes.
5577 SDValue Lane = Op.getOperand(2);
5578 if (!isa<ConstantSDNode>(Lane))
5579 return SDValue();
5580
5581 return Op;
5582}
5583
Bob Wilson2e076c42009-06-22 23:27:02 +00005584static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00005585 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00005586 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00005587 if (!isa<ConstantSDNode>(Lane))
5588 return SDValue();
5589
5590 SDValue Vec = Op.getOperand(0);
5591 if (Op.getValueType() == MVT::i32 &&
5592 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005593 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00005594 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5595 }
5596
5597 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00005598}
5599
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005600static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5601 // The only time a CONCAT_VECTORS operation can have legal types is when
5602 // two 64-bit vectors are concatenated to a 128-bit vector.
5603 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5604 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005605 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005606 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005607 SDValue Op0 = Op.getOperand(0);
5608 SDValue Op1 = Op.getOperand(1);
5609 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005610 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005611 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005612 DAG.getIntPtrConstant(0));
5613 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005614 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005615 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005616 DAG.getIntPtrConstant(1));
Wesley Peck527da1b2010-11-23 03:31:01 +00005617 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005618}
5619
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005620/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5621/// element has been zero/sign-extended, depending on the isSigned parameter,
5622/// from an integer type half its size.
5623static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5624 bool isSigned) {
5625 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5626 EVT VT = N->getValueType(0);
5627 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5628 SDNode *BVN = N->getOperand(0).getNode();
5629 if (BVN->getValueType(0) != MVT::v4i32 ||
5630 BVN->getOpcode() != ISD::BUILD_VECTOR)
5631 return false;
5632 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5633 unsigned HiElt = 1 - LoElt;
5634 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5635 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5636 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5637 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5638 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5639 return false;
5640 if (isSigned) {
5641 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5642 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5643 return true;
5644 } else {
5645 if (Hi0->isNullValue() && Hi1->isNullValue())
5646 return true;
5647 }
5648 return false;
5649 }
5650
5651 if (N->getOpcode() != ISD::BUILD_VECTOR)
5652 return false;
5653
5654 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5655 SDNode *Elt = N->getOperand(i).getNode();
5656 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5657 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5658 unsigned HalfSize = EltSize / 2;
5659 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005660 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005661 return false;
5662 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005663 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005664 return false;
5665 }
5666 continue;
5667 }
5668 return false;
5669 }
5670
5671 return true;
5672}
5673
5674/// isSignExtended - Check if a node is a vector value that is sign-extended
5675/// or a constant BUILD_VECTOR with sign-extended elements.
5676static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5677 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5678 return true;
5679 if (isExtendedBUILD_VECTOR(N, DAG, true))
5680 return true;
5681 return false;
5682}
5683
5684/// isZeroExtended - Check if a node is a vector value that is zero-extended
5685/// or a constant BUILD_VECTOR with zero-extended elements.
5686static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5687 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5688 return true;
5689 if (isExtendedBUILD_VECTOR(N, DAG, false))
5690 return true;
5691 return false;
5692}
5693
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005694static EVT getExtensionTo64Bits(const EVT &OrigVT) {
5695 if (OrigVT.getSizeInBits() >= 64)
5696 return OrigVT;
5697
5698 assert(OrigVT.isSimple() && "Expecting a simple value type");
5699
5700 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
5701 switch (OrigSimpleTy) {
5702 default: llvm_unreachable("Unexpected Vector Type");
5703 case MVT::v2i8:
5704 case MVT::v2i16:
5705 return MVT::v2i32;
5706 case MVT::v4i8:
5707 return MVT::v4i16;
5708 }
5709}
5710
Sebastian Popa204f722012-11-30 19:08:04 +00005711/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5712/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5713/// We insert the required extension here to get the vector to fill a D register.
5714static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5715 const EVT &OrigTy,
5716 const EVT &ExtTy,
5717 unsigned ExtOpcode) {
5718 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5719 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5720 // 64-bits we need to insert a new extension so that it will be 64-bits.
5721 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5722 if (OrigTy.getSizeInBits() >= 64)
5723 return N;
5724
5725 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005726 EVT NewVT = getExtensionTo64Bits(OrigTy);
5727
Andrew Trickef9de2a2013-05-25 02:42:55 +00005728 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00005729}
5730
5731/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5732/// does not do any sign/zero extension. If the original vector is less
5733/// than 64 bits, an appropriate extension will be added after the load to
5734/// reach a total size of 64 bits. We have to add the extension separately
5735/// because ARM does not have a sign/zero extending load for vectors.
5736static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005737 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
5738
5739 // The load already has the right type.
5740 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005741 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00005742 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5743 LD->isNonTemporal(), LD->isInvariant(),
5744 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005745
5746 // We need to create a zextload/sextload. We cannot just create a load
5747 // followed by a zext/zext node because LowerMUL is also run during normal
5748 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005749 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005750 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
5751 LD->getMemoryVT(), LD->isVolatile(),
5752 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00005753}
5754
5755/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5756/// extending load, or BUILD_VECTOR with extended elements, return the
5757/// unextended value. The unextended vector should be 64 bits so that it can
5758/// be used as an operand to a VMULL instruction. If the original vector size
5759/// before extension is less than 64 bits we add a an extension to resize
5760/// the vector to 64 bits.
5761static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00005762 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00005763 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5764 N->getOperand(0)->getValueType(0),
5765 N->getValueType(0),
5766 N->getOpcode());
5767
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005768 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00005769 return SkipLoadExtensionForVMULL(LD, DAG);
5770
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005771 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5772 // have been legalized as a BITCAST from v4i32.
5773 if (N->getOpcode() == ISD::BITCAST) {
5774 SDNode *BVN = N->getOperand(0).getNode();
5775 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5776 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5777 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00005778 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005779 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5780 }
5781 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5782 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5783 EVT VT = N->getValueType(0);
5784 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5785 unsigned NumElts = VT.getVectorNumElements();
5786 MVT TruncVT = MVT::getIntegerVT(EltSize);
5787 SmallVector<SDValue, 8> Ops;
5788 for (unsigned i = 0; i != NumElts; ++i) {
5789 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5790 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00005791 // Element types smaller than 32 bits are not legal, so use i32 elements.
5792 // The values are implicitly truncated so sext vs. zext doesn't matter.
5793 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005794 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005795 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topper48d114b2014-04-26 18:35:24 +00005796 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00005797}
5798
Evan Chenge2086e72011-03-29 01:56:09 +00005799static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5800 unsigned Opcode = N->getOpcode();
5801 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5802 SDNode *N0 = N->getOperand(0).getNode();
5803 SDNode *N1 = N->getOperand(1).getNode();
5804 return N0->hasOneUse() && N1->hasOneUse() &&
5805 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5806 }
5807 return false;
5808}
5809
5810static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5811 unsigned Opcode = N->getOpcode();
5812 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5813 SDNode *N0 = N->getOperand(0).getNode();
5814 SDNode *N1 = N->getOperand(1).getNode();
5815 return N0->hasOneUse() && N1->hasOneUse() &&
5816 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
5817 }
5818 return false;
5819}
5820
Bob Wilson38ab35a2010-09-01 23:50:19 +00005821static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
5822 // Multiplications are only custom-lowered for 128-bit vectors so that
5823 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
5824 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00005825 assert(VT.is128BitVector() && VT.isInteger() &&
5826 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00005827 SDNode *N0 = Op.getOperand(0).getNode();
5828 SDNode *N1 = Op.getOperand(1).getNode();
5829 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00005830 bool isMLA = false;
5831 bool isN0SExt = isSignExtended(N0, DAG);
5832 bool isN1SExt = isSignExtended(N1, DAG);
5833 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00005834 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00005835 else {
5836 bool isN0ZExt = isZeroExtended(N0, DAG);
5837 bool isN1ZExt = isZeroExtended(N1, DAG);
5838 if (isN0ZExt && isN1ZExt)
5839 NewOpc = ARMISD::VMULLu;
5840 else if (isN1SExt || isN1ZExt) {
5841 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5842 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5843 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5844 NewOpc = ARMISD::VMULLs;
5845 isMLA = true;
5846 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5847 NewOpc = ARMISD::VMULLu;
5848 isMLA = true;
5849 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5850 std::swap(N0, N1);
5851 NewOpc = ARMISD::VMULLu;
5852 isMLA = true;
5853 }
5854 }
5855
5856 if (!NewOpc) {
5857 if (VT == MVT::v2i64)
5858 // Fall through to expand this. It is not legal.
5859 return SDValue();
5860 else
5861 // Other vector multiplications are legal.
5862 return Op;
5863 }
5864 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005865
5866 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005867 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00005868 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00005869 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005870 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00005871 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005872 assert(Op0.getValueType().is64BitVector() &&
5873 Op1.getValueType().is64BitVector() &&
5874 "unexpected types for extended operands to VMULL");
5875 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
5876 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005877
Evan Chenge2086e72011-03-29 01:56:09 +00005878 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
5879 // isel lowering to take advantage of no-stall back to back vmul + vmla.
5880 // vmull q0, d4, d6
5881 // vmlal q0, d5, d6
5882 // is faster than
5883 // vaddl q0, d4, d5
5884 // vmovl q1, d6
5885 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00005886 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
5887 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005888 EVT Op1VT = Op1.getValueType();
5889 return DAG.getNode(N0->getOpcode(), DL, VT,
5890 DAG.getNode(NewOpc, DL, VT,
5891 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
5892 DAG.getNode(NewOpc, DL, VT,
5893 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00005894}
5895
Owen Anderson77aa2662011-04-05 21:48:57 +00005896static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005897LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005898 // Convert to float
5899 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
5900 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
5901 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
5902 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
5903 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
5904 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
5905 // Get reciprocal estimate.
5906 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00005907 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005908 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
5909 // Because char has a smaller range than uchar, we can actually get away
5910 // without any newton steps. This requires that we use a weird bias
5911 // of 0xb000, however (again, this has been exhaustively tested).
5912 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
5913 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
5914 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
5915 Y = DAG.getConstant(0xb000, MVT::i32);
5916 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
5917 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
5918 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
5919 // Convert back to short.
5920 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
5921 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
5922 return X;
5923}
5924
Owen Anderson77aa2662011-04-05 21:48:57 +00005925static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005926LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005927 SDValue N2;
5928 // Convert to float.
5929 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5930 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5931 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5932 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5933 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5934 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005935
Nate Begemanfa62d502011-02-11 20:53:29 +00005936 // Use reciprocal estimate and one refinement step.
5937 // float4 recip = vrecpeq_f32(yf);
5938 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00005939 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005940 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005941 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005942 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5943 N1, N2);
5944 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5945 // Because short has a smaller range than ushort, we can actually get away
5946 // with only a single newton step. This requires that we use a weird bias
5947 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005948 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00005949 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5950 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005951 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00005952 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5953 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5954 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5955 // Convert back to integer and return.
5956 // return vmovn_s32(vcvt_s32_f32(result));
5957 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5958 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5959 return N0;
5960}
5961
5962static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5963 EVT VT = Op.getValueType();
5964 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5965 "unexpected type for custom-lowering ISD::SDIV");
5966
Andrew Trickef9de2a2013-05-25 02:42:55 +00005967 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00005968 SDValue N0 = Op.getOperand(0);
5969 SDValue N1 = Op.getOperand(1);
5970 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00005971
Nate Begemanfa62d502011-02-11 20:53:29 +00005972 if (VT == MVT::v8i8) {
5973 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5974 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005975
Nate Begemanfa62d502011-02-11 20:53:29 +00005976 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5977 DAG.getIntPtrConstant(4));
5978 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00005979 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00005980 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5981 DAG.getIntPtrConstant(0));
5982 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5983 DAG.getIntPtrConstant(0));
5984
5985 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5986 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5987
5988 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5989 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00005990
Nate Begemanfa62d502011-02-11 20:53:29 +00005991 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5992 return N0;
5993 }
5994 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5995}
5996
5997static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5998 EVT VT = Op.getValueType();
5999 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
6000 "unexpected type for custom-lowering ISD::UDIV");
6001
Andrew Trickef9de2a2013-05-25 02:42:55 +00006002 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00006003 SDValue N0 = Op.getOperand(0);
6004 SDValue N1 = Op.getOperand(1);
6005 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00006006
Nate Begemanfa62d502011-02-11 20:53:29 +00006007 if (VT == MVT::v8i8) {
6008 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
6009 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006010
Nate Begemanfa62d502011-02-11 20:53:29 +00006011 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6012 DAG.getIntPtrConstant(4));
6013 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00006014 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00006015 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
6016 DAG.getIntPtrConstant(0));
6017 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
6018 DAG.getIntPtrConstant(0));
Owen Anderson77aa2662011-04-05 21:48:57 +00006019
Nate Begemanfa62d502011-02-11 20:53:29 +00006020 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
6021 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00006022
Nate Begemanfa62d502011-02-11 20:53:29 +00006023 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
6024 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00006025
6026 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begemanfa62d502011-02-11 20:53:29 +00006027 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
6028 N0);
6029 return N0;
6030 }
Owen Anderson77aa2662011-04-05 21:48:57 +00006031
Nate Begemanfa62d502011-02-11 20:53:29 +00006032 // v4i16 sdiv ... Convert to float.
6033 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
6034 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
6035 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
6036 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
6037 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006038 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00006039
6040 // Use reciprocal estimate and two refinement steps.
6041 // float4 recip = vrecpeq_f32(yf);
6042 // recip *= vrecpsq_f32(yf, recip);
6043 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006044 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006045 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006046 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006047 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006048 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006049 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006050 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006051 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006052 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006053 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6054 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6055 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6056 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006057 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006058 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6059 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
6060 N1 = DAG.getConstant(2, MVT::i32);
6061 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6062 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6063 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6064 // Convert back to integer and return.
6065 // return vmovn_u32(vcvt_s32_f32(result));
6066 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6067 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6068 return N0;
6069}
6070
Evan Chenge8916542011-08-30 01:34:54 +00006071static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6072 EVT VT = Op.getNode()->getValueType(0);
6073 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6074
6075 unsigned Opc;
6076 bool ExtraOp = false;
6077 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006078 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006079 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6080 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6081 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6082 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6083 }
6084
6085 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006086 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006087 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006088 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006089 Op.getOperand(1), Op.getOperand(2));
6090}
6091
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006092SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6093 assert(Subtarget->isTargetDarwin());
6094
6095 // For iOS, we want to call an alternative entry point: __sincos_stret,
6096 // return values are passed via sret.
6097 SDLoc dl(Op);
6098 SDValue Arg = Op.getOperand(0);
6099 EVT ArgVT = Arg.getValueType();
6100 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
6101
6102 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
6103 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6104
6105 // Pair of floats / doubles used to pass the result.
6106 StructType *RetTy = StructType::get(ArgTy, ArgTy, NULL);
6107
6108 // Create stack object for sret.
6109 const uint64_t ByteSize = TLI.getDataLayout()->getTypeAllocSize(RetTy);
6110 const unsigned StackAlign = TLI.getDataLayout()->getPrefTypeAlignment(RetTy);
6111 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6112 SDValue SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
6113
6114 ArgListTy Args;
6115 ArgListEntry Entry;
6116
6117 Entry.Node = SRet;
6118 Entry.Ty = RetTy->getPointerTo();
6119 Entry.isSExt = false;
6120 Entry.isZExt = false;
6121 Entry.isSRet = true;
6122 Args.push_back(Entry);
6123
6124 Entry.Node = Arg;
6125 Entry.Ty = ArgTy;
6126 Entry.isSExt = false;
6127 Entry.isZExt = false;
6128 Args.push_back(Entry);
6129
6130 const char *LibcallName = (ArgVT == MVT::f64)
6131 ? "__sincos_stret" : "__sincosf_stret";
6132 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
6133
6134 TargetLowering::
6135 CallLoweringInfo CLI(DAG.getEntryNode(), Type::getVoidTy(*DAG.getContext()),
6136 false, false, false, false, 0,
6137 CallingConv::C, /*isTaillCall=*/false,
6138 /*doesNotRet=*/false, /*isReturnValueUsed*/false,
6139 Callee, Args, DAG, dl);
6140 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6141
6142 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6143 MachinePointerInfo(), false, false, false, 0);
6144
6145 // Address of cos field.
6146 SDValue Add = DAG.getNode(ISD::ADD, dl, getPointerTy(), SRet,
6147 DAG.getIntPtrConstant(ArgVT.getStoreSize()));
6148 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6149 MachinePointerInfo(), false, false, false, 0);
6150
6151 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6152 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6153 LoadSin.getValue(0), LoadCos.getValue(0));
6154}
6155
Eli Friedman10f9ce22011-09-15 22:26:18 +00006156static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006157 // Monotonic load/store is legal for all targets
6158 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6159 return Op;
6160
Alp Tokercb402912014-01-24 17:20:08 +00006161 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006162 // dmb or equivalent available.
6163 return SDValue();
6164}
6165
Tim Northoverbc933082013-05-23 19:11:20 +00006166static void ReplaceREADCYCLECOUNTER(SDNode *N,
6167 SmallVectorImpl<SDValue> &Results,
6168 SelectionDAG &DAG,
6169 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006170 SDLoc DL(N);
Tim Northoverbc933082013-05-23 19:11:20 +00006171 SDValue Cycles32, OutChain;
6172
6173 if (Subtarget->hasPerfMon()) {
6174 // Under Power Management extensions, the cycle-count is:
6175 // mrc p15, #0, <Rt>, c9, c13, #0
6176 SDValue Ops[] = { N->getOperand(0), // Chain
6177 DAG.getConstant(Intrinsic::arm_mrc, MVT::i32),
6178 DAG.getConstant(15, MVT::i32),
6179 DAG.getConstant(0, MVT::i32),
6180 DAG.getConstant(9, MVT::i32),
6181 DAG.getConstant(13, MVT::i32),
6182 DAG.getConstant(0, MVT::i32)
6183 };
6184
6185 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00006186 DAG.getVTList(MVT::i32, MVT::Other), Ops);
Tim Northoverbc933082013-05-23 19:11:20 +00006187 OutChain = Cycles32.getValue(1);
6188 } else {
6189 // Intrinsic is defined to return 0 on unsupported platforms. Technically
6190 // there are older ARM CPUs that have implementation-specific ways of
6191 // obtaining this information (FIXME!).
6192 Cycles32 = DAG.getConstant(0, MVT::i32);
6193 OutChain = DAG.getEntryNode();
6194 }
6195
6196
6197 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64,
6198 Cycles32, DAG.getConstant(0, MVT::i32));
6199 Results.push_back(Cycles64);
6200 Results.push_back(OutChain);
6201}
6202
Dan Gohman21cea8a2010-04-17 15:26:15 +00006203SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006204 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006205 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Cheng10043e22007-01-19 07:51:42 +00006206 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006207 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006208 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006209 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6210 default: llvm_unreachable("unknown object format");
6211 case Triple::COFF:
6212 return LowerGlobalAddressWindows(Op, DAG);
6213 case Triple::ELF:
6214 return LowerGlobalAddressELF(Op, DAG);
6215 case Triple::MachO:
6216 return LowerGlobalAddressDarwin(Op, DAG);
6217 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006218 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006219 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006220 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6221 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006222 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006223 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006224 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006225 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006226 case ISD::SINT_TO_FP:
6227 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6228 case ISD::FP_TO_SINT:
6229 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006230 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006231 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006232 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006233 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006234 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006235 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006236 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6237 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006238 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006239 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006240 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006241 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng15b80e42009-11-12 07:13:11 +00006242 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006243 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006244 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach8546ec92010-01-18 19:58:49 +00006245 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006246 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006247 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006248 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006249 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006250 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006251 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006252 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006253 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006254 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006255 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanfa62d502011-02-11 20:53:29 +00006256 case ISD::SDIV: return LowerSDIV(Op, DAG);
6257 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006258 case ISD::ADDC:
6259 case ISD::ADDE:
6260 case ISD::SUBC:
6261 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006262 case ISD::SADDO:
6263 case ISD::UADDO:
6264 case ISD::SSUBO:
6265 case ISD::USUBO:
6266 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006267 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006268 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006269 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006270 case ISD::SDIVREM:
6271 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006272 }
Evan Cheng10043e22007-01-19 07:51:42 +00006273}
6274
Duncan Sands6ed40142008-12-01 11:39:25 +00006275/// ReplaceNodeResults - Replace the results of node with an illegal result
6276/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006277void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
6278 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006279 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006280 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006281 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006282 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006283 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peck527da1b2010-11-23 03:31:01 +00006284 case ISD::BITCAST:
6285 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006286 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006287 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006288 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006289 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006290 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006291 case ISD::READCYCLECOUNTER:
6292 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6293 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00006294 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006295 if (Res.getNode())
6296 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006297}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006298
Evan Cheng10043e22007-01-19 07:51:42 +00006299//===----------------------------------------------------------------------===//
6300// ARM Scheduler Hooks
6301//===----------------------------------------------------------------------===//
6302
Bill Wendling030b58e2011-10-06 22:18:16 +00006303/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6304/// registers the function context.
6305void ARMTargetLowering::
6306SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6307 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendling374ee192011-10-03 21:25:38 +00006308 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6309 DebugLoc dl = MI->getDebugLoc();
6310 MachineFunction *MF = MBB->getParent();
6311 MachineRegisterInfo *MRI = &MF->getRegInfo();
6312 MachineConstantPool *MCP = MF->getConstantPool();
6313 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6314 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006315
Bill Wendling374ee192011-10-03 21:25:38 +00006316 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006317 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006318
Bill Wendling374ee192011-10-03 21:25:38 +00006319 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006320 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006321 ARMConstantPoolValue *CPV =
6322 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6323 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6324
Craig Topperc7242e02012-04-20 07:30:17 +00006325 const TargetRegisterClass *TRC = isThumb ?
6326 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6327 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006328
Bill Wendling030b58e2011-10-06 22:18:16 +00006329 // Grab constant pool and fixed stack memory operands.
6330 MachineMemOperand *CPMMO =
6331 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6332 MachineMemOperand::MOLoad, 4, 4);
6333
6334 MachineMemOperand *FIMMOSt =
6335 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6336 MachineMemOperand::MOStore, 4, 4);
6337
6338 // Load the address of the dispatch MBB into the jump buffer.
6339 if (isThumb2) {
6340 // Incoming value: jbuf
6341 // ldr.n r5, LCPI1_1
6342 // orr r5, r5, #1
6343 // add r5, pc
6344 // str r5, [$jbuf, #+4] ; &jbuf[1]
6345 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6346 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6347 .addConstantPoolIndex(CPI)
6348 .addMemOperand(CPMMO));
6349 // Set the low bit because of thumb mode.
6350 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6351 AddDefaultCC(
6352 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6353 .addReg(NewVReg1, RegState::Kill)
6354 .addImm(0x01)));
6355 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6356 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6357 .addReg(NewVReg2, RegState::Kill)
6358 .addImm(PCLabelId);
6359 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6360 .addReg(NewVReg3, RegState::Kill)
6361 .addFrameIndex(FI)
6362 .addImm(36) // &jbuf[1] :: pc
6363 .addMemOperand(FIMMOSt));
6364 } else if (isThumb) {
6365 // Incoming value: jbuf
6366 // ldr.n r1, LCPI1_4
6367 // add r1, pc
6368 // mov r2, #1
6369 // orrs r1, r2
6370 // add r2, $jbuf, #+4 ; &jbuf[1]
6371 // str r1, [r2]
6372 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6373 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6374 .addConstantPoolIndex(CPI)
6375 .addMemOperand(CPMMO));
6376 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6377 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6378 .addReg(NewVReg1, RegState::Kill)
6379 .addImm(PCLabelId);
6380 // Set the low bit because of thumb mode.
6381 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6382 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6383 .addReg(ARM::CPSR, RegState::Define)
6384 .addImm(1));
6385 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6386 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6387 .addReg(ARM::CPSR, RegState::Define)
6388 .addReg(NewVReg2, RegState::Kill)
6389 .addReg(NewVReg3, RegState::Kill));
6390 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6391 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
6392 .addFrameIndex(FI)
6393 .addImm(36)); // &jbuf[1] :: pc
6394 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6395 .addReg(NewVReg4, RegState::Kill)
6396 .addReg(NewVReg5, RegState::Kill)
6397 .addImm(0)
6398 .addMemOperand(FIMMOSt));
6399 } else {
6400 // Incoming value: jbuf
6401 // ldr r1, LCPI1_1
6402 // add r1, pc, r1
6403 // str r1, [$jbuf, #+4] ; &jbuf[1]
6404 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6405 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6406 .addConstantPoolIndex(CPI)
6407 .addImm(0)
6408 .addMemOperand(CPMMO));
6409 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6410 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6411 .addReg(NewVReg1, RegState::Kill)
6412 .addImm(PCLabelId));
6413 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6414 .addReg(NewVReg2, RegState::Kill)
6415 .addFrameIndex(FI)
6416 .addImm(36) // &jbuf[1] :: pc
6417 .addMemOperand(FIMMOSt));
6418 }
6419}
6420
6421MachineBasicBlock *ARMTargetLowering::
6422EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
6423 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6424 DebugLoc dl = MI->getDebugLoc();
6425 MachineFunction *MF = MBB->getParent();
6426 MachineRegisterInfo *MRI = &MF->getRegInfo();
6427 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6428 MachineFrameInfo *MFI = MF->getFrameInfo();
6429 int FI = MFI->getFunctionContextIndex();
6430
Craig Topperc7242e02012-04-20 07:30:17 +00006431 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
6432 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen691ae332012-05-20 06:38:47 +00006433 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00006434
Bill Wendling362c1b02011-10-06 21:29:56 +00006435 // Get a mapping of the call site numbers to all of the landing pads they're
6436 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00006437 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6438 unsigned MaxCSNum = 0;
6439 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00006440 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6441 ++BB) {
Bill Wendling202803e2011-10-05 00:02:33 +00006442 if (!BB->isLandingPad()) continue;
6443
6444 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6445 // pad.
6446 for (MachineBasicBlock::iterator
6447 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6448 if (!II->isEHLabel()) continue;
6449
6450 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006451 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006452
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006453 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6454 for (SmallVectorImpl<unsigned>::iterator
6455 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6456 CSI != CSE; ++CSI) {
6457 CallSiteNumToLPad[*CSI].push_back(BB);
6458 MaxCSNum = std::max(MaxCSNum, *CSI);
6459 }
Bill Wendling202803e2011-10-05 00:02:33 +00006460 break;
6461 }
6462 }
6463
6464 // Get an ordered list of the machine basic blocks for the jump table.
6465 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00006466 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00006467 LPadList.reserve(CallSiteNumToLPad.size());
6468 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6469 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6470 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006471 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00006472 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00006473 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6474 }
Bill Wendling202803e2011-10-05 00:02:33 +00006475 }
6476
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006477 assert(!LPadList.empty() &&
6478 "No landing pad destinations for the dispatch jump table!");
6479
Bill Wendling362c1b02011-10-06 21:29:56 +00006480 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00006481 MachineJumpTableInfo *JTI =
6482 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6483 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6484 unsigned UId = AFI->createJumpTableUId();
Chad Rosier96603432013-03-01 18:30:38 +00006485 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00006486
Bill Wendling362c1b02011-10-06 21:29:56 +00006487 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00006488
6489 // Shove the dispatch's address into the return slot in the function context.
6490 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6491 DispatchBB->setIsLandingPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00006492
Bill Wendling324be982011-10-05 00:39:32 +00006493 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006494 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00006495 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006496 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00006497 else
6498 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6499
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006500 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00006501 DispatchBB->addSuccessor(TrapBB);
6502
6503 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6504 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00006505
Bill Wendling510fbcd2011-10-17 21:32:56 +00006506 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00006507 MF->insert(MF->end(), DispatchBB);
6508 MF->insert(MF->end(), DispContBB);
6509 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00006510
Bill Wendling030b58e2011-10-06 22:18:16 +00006511 // Insert code into the entry block that creates and registers the function
6512 // context.
6513 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6514
Bill Wendling030b58e2011-10-06 22:18:16 +00006515 MachineMemOperand *FIMMOLd =
Bill Wendling362c1b02011-10-06 21:29:56 +00006516 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendlingb3d46782011-10-06 23:37:36 +00006517 MachineMemOperand::MOLoad |
6518 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00006519
Chad Rosier1ec8e402012-11-06 23:05:24 +00006520 MachineInstrBuilder MIB;
6521 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6522
6523 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6524 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6525
6526 // Add a register mask with no preserved registers. This results in all
6527 // registers being marked as clobbered.
6528 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00006529
Bill Wendling85833f72011-10-18 22:49:07 +00006530 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00006531 if (Subtarget->isThumb2()) {
6532 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6533 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6534 .addFrameIndex(FI)
6535 .addImm(4)
6536 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006537
Bill Wendling85833f72011-10-18 22:49:07 +00006538 if (NumLPads < 256) {
6539 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6540 .addReg(NewVReg1)
6541 .addImm(LPadList.size()));
6542 } else {
6543 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6544 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006545 .addImm(NumLPads & 0xFFFF));
6546
6547 unsigned VReg2 = VReg1;
6548 if ((NumLPads & 0xFFFF0000) != 0) {
6549 VReg2 = MRI->createVirtualRegister(TRC);
6550 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6551 .addReg(VReg1)
6552 .addImm(NumLPads >> 16));
6553 }
6554
Bill Wendling85833f72011-10-18 22:49:07 +00006555 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6556 .addReg(NewVReg1)
6557 .addReg(VReg2));
6558 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006559
Bill Wendling5626c662011-10-06 22:53:00 +00006560 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6561 .addMBB(TrapBB)
6562 .addImm(ARMCC::HI)
6563 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00006564
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006565 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6566 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006567 .addJumpTableIndex(MJTI)
6568 .addImm(UId));
Bill Wendling202803e2011-10-05 00:02:33 +00006569
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006570 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006571 AddDefaultCC(
6572 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006573 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6574 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00006575 .addReg(NewVReg1)
6576 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6577
6578 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006579 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00006580 .addReg(NewVReg1)
Bill Wendling5626c662011-10-06 22:53:00 +00006581 .addJumpTableIndex(MJTI)
6582 .addImm(UId);
6583 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00006584 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6585 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6586 .addFrameIndex(FI)
6587 .addImm(1)
6588 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00006589
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006590 if (NumLPads < 256) {
6591 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6592 .addReg(NewVReg1)
6593 .addImm(NumLPads));
6594 } else {
6595 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00006596 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6597 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6598
6599 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006600 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006601 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006602 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006603 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006604
6605 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6606 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6607 .addReg(VReg1, RegState::Define)
6608 .addConstantPoolIndex(Idx));
6609 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6610 .addReg(NewVReg1)
6611 .addReg(VReg1));
6612 }
6613
Bill Wendlingb3d46782011-10-06 23:37:36 +00006614 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6615 .addMBB(TrapBB)
6616 .addImm(ARMCC::HI)
6617 .addReg(ARM::CPSR);
6618
6619 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6620 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6621 .addReg(ARM::CPSR, RegState::Define)
6622 .addReg(NewVReg1)
6623 .addImm(2));
6624
6625 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00006626 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendlingb3d46782011-10-06 23:37:36 +00006627 .addJumpTableIndex(MJTI)
6628 .addImm(UId));
6629
6630 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6631 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6632 .addReg(ARM::CPSR, RegState::Define)
6633 .addReg(NewVReg2, RegState::Kill)
6634 .addReg(NewVReg3));
6635
6636 MachineMemOperand *JTMMOLd =
6637 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6638 MachineMemOperand::MOLoad, 4, 4);
6639
6640 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6641 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6642 .addReg(NewVReg4, RegState::Kill)
6643 .addImm(0)
6644 .addMemOperand(JTMMOLd));
6645
Chad Rosier96603432013-03-01 18:30:38 +00006646 unsigned NewVReg6 = NewVReg5;
6647 if (RelocM == Reloc::PIC_) {
6648 NewVReg6 = MRI->createVirtualRegister(TRC);
6649 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6650 .addReg(ARM::CPSR, RegState::Define)
6651 .addReg(NewVReg5, RegState::Kill)
6652 .addReg(NewVReg3));
6653 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00006654
6655 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6656 .addReg(NewVReg6, RegState::Kill)
6657 .addJumpTableIndex(MJTI)
6658 .addImm(UId);
Bill Wendling5626c662011-10-06 22:53:00 +00006659 } else {
6660 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6661 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6662 .addFrameIndex(FI)
6663 .addImm(4)
6664 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00006665
Bill Wendling4969dcd2011-10-18 22:52:20 +00006666 if (NumLPads < 256) {
6667 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6668 .addReg(NewVReg1)
6669 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00006670 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00006671 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6672 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006673 .addImm(NumLPads & 0xFFFF));
6674
6675 unsigned VReg2 = VReg1;
6676 if ((NumLPads & 0xFFFF0000) != 0) {
6677 VReg2 = MRI->createVirtualRegister(TRC);
6678 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6679 .addReg(VReg1)
6680 .addImm(NumLPads >> 16));
6681 }
6682
Bill Wendling4969dcd2011-10-18 22:52:20 +00006683 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6684 .addReg(NewVReg1)
6685 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00006686 } else {
6687 MachineConstantPool *ConstantPool = MF->getConstantPool();
6688 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6689 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6690
6691 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006692 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006693 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006694 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006695 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6696
6697 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6698 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6699 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00006700 .addConstantPoolIndex(Idx)
6701 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00006702 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6703 .addReg(NewVReg1)
6704 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00006705 }
6706
Bill Wendling5626c662011-10-06 22:53:00 +00006707 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6708 .addMBB(TrapBB)
6709 .addImm(ARMCC::HI)
6710 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00006711
Bill Wendling973c8172011-10-18 22:11:18 +00006712 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006713 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00006714 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006715 .addReg(NewVReg1)
6716 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00006717 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6718 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006719 .addJumpTableIndex(MJTI)
6720 .addImm(UId));
6721
6722 MachineMemOperand *JTMMOLd =
6723 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6724 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00006725 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006726 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00006727 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6728 .addReg(NewVReg3, RegState::Kill)
6729 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006730 .addImm(0)
6731 .addMemOperand(JTMMOLd));
6732
Chad Rosier96603432013-03-01 18:30:38 +00006733 if (RelocM == Reloc::PIC_) {
6734 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
6735 .addReg(NewVReg5, RegState::Kill)
6736 .addReg(NewVReg4)
6737 .addJumpTableIndex(MJTI)
6738 .addImm(UId);
6739 } else {
6740 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
6741 .addReg(NewVReg5, RegState::Kill)
6742 .addJumpTableIndex(MJTI)
6743 .addImm(UId);
6744 }
Bill Wendling5626c662011-10-06 22:53:00 +00006745 }
Bill Wendling202803e2011-10-05 00:02:33 +00006746
Bill Wendling324be982011-10-05 00:39:32 +00006747 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006748 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00006749 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006750 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6751 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006752 if (SeenMBBs.insert(CurMBB))
Bill Wendling883ec972011-10-07 23:18:02 +00006753 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006754 }
6755
Bill Wendling26d27802011-10-17 05:25:09 +00006756 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00006757 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00006758 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling883ec972011-10-07 23:18:02 +00006759 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6760 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6761 MachineBasicBlock *BB = *I;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006762
6763 // Remove the landing pad successor from the invoke block and replace it
6764 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00006765 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6766 BB->succ_end());
6767 while (!Successors.empty()) {
6768 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling883ec972011-10-07 23:18:02 +00006769 if (SMBB->isLandingPad()) {
6770 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00006771 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006772 }
6773 }
6774
6775 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006776
6777 // Find the invoke call and mark all of the callee-saved registers as
6778 // 'implicit defined' so that they're spilled. This prevents code from
6779 // moving instructions to before the EH block, where they will never be
6780 // executed.
6781 for (MachineBasicBlock::reverse_iterator
6782 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00006783 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006784
6785 DenseMap<unsigned, bool> DefRegs;
6786 for (MachineInstr::mop_iterator
6787 OI = II->operands_begin(), OE = II->operands_end();
6788 OI != OE; ++OI) {
6789 if (!OI->isReg()) continue;
6790 DefRegs[OI->getReg()] = true;
6791 }
6792
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00006793 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006794
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006795 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00006796 unsigned Reg = SavedRegs[i];
6797 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00006798 !ARM::tGPRRegClass.contains(Reg) &&
6799 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006800 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006801 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006802 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006803 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006804 continue;
6805 if (!DefRegs[Reg])
6806 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006807 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006808
6809 break;
6810 }
Bill Wendling883ec972011-10-07 23:18:02 +00006811 }
Bill Wendling324be982011-10-05 00:39:32 +00006812
Bill Wendling617075f2011-10-18 18:30:49 +00006813 // Mark all former landing pads as non-landing pads. The dispatch is the only
6814 // landing pad now.
6815 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6816 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6817 (*I)->setIsLandingPad(false);
6818
Bill Wendling324be982011-10-05 00:39:32 +00006819 // The instruction is gone now.
6820 MI->eraseFromParent();
6821
Bill Wendling374ee192011-10-03 21:25:38 +00006822 return MBB;
6823}
6824
Evan Cheng0cc4ad92010-07-13 19:27:42 +00006825static
6826MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6827 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6828 E = MBB->succ_end(); I != E; ++I)
6829 if (*I != Succ)
6830 return *I;
6831 llvm_unreachable("Expecting a BB with two successors!");
6832}
6833
Manman Renb504f492013-10-29 22:27:32 +00006834/// Return the load opcode for a given load size. If load size >= 8,
6835/// neon opcode will be returned.
6836static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
6837 if (LdSize >= 8)
6838 return LdSize == 16 ? ARM::VLD1q32wb_fixed
6839 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
6840 if (IsThumb1)
6841 return LdSize == 4 ? ARM::tLDRi
6842 : LdSize == 2 ? ARM::tLDRHi
6843 : LdSize == 1 ? ARM::tLDRBi : 0;
6844 if (IsThumb2)
6845 return LdSize == 4 ? ARM::t2LDR_POST
6846 : LdSize == 2 ? ARM::t2LDRH_POST
6847 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
6848 return LdSize == 4 ? ARM::LDR_POST_IMM
6849 : LdSize == 2 ? ARM::LDRH_POST
6850 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
6851}
6852
6853/// Return the store opcode for a given store size. If store size >= 8,
6854/// neon opcode will be returned.
6855static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
6856 if (StSize >= 8)
6857 return StSize == 16 ? ARM::VST1q32wb_fixed
6858 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
6859 if (IsThumb1)
6860 return StSize == 4 ? ARM::tSTRi
6861 : StSize == 2 ? ARM::tSTRHi
6862 : StSize == 1 ? ARM::tSTRBi : 0;
6863 if (IsThumb2)
6864 return StSize == 4 ? ARM::t2STR_POST
6865 : StSize == 2 ? ARM::t2STRH_POST
6866 : StSize == 1 ? ARM::t2STRB_POST : 0;
6867 return StSize == 4 ? ARM::STR_POST_IMM
6868 : StSize == 2 ? ARM::STRH_POST
6869 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
6870}
6871
6872/// Emit a post-increment load operation with given size. The instructions
6873/// will be added to BB at Pos.
6874static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
6875 const TargetInstrInfo *TII, DebugLoc dl,
6876 unsigned LdSize, unsigned Data, unsigned AddrIn,
6877 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6878 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
6879 assert(LdOpc != 0 && "Should have a load opcode");
6880 if (LdSize >= 8) {
6881 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6882 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6883 .addImm(0));
6884 } else if (IsThumb1) {
6885 // load + update AddrIn
6886 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6887 .addReg(AddrIn).addImm(0));
6888 MachineInstrBuilder MIB =
6889 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6890 MIB = AddDefaultT1CC(MIB);
6891 MIB.addReg(AddrIn).addImm(LdSize);
6892 AddDefaultPred(MIB);
6893 } else if (IsThumb2) {
6894 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6895 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6896 .addImm(LdSize));
6897 } else { // arm
6898 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6899 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6900 .addReg(0).addImm(LdSize));
6901 }
6902}
6903
6904/// Emit a post-increment store operation with given size. The instructions
6905/// will be added to BB at Pos.
6906static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
6907 const TargetInstrInfo *TII, DebugLoc dl,
6908 unsigned StSize, unsigned Data, unsigned AddrIn,
6909 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6910 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
6911 assert(StOpc != 0 && "Should have a store opcode");
6912 if (StSize >= 8) {
6913 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6914 .addReg(AddrIn).addImm(0).addReg(Data));
6915 } else if (IsThumb1) {
6916 // store + update AddrIn
6917 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
6918 .addReg(AddrIn).addImm(0));
6919 MachineInstrBuilder MIB =
6920 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6921 MIB = AddDefaultT1CC(MIB);
6922 MIB.addReg(AddrIn).addImm(StSize);
6923 AddDefaultPred(MIB);
6924 } else if (IsThumb2) {
6925 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6926 .addReg(Data).addReg(AddrIn).addImm(StSize));
6927 } else { // arm
6928 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6929 .addReg(Data).addReg(AddrIn).addReg(0)
6930 .addImm(StSize));
6931 }
6932}
6933
David Peixottoc32e24a2013-10-17 19:49:22 +00006934MachineBasicBlock *
6935ARMTargetLowering::EmitStructByval(MachineInstr *MI,
6936 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00006937 // This pseudo instruction has 3 operands: dst, src, size
6938 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6939 // Otherwise, we will generate unrolled scalar copies.
6940 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6941 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6942 MachineFunction::iterator It = BB;
6943 ++It;
6944
6945 unsigned dest = MI->getOperand(0).getReg();
6946 unsigned src = MI->getOperand(1).getReg();
6947 unsigned SizeVal = MI->getOperand(2).getImm();
6948 unsigned Align = MI->getOperand(3).getImm();
6949 DebugLoc dl = MI->getDebugLoc();
6950
Manman Rene8735522012-06-01 19:33:18 +00006951 MachineFunction *MF = BB->getParent();
6952 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00006953 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00006954 const TargetRegisterClass *TRC = nullptr;
6955 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00006956
6957 bool IsThumb1 = Subtarget->isThumb1Only();
6958 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00006959
6960 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00006961 UnitSize = 1;
6962 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00006963 UnitSize = 2;
6964 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00006965 // Check whether we can use NEON instructions.
Bill Wendling698e84f2012-12-30 10:32:01 +00006966 if (!MF->getFunction()->getAttributes().
6967 hasAttribute(AttributeSet::FunctionIndex,
6968 Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00006969 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00006970 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00006971 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00006972 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00006973 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00006974 }
6975 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00006976 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00006977 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00006978 }
Manman Ren6e1fd462012-06-18 22:23:48 +00006979
David Peixottob0653e532013-10-24 16:39:36 +00006980 // Select the correct opcode and register class for unit size load/store
6981 bool IsNeon = UnitSize >= 8;
6982 TRC = (IsThumb1 || IsThumb2) ? (const TargetRegisterClass *)&ARM::tGPRRegClass
6983 : (const TargetRegisterClass *)&ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00006984 if (IsNeon)
David Peixottob0653e532013-10-24 16:39:36 +00006985 VecTRC = UnitSize == 16
6986 ? (const TargetRegisterClass *)&ARM::DPairRegClass
6987 : UnitSize == 8
6988 ? (const TargetRegisterClass *)&ARM::DPRRegClass
Craig Topper062a2ba2014-04-25 05:30:21 +00006989 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00006990
Manman Rene8735522012-06-01 19:33:18 +00006991 unsigned BytesLeft = SizeVal % UnitSize;
6992 unsigned LoopSize = SizeVal - BytesLeft;
6993
6994 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6995 // Use LDR and STR to copy.
6996 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6997 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6998 unsigned srcIn = src;
6999 unsigned destIn = dest;
7000 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00007001 unsigned srcOut = MRI.createVirtualRegister(TRC);
7002 unsigned destOut = MRI.createVirtualRegister(TRC);
7003 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007004 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
7005 IsThumb1, IsThumb2);
7006 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
7007 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007008 srcIn = srcOut;
7009 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007010 }
7011
7012 // Handle the leftover bytes with LDRB and STRB.
7013 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
7014 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00007015 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007016 unsigned srcOut = MRI.createVirtualRegister(TRC);
7017 unsigned destOut = MRI.createVirtualRegister(TRC);
7018 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007019 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
7020 IsThumb1, IsThumb2);
7021 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
7022 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007023 srcIn = srcOut;
7024 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007025 }
7026 MI->eraseFromParent(); // The instruction is gone now.
7027 return BB;
7028 }
7029
7030 // Expand the pseudo op to a loop.
7031 // thisMBB:
7032 // ...
7033 // movw varEnd, # --> with thumb2
7034 // movt varEnd, #
7035 // ldrcp varEnd, idx --> without thumb2
7036 // fallthrough --> loopMBB
7037 // loopMBB:
7038 // PHI varPhi, varEnd, varLoop
7039 // PHI srcPhi, src, srcLoop
7040 // PHI destPhi, dst, destLoop
7041 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7042 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7043 // subs varLoop, varPhi, #UnitSize
7044 // bne loopMBB
7045 // fallthrough --> exitMBB
7046 // exitMBB:
7047 // epilogue to handle left-over bytes
7048 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7049 // [destOut] = STRB_POST(scratch, destLoop, 1)
7050 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7051 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7052 MF->insert(It, loopMBB);
7053 MF->insert(It, exitMBB);
7054
7055 // Transfer the remainder of BB and its successor edges to exitMBB.
7056 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007057 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007058 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7059
7060 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007061 unsigned varEnd = MRI.createVirtualRegister(TRC);
7062 if (IsThumb2) {
7063 unsigned Vtmp = varEnd;
7064 if ((LoopSize & 0xFFFF0000) != 0)
7065 Vtmp = MRI.createVirtualRegister(TRC);
7066 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), Vtmp)
7067 .addImm(LoopSize & 0xFFFF));
7068
7069 if ((LoopSize & 0xFFFF0000) != 0)
7070 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
7071 .addReg(Vtmp).addImm(LoopSize >> 16));
7072 } else {
7073 MachineConstantPool *ConstantPool = MF->getConstantPool();
7074 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7075 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7076
7077 // MachineConstantPool wants an explicit alignment.
7078 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
7079 if (Align == 0)
7080 Align = getDataLayout()->getTypeAllocSize(C->getType());
7081 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7082
7083 if (IsThumb1)
7084 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7085 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7086 else
7087 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7088 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7089 }
Manman Rene8735522012-06-01 19:33:18 +00007090 BB->addSuccessor(loopMBB);
7091
7092 // Generate the loop body:
7093 // varPhi = PHI(varLoop, varEnd)
7094 // srcPhi = PHI(srcLoop, src)
7095 // destPhi = PHI(destLoop, dst)
7096 MachineBasicBlock *entryBB = BB;
7097 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007098 unsigned varLoop = MRI.createVirtualRegister(TRC);
7099 unsigned varPhi = MRI.createVirtualRegister(TRC);
7100 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7101 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7102 unsigned destLoop = MRI.createVirtualRegister(TRC);
7103 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007104
7105 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7106 .addReg(varLoop).addMBB(loopMBB)
7107 .addReg(varEnd).addMBB(entryBB);
7108 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7109 .addReg(srcLoop).addMBB(loopMBB)
7110 .addReg(src).addMBB(entryBB);
7111 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7112 .addReg(destLoop).addMBB(loopMBB)
7113 .addReg(dest).addMBB(entryBB);
7114
7115 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7116 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007117 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007118 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7119 IsThumb1, IsThumb2);
7120 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7121 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007122
7123 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007124 if (IsThumb1) {
7125 MachineInstrBuilder MIB =
7126 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7127 MIB = AddDefaultT1CC(MIB);
7128 MIB.addReg(varPhi).addImm(UnitSize);
7129 AddDefaultPred(MIB);
7130 } else {
7131 MachineInstrBuilder MIB =
7132 BuildMI(*BB, BB->end(), dl,
7133 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7134 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7135 MIB->getOperand(5).setReg(ARM::CPSR);
7136 MIB->getOperand(5).setIsDef(true);
7137 }
7138 BuildMI(*BB, BB->end(), dl,
7139 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7140 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007141
7142 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7143 BB->addSuccessor(loopMBB);
7144 BB->addSuccessor(exitMBB);
7145
7146 // Add epilogue to handle BytesLeft.
7147 BB = exitMBB;
7148 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007149
7150 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7151 // [destOut] = STRB_POST(scratch, destLoop, 1)
7152 unsigned srcIn = srcLoop;
7153 unsigned destIn = destLoop;
7154 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007155 unsigned srcOut = MRI.createVirtualRegister(TRC);
7156 unsigned destOut = MRI.createVirtualRegister(TRC);
7157 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007158 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7159 IsThumb1, IsThumb2);
7160 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7161 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007162 srcIn = srcOut;
7163 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007164 }
7165
7166 MI->eraseFromParent(); // The instruction is gone now.
7167 return BB;
7168}
7169
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007170MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007171ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007172 MachineBasicBlock *BB) const {
Evan Cheng10043e22007-01-19 07:51:42 +00007173 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007174 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007175 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007176 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007177 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007178 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007179 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007180 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007181 // The Thumb2 pre-indexed stores have the same MI operands, they just
7182 // define them differently in the .td files from the isel patterns, so
7183 // they need pseudos.
7184 case ARM::t2STR_preidx:
7185 MI->setDesc(TII->get(ARM::t2STR_PRE));
7186 return BB;
7187 case ARM::t2STRB_preidx:
7188 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7189 return BB;
7190 case ARM::t2STRH_preidx:
7191 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7192 return BB;
7193
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007194 case ARM::STRi_preidx:
7195 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007196 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007197 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7198 // Decode the offset.
7199 unsigned Offset = MI->getOperand(4).getImm();
7200 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7201 Offset = ARM_AM::getAM2Offset(Offset);
7202 if (isSub)
7203 Offset = -Offset;
7204
Jim Grosbachf402f692011-08-12 21:02:34 +00007205 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007206 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007207 .addOperand(MI->getOperand(0)) // Rn_wb
7208 .addOperand(MI->getOperand(1)) // Rt
7209 .addOperand(MI->getOperand(2)) // Rn
7210 .addImm(Offset) // offset (skip GPR==zero_reg)
7211 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007212 .addOperand(MI->getOperand(6))
7213 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007214 MI->eraseFromParent();
7215 return BB;
7216 }
7217 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007218 case ARM::STRBr_preidx:
7219 case ARM::STRH_preidx: {
7220 unsigned NewOpc;
7221 switch (MI->getOpcode()) {
7222 default: llvm_unreachable("unexpected opcode!");
7223 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7224 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7225 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7226 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007227 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7228 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7229 MIB.addOperand(MI->getOperand(i));
7230 MI->eraseFromParent();
7231 return BB;
7232 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007233
Evan Chengbb2af352009-08-12 05:17:19 +00007234 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007235 // To "insert" a SELECT_CC instruction, we actually have to insert the
7236 // diamond control-flow pattern. The incoming instruction knows the
7237 // destination vreg to set, the condition code register to branch on, the
7238 // true/false values to select between, and a branch opcode to use.
7239 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007240 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007241 ++It;
7242
7243 // thisMBB:
7244 // ...
7245 // TrueVal = ...
7246 // cmpTY ccX, r1, r2
7247 // bCC copy1MBB
7248 // fallthrough --> copy0MBB
7249 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007250 MachineFunction *F = BB->getParent();
7251 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7252 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007253 F->insert(It, copy0MBB);
7254 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007255
7256 // Transfer the remainder of BB and its successor edges to sinkMBB.
7257 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007258 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007259 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7260
Dan Gohmanf4f04102010-07-06 15:49:48 +00007261 BB->addSuccessor(copy0MBB);
7262 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007263
Dan Gohman34396292010-07-06 20:24:04 +00007264 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7265 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7266
Evan Cheng10043e22007-01-19 07:51:42 +00007267 // copy0MBB:
7268 // %FalseValue = ...
7269 // # fallthrough to sinkMBB
7270 BB = copy0MBB;
7271
7272 // Update machine-CFG edges
7273 BB->addSuccessor(sinkMBB);
7274
7275 // sinkMBB:
7276 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7277 // ...
7278 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007279 BuildMI(*BB, BB->begin(), dl,
7280 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007281 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7282 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7283
Dan Gohman34396292010-07-06 20:24:04 +00007284 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007285 return BB;
7286 }
Evan Chengb972e562009-08-07 00:34:42 +00007287
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007288 case ARM::BCCi64:
7289 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007290 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007291 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007292
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007293 // Compare both parts that make up the double comparison separately for
7294 // equality.
7295 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7296
7297 unsigned LHS1 = MI->getOperand(1).getReg();
7298 unsigned LHS2 = MI->getOperand(2).getReg();
7299 if (RHSisZero) {
7300 AddDefaultPred(BuildMI(BB, dl,
7301 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7302 .addReg(LHS1).addImm(0));
7303 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7304 .addReg(LHS2).addImm(0)
7305 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7306 } else {
7307 unsigned RHS1 = MI->getOperand(3).getReg();
7308 unsigned RHS2 = MI->getOperand(4).getReg();
7309 AddDefaultPred(BuildMI(BB, dl,
7310 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7311 .addReg(LHS1).addReg(RHS1));
7312 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7313 .addReg(LHS2).addReg(RHS2)
7314 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7315 }
7316
7317 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7318 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7319 if (MI->getOperand(0).getImm() == ARMCC::NE)
7320 std::swap(destMBB, exitMBB);
7321
7322 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7323 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007324 if (isThumb2)
7325 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7326 else
7327 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007328
7329 MI->eraseFromParent(); // The pseudo instruction is gone now.
7330 return BB;
7331 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007332
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007333 case ARM::Int_eh_sjlj_setjmp:
7334 case ARM::Int_eh_sjlj_setjmp_nofp:
7335 case ARM::tInt_eh_sjlj_setjmp:
7336 case ARM::t2Int_eh_sjlj_setjmp:
7337 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7338 EmitSjLjDispatchBlock(MI, BB);
7339 return BB;
7340
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007341 case ARM::ABS:
7342 case ARM::t2ABS: {
7343 // To insert an ABS instruction, we have to insert the
7344 // diamond control-flow pattern. The incoming instruction knows the
7345 // source vreg to test against 0, the destination vreg to set,
7346 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007347 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007348 // It transforms
7349 // V1 = ABS V0
7350 // into
7351 // V2 = MOVS V0
7352 // BCC (branch to SinkBB if V0 >= 0)
7353 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007354 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007355 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7356 MachineFunction::iterator BBI = BB;
7357 ++BBI;
7358 MachineFunction *Fn = BB->getParent();
7359 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7360 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7361 Fn->insert(BBI, RSBBB);
7362 Fn->insert(BBI, SinkBB);
7363
7364 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7365 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7366 bool isThumb2 = Subtarget->isThumb2();
7367 MachineRegisterInfo &MRI = Fn->getRegInfo();
7368 // In Thumb mode S must not be specified if source register is the SP or
7369 // PC and if destination register is the SP, so restrict register class
Craig Topperc7242e02012-04-20 07:30:17 +00007370 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
7371 (const TargetRegisterClass*)&ARM::rGPRRegClass :
7372 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007373
7374 // Transfer the remainder of BB and its successor edges to sinkMBB.
7375 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007376 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007377 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7378
7379 BB->addSuccessor(RSBBB);
7380 BB->addSuccessor(SinkBB);
7381
7382 // fall through to SinkMBB
7383 RSBBB->addSuccessor(SinkBB);
7384
Manman Rene0763c72012-06-15 21:32:12 +00007385 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00007386 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00007387 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7388 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007389
7390 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00007391 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007392 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7393 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7394
7395 // insert rsbri in RSBBB
7396 // Note: BCC and rsbri will be converted into predicated rsbmi
7397 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00007398 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007399 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Rene0763c72012-06-15 21:32:12 +00007400 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007401 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7402
Andrew Trick3f07c422011-10-18 18:40:53 +00007403 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007404 // reuse ABSDstReg to not change uses of ABS instruction
7405 BuildMI(*SinkBB, SinkBB->begin(), dl,
7406 TII->get(ARM::PHI), ABSDstReg)
7407 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00007408 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007409
7410 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00007411 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007412
7413 // return last added BB
7414 return SinkBB;
7415 }
Manman Rene8735522012-06-01 19:33:18 +00007416 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00007417 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00007418 return EmitStructByval(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00007419 }
7420}
7421
Evan Chenge6fba772011-08-30 19:09:48 +00007422void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7423 SDNode *Node) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007424 if (!MI->hasPostISelHook()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007425 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
7426 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
7427 return;
7428 }
7429
Evan Cheng7f8e5632011-12-07 07:15:52 +00007430 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00007431 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7432 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7433 // operand is still set to noreg. If needed, set the optional operand's
7434 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00007435 //
Andrew Trick88b24502011-10-18 19:18:52 +00007436 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00007437
Andrew Trick924123a2011-09-21 02:20:46 +00007438 // Rename pseudo opcodes.
7439 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7440 if (NewOpc) {
7441 const ARMBaseInstrInfo *TII =
7442 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick88b24502011-10-18 19:18:52 +00007443 MCID = &TII->get(NewOpc);
7444
7445 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7446 "converted opcode should be the same except for cc_out");
7447
7448 MI->setDesc(*MCID);
7449
7450 // Add the optional cc_out operand
7451 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00007452 }
Andrew Trick88b24502011-10-18 19:18:52 +00007453 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00007454
7455 // Any ARM instruction that sets the 's' bit should specify an optional
7456 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00007457 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007458 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007459 return;
7460 }
Andrew Trick924123a2011-09-21 02:20:46 +00007461 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7462 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007463 bool definesCPSR = false;
7464 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00007465 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00007466 i != e; ++i) {
7467 const MachineOperand &MO = MI->getOperand(i);
7468 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7469 definesCPSR = true;
7470 if (MO.isDead())
7471 deadCPSR = true;
7472 MI->RemoveOperand(i);
7473 break;
Evan Chenge6fba772011-08-30 19:09:48 +00007474 }
7475 }
Andrew Trick8586e622011-09-20 03:17:40 +00007476 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00007477 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007478 return;
7479 }
7480 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00007481 if (deadCPSR) {
7482 assert(!MI->getOperand(ccOutIdx).getReg() &&
7483 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00007484 return;
Andrew Trick924123a2011-09-21 02:20:46 +00007485 }
Andrew Trick8586e622011-09-20 03:17:40 +00007486
Andrew Trick924123a2011-09-21 02:20:46 +00007487 // If this instruction was defined with an optional CPSR def and its dag node
7488 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007489 MachineOperand &MO = MI->getOperand(ccOutIdx);
7490 MO.setReg(ARM::CPSR);
7491 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00007492}
7493
Evan Cheng10043e22007-01-19 07:51:42 +00007494//===----------------------------------------------------------------------===//
7495// ARM Optimization Hooks
7496//===----------------------------------------------------------------------===//
7497
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007498// Helper function that checks if N is a null or all ones constant.
7499static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7500 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7501 if (!C)
7502 return false;
7503 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7504}
7505
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007506// Return true if N is conditionally 0 or all ones.
7507// Detects these expressions where cc is an i1 value:
7508//
7509// (select cc 0, y) [AllOnes=0]
7510// (select cc y, 0) [AllOnes=0]
7511// (zext cc) [AllOnes=0]
7512// (sext cc) [AllOnes=0/1]
7513// (select cc -1, y) [AllOnes=1]
7514// (select cc y, -1) [AllOnes=1]
7515//
7516// Invert is set when N is the null/all ones constant when CC is false.
7517// OtherOp is set to the alternative value of N.
7518static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7519 SDValue &CC, bool &Invert,
7520 SDValue &OtherOp,
7521 SelectionDAG &DAG) {
7522 switch (N->getOpcode()) {
7523 default: return false;
7524 case ISD::SELECT: {
7525 CC = N->getOperand(0);
7526 SDValue N1 = N->getOperand(1);
7527 SDValue N2 = N->getOperand(2);
7528 if (isZeroOrAllOnes(N1, AllOnes)) {
7529 Invert = false;
7530 OtherOp = N2;
7531 return true;
7532 }
7533 if (isZeroOrAllOnes(N2, AllOnes)) {
7534 Invert = true;
7535 OtherOp = N1;
7536 return true;
7537 }
7538 return false;
7539 }
7540 case ISD::ZERO_EXTEND:
7541 // (zext cc) can never be the all ones value.
7542 if (AllOnes)
7543 return false;
7544 // Fall through.
7545 case ISD::SIGN_EXTEND: {
7546 EVT VT = N->getValueType(0);
7547 CC = N->getOperand(0);
7548 if (CC.getValueType() != MVT::i1)
7549 return false;
7550 Invert = !AllOnes;
7551 if (AllOnes)
7552 // When looking for an AllOnes constant, N is an sext, and the 'other'
7553 // value is 0.
7554 OtherOp = DAG.getConstant(0, VT);
7555 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7556 // When looking for a 0 constant, N can be zext or sext.
7557 OtherOp = DAG.getConstant(1, VT);
7558 else
7559 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7560 return true;
7561 }
7562 }
7563}
7564
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007565// Combine a constant select operand into its use:
7566//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007567// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7568// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7569// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7570// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7571// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007572//
7573// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007574// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007575//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007576// Also recognize sext/zext from i1:
7577//
7578// (add (zext cc), x) -> (select cc (add x, 1), x)
7579// (add (sext cc), x) -> (select cc (add x, -1), x)
7580//
7581// These transformations eventually create predicated instructions.
7582//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007583// @param N The node to transform.
7584// @param Slct The N operand that is a select.
7585// @param OtherOp The other N operand (x above).
7586// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007587// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007588// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00007589static
7590SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007591 TargetLowering::DAGCombinerInfo &DCI,
7592 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00007593 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007594 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007595 SDValue NonConstantVal;
7596 SDValue CCOp;
7597 bool SwapSelectOps;
7598 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7599 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007600 return SDValue();
7601
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007602 // Slct is now know to be the desired identity constant when CC is true.
7603 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007604 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007605 OtherOp, NonConstantVal);
7606 // Unless SwapSelectOps says CC should be false.
7607 if (SwapSelectOps)
7608 std::swap(TrueVal, FalseVal);
7609
Andrew Trickef9de2a2013-05-25 02:42:55 +00007610 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007611 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00007612}
7613
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007614// Attempt combineSelectAndUse on each operand of a commutative operator N.
7615static
7616SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7617 TargetLowering::DAGCombinerInfo &DCI) {
7618 SDValue N0 = N->getOperand(0);
7619 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007620 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007621 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7622 if (Result.getNode())
7623 return Result;
7624 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007625 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007626 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7627 if (Result.getNode())
7628 return Result;
7629 }
7630 return SDValue();
7631}
7632
Eric Christopher1b8b94192011-06-29 21:10:36 +00007633// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00007634// (only after legalization).
7635static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7636 TargetLowering::DAGCombinerInfo &DCI,
7637 const ARMSubtarget *Subtarget) {
7638
7639 // Only perform optimization if after legalize, and if NEON is available. We
7640 // also expected both operands to be BUILD_VECTORs.
7641 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7642 || N0.getOpcode() != ISD::BUILD_VECTOR
7643 || N1.getOpcode() != ISD::BUILD_VECTOR)
7644 return SDValue();
7645
7646 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7647 EVT VT = N->getValueType(0);
7648 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7649 return SDValue();
7650
7651 // Check that the vector operands are of the right form.
7652 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7653 // operands, where N is the size of the formed vector.
7654 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7655 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007656
7657 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00007658 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00007659 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00007660 SDValue Vec = N0->getOperand(0)->getOperand(0);
7661 SDNode *V = Vec.getNode();
7662 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00007663
Eric Christopher1b8b94192011-06-29 21:10:36 +00007664 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007665 // check to see if each of their operands are an EXTRACT_VECTOR with
7666 // the same vector and appropriate index.
7667 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7668 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7669 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00007670
Tanya Lattnere9e67052011-06-14 23:48:48 +00007671 SDValue ExtVec0 = N0->getOperand(i);
7672 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007673
Tanya Lattnere9e67052011-06-14 23:48:48 +00007674 // First operand is the vector, verify its the same.
7675 if (V != ExtVec0->getOperand(0).getNode() ||
7676 V != ExtVec1->getOperand(0).getNode())
7677 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00007678
Tanya Lattnere9e67052011-06-14 23:48:48 +00007679 // Second is the constant, verify its correct.
7680 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7681 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00007682
Tanya Lattnere9e67052011-06-14 23:48:48 +00007683 // For the constant, we want to see all the even or all the odd.
7684 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7685 || C1->getZExtValue() != nextIndex+1)
7686 return SDValue();
7687
7688 // Increment index.
7689 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007690 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00007691 return SDValue();
7692 }
7693
7694 // Create VPADDL node.
7695 SelectionDAG &DAG = DCI.DAG;
7696 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00007697
7698 // Build operand list.
7699 SmallVector<SDValue, 8> Ops;
7700 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7701 TLI.getPointerTy()));
7702
7703 // Input is the vector.
7704 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007705
Tanya Lattnere9e67052011-06-14 23:48:48 +00007706 // Get widened type and narrowed type.
7707 MVT widenType;
7708 unsigned numElem = VT.getVectorNumElements();
Silviu Barangaa3106e62014-04-03 10:44:27 +00007709
7710 EVT inputLaneType = Vec.getValueType().getVectorElementType();
7711 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00007712 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7713 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7714 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7715 default:
Craig Toppere55c5562012-02-07 02:50:20 +00007716 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00007717 }
7718
Craig Topper48d114b2014-04-26 18:35:24 +00007719 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00007720 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
7721 return DAG.getNode(ExtOp, SDLoc(N), VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00007722}
7723
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007724static SDValue findMUL_LOHI(SDValue V) {
7725 if (V->getOpcode() == ISD::UMUL_LOHI ||
7726 V->getOpcode() == ISD::SMUL_LOHI)
7727 return V;
7728 return SDValue();
7729}
7730
7731static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7732 TargetLowering::DAGCombinerInfo &DCI,
7733 const ARMSubtarget *Subtarget) {
7734
7735 if (Subtarget->isThumb1Only()) return SDValue();
7736
7737 // Only perform the checks after legalize when the pattern is available.
7738 if (DCI.isBeforeLegalize()) return SDValue();
7739
7740 // Look for multiply add opportunities.
7741 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7742 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7743 // a glue link from the first add to the second add.
7744 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7745 // a S/UMLAL instruction.
7746 // loAdd UMUL_LOHI
7747 // \ / :lo \ :hi
7748 // \ / \ [no multiline comment]
7749 // ADDC | hiAdd
7750 // \ :glue / /
7751 // \ / /
7752 // ADDE
7753 //
7754 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7755 SDValue AddcOp0 = AddcNode->getOperand(0);
7756 SDValue AddcOp1 = AddcNode->getOperand(1);
7757
7758 // Check if the two operands are from the same mul_lohi node.
7759 if (AddcOp0.getNode() == AddcOp1.getNode())
7760 return SDValue();
7761
7762 assert(AddcNode->getNumValues() == 2 &&
7763 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00007764 "Expect ADDC with two result values. First: i32");
7765
7766 // Check that we have a glued ADDC node.
7767 if (AddcNode->getValueType(1) != MVT::Glue)
7768 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007769
7770 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7771 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7772 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7773 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7774 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7775 return SDValue();
7776
7777 // Look for the glued ADDE.
7778 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00007779 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007780 return SDValue();
7781
7782 // Make sure it is really an ADDE.
7783 if (AddeNode->getOpcode() != ISD::ADDE)
7784 return SDValue();
7785
7786 assert(AddeNode->getNumOperands() == 3 &&
7787 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7788 "ADDE node has the wrong inputs");
7789
7790 // Check for the triangle shape.
7791 SDValue AddeOp0 = AddeNode->getOperand(0);
7792 SDValue AddeOp1 = AddeNode->getOperand(1);
7793
7794 // Make sure that the ADDE operands are not coming from the same node.
7795 if (AddeOp0.getNode() == AddeOp1.getNode())
7796 return SDValue();
7797
7798 // Find the MUL_LOHI node walking up ADDE's operands.
7799 bool IsLeftOperandMUL = false;
7800 SDValue MULOp = findMUL_LOHI(AddeOp0);
7801 if (MULOp == SDValue())
7802 MULOp = findMUL_LOHI(AddeOp1);
7803 else
7804 IsLeftOperandMUL = true;
7805 if (MULOp == SDValue())
7806 return SDValue();
7807
7808 // Figure out the right opcode.
7809 unsigned Opc = MULOp->getOpcode();
7810 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7811
7812 // Figure out the high and low input values to the MLAL node.
7813 SDValue* HiMul = &MULOp;
Craig Topper062a2ba2014-04-25 05:30:21 +00007814 SDValue* HiAdd = nullptr;
7815 SDValue* LoMul = nullptr;
7816 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007817
7818 if (IsLeftOperandMUL)
7819 HiAdd = &AddeOp1;
7820 else
7821 HiAdd = &AddeOp0;
7822
7823
7824 if (AddcOp0->getOpcode() == Opc) {
7825 LoMul = &AddcOp0;
7826 LowAdd = &AddcOp1;
7827 }
7828 if (AddcOp1->getOpcode() == Opc) {
7829 LoMul = &AddcOp1;
7830 LowAdd = &AddcOp0;
7831 }
7832
Craig Topper062a2ba2014-04-25 05:30:21 +00007833 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007834 return SDValue();
7835
7836 if (LoMul->getNode() != HiMul->getNode())
7837 return SDValue();
7838
7839 // Create the merged node.
7840 SelectionDAG &DAG = DCI.DAG;
7841
7842 // Build operand list.
7843 SmallVector<SDValue, 8> Ops;
7844 Ops.push_back(LoMul->getOperand(0));
7845 Ops.push_back(LoMul->getOperand(1));
7846 Ops.push_back(*LowAdd);
7847 Ops.push_back(*HiAdd);
7848
Andrew Trickef9de2a2013-05-25 02:42:55 +00007849 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00007850 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007851
7852 // Replace the ADDs' nodes uses by the MLA node's values.
7853 SDValue HiMLALResult(MLALNode.getNode(), 1);
7854 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7855
7856 SDValue LoMLALResult(MLALNode.getNode(), 0);
7857 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7858
7859 // Return original node to notify the driver to stop replacing.
7860 SDValue resNode(AddcNode, 0);
7861 return resNode;
7862}
7863
7864/// PerformADDCCombine - Target-specific dag combine transform from
7865/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7866static SDValue PerformADDCCombine(SDNode *N,
7867 TargetLowering::DAGCombinerInfo &DCI,
7868 const ARMSubtarget *Subtarget) {
7869
7870 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7871
7872}
7873
Bob Wilson728eb292010-07-29 20:34:14 +00007874/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7875/// operands N0 and N1. This is a helper for PerformADDCombine that is
7876/// called with the default operands, and if that fails, with commuted
7877/// operands.
7878static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007879 TargetLowering::DAGCombinerInfo &DCI,
7880 const ARMSubtarget *Subtarget){
7881
7882 // Attempt to create vpaddl for this add.
7883 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7884 if (Result.getNode())
7885 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007886
Chris Lattner4147f082009-03-12 06:52:53 +00007887 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007888 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00007889 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7890 if (Result.getNode()) return Result;
7891 }
Chris Lattner4147f082009-03-12 06:52:53 +00007892 return SDValue();
7893}
7894
Bob Wilson728eb292010-07-29 20:34:14 +00007895/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7896///
7897static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007898 TargetLowering::DAGCombinerInfo &DCI,
7899 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00007900 SDValue N0 = N->getOperand(0);
7901 SDValue N1 = N->getOperand(1);
7902
7903 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007904 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00007905 if (Result.getNode())
7906 return Result;
7907
7908 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007909 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00007910}
7911
Chris Lattner4147f082009-03-12 06:52:53 +00007912/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00007913///
Chris Lattner4147f082009-03-12 06:52:53 +00007914static SDValue PerformSUBCombine(SDNode *N,
7915 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00007916 SDValue N0 = N->getOperand(0);
7917 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00007918
Chris Lattner4147f082009-03-12 06:52:53 +00007919 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007920 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00007921 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7922 if (Result.getNode()) return Result;
7923 }
Bob Wilson7117a912009-03-20 22:42:55 +00007924
Chris Lattner4147f082009-03-12 06:52:53 +00007925 return SDValue();
7926}
7927
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007928/// PerformVMULCombine
7929/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7930/// special multiplier accumulator forwarding.
7931/// vmul d3, d0, d2
7932/// vmla d3, d1, d2
7933/// is faster than
7934/// vadd d3, d0, d1
7935/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00007936// However, for (A + B) * (A + B),
7937// vadd d2, d0, d1
7938// vmul d3, d0, d2
7939// vmla d3, d1, d2
7940// is slower than
7941// vadd d2, d0, d1
7942// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007943static SDValue PerformVMULCombine(SDNode *N,
7944 TargetLowering::DAGCombinerInfo &DCI,
7945 const ARMSubtarget *Subtarget) {
7946 if (!Subtarget->hasVMLxForwarding())
7947 return SDValue();
7948
7949 SelectionDAG &DAG = DCI.DAG;
7950 SDValue N0 = N->getOperand(0);
7951 SDValue N1 = N->getOperand(1);
7952 unsigned Opcode = N0.getOpcode();
7953 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7954 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00007955 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007956 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7957 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7958 return SDValue();
7959 std::swap(N0, N1);
7960 }
7961
Weiming Zhao2052f482013-09-25 23:12:06 +00007962 if (N0 == N1)
7963 return SDValue();
7964
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007965 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007966 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007967 SDValue N00 = N0->getOperand(0);
7968 SDValue N01 = N0->getOperand(1);
7969 return DAG.getNode(Opcode, DL, VT,
7970 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7971 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7972}
7973
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007974static SDValue PerformMULCombine(SDNode *N,
7975 TargetLowering::DAGCombinerInfo &DCI,
7976 const ARMSubtarget *Subtarget) {
7977 SelectionDAG &DAG = DCI.DAG;
7978
7979 if (Subtarget->isThumb1Only())
7980 return SDValue();
7981
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007982 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7983 return SDValue();
7984
7985 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007986 if (VT.is64BitVector() || VT.is128BitVector())
7987 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007988 if (VT != MVT::i32)
7989 return SDValue();
7990
7991 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7992 if (!C)
7993 return SDValue();
7994
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007995 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00007996 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007997
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007998 ShiftAmt = ShiftAmt & (32 - 1);
7999 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008000 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008001
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008002 SDValue Res;
8003 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008004
8005 if (MulAmt >= 0) {
8006 if (isPowerOf2_32(MulAmt - 1)) {
8007 // (mul x, 2^N + 1) => (add (shl x, N), x)
8008 Res = DAG.getNode(ISD::ADD, DL, VT,
8009 V,
8010 DAG.getNode(ISD::SHL, DL, VT,
8011 V,
8012 DAG.getConstant(Log2_32(MulAmt - 1),
8013 MVT::i32)));
8014 } else if (isPowerOf2_32(MulAmt + 1)) {
8015 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8016 Res = DAG.getNode(ISD::SUB, DL, VT,
8017 DAG.getNode(ISD::SHL, DL, VT,
8018 V,
8019 DAG.getConstant(Log2_32(MulAmt + 1),
8020 MVT::i32)),
8021 V);
8022 } else
8023 return SDValue();
8024 } else {
8025 uint64_t MulAmtAbs = -MulAmt;
8026 if (isPowerOf2_32(MulAmtAbs + 1)) {
8027 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8028 Res = DAG.getNode(ISD::SUB, DL, VT,
8029 V,
8030 DAG.getNode(ISD::SHL, DL, VT,
8031 V,
8032 DAG.getConstant(Log2_32(MulAmtAbs + 1),
8033 MVT::i32)));
8034 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8035 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8036 Res = DAG.getNode(ISD::ADD, DL, VT,
8037 V,
8038 DAG.getNode(ISD::SHL, DL, VT,
8039 V,
8040 DAG.getConstant(Log2_32(MulAmtAbs-1),
8041 MVT::i32)));
8042 Res = DAG.getNode(ISD::SUB, DL, VT,
8043 DAG.getConstant(0, MVT::i32),Res);
8044
8045 } else
8046 return SDValue();
8047 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008048
8049 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008050 Res = DAG.getNode(ISD::SHL, DL, VT,
8051 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008052
8053 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008054 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008055 return SDValue();
8056}
8057
Owen Anderson30c48922010-11-05 19:27:46 +00008058static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008059 TargetLowering::DAGCombinerInfo &DCI,
8060 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008061
Owen Anderson30c48922010-11-05 19:27:46 +00008062 // Attempt to use immediate-form VBIC
8063 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008064 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008065 EVT VT = N->getValueType(0);
8066 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008067
Tanya Lattner266792a2011-04-07 15:24:20 +00008068 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8069 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008070
Owen Anderson30c48922010-11-05 19:27:46 +00008071 APInt SplatBits, SplatUndef;
8072 unsigned SplatBitSize;
8073 bool HasAnyUndefs;
8074 if (BVN &&
8075 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8076 if (SplatBitSize <= 64) {
8077 EVT VbicVT;
8078 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8079 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00008080 DAG, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008081 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008082 if (Val.getNode()) {
8083 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008084 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008085 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008086 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008087 }
8088 }
8089 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008090
Evan Chenge87681c2012-02-23 01:19:06 +00008091 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008092 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8093 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8094 if (Result.getNode())
8095 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008096 }
8097
Owen Anderson30c48922010-11-05 19:27:46 +00008098 return SDValue();
8099}
8100
Jim Grosbach11013ed2010-07-16 23:05:05 +00008101/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8102static SDValue PerformORCombine(SDNode *N,
8103 TargetLowering::DAGCombinerInfo &DCI,
8104 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008105 // Attempt to use immediate-form VORR
8106 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008107 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008108 EVT VT = N->getValueType(0);
8109 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008110
Tanya Lattner266792a2011-04-07 15:24:20 +00008111 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8112 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008113
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008114 APInt SplatBits, SplatUndef;
8115 unsigned SplatBitSize;
8116 bool HasAnyUndefs;
8117 if (BVN && Subtarget->hasNEON() &&
8118 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8119 if (SplatBitSize <= 64) {
8120 EVT VorrVT;
8121 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8122 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00008123 DAG, VorrVT, VT.is128BitVector(),
8124 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008125 if (Val.getNode()) {
8126 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008127 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008128 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008129 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008130 }
8131 }
8132 }
8133
Evan Chenge87681c2012-02-23 01:19:06 +00008134 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008135 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8136 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8137 if (Result.getNode())
8138 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008139 }
8140
Nadav Rotem3a94c542012-08-13 18:52:44 +00008141 // The code below optimizes (or (and X, Y), Z).
8142 // The AND operand needs to have a single user to make these optimizations
8143 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008144 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008145 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008146 return SDValue();
8147 SDValue N1 = N->getOperand(1);
8148
8149 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8150 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8151 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8152 APInt SplatUndef;
8153 unsigned SplatBitSize;
8154 bool HasAnyUndefs;
8155
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008156 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008157 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008158 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8159 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008160 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008161 HasAnyUndefs) && !HasAnyUndefs) {
8162 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8163 HasAnyUndefs) && !HasAnyUndefs) {
8164 // Ensure that the bit width of the constants are the same and that
8165 // the splat arguments are logical inverses as per the pattern we
8166 // are trying to simplify.
8167 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8168 SplatBits0 == ~SplatBits1) {
8169 // Canonicalize the vector type to make instruction selection
8170 // simpler.
8171 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8172 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8173 N0->getOperand(1),
8174 N0->getOperand(0),
8175 N1->getOperand(0));
8176 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8177 }
8178 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008179 }
8180 }
8181
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008182 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8183 // reasonable.
8184
Jim Grosbach11013ed2010-07-16 23:05:05 +00008185 // BFI is only available on V6T2+
8186 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8187 return SDValue();
8188
Andrew Trickef9de2a2013-05-25 02:42:55 +00008189 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008190 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008191 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008192 //
8193 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008194 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008195 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008196 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008197 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008198 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008199
Jim Grosbach11013ed2010-07-16 23:05:05 +00008200 if (VT != MVT::i32)
8201 return SDValue();
8202
Evan Cheng2e51bb42010-12-13 20:32:54 +00008203 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008204
Jim Grosbach11013ed2010-07-16 23:05:05 +00008205 // The value and the mask need to be constants so we can verify this is
8206 // actually a bitfield set. If the mask is 0xffff, we can do better
8207 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008208 SDValue MaskOp = N0.getOperand(1);
8209 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8210 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008211 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008212 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008213 if (Mask == 0xffff)
8214 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008215 SDValue Res;
8216 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008217 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8218 if (N1C) {
8219 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008220 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008221 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008222
Evan Cheng34345752010-12-11 04:11:38 +00008223 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008224 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008225
Evan Cheng2e51bb42010-12-13 20:32:54 +00008226 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Cheng34345752010-12-11 04:11:38 +00008227 DAG.getConstant(Val, MVT::i32),
8228 DAG.getConstant(Mask, MVT::i32));
8229
8230 // Do not add new nodes to DAG combiner worklist.
8231 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008232 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008233 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008234 } else if (N1.getOpcode() == ISD::AND) {
8235 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008236 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8237 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008238 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008239 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008240
Eric Christopherd5530962011-03-26 01:21:03 +00008241 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8242 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008243 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008244 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008245 // The pack halfword instruction works better for masks that fit it,
8246 // so use that when it's available.
8247 if (Subtarget->hasT2ExtractPack() &&
8248 (Mask == 0xffff || Mask == 0xffff0000))
8249 return SDValue();
8250 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008251 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008252 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopherd5530962011-03-26 01:21:03 +00008253 DAG.getConstant(amt, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008254 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008255 DAG.getConstant(Mask, MVT::i32));
8256 // Do not add new nodes to DAG combiner worklist.
8257 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008258 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008259 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008260 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008261 // The pack halfword instruction works better for masks that fit it,
8262 // so use that when it's available.
8263 if (Subtarget->hasT2ExtractPack() &&
8264 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8265 return SDValue();
8266 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008267 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008268 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008269 DAG.getConstant(lsb, MVT::i32));
8270 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopherd5530962011-03-26 01:21:03 +00008271 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008272 // Do not add new nodes to DAG combiner worklist.
8273 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008274 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008275 }
8276 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008277
Evan Cheng2e51bb42010-12-13 20:32:54 +00008278 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8279 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8280 ARM::isBitFieldInvertedMask(~Mask)) {
8281 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8282 // where lsb(mask) == #shamt and masked bits of B are known zero.
8283 SDValue ShAmt = N00.getOperand(1);
8284 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008285 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008286 if (ShAmtC != LSB)
8287 return SDValue();
8288
8289 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8290 DAG.getConstant(~Mask, MVT::i32));
8291
8292 // Do not add new nodes to DAG combiner worklist.
8293 DCI.CombineTo(N, Res, false);
8294 }
8295
Jim Grosbach11013ed2010-07-16 23:05:05 +00008296 return SDValue();
8297}
8298
Evan Chenge87681c2012-02-23 01:19:06 +00008299static SDValue PerformXORCombine(SDNode *N,
8300 TargetLowering::DAGCombinerInfo &DCI,
8301 const ARMSubtarget *Subtarget) {
8302 EVT VT = N->getValueType(0);
8303 SelectionDAG &DAG = DCI.DAG;
8304
8305 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8306 return SDValue();
8307
8308 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008309 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8310 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8311 if (Result.getNode())
8312 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008313 }
8314
8315 return SDValue();
8316}
8317
Evan Cheng6d02d902011-06-15 01:12:31 +00008318/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8319/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00008320static SDValue PerformBFICombine(SDNode *N,
8321 TargetLowering::DAGCombinerInfo &DCI) {
8322 SDValue N1 = N->getOperand(1);
8323 if (N1.getOpcode() == ISD::AND) {
8324 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8325 if (!N11C)
8326 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008327 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008328 unsigned LSB = countTrailingZeros(~InvMask);
8329 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Evan Cheng6d02d902011-06-15 01:12:31 +00008330 unsigned Mask = (1 << Width)-1;
Evan Chengc1778132010-12-14 03:22:07 +00008331 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008332 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008333 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00008334 N->getOperand(0), N1.getOperand(0),
8335 N->getOperand(2));
8336 }
8337 return SDValue();
8338}
8339
Bob Wilson22806742010-09-22 22:09:21 +00008340/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8341/// ARMISD::VMOVRRD.
8342static SDValue PerformVMOVRRDCombine(SDNode *N,
8343 TargetLowering::DAGCombinerInfo &DCI) {
8344 // vmovrrd(vmovdrr x, y) -> x,y
8345 SDValue InDouble = N->getOperand(0);
8346 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
8347 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008348
8349 // vmovrrd(load f64) -> (load i32), (load i32)
8350 SDNode *InNode = InDouble.getNode();
8351 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8352 InNode->getValueType(0) == MVT::f64 &&
8353 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8354 !cast<LoadSDNode>(InNode)->isVolatile()) {
8355 // TODO: Should this be done for non-FrameIndex operands?
8356 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8357
8358 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008359 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008360 SDValue BasePtr = LD->getBasePtr();
8361 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8362 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008363 LD->isNonTemporal(), LD->isInvariant(),
8364 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008365
8366 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8367 DAG.getConstant(4, MVT::i32));
8368 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8369 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008370 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008371 std::min(4U, LD->getAlignment() / 2));
8372
8373 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
8374 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
8375 DCI.RemoveFromWorklist(LD);
8376 DAG.DeleteNode(LD);
8377 return Result;
8378 }
8379
Bob Wilson22806742010-09-22 22:09:21 +00008380 return SDValue();
8381}
8382
8383/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8384/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8385static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8386 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8387 SDValue Op0 = N->getOperand(0);
8388 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00008389 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008390 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008391 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008392 Op1 = Op1.getOperand(0);
8393 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8394 Op0.getNode() == Op1.getNode() &&
8395 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008396 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00008397 N->getValueType(0), Op0.getOperand(0));
8398 return SDValue();
8399}
8400
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008401/// PerformSTORECombine - Target-specific dag combine xforms for
8402/// ISD::STORE.
8403static SDValue PerformSTORECombine(SDNode *N,
8404 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008405 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008406 if (St->isVolatile())
8407 return SDValue();
8408
Andrew Trickbc325162012-07-18 18:34:24 +00008409 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosiere0e38f62012-04-09 20:32:02 +00008410 // pack all of the elements in one place. Next, store to memory in fewer
8411 // chunks.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008412 SDValue StVal = St->getValue();
Chad Rosiere0e38f62012-04-09 20:32:02 +00008413 EVT VT = StVal.getValueType();
8414 if (St->isTruncatingStore() && VT.isVector()) {
8415 SelectionDAG &DAG = DCI.DAG;
8416 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8417 EVT StVT = St->getMemoryVT();
8418 unsigned NumElems = VT.getVectorNumElements();
8419 assert(StVT != VT && "Cannot truncate to the same type");
8420 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
8421 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
8422
8423 // From, To sizes and ElemCount must be pow of two
8424 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
8425
8426 // We are going to use the original vector elt for storing.
8427 // Accumulated smaller vector elements must be a multiple of the store size.
8428 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
8429
8430 unsigned SizeRatio = FromEltSz / ToEltSz;
8431 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
8432
8433 // Create a type on which we perform the shuffle.
8434 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
8435 NumElems*SizeRatio);
8436 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
8437
Andrew Trickef9de2a2013-05-25 02:42:55 +00008438 SDLoc DL(St);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008439 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
8440 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
8441 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
8442
8443 // Can't shuffle using an illegal type.
8444 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
8445
8446 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
8447 DAG.getUNDEF(WideVec.getValueType()),
8448 ShuffleVec.data());
8449 // At this point all of the data is stored at the bottom of the
8450 // register. We now need to save it to mem.
8451
8452 // Find the largest store unit
8453 MVT StoreType = MVT::i8;
8454 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
8455 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
8456 MVT Tp = (MVT::SimpleValueType)tp;
8457 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
8458 StoreType = Tp;
8459 }
8460 // Didn't find a legal store type.
8461 if (!TLI.isTypeLegal(StoreType))
8462 return SDValue();
8463
8464 // Bitcast the original vector into a vector of store-size units
8465 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
8466 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
8467 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
8468 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
8469 SmallVector<SDValue, 8> Chains;
8470 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
8471 TLI.getPointerTy());
8472 SDValue BasePtr = St->getBasePtr();
8473
8474 // Perform one or more big stores into memory.
8475 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
8476 for (unsigned I = 0; I < E; I++) {
8477 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
8478 StoreType, ShuffWide,
8479 DAG.getIntPtrConstant(I));
8480 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
8481 St->getPointerInfo(), St->isVolatile(),
8482 St->isNonTemporal(), St->getAlignment());
8483 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
8484 Increment);
8485 Chains.push_back(Ch);
8486 }
Craig Topper48d114b2014-04-26 18:35:24 +00008487 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008488 }
8489
8490 if (!ISD::isNormalStore(St))
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008491 return SDValue();
8492
Chad Rosier99cbde92012-04-09 19:38:15 +00008493 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8494 // ARM stores of arguments in the same cache line.
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008495 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier99cbde92012-04-09 19:38:15 +00008496 StVal.getNode()->hasOneUse()) {
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008497 SelectionDAG &DAG = DCI.DAG;
Christian Pirkerb5728192014-05-08 14:06:24 +00008498 bool isBigEndian = DAG.getTargetLoweringInfo().isBigEndian();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008499 SDLoc DL(St);
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008500 SDValue BasePtr = St->getBasePtr();
8501 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
Christian Pirkerb5728192014-05-08 14:06:24 +00008502 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
8503 BasePtr, St->getPointerInfo(), St->isVolatile(),
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008504 St->isNonTemporal(), St->getAlignment());
8505
8506 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8507 DAG.getConstant(4, MVT::i32));
Christian Pirkerb5728192014-05-08 14:06:24 +00008508 return DAG.getStore(NewST1.getValue(0), DL,
8509 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008510 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8511 St->isNonTemporal(),
8512 std::min(4U, St->getAlignment() / 2));
8513 }
8514
8515 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008516 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8517 return SDValue();
8518
Chad Rosier99cbde92012-04-09 19:38:15 +00008519 // Bitcast an i64 store extracted from a vector to f64.
8520 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008521 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008522 SDLoc dl(StVal);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008523 SDValue IntVec = StVal.getOperand(0);
8524 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8525 IntVec.getValueType().getVectorNumElements());
8526 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8527 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8528 Vec, StVal.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008529 dl = SDLoc(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008530 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8531 // Make the DAGCombiner fold the bitcasts.
8532 DCI.AddToWorklist(Vec.getNode());
8533 DCI.AddToWorklist(ExtElt.getNode());
8534 DCI.AddToWorklist(V.getNode());
8535 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8536 St->getPointerInfo(), St->isVolatile(),
8537 St->isNonTemporal(), St->getAlignment(),
8538 St->getTBAAInfo());
8539}
8540
8541/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8542/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8543/// i64 vector to have f64 elements, since the value can then be loaded
8544/// directly into a VFP register.
8545static bool hasNormalLoadOperand(SDNode *N) {
8546 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8547 for (unsigned i = 0; i < NumElts; ++i) {
8548 SDNode *Elt = N->getOperand(i).getNode();
8549 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8550 return true;
8551 }
8552 return false;
8553}
8554
Bob Wilsoncb6db982010-09-17 22:59:05 +00008555/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8556/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008557static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8558 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilsoncb6db982010-09-17 22:59:05 +00008559 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8560 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8561 // into a pair of GPRs, which is fine when the value is used as a scalar,
8562 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008563 SelectionDAG &DAG = DCI.DAG;
8564 if (N->getNumOperands() == 2) {
8565 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8566 if (RV.getNode())
8567 return RV;
8568 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00008569
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008570 // Load i64 elements as f64 values so that type legalization does not split
8571 // them up into i32 values.
8572 EVT VT = N->getValueType(0);
8573 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8574 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008575 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008576 SmallVector<SDValue, 8> Ops;
8577 unsigned NumElts = VT.getVectorNumElements();
8578 for (unsigned i = 0; i < NumElts; ++i) {
8579 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8580 Ops.push_back(V);
8581 // Make the DAGCombiner fold the bitcast.
8582 DCI.AddToWorklist(V.getNode());
8583 }
8584 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00008585 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008586 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8587}
8588
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008589/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
8590static SDValue
8591PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8592 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
8593 // At that time, we may have inserted bitcasts from integer to float.
8594 // If these bitcasts have survived DAGCombine, change the lowering of this
8595 // BUILD_VECTOR in something more vector friendly, i.e., that does not
8596 // force to use floating point types.
8597
8598 // Make sure we can change the type of the vector.
8599 // This is possible iff:
8600 // 1. The vector is only used in a bitcast to a integer type. I.e.,
8601 // 1.1. Vector is used only once.
8602 // 1.2. Use is a bit convert to an integer type.
8603 // 2. The size of its operands are 32-bits (64-bits are not legal).
8604 EVT VT = N->getValueType(0);
8605 EVT EltVT = VT.getVectorElementType();
8606
8607 // Check 1.1. and 2.
8608 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
8609 return SDValue();
8610
8611 // By construction, the input type must be float.
8612 assert(EltVT == MVT::f32 && "Unexpected type!");
8613
8614 // Check 1.2.
8615 SDNode *Use = *N->use_begin();
8616 if (Use->getOpcode() != ISD::BITCAST ||
8617 Use->getValueType(0).isFloatingPoint())
8618 return SDValue();
8619
8620 // Check profitability.
8621 // Model is, if more than half of the relevant operands are bitcast from
8622 // i32, turn the build_vector into a sequence of insert_vector_elt.
8623 // Relevant operands are everything that is not statically
8624 // (i.e., at compile time) bitcasted.
8625 unsigned NumOfBitCastedElts = 0;
8626 unsigned NumElts = VT.getVectorNumElements();
8627 unsigned NumOfRelevantElts = NumElts;
8628 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
8629 SDValue Elt = N->getOperand(Idx);
8630 if (Elt->getOpcode() == ISD::BITCAST) {
8631 // Assume only bit cast to i32 will go away.
8632 if (Elt->getOperand(0).getValueType() == MVT::i32)
8633 ++NumOfBitCastedElts;
8634 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
8635 // Constants are statically casted, thus do not count them as
8636 // relevant operands.
8637 --NumOfRelevantElts;
8638 }
8639
8640 // Check if more than half of the elements require a non-free bitcast.
8641 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
8642 return SDValue();
8643
8644 SelectionDAG &DAG = DCI.DAG;
8645 // Create the new vector type.
8646 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
8647 // Check if the type is legal.
8648 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8649 if (!TLI.isTypeLegal(VecVT))
8650 return SDValue();
8651
8652 // Combine:
8653 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
8654 // => BITCAST INSERT_VECTOR_ELT
8655 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
8656 // (BITCAST EN), N.
8657 SDValue Vec = DAG.getUNDEF(VecVT);
8658 SDLoc dl(N);
8659 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
8660 SDValue V = N->getOperand(Idx);
8661 if (V.getOpcode() == ISD::UNDEF)
8662 continue;
8663 if (V.getOpcode() == ISD::BITCAST &&
8664 V->getOperand(0).getValueType() == MVT::i32)
8665 // Fold obvious case.
8666 V = V.getOperand(0);
8667 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00008668 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008669 // Make the DAGCombiner fold the bitcasts.
8670 DCI.AddToWorklist(V.getNode());
8671 }
8672 SDValue LaneIdx = DAG.getConstant(Idx, MVT::i32);
8673 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
8674 }
8675 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
8676 // Make the DAGCombiner fold the bitcasts.
8677 DCI.AddToWorklist(Vec.getNode());
8678 return Vec;
8679}
8680
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008681/// PerformInsertEltCombine - Target-specific dag combine xforms for
8682/// ISD::INSERT_VECTOR_ELT.
8683static SDValue PerformInsertEltCombine(SDNode *N,
8684 TargetLowering::DAGCombinerInfo &DCI) {
8685 // Bitcast an i64 load inserted into a vector to f64.
8686 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8687 EVT VT = N->getValueType(0);
8688 SDNode *Elt = N->getOperand(1).getNode();
8689 if (VT.getVectorElementType() != MVT::i64 ||
8690 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8691 return SDValue();
8692
8693 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008694 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008695 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8696 VT.getVectorNumElements());
8697 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8698 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8699 // Make the DAGCombiner fold the bitcasts.
8700 DCI.AddToWorklist(Vec.getNode());
8701 DCI.AddToWorklist(V.getNode());
8702 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8703 Vec, V, N->getOperand(2));
8704 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00008705}
8706
Bob Wilsonc7334a12010-10-27 20:38:28 +00008707/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8708/// ISD::VECTOR_SHUFFLE.
8709static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8710 // The LLVM shufflevector instruction does not require the shuffle mask
8711 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8712 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8713 // operands do not match the mask length, they are extended by concatenating
8714 // them with undef vectors. That is probably the right thing for other
8715 // targets, but for NEON it is better to concatenate two double-register
8716 // size vector operands into a single quad-register size vector. Do that
8717 // transformation here:
8718 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8719 // shuffle(concat(v1, v2), undef)
8720 SDValue Op0 = N->getOperand(0);
8721 SDValue Op1 = N->getOperand(1);
8722 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8723 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8724 Op0.getNumOperands() != 2 ||
8725 Op1.getNumOperands() != 2)
8726 return SDValue();
8727 SDValue Concat0Op1 = Op0.getOperand(1);
8728 SDValue Concat1Op1 = Op1.getOperand(1);
8729 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8730 Concat1Op1.getOpcode() != ISD::UNDEF)
8731 return SDValue();
8732 // Skip the transformation if any of the types are illegal.
8733 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8734 EVT VT = N->getValueType(0);
8735 if (!TLI.isTypeLegal(VT) ||
8736 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8737 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8738 return SDValue();
8739
Andrew Trickef9de2a2013-05-25 02:42:55 +00008740 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008741 Op0.getOperand(0), Op1.getOperand(0));
8742 // Translate the shuffle mask.
8743 SmallVector<int, 16> NewMask;
8744 unsigned NumElts = VT.getVectorNumElements();
8745 unsigned HalfElts = NumElts/2;
8746 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8747 for (unsigned n = 0; n < NumElts; ++n) {
8748 int MaskElt = SVN->getMaskElt(n);
8749 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00008750 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00008751 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00008752 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00008753 NewElt = HalfElts + MaskElt - NumElts;
8754 NewMask.push_back(NewElt);
8755 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00008756 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008757 DAG.getUNDEF(VT), NewMask.data());
8758}
8759
Bob Wilson06fce872011-02-07 17:43:21 +00008760/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8761/// NEON load/store intrinsics to merge base address updates.
8762static SDValue CombineBaseUpdate(SDNode *N,
8763 TargetLowering::DAGCombinerInfo &DCI) {
8764 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8765 return SDValue();
8766
8767 SelectionDAG &DAG = DCI.DAG;
8768 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8769 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8770 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8771 SDValue Addr = N->getOperand(AddrOpIdx);
8772
8773 // Search for a use of the address operand that is an increment.
8774 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8775 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8776 SDNode *User = *UI;
8777 if (User->getOpcode() != ISD::ADD ||
8778 UI.getUse().getResNo() != Addr.getResNo())
8779 continue;
8780
8781 // Check that the add is independent of the load/store. Otherwise, folding
8782 // it would create a cycle.
8783 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8784 continue;
8785
8786 // Find the new opcode for the updating load/store.
8787 bool isLoad = true;
8788 bool isLaneOp = false;
8789 unsigned NewOpc = 0;
8790 unsigned NumVecs = 0;
8791 if (isIntrinsic) {
8792 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8793 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00008794 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008795 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8796 NumVecs = 1; break;
8797 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8798 NumVecs = 2; break;
8799 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8800 NumVecs = 3; break;
8801 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8802 NumVecs = 4; break;
8803 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8804 NumVecs = 2; isLaneOp = true; break;
8805 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8806 NumVecs = 3; isLaneOp = true; break;
8807 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8808 NumVecs = 4; isLaneOp = true; break;
8809 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8810 NumVecs = 1; isLoad = false; break;
8811 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8812 NumVecs = 2; isLoad = false; break;
8813 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8814 NumVecs = 3; isLoad = false; break;
8815 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8816 NumVecs = 4; isLoad = false; break;
8817 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8818 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8819 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8820 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8821 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8822 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8823 }
8824 } else {
8825 isLaneOp = true;
8826 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00008827 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008828 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8829 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8830 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8831 }
8832 }
8833
8834 // Find the size of memory referenced by the load/store.
8835 EVT VecTy;
8836 if (isLoad)
8837 VecTy = N->getValueType(0);
Owen Anderson77aa2662011-04-05 21:48:57 +00008838 else
Bob Wilson06fce872011-02-07 17:43:21 +00008839 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8840 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8841 if (isLaneOp)
8842 NumBytes /= VecTy.getVectorNumElements();
8843
8844 // If the increment is a constant, it must match the memory ref size.
8845 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8846 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8847 uint64_t IncVal = CInc->getZExtValue();
8848 if (IncVal != NumBytes)
8849 continue;
8850 } else if (NumBytes >= 3 * 16) {
8851 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8852 // separate instructions that make it harder to use a non-constant update.
8853 continue;
8854 }
8855
8856 // Create the new updating load/store node.
8857 EVT Tys[6];
8858 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8859 unsigned n;
8860 for (n = 0; n < NumResultVecs; ++n)
8861 Tys[n] = VecTy;
8862 Tys[n++] = MVT::i32;
8863 Tys[n] = MVT::Other;
Craig Topperabb4ac72014-04-16 06:10:51 +00008864 SDVTList SDTys = DAG.getVTList(ArrayRef<EVT>(Tys, NumResultVecs+2));
Bob Wilson06fce872011-02-07 17:43:21 +00008865 SmallVector<SDValue, 8> Ops;
8866 Ops.push_back(N->getOperand(0)); // incoming chain
8867 Ops.push_back(N->getOperand(AddrOpIdx));
8868 Ops.push_back(Inc);
8869 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8870 Ops.push_back(N->getOperand(i));
8871 }
8872 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008873 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00008874 Ops, MemInt->getMemoryVT(),
Bob Wilson06fce872011-02-07 17:43:21 +00008875 MemInt->getMemOperand());
8876
8877 // Update the uses.
8878 std::vector<SDValue> NewResults;
8879 for (unsigned i = 0; i < NumResultVecs; ++i) {
8880 NewResults.push_back(SDValue(UpdN.getNode(), i));
8881 }
8882 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8883 DCI.CombineTo(N, NewResults);
8884 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8885
8886 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00008887 }
Bob Wilson06fce872011-02-07 17:43:21 +00008888 return SDValue();
8889}
8890
Bob Wilson2d790df2010-11-28 06:51:26 +00008891/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8892/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8893/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8894/// return true.
8895static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8896 SelectionDAG &DAG = DCI.DAG;
8897 EVT VT = N->getValueType(0);
8898 // vldN-dup instructions only support 64-bit vectors for N > 1.
8899 if (!VT.is64BitVector())
8900 return false;
8901
8902 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8903 SDNode *VLD = N->getOperand(0).getNode();
8904 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8905 return false;
8906 unsigned NumVecs = 0;
8907 unsigned NewOpc = 0;
8908 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8909 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8910 NumVecs = 2;
8911 NewOpc = ARMISD::VLD2DUP;
8912 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8913 NumVecs = 3;
8914 NewOpc = ARMISD::VLD3DUP;
8915 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8916 NumVecs = 4;
8917 NewOpc = ARMISD::VLD4DUP;
8918 } else {
8919 return false;
8920 }
8921
8922 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8923 // numbers match the load.
8924 unsigned VLDLaneNo =
8925 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8926 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8927 UI != UE; ++UI) {
8928 // Ignore uses of the chain result.
8929 if (UI.getUse().getResNo() == NumVecs)
8930 continue;
8931 SDNode *User = *UI;
8932 if (User->getOpcode() != ARMISD::VDUPLANE ||
8933 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8934 return false;
8935 }
8936
8937 // Create the vldN-dup node.
8938 EVT Tys[5];
8939 unsigned n;
8940 for (n = 0; n < NumVecs; ++n)
8941 Tys[n] = VT;
8942 Tys[n] = MVT::Other;
Craig Topperabb4ac72014-04-16 06:10:51 +00008943 SDVTList SDTys = DAG.getVTList(ArrayRef<EVT>(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00008944 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8945 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008946 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00008947 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00008948 VLDMemInt->getMemOperand());
8949
8950 // Update the uses.
8951 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8952 UI != UE; ++UI) {
8953 unsigned ResNo = UI.getUse().getResNo();
8954 // Ignore uses of the chain result.
8955 if (ResNo == NumVecs)
8956 continue;
8957 SDNode *User = *UI;
8958 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8959 }
8960
8961 // Now the vldN-lane intrinsic is dead except for its chain result.
8962 // Update uses of the chain.
8963 std::vector<SDValue> VLDDupResults;
8964 for (unsigned n = 0; n < NumVecs; ++n)
8965 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8966 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8967 DCI.CombineTo(VLD, VLDDupResults);
8968
8969 return true;
8970}
8971
Bob Wilson103a0dc2010-07-14 01:22:12 +00008972/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8973/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00008974static SDValue PerformVDUPLANECombine(SDNode *N,
8975 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00008976 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00008977
Bob Wilson2d790df2010-11-28 06:51:26 +00008978 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8979 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8980 if (CombineVLDDUP(N, DCI))
8981 return SDValue(N, 0);
8982
8983 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8984 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00008985 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00008986 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00008987 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00008988 return SDValue();
8989
8990 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8991 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8992 // The canonical VMOV for a zero vector uses a 32-bit element size.
8993 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8994 unsigned EltBits;
8995 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8996 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00008997 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00008998 if (EltSize > VT.getVectorElementType().getSizeInBits())
8999 return SDValue();
9000
Andrew Trickef9de2a2013-05-25 02:42:55 +00009001 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009002}
9003
Eric Christopher1b8b94192011-06-29 21:10:36 +00009004// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00009005// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
9006static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
9007{
Chad Rosier6b610b32011-06-28 17:26:57 +00009008 integerPart cN;
9009 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009010 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
9011 I != E; I++) {
9012 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
9013 if (!C)
9014 return false;
9015
Eric Christopher1b8b94192011-06-29 21:10:36 +00009016 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009017 APFloat APF = C->getValueAPF();
9018 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
9019 != APFloat::opOK || !isExact)
9020 return false;
9021
9022 c0 = (I == 0) ? cN : c0;
9023 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
9024 return false;
9025 }
9026 C = c0;
9027 return true;
9028}
9029
9030/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
9031/// can replace combinations of VMUL and VCVT (floating-point to integer)
9032/// when the VMUL has a constant operand that is a power of 2.
9033///
9034/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9035/// vmul.f32 d16, d17, d16
9036/// vcvt.s32.f32 d16, d16
9037/// becomes:
9038/// vcvt.s32.f32 d16, d16, #3
9039static SDValue PerformVCVTCombine(SDNode *N,
9040 TargetLowering::DAGCombinerInfo &DCI,
9041 const ARMSubtarget *Subtarget) {
9042 SelectionDAG &DAG = DCI.DAG;
9043 SDValue Op = N->getOperand(0);
9044
9045 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
9046 Op.getOpcode() != ISD::FMUL)
9047 return SDValue();
9048
9049 uint64_t C;
9050 SDValue N0 = Op->getOperand(0);
9051 SDValue ConstVec = Op->getOperand(1);
9052 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
9053
Eric Christopher1b8b94192011-06-29 21:10:36 +00009054 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00009055 !isConstVecPow2(ConstVec, isSigned, C))
9056 return SDValue();
9057
Tim Northover7cbc2152013-06-28 15:29:25 +00009058 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
9059 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
9060 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9061 // These instructions only exist converting from f32 to i32. We can handle
9062 // smaller integers by generating an extra truncate, but larger ones would
9063 // be lossy.
9064 return SDValue();
9065 }
9066
Chad Rosierfa8d8932011-06-24 19:23:04 +00009067 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9068 Intrinsic::arm_neon_vcvtfp2fxu;
Tim Northover7cbc2152013-06-28 15:29:25 +00009069 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9070 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
9071 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9072 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
9073 DAG.getConstant(Log2_64(C), MVT::i32));
9074
9075 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9076 FixConv = DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), FixConv);
9077
9078 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009079}
9080
9081/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9082/// can replace combinations of VCVT (integer to floating-point) and VDIV
9083/// when the VDIV has a constant operand that is a power of 2.
9084///
9085/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9086/// vcvt.f32.s32 d16, d16
9087/// vdiv.f32 d16, d17, d16
9088/// becomes:
9089/// vcvt.f32.s32 d16, d16, #3
9090static SDValue PerformVDIVCombine(SDNode *N,
9091 TargetLowering::DAGCombinerInfo &DCI,
9092 const ARMSubtarget *Subtarget) {
9093 SelectionDAG &DAG = DCI.DAG;
9094 SDValue Op = N->getOperand(0);
9095 unsigned OpOpcode = Op.getNode()->getOpcode();
9096
9097 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
9098 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9099 return SDValue();
9100
9101 uint64_t C;
9102 SDValue ConstVec = N->getOperand(1);
9103 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
9104
9105 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9106 !isConstVecPow2(ConstVec, isSigned, C))
9107 return SDValue();
9108
Tim Northover7cbc2152013-06-28 15:29:25 +00009109 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
9110 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
9111 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9112 // These instructions only exist converting from i32 to f32. We can handle
9113 // smaller integers by generating an extra extend, but larger ones would
9114 // be lossy.
9115 return SDValue();
9116 }
9117
9118 SDValue ConvInput = Op.getOperand(0);
9119 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9120 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9121 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
9122 SDLoc(N), NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9123 ConvInput);
9124
Eric Christopher1b8b94192011-06-29 21:10:36 +00009125 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00009126 Intrinsic::arm_neon_vcvtfxu2fp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009127 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
Chad Rosierfa8d8932011-06-24 19:23:04 +00009128 Op.getValueType(),
Eric Christopher1b8b94192011-06-29 21:10:36 +00009129 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Tim Northover7cbc2152013-06-28 15:29:25 +00009130 ConvInput, DAG.getConstant(Log2_64(C), MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00009131}
9132
9133/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00009134/// operand of a vector shift operation, where all the elements of the
9135/// build_vector must have the same constant integer value.
9136static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9137 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00009138 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00009139 Op = Op.getOperand(0);
9140 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9141 APInt SplatBits, SplatUndef;
9142 unsigned SplatBitSize;
9143 bool HasAnyUndefs;
9144 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9145 HasAnyUndefs, ElementBits) ||
9146 SplatBitSize > ElementBits)
9147 return false;
9148 Cnt = SplatBits.getSExtValue();
9149 return true;
9150}
9151
9152/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9153/// operand of a vector shift left operation. That value must be in the range:
9154/// 0 <= Value < ElementBits for a left shift; or
9155/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009156static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009157 assert(VT.isVector() && "vector shift count is not a vector type");
9158 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9159 if (! getVShiftImm(Op, ElementBits, Cnt))
9160 return false;
9161 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9162}
9163
9164/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9165/// operand of a vector shift right operation. For a shift opcode, the value
9166/// is positive, but for an intrinsic the value count must be negative. The
9167/// absolute value must be in the range:
9168/// 1 <= |Value| <= ElementBits for a right shift; or
9169/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009170static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00009171 int64_t &Cnt) {
9172 assert(VT.isVector() && "vector shift count is not a vector type");
9173 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9174 if (! getVShiftImm(Op, ElementBits, Cnt))
9175 return false;
9176 if (isIntrinsic)
9177 Cnt = -Cnt;
9178 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9179}
9180
9181/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9182static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9183 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9184 switch (IntNo) {
9185 default:
9186 // Don't do anything for most intrinsics.
9187 break;
9188
9189 // Vector shifts: check for immediate versions and lower them.
9190 // Note: This is done during DAG combining instead of DAG legalizing because
9191 // the build_vectors for 64-bit vector element shift counts are generally
9192 // not legal, and it is hard to see their values after they get legalized to
9193 // loads from a constant pool.
9194 case Intrinsic::arm_neon_vshifts:
9195 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009196 case Intrinsic::arm_neon_vrshifts:
9197 case Intrinsic::arm_neon_vrshiftu:
9198 case Intrinsic::arm_neon_vrshiftn:
9199 case Intrinsic::arm_neon_vqshifts:
9200 case Intrinsic::arm_neon_vqshiftu:
9201 case Intrinsic::arm_neon_vqshiftsu:
9202 case Intrinsic::arm_neon_vqshiftns:
9203 case Intrinsic::arm_neon_vqshiftnu:
9204 case Intrinsic::arm_neon_vqshiftnsu:
9205 case Intrinsic::arm_neon_vqrshiftns:
9206 case Intrinsic::arm_neon_vqrshiftnu:
9207 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009208 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009209 int64_t Cnt;
9210 unsigned VShiftOpc = 0;
9211
9212 switch (IntNo) {
9213 case Intrinsic::arm_neon_vshifts:
9214 case Intrinsic::arm_neon_vshiftu:
9215 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9216 VShiftOpc = ARMISD::VSHL;
9217 break;
9218 }
9219 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9220 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9221 ARMISD::VSHRs : ARMISD::VSHRu);
9222 break;
9223 }
9224 return SDValue();
9225
Bob Wilson2e076c42009-06-22 23:27:02 +00009226 case Intrinsic::arm_neon_vrshifts:
9227 case Intrinsic::arm_neon_vrshiftu:
9228 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9229 break;
9230 return SDValue();
9231
9232 case Intrinsic::arm_neon_vqshifts:
9233 case Intrinsic::arm_neon_vqshiftu:
9234 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9235 break;
9236 return SDValue();
9237
9238 case Intrinsic::arm_neon_vqshiftsu:
9239 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9240 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +00009241 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009242
Bob Wilson2e076c42009-06-22 23:27:02 +00009243 case Intrinsic::arm_neon_vrshiftn:
9244 case Intrinsic::arm_neon_vqshiftns:
9245 case Intrinsic::arm_neon_vqshiftnu:
9246 case Intrinsic::arm_neon_vqshiftnsu:
9247 case Intrinsic::arm_neon_vqrshiftns:
9248 case Intrinsic::arm_neon_vqrshiftnu:
9249 case Intrinsic::arm_neon_vqrshiftnsu:
9250 // Narrowing shifts require an immediate right shift.
9251 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9252 break;
Jim Grosbach84511e12010-06-02 21:53:11 +00009253 llvm_unreachable("invalid shift count for narrowing vector shift "
9254 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009255
9256 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00009257 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00009258 }
9259
9260 switch (IntNo) {
9261 case Intrinsic::arm_neon_vshifts:
9262 case Intrinsic::arm_neon_vshiftu:
9263 // Opcode already set above.
9264 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00009265 case Intrinsic::arm_neon_vrshifts:
9266 VShiftOpc = ARMISD::VRSHRs; break;
9267 case Intrinsic::arm_neon_vrshiftu:
9268 VShiftOpc = ARMISD::VRSHRu; break;
9269 case Intrinsic::arm_neon_vrshiftn:
9270 VShiftOpc = ARMISD::VRSHRN; break;
9271 case Intrinsic::arm_neon_vqshifts:
9272 VShiftOpc = ARMISD::VQSHLs; break;
9273 case Intrinsic::arm_neon_vqshiftu:
9274 VShiftOpc = ARMISD::VQSHLu; break;
9275 case Intrinsic::arm_neon_vqshiftsu:
9276 VShiftOpc = ARMISD::VQSHLsu; break;
9277 case Intrinsic::arm_neon_vqshiftns:
9278 VShiftOpc = ARMISD::VQSHRNs; break;
9279 case Intrinsic::arm_neon_vqshiftnu:
9280 VShiftOpc = ARMISD::VQSHRNu; break;
9281 case Intrinsic::arm_neon_vqshiftnsu:
9282 VShiftOpc = ARMISD::VQSHRNsu; break;
9283 case Intrinsic::arm_neon_vqrshiftns:
9284 VShiftOpc = ARMISD::VQRSHRNs; break;
9285 case Intrinsic::arm_neon_vqrshiftnu:
9286 VShiftOpc = ARMISD::VQRSHRNu; break;
9287 case Intrinsic::arm_neon_vqrshiftnsu:
9288 VShiftOpc = ARMISD::VQRSHRNsu; break;
9289 }
9290
Andrew Trickef9de2a2013-05-25 02:42:55 +00009291 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009292 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009293 }
9294
9295 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009296 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009297 int64_t Cnt;
9298 unsigned VShiftOpc = 0;
9299
9300 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9301 VShiftOpc = ARMISD::VSLI;
9302 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9303 VShiftOpc = ARMISD::VSRI;
9304 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009305 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009306 }
9307
Andrew Trickef9de2a2013-05-25 02:42:55 +00009308 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +00009309 N->getOperand(1), N->getOperand(2),
Owen Anderson9f944592009-08-11 20:47:22 +00009310 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009311 }
9312
9313 case Intrinsic::arm_neon_vqrshifts:
9314 case Intrinsic::arm_neon_vqrshiftu:
9315 // No immediate versions of these to check for.
9316 break;
9317 }
9318
9319 return SDValue();
9320}
9321
9322/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9323/// lowers them. As with the vector shift intrinsics, this is done during DAG
9324/// combining instead of DAG legalizing because the build_vectors for 64-bit
9325/// vector element shift counts are generally not legal, and it is hard to see
9326/// their values after they get legalized to loads from a constant pool.
9327static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9328 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009329 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +00009330 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9331 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9332 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9333 SDValue N1 = N->getOperand(1);
9334 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9335 SDValue N0 = N->getOperand(0);
9336 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9337 DAG.MaskedValueIsZero(N0.getOperand(0),
9338 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009339 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +00009340 }
9341 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009342
9343 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +00009344 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9345 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +00009346 return SDValue();
9347
9348 assert(ST->hasNEON() && "unexpected vector shift");
9349 int64_t Cnt;
9350
9351 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009352 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009353
9354 case ISD::SHL:
9355 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009356 return DAG.getNode(ARMISD::VSHL, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009357 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009358 break;
9359
9360 case ISD::SRA:
9361 case ISD::SRL:
9362 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9363 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9364 ARMISD::VSHRs : ARMISD::VSHRu);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009365 return DAG.getNode(VShiftOpc, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009366 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009367 }
9368 }
9369 return SDValue();
9370}
9371
9372/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9373/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9374static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9375 const ARMSubtarget *ST) {
9376 SDValue N0 = N->getOperand(0);
9377
9378 // Check for sign- and zero-extensions of vector extract operations of 8-
9379 // and 16-bit vector elements. NEON supports these directly. They are
9380 // handled during DAG combining because type legalization will promote them
9381 // to 32-bit types and it is messy to recognize the operations after that.
9382 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9383 SDValue Vec = N0.getOperand(0);
9384 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009385 EVT VT = N->getValueType(0);
9386 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009387 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9388
Owen Anderson9f944592009-08-11 20:47:22 +00009389 if (VT == MVT::i32 &&
9390 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +00009391 TLI.isTypeLegal(Vec.getValueType()) &&
9392 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009393
9394 unsigned Opc = 0;
9395 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009396 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009397 case ISD::SIGN_EXTEND:
9398 Opc = ARMISD::VGETLANEs;
9399 break;
9400 case ISD::ZERO_EXTEND:
9401 case ISD::ANY_EXTEND:
9402 Opc = ARMISD::VGETLANEu;
9403 break;
9404 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009405 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +00009406 }
9407 }
9408
9409 return SDValue();
9410}
9411
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009412/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9413/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9414static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9415 const ARMSubtarget *ST) {
9416 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng55f0c6b2010-07-15 22:07:12 +00009417 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009418 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9419 // a NaN; only do the transformation when it matches that behavior.
9420
9421 // For now only do this when using NEON for FP operations; if using VFP, it
9422 // is not obvious that the benefit outweighs the cost of switching to the
9423 // NEON pipeline.
9424 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9425 N->getValueType(0) != MVT::f32)
9426 return SDValue();
9427
9428 SDValue CondLHS = N->getOperand(0);
9429 SDValue CondRHS = N->getOperand(1);
9430 SDValue LHS = N->getOperand(2);
9431 SDValue RHS = N->getOperand(3);
9432 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9433
9434 unsigned Opcode = 0;
9435 bool IsReversed;
Bob Wilsonba8ac742010-02-24 22:15:53 +00009436 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009437 IsReversed = false; // x CC y ? x : y
Bob Wilsonba8ac742010-02-24 22:15:53 +00009438 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009439 IsReversed = true ; // x CC y ? y : x
9440 } else {
9441 return SDValue();
9442 }
9443
Bob Wilsonba8ac742010-02-24 22:15:53 +00009444 bool IsUnordered;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009445 switch (CC) {
9446 default: break;
9447 case ISD::SETOLT:
9448 case ISD::SETOLE:
9449 case ISD::SETLT:
9450 case ISD::SETLE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009451 case ISD::SETULT:
9452 case ISD::SETULE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009453 // If LHS is NaN, an ordered comparison will be false and the result will
9454 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9455 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9456 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9457 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9458 break;
9459 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9460 // will return -0, so vmin can only be used for unsafe math or if one of
9461 // the operands is known to be nonzero.
9462 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009463 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009464 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9465 break;
9466 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009467 break;
9468
9469 case ISD::SETOGT:
9470 case ISD::SETOGE:
9471 case ISD::SETGT:
9472 case ISD::SETGE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009473 case ISD::SETUGT:
9474 case ISD::SETUGE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009475 // If LHS is NaN, an ordered comparison will be false and the result will
9476 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9477 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9478 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9479 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9480 break;
9481 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9482 // will return +0, so vmax can only be used for unsafe math or if one of
9483 // the operands is known to be nonzero.
9484 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009485 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009486 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9487 break;
9488 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009489 break;
9490 }
9491
9492 if (!Opcode)
9493 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009494 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009495}
9496
Evan Chengf863e3f2011-07-13 00:42:17 +00009497/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9498SDValue
9499ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9500 SDValue Cmp = N->getOperand(4);
9501 if (Cmp.getOpcode() != ARMISD::CMPZ)
9502 // Only looking at EQ and NE cases.
9503 return SDValue();
9504
9505 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009506 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +00009507 SDValue LHS = Cmp.getOperand(0);
9508 SDValue RHS = Cmp.getOperand(1);
9509 SDValue FalseVal = N->getOperand(0);
9510 SDValue TrueVal = N->getOperand(1);
9511 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +00009512 ARMCC::CondCodes CC =
9513 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +00009514
9515 // Simplify
9516 // mov r1, r0
9517 // cmp r1, x
9518 // mov r0, y
9519 // moveq r0, x
9520 // to
9521 // cmp r0, x
9522 // movne r0, y
9523 //
9524 // mov r1, r0
9525 // cmp r1, x
9526 // mov r0, x
9527 // movne r0, y
9528 // to
9529 // cmp r0, x
9530 // movne r0, y
9531 /// FIXME: Turn this into a target neutral optimization?
9532 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +00009533 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +00009534 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9535 N->getOperand(3), Cmp);
9536 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9537 SDValue ARMcc;
9538 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9539 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9540 N->getOperand(3), NewCmp);
9541 }
9542
9543 if (Res.getNode()) {
9544 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00009545 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +00009546 // Capture demanded bits information that would be otherwise lost.
9547 if (KnownZero == 0xfffffffe)
9548 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9549 DAG.getValueType(MVT::i1));
9550 else if (KnownZero == 0xffffff00)
9551 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9552 DAG.getValueType(MVT::i8));
9553 else if (KnownZero == 0xffff0000)
9554 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9555 DAG.getValueType(MVT::i16));
9556 }
9557
9558 return Res;
9559}
9560
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009561SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +00009562 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009563 switch (N->getOpcode()) {
9564 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009565 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +00009566 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009567 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009568 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009569 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +00009570 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9571 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +00009572 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00009573 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson22806742010-09-22 22:09:21 +00009574 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009575 case ISD::STORE: return PerformSTORECombine(N, DCI);
9576 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
9577 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +00009578 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +00009579 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009580 case ISD::FP_TO_SINT:
9581 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9582 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009583 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00009584 case ISD::SHL:
9585 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009586 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00009587 case ISD::SIGN_EXTEND:
9588 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009589 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9590 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +00009591 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson06fce872011-02-07 17:43:21 +00009592 case ARMISD::VLD2DUP:
9593 case ARMISD::VLD3DUP:
9594 case ARMISD::VLD4DUP:
9595 return CombineBaseUpdate(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009596 case ARMISD::BUILD_VECTOR:
9597 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009598 case ISD::INTRINSIC_VOID:
9599 case ISD::INTRINSIC_W_CHAIN:
9600 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9601 case Intrinsic::arm_neon_vld1:
9602 case Intrinsic::arm_neon_vld2:
9603 case Intrinsic::arm_neon_vld3:
9604 case Intrinsic::arm_neon_vld4:
9605 case Intrinsic::arm_neon_vld2lane:
9606 case Intrinsic::arm_neon_vld3lane:
9607 case Intrinsic::arm_neon_vld4lane:
9608 case Intrinsic::arm_neon_vst1:
9609 case Intrinsic::arm_neon_vst2:
9610 case Intrinsic::arm_neon_vst3:
9611 case Intrinsic::arm_neon_vst4:
9612 case Intrinsic::arm_neon_vst2lane:
9613 case Intrinsic::arm_neon_vst3lane:
9614 case Intrinsic::arm_neon_vst4lane:
9615 return CombineBaseUpdate(N, DCI);
9616 default: break;
9617 }
9618 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009619 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009620 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009621}
9622
Evan Chengd42641c2011-02-02 01:06:55 +00009623bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9624 EVT VT) const {
9625 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9626}
9627
Matt Arsenault25793a32014-02-05 23:15:53 +00009628bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, unsigned,
9629 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009630 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +00009631 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009632
9633 switch (VT.getSimpleVT().SimpleTy) {
9634 default:
9635 return false;
9636 case MVT::i8:
9637 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009638 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009639 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +00009640 if (AllowsUnaligned) {
9641 if (Fast)
9642 *Fast = Subtarget->hasV7Ops();
9643 return true;
9644 }
9645 return false;
9646 }
Evan Chengeec6bc62012-08-15 17:44:53 +00009647 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009648 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009649 // For any little-endian targets with neon, we can support unaligned ld/st
9650 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +00009651 // A big-endian target may also explicitly support unaligned accesses
Evan Cheng79e2ca92012-12-10 23:21:26 +00009652 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
9653 if (Fast)
9654 *Fast = true;
9655 return true;
9656 }
9657 return false;
9658 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009659 }
9660}
9661
Lang Hames9929c422011-11-02 22:52:45 +00009662static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9663 unsigned AlignCheck) {
9664 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9665 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9666}
9667
9668EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9669 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00009670 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +00009671 bool MemcpyStrSrc,
9672 MachineFunction &MF) const {
9673 const Function *F = MF.getFunction();
9674
9675 // See if we can use NEON instructions for this...
Evan Cheng962711e2012-12-12 02:34:41 +00009676 if ((!IsMemset || ZeroMemset) &&
Evan Cheng79e2ca92012-12-10 23:21:26 +00009677 Subtarget->hasNEON() &&
Bill Wendling698e84f2012-12-30 10:32:01 +00009678 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
9679 Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009680 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +00009681 if (Size >= 16 &&
9682 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault25793a32014-02-05 23:15:53 +00009683 (allowsUnalignedMemoryAccesses(MVT::v2f64, 0, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009684 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +00009685 } else if (Size >= 8 &&
9686 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault25793a32014-02-05 23:15:53 +00009687 (allowsUnalignedMemoryAccesses(MVT::f64, 0, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009688 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +00009689 }
9690 }
9691
Lang Hamesb85fcd02011-11-08 18:56:23 +00009692 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +00009693 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009694 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +00009695 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009696 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +00009697
Lang Hames9929c422011-11-02 22:52:45 +00009698 // Let the target-independent logic figure it out.
9699 return MVT::Other;
9700}
9701
Evan Cheng9ec512d2012-12-06 19:13:27 +00009702bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
9703 if (Val.getOpcode() != ISD::LOAD)
9704 return false;
9705
9706 EVT VT1 = Val.getValueType();
9707 if (!VT1.isSimple() || !VT1.isInteger() ||
9708 !VT2.isSimple() || !VT2.isInteger())
9709 return false;
9710
9711 switch (VT1.getSimpleVT().SimpleTy) {
9712 default: break;
9713 case MVT::i1:
9714 case MVT::i8:
9715 case MVT::i16:
9716 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
9717 return true;
9718 }
9719
9720 return false;
9721}
9722
Tim Northovercc2e9032013-08-06 13:58:03 +00009723bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
9724 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
9725 return false;
9726
9727 if (!isTypeLegal(EVT::getEVT(Ty1)))
9728 return false;
9729
9730 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
9731
9732 // Assuming the caller doesn't have a zeroext or signext return parameter,
9733 // truncation all the way down to i1 is valid.
9734 return true;
9735}
9736
9737
Evan Chengdc49a8d2009-08-14 20:09:37 +00009738static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9739 if (V < 0)
9740 return false;
9741
9742 unsigned Scale = 1;
9743 switch (VT.getSimpleVT().SimpleTy) {
9744 default: return false;
9745 case MVT::i1:
9746 case MVT::i8:
9747 // Scale == 1;
9748 break;
9749 case MVT::i16:
9750 // Scale == 2;
9751 Scale = 2;
9752 break;
9753 case MVT::i32:
9754 // Scale == 4;
9755 Scale = 4;
9756 break;
9757 }
9758
9759 if ((V & (Scale - 1)) != 0)
9760 return false;
9761 V /= Scale;
9762 return V == (V & ((1LL << 5) - 1));
9763}
9764
9765static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9766 const ARMSubtarget *Subtarget) {
9767 bool isNeg = false;
9768 if (V < 0) {
9769 isNeg = true;
9770 V = - V;
9771 }
9772
9773 switch (VT.getSimpleVT().SimpleTy) {
9774 default: return false;
9775 case MVT::i1:
9776 case MVT::i8:
9777 case MVT::i16:
9778 case MVT::i32:
9779 // + imm12 or - imm8
9780 if (isNeg)
9781 return V == (V & ((1LL << 8) - 1));
9782 return V == (V & ((1LL << 12) - 1));
9783 case MVT::f32:
9784 case MVT::f64:
9785 // Same as ARM mode. FIXME: NEON?
9786 if (!Subtarget->hasVFP2())
9787 return false;
9788 if ((V & 3) != 0)
9789 return false;
9790 V >>= 2;
9791 return V == (V & ((1LL << 8) - 1));
9792 }
9793}
9794
Evan Cheng2150b922007-03-12 23:30:29 +00009795/// isLegalAddressImmediate - Return true if the integer value can be used
9796/// as the offset of the target addressing mode for load / store of the
9797/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009798static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009799 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +00009800 if (V == 0)
9801 return true;
9802
Evan Chengce5dfb62009-03-09 19:15:00 +00009803 if (!VT.isSimple())
9804 return false;
9805
Evan Chengdc49a8d2009-08-14 20:09:37 +00009806 if (Subtarget->isThumb1Only())
9807 return isLegalT1AddressImmediate(V, VT);
9808 else if (Subtarget->isThumb2())
9809 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +00009810
Evan Chengdc49a8d2009-08-14 20:09:37 +00009811 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +00009812 if (V < 0)
9813 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +00009814 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +00009815 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +00009816 case MVT::i1:
9817 case MVT::i8:
9818 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +00009819 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009820 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +00009821 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +00009822 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009823 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +00009824 case MVT::f32:
9825 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009826 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +00009827 return false;
Evan Chengbef131de2007-05-03 02:00:18 +00009828 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +00009829 return false;
9830 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009831 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +00009832 }
Evan Cheng10043e22007-01-19 07:51:42 +00009833}
9834
Evan Chengdc49a8d2009-08-14 20:09:37 +00009835bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9836 EVT VT) const {
9837 int Scale = AM.Scale;
9838 if (Scale < 0)
9839 return false;
9840
9841 switch (VT.getSimpleVT().SimpleTy) {
9842 default: return false;
9843 case MVT::i1:
9844 case MVT::i8:
9845 case MVT::i16:
9846 case MVT::i32:
9847 if (Scale == 1)
9848 return true;
9849 // r + r << imm
9850 Scale = Scale & ~1;
9851 return Scale == 2 || Scale == 4 || Scale == 8;
9852 case MVT::i64:
9853 // r + r
9854 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9855 return true;
9856 return false;
9857 case MVT::isVoid:
9858 // Note, we allow "void" uses (basically, uses that aren't loads or
9859 // stores), because arm allows folding a scale into many arithmetic
9860 // operations. This should be made more precise and revisited later.
9861
9862 // Allow r << imm, but the imm has to be a multiple of two.
9863 if (Scale & 1) return false;
9864 return isPowerOf2_32(Scale);
9865 }
9866}
9867
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009868/// isLegalAddressingMode - Return true if the addressing mode represented
9869/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson7117a912009-03-20 22:42:55 +00009870bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00009871 Type *Ty) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009872 EVT VT = getValueType(Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +00009873 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +00009874 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009875
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009876 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +00009877 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009878 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009879
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009880 switch (AM.Scale) {
9881 case 0: // no scale reg, must be "r+i" or "r", or "i".
9882 break;
9883 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009884 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009885 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +00009886 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009887 default:
Chris Lattner502c3f42007-04-13 06:50:55 +00009888 // ARM doesn't support any R+R*scale+imm addr modes.
9889 if (AM.BaseOffs)
9890 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009891
Bob Wilson866c1742009-04-08 17:55:28 +00009892 if (!VT.isSimple())
9893 return false;
9894
Evan Chengdc49a8d2009-08-14 20:09:37 +00009895 if (Subtarget->isThumb2())
9896 return isLegalT2ScaledAddressingMode(AM, VT);
9897
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009898 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +00009899 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009900 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +00009901 case MVT::i1:
9902 case MVT::i8:
9903 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009904 if (Scale < 0) Scale = -Scale;
9905 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009906 return true;
9907 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +00009908 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +00009909 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009910 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009911 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009912 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009913 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +00009914 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009915
Owen Anderson9f944592009-08-11 20:47:22 +00009916 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009917 // Note, we allow "void" uses (basically, uses that aren't loads or
9918 // stores), because arm allows folding a scale into many arithmetic
9919 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +00009920
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009921 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +00009922 if (Scale & 1) return false;
9923 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009924 }
Evan Cheng2150b922007-03-12 23:30:29 +00009925 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009926 return true;
Evan Cheng2150b922007-03-12 23:30:29 +00009927}
9928
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009929/// isLegalICmpImmediate - Return true if the specified immediate is legal
9930/// icmp immediate, that is the target has icmp instructions which can compare
9931/// a register against the immediate without having to materialize the
9932/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +00009933bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +00009934 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009935 if (!Subtarget->isThumb())
Chandler Carruth8a102c22012-04-06 20:10:52 +00009936 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009937 if (Subtarget->isThumb2())
Chandler Carruth8a102c22012-04-06 20:10:52 +00009938 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +00009939 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +00009940 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009941}
9942
Andrew Tricka22cdb72012-07-18 18:34:27 +00009943/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9944/// *or sub* immediate, that is the target has add or sub instructions which can
9945/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +00009946/// immediate into a register.
9947bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +00009948 // Same encoding for add/sub, just flip the sign.
9949 int64_t AbsImm = llvm::abs64(Imm);
9950 if (!Subtarget->isThumb())
9951 return ARM_AM::getSOImmVal(AbsImm) != -1;
9952 if (Subtarget->isThumb2())
9953 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9954 // Thumb1 only has 8-bit unsigned immediate.
9955 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +00009956}
9957
Owen Anderson53aa7a92009-08-10 22:56:29 +00009958static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +00009959 bool isSEXTLoad, SDValue &Base,
9960 SDValue &Offset, bool &isInc,
9961 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +00009962 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9963 return false;
9964
Owen Anderson9f944592009-08-11 20:47:22 +00009965 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +00009966 // AddressingMode 3
9967 Base = Ptr->getOperand(0);
9968 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00009969 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +00009970 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +00009971 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +00009972 isInc = false;
9973 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9974 return true;
9975 }
9976 }
9977 isInc = (Ptr->getOpcode() == ISD::ADD);
9978 Offset = Ptr->getOperand(1);
9979 return true;
Owen Anderson9f944592009-08-11 20:47:22 +00009980 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +00009981 // AddressingMode 2
9982 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00009983 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +00009984 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +00009985 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +00009986 isInc = false;
9987 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9988 Base = Ptr->getOperand(0);
9989 return true;
9990 }
9991 }
9992
9993 if (Ptr->getOpcode() == ISD::ADD) {
9994 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +00009995 ARM_AM::ShiftOpc ShOpcVal=
9996 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +00009997 if (ShOpcVal != ARM_AM::no_shift) {
9998 Base = Ptr->getOperand(1);
9999 Offset = Ptr->getOperand(0);
10000 } else {
10001 Base = Ptr->getOperand(0);
10002 Offset = Ptr->getOperand(1);
10003 }
10004 return true;
10005 }
10006
10007 isInc = (Ptr->getOpcode() == ISD::ADD);
10008 Base = Ptr->getOperand(0);
10009 Offset = Ptr->getOperand(1);
10010 return true;
10011 }
10012
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000010013 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000010014 return false;
10015}
10016
Owen Anderson53aa7a92009-08-10 22:56:29 +000010017static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010018 bool isSEXTLoad, SDValue &Base,
10019 SDValue &Offset, bool &isInc,
10020 SelectionDAG &DAG) {
10021 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10022 return false;
10023
10024 Base = Ptr->getOperand(0);
10025 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
10026 int RHSC = (int)RHS->getZExtValue();
10027 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
10028 assert(Ptr->getOpcode() == ISD::ADD);
10029 isInc = false;
10030 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10031 return true;
10032 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
10033 isInc = Ptr->getOpcode() == ISD::ADD;
10034 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
10035 return true;
10036 }
10037 }
10038
10039 return false;
10040}
10041
Evan Cheng10043e22007-01-19 07:51:42 +000010042/// getPreIndexedAddressParts - returns true by value, base pointer and
10043/// offset pointer and addressing mode by reference if the node's address
10044/// can be legally represented as pre-indexed load / store address.
10045bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010046ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
10047 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010048 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010049 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010050 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010051 return false;
10052
Owen Anderson53aa7a92009-08-10 22:56:29 +000010053 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010054 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010055 bool isSEXTLoad = false;
10056 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10057 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010058 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010059 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10060 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10061 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010062 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010063 } else
10064 return false;
10065
10066 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010067 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010068 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010069 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10070 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010071 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010072 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000010073 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000010074 if (!isLegal)
10075 return false;
10076
10077 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10078 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010079}
10080
10081/// getPostIndexedAddressParts - returns true by value, base pointer and
10082/// offset pointer and addressing mode by reference if this node can be
10083/// combined with a load / store to form a post-indexed load / store.
10084bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010085 SDValue &Base,
10086 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010087 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010088 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010089 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010090 return false;
10091
Owen Anderson53aa7a92009-08-10 22:56:29 +000010092 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010093 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010094 bool isSEXTLoad = false;
10095 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010096 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010097 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010098 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10099 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010100 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010101 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010102 } else
10103 return false;
10104
10105 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010106 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010107 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010108 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000010109 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010110 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010111 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10112 isInc, DAG);
10113 if (!isLegal)
10114 return false;
10115
Evan Chengf19384d2010-05-18 21:31:17 +000010116 if (Ptr != Base) {
10117 // Swap base ptr and offset to catch more post-index load / store when
10118 // it's legal. In Thumb2 mode, offset must be an immediate.
10119 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10120 !Subtarget->isThumb2())
10121 std::swap(Base, Offset);
10122
10123 // Post-indexed load / store update the base pointer.
10124 if (Ptr != Base)
10125 return false;
10126 }
10127
Evan Cheng84c6cda2009-07-02 07:28:31 +000010128 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10129 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010130}
10131
Jay Foada0653a32014-05-14 21:14:37 +000010132void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10133 APInt &KnownZero,
10134 APInt &KnownOne,
10135 const SelectionDAG &DAG,
10136 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000010137 unsigned BitWidth = KnownOne.getBitWidth();
10138 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000010139 switch (Op.getOpcode()) {
10140 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000010141 case ARMISD::ADDC:
10142 case ARMISD::ADDE:
10143 case ARMISD::SUBC:
10144 case ARMISD::SUBE:
10145 // These nodes' second result is a boolean
10146 if (Op.getResNo() == 0)
10147 break;
10148 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10149 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010150 case ARMISD::CMOV: {
10151 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000010152 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010153 if (KnownZero == 0 && KnownOne == 0) return;
10154
Dan Gohmanf990faf2008-02-13 00:35:47 +000010155 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000010156 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010157 KnownZero &= KnownZeroRHS;
10158 KnownOne &= KnownOneRHS;
10159 return;
10160 }
Tim Northover01b4aa92014-04-03 15:10:35 +000010161 case ISD::INTRINSIC_W_CHAIN: {
10162 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
10163 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
10164 switch (IntID) {
10165 default: return;
10166 case Intrinsic::arm_ldaex:
10167 case Intrinsic::arm_ldrex: {
10168 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
10169 unsigned MemBits = VT.getScalarType().getSizeInBits();
10170 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
10171 return;
10172 }
10173 }
10174 }
Evan Cheng10043e22007-01-19 07:51:42 +000010175 }
10176}
10177
10178//===----------------------------------------------------------------------===//
10179// ARM Inline Assembly Support
10180//===----------------------------------------------------------------------===//
10181
Evan Cheng078b0b02011-01-08 01:24:27 +000010182bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10183 // Looking for "rev" which is V6+.
10184 if (!Subtarget->hasV6Ops())
10185 return false;
10186
10187 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10188 std::string AsmStr = IA->getAsmString();
10189 SmallVector<StringRef, 4> AsmPieces;
10190 SplitString(AsmStr, AsmPieces, ";\n");
10191
10192 switch (AsmPieces.size()) {
10193 default: return false;
10194 case 1:
10195 AsmStr = AsmPieces[0];
10196 AsmPieces.clear();
10197 SplitString(AsmStr, AsmPieces, " \t,");
10198
10199 // rev $0, $1
10200 if (AsmPieces.size() == 3 &&
10201 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10202 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010203 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010204 if (Ty && Ty->getBitWidth() == 32)
10205 return IntrinsicLowering::LowerToByteSwap(CI);
10206 }
10207 break;
10208 }
10209
10210 return false;
10211}
10212
Evan Cheng10043e22007-01-19 07:51:42 +000010213/// getConstraintType - Given a constraint letter, return the type of
10214/// constraint it is for this target.
10215ARMTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +000010216ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
10217 if (Constraint.size() == 1) {
10218 switch (Constraint[0]) {
10219 default: break;
10220 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010221 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010222 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010223 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010224 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010225 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010226 // An address with a single base register. Due to the way we
10227 // currently handle addresses it is the same as an 'r' memory constraint.
10228 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010229 }
Eric Christophere256cd02011-06-21 22:10:57 +000010230 } else if (Constraint.size() == 2) {
10231 switch (Constraint[0]) {
10232 default: break;
10233 // All 'U+' constraints are addresses.
10234 case 'U': return C_Memory;
10235 }
Evan Cheng10043e22007-01-19 07:51:42 +000010236 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010237 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010238}
10239
John Thompsone8360b72010-10-29 17:29:13 +000010240/// Examine constraint type and operand type and determine a weight value.
10241/// This object must already have been set up with the operand type
10242/// and the current alternative constraint selected.
10243TargetLowering::ConstraintWeight
10244ARMTargetLowering::getSingleConstraintMatchWeight(
10245 AsmOperandInfo &info, const char *constraint) const {
10246 ConstraintWeight weight = CW_Invalid;
10247 Value *CallOperandVal = info.CallOperandVal;
10248 // If we don't have a value, we can't do a match,
10249 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010250 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010251 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010252 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010253 // Look at the constraint type.
10254 switch (*constraint) {
10255 default:
10256 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10257 break;
10258 case 'l':
10259 if (type->isIntegerTy()) {
10260 if (Subtarget->isThumb())
10261 weight = CW_SpecificReg;
10262 else
10263 weight = CW_Register;
10264 }
10265 break;
10266 case 'w':
10267 if (type->isFloatingPointTy())
10268 weight = CW_Register;
10269 break;
10270 }
10271 return weight;
10272}
10273
Eric Christophercf2007c2011-06-30 23:50:52 +000010274typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10275RCPair
Evan Cheng10043e22007-01-19 07:51:42 +000010276ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000010277 MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000010278 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010279 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000010280 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000010281 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010282 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010283 return RCPair(0U, &ARM::tGPRRegClass);
10284 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000010285 case 'h': // High regs or no regs.
10286 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010287 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000010288 break;
Chris Lattner6223e832007-04-02 17:24:08 +000010289 case 'r':
Craig Topperc7242e02012-04-20 07:30:17 +000010290 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010291 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000010292 if (VT == MVT::Other)
10293 break;
Owen Anderson9f944592009-08-11 20:47:22 +000010294 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010295 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000010296 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010297 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000010298 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010299 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010300 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010301 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000010302 if (VT == MVT::Other)
10303 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010304 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010305 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010306 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010307 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010308 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010309 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010310 break;
Eric Christopherc011d312011-07-01 00:30:46 +000010311 case 't':
10312 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010313 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000010314 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010315 }
10316 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000010317 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000010318 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000010319
Evan Cheng10043e22007-01-19 07:51:42 +000010320 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10321}
10322
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010323/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10324/// vector. If it is invalid, don't add anything to Ops.
10325void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010326 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010327 std::vector<SDValue>&Ops,
10328 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000010329 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010330
Eric Christopherde9399b2011-06-02 23:16:42 +000010331 // Currently only support length 1 constraints.
10332 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010333
Eric Christopherde9399b2011-06-02 23:16:42 +000010334 char ConstraintLetter = Constraint[0];
10335 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010336 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000010337 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010338 case 'I': case 'J': case 'K': case 'L':
10339 case 'M': case 'N': case 'O':
10340 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10341 if (!C)
10342 return;
10343
10344 int64_t CVal64 = C->getSExtValue();
10345 int CVal = (int) CVal64;
10346 // None of these constraints allow values larger than 32 bits. Check
10347 // that the value fits in an int.
10348 if (CVal != CVal64)
10349 return;
10350
Eric Christopherde9399b2011-06-02 23:16:42 +000010351 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000010352 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000010353 // Constant suitable for movw, must be between 0 and
10354 // 65535.
10355 if (Subtarget->hasV6T2Ops())
10356 if (CVal >= 0 && CVal <= 65535)
10357 break;
10358 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010359 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000010360 if (Subtarget->isThumb1Only()) {
10361 // This must be a constant between 0 and 255, for ADD
10362 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010363 if (CVal >= 0 && CVal <= 255)
10364 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010365 } else if (Subtarget->isThumb2()) {
10366 // A constant that can be used as an immediate value in a
10367 // data-processing instruction.
10368 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10369 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010370 } else {
10371 // A constant that can be used as an immediate value in a
10372 // data-processing instruction.
10373 if (ARM_AM::getSOImmVal(CVal) != -1)
10374 break;
10375 }
10376 return;
10377
10378 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000010379 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010380 // This must be a constant between -255 and -1, for negated ADD
10381 // immediates. This can be used in GCC with an "n" modifier that
10382 // prints the negated value, for use with SUB instructions. It is
10383 // not useful otherwise but is implemented for compatibility.
10384 if (CVal >= -255 && CVal <= -1)
10385 break;
10386 } else {
10387 // This must be a constant between -4095 and 4095. It is not clear
10388 // what this constraint is intended for. Implemented for
10389 // compatibility with GCC.
10390 if (CVal >= -4095 && CVal <= 4095)
10391 break;
10392 }
10393 return;
10394
10395 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000010396 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010397 // A 32-bit value where only one byte has a nonzero value. Exclude
10398 // zero to match GCC. This constraint is used by GCC internally for
10399 // constants that can be loaded with a move/shift combination.
10400 // It is not useful otherwise but is implemented for compatibility.
10401 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10402 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010403 } else if (Subtarget->isThumb2()) {
10404 // A constant whose bitwise inverse can be used as an immediate
10405 // value in a data-processing instruction. This can be used in GCC
10406 // with a "B" modifier that prints the inverted value, for use with
10407 // BIC and MVN instructions. It is not useful otherwise but is
10408 // implemented for compatibility.
10409 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10410 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010411 } else {
10412 // A constant whose bitwise inverse can be used as an immediate
10413 // value in a data-processing instruction. This can be used in GCC
10414 // with a "B" modifier that prints the inverted value, for use with
10415 // BIC and MVN instructions. It is not useful otherwise but is
10416 // implemented for compatibility.
10417 if (ARM_AM::getSOImmVal(~CVal) != -1)
10418 break;
10419 }
10420 return;
10421
10422 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000010423 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010424 // This must be a constant between -7 and 7,
10425 // for 3-operand ADD/SUB immediate instructions.
10426 if (CVal >= -7 && CVal < 7)
10427 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010428 } else if (Subtarget->isThumb2()) {
10429 // A constant whose negation can be used as an immediate value in a
10430 // data-processing instruction. This can be used in GCC with an "n"
10431 // modifier that prints the negated value, for use with SUB
10432 // instructions. It is not useful otherwise but is implemented for
10433 // compatibility.
10434 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10435 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010436 } else {
10437 // A constant whose negation can be used as an immediate value in a
10438 // data-processing instruction. This can be used in GCC with an "n"
10439 // modifier that prints the negated value, for use with SUB
10440 // instructions. It is not useful otherwise but is implemented for
10441 // compatibility.
10442 if (ARM_AM::getSOImmVal(-CVal) != -1)
10443 break;
10444 }
10445 return;
10446
10447 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000010448 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010449 // This must be a multiple of 4 between 0 and 1020, for
10450 // ADD sp + immediate.
10451 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10452 break;
10453 } else {
10454 // A power of two or a constant between 0 and 32. This is used in
10455 // GCC for the shift amount on shifted register operands, but it is
10456 // useful in general for any shift amounts.
10457 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10458 break;
10459 }
10460 return;
10461
10462 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000010463 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010464 // This must be a constant between 0 and 31, for shift amounts.
10465 if (CVal >= 0 && CVal <= 31)
10466 break;
10467 }
10468 return;
10469
10470 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000010471 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010472 // This must be a multiple of 4 between -508 and 508, for
10473 // ADD/SUB sp = sp + immediate.
10474 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10475 break;
10476 }
10477 return;
10478 }
10479 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10480 break;
10481 }
10482
10483 if (Result.getNode()) {
10484 Ops.push_back(Result);
10485 return;
10486 }
Dale Johannesence97d552010-06-25 21:55:36 +000010487 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010488}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010489
Renato Golin87610692013-07-16 09:32:17 +000010490SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
10491 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only");
10492 unsigned Opcode = Op->getOpcode();
10493 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
10494 "Invalid opcode for Div/Rem lowering");
10495 bool isSigned = (Opcode == ISD::SDIVREM);
10496 EVT VT = Op->getValueType(0);
10497 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
10498
10499 RTLIB::Libcall LC;
10500 switch (VT.getSimpleVT().SimpleTy) {
10501 default: llvm_unreachable("Unexpected request for libcall!");
10502 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
10503 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
10504 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
10505 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
10506 }
10507
10508 SDValue InChain = DAG.getEntryNode();
10509
10510 TargetLowering::ArgListTy Args;
10511 TargetLowering::ArgListEntry Entry;
10512 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
10513 EVT ArgVT = Op->getOperand(i).getValueType();
10514 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
10515 Entry.Node = Op->getOperand(i);
10516 Entry.Ty = ArgTy;
10517 Entry.isSExt = isSigned;
10518 Entry.isZExt = !isSigned;
10519 Args.push_back(Entry);
10520 }
10521
10522 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
10523 getPointerTy());
10524
10525 Type *RetTy = (Type*)StructType::get(Ty, Ty, NULL);
10526
10527 SDLoc dl(Op);
10528 TargetLowering::
10529 CallLoweringInfo CLI(InChain, RetTy, isSigned, !isSigned, false, true,
10530 0, getLibcallCallingConv(LC), /*isTailCall=*/false,
10531 /*doesNotReturn=*/false, /*isReturnValueUsed=*/true,
10532 Callee, Args, DAG, dl);
10533 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
10534
10535 return CallInfo.first;
10536}
10537
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010538bool
10539ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10540 // The ARM target isn't yet aware of offsets.
10541 return false;
10542}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010543
Jim Grosbach11013ed2010-07-16 23:05:05 +000010544bool ARM::isBitFieldInvertedMask(unsigned v) {
10545 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010546 return false;
10547
Jim Grosbach11013ed2010-07-16 23:05:05 +000010548 // there can be 1's on either or both "outsides", all the "inside"
10549 // bits must be 0's
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010550 unsigned TO = CountTrailingOnes_32(v);
10551 unsigned LO = CountLeadingOnes_32(v);
10552 v = (v >> TO) << TO;
10553 v = (v << LO) >> LO;
10554 return v == 0;
Jim Grosbach11013ed2010-07-16 23:05:05 +000010555}
10556
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010557/// isFPImmLegal - Returns true if the target can instruction select the
10558/// specified FP immediate natively. If false, the legalizer will
10559/// materialize the FP immediate as a load from a constant pool.
10560bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10561 if (!Subtarget->hasVFP3())
10562 return false;
10563 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010564 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010565 if (VT == MVT::f64)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010566 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010567 return false;
10568}
Bob Wilson5549d492010-09-21 17:56:22 +000010569
Wesley Peck527da1b2010-11-23 03:31:01 +000010570/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000010571/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10572/// specified in the intrinsic calls.
10573bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10574 const CallInst &I,
10575 unsigned Intrinsic) const {
10576 switch (Intrinsic) {
10577 case Intrinsic::arm_neon_vld1:
10578 case Intrinsic::arm_neon_vld2:
10579 case Intrinsic::arm_neon_vld3:
10580 case Intrinsic::arm_neon_vld4:
10581 case Intrinsic::arm_neon_vld2lane:
10582 case Intrinsic::arm_neon_vld3lane:
10583 case Intrinsic::arm_neon_vld4lane: {
10584 Info.opc = ISD::INTRINSIC_W_CHAIN;
10585 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010586 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010587 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10588 Info.ptrVal = I.getArgOperand(0);
10589 Info.offset = 0;
10590 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10591 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10592 Info.vol = false; // volatile loads with NEON intrinsics not supported
10593 Info.readMem = true;
10594 Info.writeMem = false;
10595 return true;
10596 }
10597 case Intrinsic::arm_neon_vst1:
10598 case Intrinsic::arm_neon_vst2:
10599 case Intrinsic::arm_neon_vst3:
10600 case Intrinsic::arm_neon_vst4:
10601 case Intrinsic::arm_neon_vst2lane:
10602 case Intrinsic::arm_neon_vst3lane:
10603 case Intrinsic::arm_neon_vst4lane: {
10604 Info.opc = ISD::INTRINSIC_VOID;
10605 // Conservatively set memVT to the entire set of vectors stored.
10606 unsigned NumElts = 0;
10607 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000010608 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000010609 if (!ArgTy->isVectorTy())
10610 break;
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010611 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010612 }
10613 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10614 Info.ptrVal = I.getArgOperand(0);
10615 Info.offset = 0;
10616 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10617 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10618 Info.vol = false; // volatile stores with NEON intrinsics not supported
10619 Info.readMem = false;
10620 Info.writeMem = true;
10621 return true;
10622 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010623 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010624 case Intrinsic::arm_ldrex: {
10625 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
10626 Info.opc = ISD::INTRINSIC_W_CHAIN;
10627 Info.memVT = MVT::getVT(PtrTy->getElementType());
10628 Info.ptrVal = I.getArgOperand(0);
10629 Info.offset = 0;
10630 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10631 Info.vol = true;
10632 Info.readMem = true;
10633 Info.writeMem = false;
10634 return true;
10635 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010636 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010637 case Intrinsic::arm_strex: {
10638 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
10639 Info.opc = ISD::INTRINSIC_W_CHAIN;
10640 Info.memVT = MVT::getVT(PtrTy->getElementType());
10641 Info.ptrVal = I.getArgOperand(1);
10642 Info.offset = 0;
10643 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10644 Info.vol = true;
10645 Info.readMem = false;
10646 Info.writeMem = true;
10647 return true;
10648 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010649 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010650 case Intrinsic::arm_strexd: {
10651 Info.opc = ISD::INTRINSIC_W_CHAIN;
10652 Info.memVT = MVT::i64;
10653 Info.ptrVal = I.getArgOperand(2);
10654 Info.offset = 0;
10655 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010656 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010657 Info.readMem = false;
10658 Info.writeMem = true;
10659 return true;
10660 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010661 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010662 case Intrinsic::arm_ldrexd: {
10663 Info.opc = ISD::INTRINSIC_W_CHAIN;
10664 Info.memVT = MVT::i64;
10665 Info.ptrVal = I.getArgOperand(0);
10666 Info.offset = 0;
10667 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010668 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010669 Info.readMem = true;
10670 Info.writeMem = false;
10671 return true;
10672 }
Bob Wilson5549d492010-09-21 17:56:22 +000010673 default:
10674 break;
10675 }
10676
10677 return false;
10678}
Juergen Ributzka659ce002014-01-28 01:20:14 +000010679
10680/// \brief Returns true if it is beneficial to convert a load of a constant
10681/// to just the constant itself.
10682bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
10683 Type *Ty) const {
10684 assert(Ty->isIntegerTy());
10685
10686 unsigned Bits = Ty->getPrimitiveSizeInBits();
10687 if (Bits == 0 || Bits > 32)
10688 return false;
10689 return true;
10690}
Tim Northover037f26f22014-04-17 18:22:47 +000010691
10692bool ARMTargetLowering::shouldExpandAtomicInIR(Instruction *Inst) const {
10693 // Loads and stores less than 64-bits are already atomic; ones above that
10694 // are doomed anyway, so defer to the default libcall and blame the OS when
10695 // things go wrong:
10696 if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
10697 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() == 64;
10698 else if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
10699 return LI->getType()->getPrimitiveSizeInBits() == 64;
10700
10701 // For the real atomic operations, we have ldrex/strex up to 64 bits.
10702 return Inst->getType()->getPrimitiveSizeInBits() <= 64;
10703}
10704
10705Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
10706 AtomicOrdering Ord) const {
10707 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
10708 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
10709 bool IsAcquire =
10710 Ord == Acquire || Ord == AcquireRelease || Ord == SequentiallyConsistent;
10711
10712 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
10713 // intrinsic must return {i32, i32} and we have to recombine them into a
10714 // single i64 here.
10715 if (ValTy->getPrimitiveSizeInBits() == 64) {
10716 Intrinsic::ID Int =
10717 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
10718 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
10719
10720 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
10721 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
10722
10723 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
10724 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000010725 if (!Subtarget->isLittle())
10726 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000010727 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
10728 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
10729 return Builder.CreateOr(
10730 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
10731 }
10732
10733 Type *Tys[] = { Addr->getType() };
10734 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
10735 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
10736
10737 return Builder.CreateTruncOrBitCast(
10738 Builder.CreateCall(Ldrex, Addr),
10739 cast<PointerType>(Addr->getType())->getElementType());
10740}
10741
10742Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
10743 Value *Addr,
10744 AtomicOrdering Ord) const {
10745 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
10746 bool IsRelease =
10747 Ord == Release || Ord == AcquireRelease || Ord == SequentiallyConsistent;
10748
10749 // Since the intrinsics must have legal type, the i64 intrinsics take two
10750 // parameters: "i32, i32". We must marshal Val into the appropriate form
10751 // before the call.
10752 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
10753 Intrinsic::ID Int =
10754 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
10755 Function *Strex = Intrinsic::getDeclaration(M, Int);
10756 Type *Int32Ty = Type::getInt32Ty(M->getContext());
10757
10758 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
10759 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000010760 if (!Subtarget->isLittle())
10761 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000010762 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
10763 return Builder.CreateCall3(Strex, Lo, Hi, Addr);
10764 }
10765
10766 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
10767 Type *Tys[] = { Addr->getType() };
10768 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
10769
10770 return Builder.CreateCall2(
10771 Strex, Builder.CreateZExtOrBitCast(
10772 Val, Strex->getFunctionType()->getParamType(0)),
10773 Addr);
10774}
Oliver Stannardc24f2172014-05-09 14:01:47 +000010775
10776enum HABaseType {
10777 HA_UNKNOWN = 0,
10778 HA_FLOAT,
10779 HA_DOUBLE,
10780 HA_VECT64,
10781 HA_VECT128
10782};
10783
10784static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
10785 uint64_t &Members) {
10786 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
10787 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
10788 uint64_t SubMembers = 0;
10789 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
10790 return false;
10791 Members += SubMembers;
10792 }
10793 } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
10794 uint64_t SubMembers = 0;
10795 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
10796 return false;
10797 Members += SubMembers * AT->getNumElements();
10798 } else if (Ty->isFloatTy()) {
10799 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
10800 return false;
10801 Members = 1;
10802 Base = HA_FLOAT;
10803 } else if (Ty->isDoubleTy()) {
10804 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
10805 return false;
10806 Members = 1;
10807 Base = HA_DOUBLE;
10808 } else if (const VectorType *VT = dyn_cast<VectorType>(Ty)) {
10809 Members = 1;
10810 switch (Base) {
10811 case HA_FLOAT:
10812 case HA_DOUBLE:
10813 return false;
10814 case HA_VECT64:
10815 return VT->getBitWidth() == 64;
10816 case HA_VECT128:
10817 return VT->getBitWidth() == 128;
10818 case HA_UNKNOWN:
10819 switch (VT->getBitWidth()) {
10820 case 64:
10821 Base = HA_VECT64;
10822 return true;
10823 case 128:
10824 Base = HA_VECT128;
10825 return true;
10826 default:
10827 return false;
10828 }
10829 }
10830 }
10831
10832 return (Members > 0 && Members <= 4);
10833}
10834
10835/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate.
10836bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
10837 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
10838 if (getEffectiveCallingConv(CallConv, isVarArg) ==
10839 CallingConv::ARM_AAPCS_VFP) {
10840 HABaseType Base = HA_UNKNOWN;
10841 uint64_t Members = 0;
10842 bool result = isHomogeneousAggregate(Ty, Base, Members);
10843 DEBUG(dbgs() << "isHA: " << result << " "; Ty->dump(); dbgs() << "\n");
10844 return result;
10845 } else {
10846 return false;
10847 }
10848}