blob: cf849951088adb901296c676e453d322bba01ae2 [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
Eric Christopher89958332014-05-31 00:07:32 +0000158static TargetLoweringObjectFile *createTLOF(const Triple &TT) {
159 if (TT.isOSBinFormatMachO())
Bill Wendlingbbcaa402010-03-15 21:09:38 +0000160 return new TargetLoweringObjectFileMachO();
Eric Christopher89958332014-05-31 00:07:32 +0000161 if (TT.isOSWindows())
Saleem Abdulrasool46fed302014-05-17 04:28:08 +0000162 return new TargetLoweringObjectFileCOFF();
Chris Lattner4e7dfaf2009-08-02 00:34:36 +0000163 return new ARMElfTargetObjectFile();
Chris Lattner5e693ed2009-07-28 03:13:23 +0000164}
165
Evan Cheng10043e22007-01-19 07:51:42 +0000166ARMTargetLowering::ARMTargetLowering(TargetMachine &TM)
Eric Christopher89958332014-05-31 00:07:32 +0000167 : TargetLowering(TM, createTLOF(Triple(TM.getTargetTriple()))) {
Evan Cheng10043e22007-01-19 07:51:42 +0000168 Subtarget = &TM.getSubtarget<ARMSubtarget>();
Evan Chengdf907f42010-07-23 22:39:59 +0000169 RegInfo = TM.getRegisterInfo();
Evan Chengbf407072010-09-10 01:29:16 +0000170 Itins = TM.getInstrItineraryData();
Evan Cheng10043e22007-01-19 07:51:42 +0000171
Duncan Sandsf2641e12011-09-06 19:07:46 +0000172 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
173
Tim Northoverd6a729b2014-01-06 14:28:05 +0000174 if (Subtarget->isTargetMachO()) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000175 // Uses VFP for Thumb libfuncs if available.
Jim Grosbach1d1d6d42013-10-24 23:07:11 +0000176 if (Subtarget->isThumb() && Subtarget->hasVFP2() &&
Tim Northover978d25f2014-04-22 10:10:09 +0000177 Subtarget->hasARMOps() && !TM.Options.UseSoftFloat) {
Evan Chengc9f22fd12007-04-27 08:15:43 +0000178 // Single-precision floating-point arithmetic.
179 setLibcallName(RTLIB::ADD_F32, "__addsf3vfp");
180 setLibcallName(RTLIB::SUB_F32, "__subsf3vfp");
181 setLibcallName(RTLIB::MUL_F32, "__mulsf3vfp");
182 setLibcallName(RTLIB::DIV_F32, "__divsf3vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000183
Evan Chengc9f22fd12007-04-27 08:15:43 +0000184 // Double-precision floating-point arithmetic.
185 setLibcallName(RTLIB::ADD_F64, "__adddf3vfp");
186 setLibcallName(RTLIB::SUB_F64, "__subdf3vfp");
187 setLibcallName(RTLIB::MUL_F64, "__muldf3vfp");
188 setLibcallName(RTLIB::DIV_F64, "__divdf3vfp");
Evan Cheng143576d2007-01-31 09:30:58 +0000189
Evan Chengc9f22fd12007-04-27 08:15:43 +0000190 // Single-precision comparisons.
191 setLibcallName(RTLIB::OEQ_F32, "__eqsf2vfp");
192 setLibcallName(RTLIB::UNE_F32, "__nesf2vfp");
193 setLibcallName(RTLIB::OLT_F32, "__ltsf2vfp");
194 setLibcallName(RTLIB::OLE_F32, "__lesf2vfp");
195 setLibcallName(RTLIB::OGE_F32, "__gesf2vfp");
196 setLibcallName(RTLIB::OGT_F32, "__gtsf2vfp");
197 setLibcallName(RTLIB::UO_F32, "__unordsf2vfp");
198 setLibcallName(RTLIB::O_F32, "__unordsf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000199
Evan Chengc9f22fd12007-04-27 08:15:43 +0000200 setCmpLibcallCC(RTLIB::OEQ_F32, ISD::SETNE);
201 setCmpLibcallCC(RTLIB::UNE_F32, ISD::SETNE);
202 setCmpLibcallCC(RTLIB::OLT_F32, ISD::SETNE);
203 setCmpLibcallCC(RTLIB::OLE_F32, ISD::SETNE);
204 setCmpLibcallCC(RTLIB::OGE_F32, ISD::SETNE);
205 setCmpLibcallCC(RTLIB::OGT_F32, ISD::SETNE);
206 setCmpLibcallCC(RTLIB::UO_F32, ISD::SETNE);
207 setCmpLibcallCC(RTLIB::O_F32, ISD::SETEQ);
Evan Cheng143576d2007-01-31 09:30:58 +0000208
Evan Chengc9f22fd12007-04-27 08:15:43 +0000209 // Double-precision comparisons.
210 setLibcallName(RTLIB::OEQ_F64, "__eqdf2vfp");
211 setLibcallName(RTLIB::UNE_F64, "__nedf2vfp");
212 setLibcallName(RTLIB::OLT_F64, "__ltdf2vfp");
213 setLibcallName(RTLIB::OLE_F64, "__ledf2vfp");
214 setLibcallName(RTLIB::OGE_F64, "__gedf2vfp");
215 setLibcallName(RTLIB::OGT_F64, "__gtdf2vfp");
216 setLibcallName(RTLIB::UO_F64, "__unorddf2vfp");
217 setLibcallName(RTLIB::O_F64, "__unorddf2vfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000218
Evan Chengc9f22fd12007-04-27 08:15:43 +0000219 setCmpLibcallCC(RTLIB::OEQ_F64, ISD::SETNE);
220 setCmpLibcallCC(RTLIB::UNE_F64, ISD::SETNE);
221 setCmpLibcallCC(RTLIB::OLT_F64, ISD::SETNE);
222 setCmpLibcallCC(RTLIB::OLE_F64, ISD::SETNE);
223 setCmpLibcallCC(RTLIB::OGE_F64, ISD::SETNE);
224 setCmpLibcallCC(RTLIB::OGT_F64, ISD::SETNE);
225 setCmpLibcallCC(RTLIB::UO_F64, ISD::SETNE);
226 setCmpLibcallCC(RTLIB::O_F64, ISD::SETEQ);
Evan Cheng10043e22007-01-19 07:51:42 +0000227
Evan Chengc9f22fd12007-04-27 08:15:43 +0000228 // Floating-point to integer conversions.
229 // i64 conversions are done via library routines even when generating VFP
230 // instructions, so use the same ones.
231 setLibcallName(RTLIB::FPTOSINT_F64_I32, "__fixdfsivfp");
232 setLibcallName(RTLIB::FPTOUINT_F64_I32, "__fixunsdfsivfp");
233 setLibcallName(RTLIB::FPTOSINT_F32_I32, "__fixsfsivfp");
234 setLibcallName(RTLIB::FPTOUINT_F32_I32, "__fixunssfsivfp");
Evan Cheng10043e22007-01-19 07:51:42 +0000235
Evan Chengc9f22fd12007-04-27 08:15:43 +0000236 // Conversions between floating types.
237 setLibcallName(RTLIB::FPROUND_F64_F32, "__truncdfsf2vfp");
238 setLibcallName(RTLIB::FPEXT_F32_F64, "__extendsfdf2vfp");
239
240 // Integer to floating-point conversions.
241 // i64 conversions are done via library routines even when generating VFP
242 // instructions, so use the same ones.
Bob Wilsondc40d5a2009-03-20 23:16:43 +0000243 // FIXME: There appears to be some naming inconsistency in ARM libgcc:
244 // e.g., __floatunsidf vs. __floatunssidfvfp.
Evan Chengc9f22fd12007-04-27 08:15:43 +0000245 setLibcallName(RTLIB::SINTTOFP_I32_F64, "__floatsidfvfp");
246 setLibcallName(RTLIB::UINTTOFP_I32_F64, "__floatunssidfvfp");
247 setLibcallName(RTLIB::SINTTOFP_I32_F32, "__floatsisfvfp");
248 setLibcallName(RTLIB::UINTTOFP_I32_F32, "__floatunssisfvfp");
249 }
Evan Cheng10043e22007-01-19 07:51:42 +0000250 }
251
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000252 // These libcalls are not available in 32-bit.
Craig Topper062a2ba2014-04-25 05:30:21 +0000253 setLibcallName(RTLIB::SHL_I128, nullptr);
254 setLibcallName(RTLIB::SRL_I128, nullptr);
255 setLibcallName(RTLIB::SRA_I128, nullptr);
Bob Wilsonccbc17b2009-05-22 17:38:41 +0000256
Saleem Abdulrasoolcd130822014-04-02 20:32:05 +0000257 if (Subtarget->isAAPCS_ABI() && !Subtarget->isTargetMachO() &&
258 !Subtarget->isTargetWindows()) {
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000259 static const struct {
260 const RTLIB::Libcall Op;
261 const char * const Name;
262 const CallingConv::ID CC;
263 const ISD::CondCode Cond;
264 } LibraryCalls[] = {
265 // Double-precision floating-point arithmetic helper functions
266 // RTABI chapter 4.1.2, Table 2
267 { RTLIB::ADD_F64, "__aeabi_dadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
268 { RTLIB::DIV_F64, "__aeabi_ddiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
269 { RTLIB::MUL_F64, "__aeabi_dmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
270 { RTLIB::SUB_F64, "__aeabi_dsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000271
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000272 // Double-precision floating-point comparison helper functions
273 // RTABI chapter 4.1.2, Table 3
274 { RTLIB::OEQ_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
275 { RTLIB::UNE_F64, "__aeabi_dcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
276 { RTLIB::OLT_F64, "__aeabi_dcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
277 { RTLIB::OLE_F64, "__aeabi_dcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
278 { RTLIB::OGE_F64, "__aeabi_dcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
279 { RTLIB::OGT_F64, "__aeabi_dcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
280 { RTLIB::UO_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
281 { RTLIB::O_F64, "__aeabi_dcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000282
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000283 // Single-precision floating-point arithmetic helper functions
284 // RTABI chapter 4.1.2, Table 4
285 { RTLIB::ADD_F32, "__aeabi_fadd", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
286 { RTLIB::DIV_F32, "__aeabi_fdiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
287 { RTLIB::MUL_F32, "__aeabi_fmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
288 { RTLIB::SUB_F32, "__aeabi_fsub", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000289
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000290 // Single-precision floating-point comparison helper functions
291 // RTABI chapter 4.1.2, Table 5
292 { RTLIB::OEQ_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETNE },
293 { RTLIB::UNE_F32, "__aeabi_fcmpeq", CallingConv::ARM_AAPCS, ISD::SETEQ },
294 { RTLIB::OLT_F32, "__aeabi_fcmplt", CallingConv::ARM_AAPCS, ISD::SETNE },
295 { RTLIB::OLE_F32, "__aeabi_fcmple", CallingConv::ARM_AAPCS, ISD::SETNE },
296 { RTLIB::OGE_F32, "__aeabi_fcmpge", CallingConv::ARM_AAPCS, ISD::SETNE },
297 { RTLIB::OGT_F32, "__aeabi_fcmpgt", CallingConv::ARM_AAPCS, ISD::SETNE },
298 { RTLIB::UO_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETNE },
299 { RTLIB::O_F32, "__aeabi_fcmpun", CallingConv::ARM_AAPCS, ISD::SETEQ },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000300
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000301 // Floating-point to integer conversions.
302 // RTABI chapter 4.1.2, Table 6
303 { RTLIB::FPTOSINT_F64_I32, "__aeabi_d2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
304 { RTLIB::FPTOUINT_F64_I32, "__aeabi_d2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
305 { RTLIB::FPTOSINT_F64_I64, "__aeabi_d2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
306 { RTLIB::FPTOUINT_F64_I64, "__aeabi_d2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
307 { RTLIB::FPTOSINT_F32_I32, "__aeabi_f2iz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
308 { RTLIB::FPTOUINT_F32_I32, "__aeabi_f2uiz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
309 { RTLIB::FPTOSINT_F32_I64, "__aeabi_f2lz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
310 { RTLIB::FPTOUINT_F32_I64, "__aeabi_f2ulz", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000311
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000312 // Conversions between floating types.
313 // RTABI chapter 4.1.2, Table 7
314 { RTLIB::FPROUND_F64_F32, "__aeabi_d2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
315 { RTLIB::FPEXT_F32_F64, "__aeabi_f2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000316
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000317 // Integer to floating-point conversions.
318 // RTABI chapter 4.1.2, Table 8
319 { RTLIB::SINTTOFP_I32_F64, "__aeabi_i2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
320 { RTLIB::UINTTOFP_I32_F64, "__aeabi_ui2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
321 { RTLIB::SINTTOFP_I64_F64, "__aeabi_l2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
322 { RTLIB::UINTTOFP_I64_F64, "__aeabi_ul2d", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
323 { RTLIB::SINTTOFP_I32_F32, "__aeabi_i2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
324 { RTLIB::UINTTOFP_I32_F32, "__aeabi_ui2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
325 { RTLIB::SINTTOFP_I64_F32, "__aeabi_l2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
326 { RTLIB::UINTTOFP_I64_F32, "__aeabi_ul2f", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000327
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000328 // Long long helper functions
329 // RTABI chapter 4.2, Table 9
330 { RTLIB::MUL_I64, "__aeabi_lmul", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
331 { RTLIB::SHL_I64, "__aeabi_llsl", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
332 { RTLIB::SRL_I64, "__aeabi_llsr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
333 { RTLIB::SRA_I64, "__aeabi_lasr", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Anton Korobeynikov81bdc932010-09-28 21:39:26 +0000334
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000335 // Integer division functions
336 // RTABI chapter 4.3.1
337 { RTLIB::SDIV_I8, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
338 { RTLIB::SDIV_I16, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
339 { RTLIB::SDIV_I32, "__aeabi_idiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
340 { RTLIB::SDIV_I64, "__aeabi_ldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
341 { RTLIB::UDIV_I8, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
342 { RTLIB::UDIV_I16, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
343 { RTLIB::UDIV_I32, "__aeabi_uidiv", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
344 { RTLIB::UDIV_I64, "__aeabi_uldivmod", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
Renato Golin4cd51872011-05-22 21:41:23 +0000345
Saleem Abdulrasool8bfb1922014-05-18 16:39:11 +0000346 // Memory operations
347 // RTABI chapter 4.3.4
348 { RTLIB::MEMCPY, "__aeabi_memcpy", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
349 { RTLIB::MEMMOVE, "__aeabi_memmove", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
350 { RTLIB::MEMSET, "__aeabi_memset", CallingConv::ARM_AAPCS, ISD::SETCC_INVALID },
351 };
352
353 for (const auto &LC : LibraryCalls) {
354 setLibcallName(LC.Op, LC.Name);
355 setLibcallCallingConv(LC.Op, LC.CC);
356 if (LC.Cond != ISD::SETCC_INVALID)
357 setCmpLibcallCC(LC.Op, LC.Cond);
358 }
Anton Korobeynikova6b3ce22009-08-14 20:10:52 +0000359 }
360
Saleem Abdulrasool056fc3d2014-05-16 05:41:33 +0000361 if (Subtarget->isTargetWindows()) {
362 static const struct {
363 const RTLIB::Libcall Op;
364 const char * const Name;
365 const CallingConv::ID CC;
366 } LibraryCalls[] = {
367 { RTLIB::FPTOSINT_F32_I64, "__stoi64", CallingConv::ARM_AAPCS_VFP },
368 { RTLIB::FPTOSINT_F64_I64, "__dtoi64", CallingConv::ARM_AAPCS_VFP },
369 { RTLIB::FPTOUINT_F32_I64, "__stou64", CallingConv::ARM_AAPCS_VFP },
370 { RTLIB::FPTOUINT_F64_I64, "__dtou64", CallingConv::ARM_AAPCS_VFP },
371 { RTLIB::SINTTOFP_I64_F32, "__i64tos", CallingConv::ARM_AAPCS_VFP },
372 { RTLIB::SINTTOFP_I64_F64, "__i64tod", CallingConv::ARM_AAPCS_VFP },
373 { RTLIB::UINTTOFP_I64_F32, "__u64tos", CallingConv::ARM_AAPCS_VFP },
374 { RTLIB::UINTTOFP_I64_F64, "__u64tod", CallingConv::ARM_AAPCS_VFP },
375 };
376
377 for (const auto &LC : LibraryCalls) {
378 setLibcallName(LC.Op, LC.Name);
379 setLibcallCallingConv(LC.Op, LC.CC);
380 }
381 }
382
Bob Wilsonbc158992011-10-07 16:59:21 +0000383 // Use divmod compiler-rt calls for iOS 5.0 and later.
Cameron Esfahani943908b2013-08-29 20:23:14 +0000384 if (Subtarget->getTargetTriple().isiOS() &&
Bob Wilsonbc158992011-10-07 16:59:21 +0000385 !Subtarget->getTargetTriple().isOSVersionLT(5, 0)) {
386 setLibcallName(RTLIB::SDIVREM_I32, "__divmodsi4");
387 setLibcallName(RTLIB::UDIVREM_I32, "__udivmodsi4");
388 }
389
David Goodwin22c2fba2009-07-08 23:10:31 +0000390 if (Subtarget->isThumb1Only())
Craig Topperc7242e02012-04-20 07:30:17 +0000391 addRegisterClass(MVT::i32, &ARM::tGPRRegClass);
Jim Grosbachfde21102009-04-07 20:34:09 +0000392 else
Craig Topperc7242e02012-04-20 07:30:17 +0000393 addRegisterClass(MVT::i32, &ARM::GPRRegClass);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000394 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
395 !Subtarget->isThumb1Only()) {
Craig Topperc7242e02012-04-20 07:30:17 +0000396 addRegisterClass(MVT::f32, &ARM::SPRRegClass);
Jim Grosbach4d5dc3e2010-08-11 15:44:15 +0000397 if (!Subtarget->isFPOnlySP())
Craig Topperc7242e02012-04-20 07:30:17 +0000398 addRegisterClass(MVT::f64, &ARM::DPRRegClass);
Bob Wilson7117a912009-03-20 22:42:55 +0000399
Owen Anderson9f944592009-08-11 20:47:22 +0000400 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000401 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000402
Eli Friedman6f84fed2011-11-08 01:43:53 +0000403 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
404 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
405 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
406 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
407 setTruncStoreAction((MVT::SimpleValueType)VT,
408 (MVT::SimpleValueType)InnerVT, Expand);
409 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
410 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
411 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Benjamin Kramer4dae5982014-04-26 12:06:28 +0000412
413 setOperationAction(ISD::MULHS, (MVT::SimpleValueType)VT, Expand);
414 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
415 setOperationAction(ISD::MULHU, (MVT::SimpleValueType)VT, Expand);
416 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
Benjamin Kramerf3ad2352014-05-19 13:12:38 +0000417
418 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
Eli Friedman6f84fed2011-11-08 01:43:53 +0000419 }
420
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000421 setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
Tim Northoverf79c3a52013-08-20 08:57:11 +0000422 setOperationAction(ISD::ConstantFP, MVT::f64, Custom);
Lang Hamesc35ee8b2012-03-15 18:49:02 +0000423
Bob Wilson2e076c42009-06-22 23:27:02 +0000424 if (Subtarget->hasNEON()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000425 addDRTypeForNEON(MVT::v2f32);
426 addDRTypeForNEON(MVT::v8i8);
427 addDRTypeForNEON(MVT::v4i16);
428 addDRTypeForNEON(MVT::v2i32);
429 addDRTypeForNEON(MVT::v1i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000430
Owen Anderson9f944592009-08-11 20:47:22 +0000431 addQRTypeForNEON(MVT::v4f32);
432 addQRTypeForNEON(MVT::v2f64);
433 addQRTypeForNEON(MVT::v16i8);
434 addQRTypeForNEON(MVT::v8i16);
435 addQRTypeForNEON(MVT::v4i32);
436 addQRTypeForNEON(MVT::v2i64);
Bob Wilson2e076c42009-06-22 23:27:02 +0000437
Bob Wilson194a2512009-09-15 23:55:57 +0000438 // v2f64 is legal so that QR subregs can be extracted as f64 elements, but
439 // neither Neon nor VFP support any arithmetic operations on it.
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000440 // The same with v4f32. But keep in mind that vadd, vsub, vmul are natively
441 // supported for v4f32.
Bob Wilson194a2512009-09-15 23:55:57 +0000442 setOperationAction(ISD::FADD, MVT::v2f64, Expand);
443 setOperationAction(ISD::FSUB, MVT::v2f64, Expand);
444 setOperationAction(ISD::FMUL, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000445 // FIXME: Code duplication: FDIV and FREM are expanded always, see
446 // ARMTargetLowering::addTypeForNEON method for details.
Bob Wilson194a2512009-09-15 23:55:57 +0000447 setOperationAction(ISD::FDIV, MVT::v2f64, Expand);
448 setOperationAction(ISD::FREM, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000449 // FIXME: Create unittest.
450 // In another words, find a way when "copysign" appears in DAG with vector
451 // operands.
Bob Wilson194a2512009-09-15 23:55:57 +0000452 setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000453 // FIXME: Code duplication: SETCC has custom operation action, see
454 // ARMTargetLowering::addTypeForNEON method for details.
Duncan Sandsf2641e12011-09-06 19:07:46 +0000455 setOperationAction(ISD::SETCC, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000456 // FIXME: Create unittest for FNEG and for FABS.
Bob Wilson194a2512009-09-15 23:55:57 +0000457 setOperationAction(ISD::FNEG, MVT::v2f64, Expand);
458 setOperationAction(ISD::FABS, MVT::v2f64, Expand);
459 setOperationAction(ISD::FSQRT, MVT::v2f64, Expand);
460 setOperationAction(ISD::FSIN, MVT::v2f64, Expand);
461 setOperationAction(ISD::FCOS, MVT::v2f64, Expand);
462 setOperationAction(ISD::FPOWI, MVT::v2f64, Expand);
463 setOperationAction(ISD::FPOW, MVT::v2f64, Expand);
464 setOperationAction(ISD::FLOG, MVT::v2f64, Expand);
465 setOperationAction(ISD::FLOG2, MVT::v2f64, Expand);
466 setOperationAction(ISD::FLOG10, MVT::v2f64, Expand);
467 setOperationAction(ISD::FEXP, MVT::v2f64, Expand);
468 setOperationAction(ISD::FEXP2, MVT::v2f64, Expand);
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000469 // FIXME: Create unittest for FCEIL, FTRUNC, FRINT, FNEARBYINT, FFLOOR.
Bob Wilson194a2512009-09-15 23:55:57 +0000470 setOperationAction(ISD::FCEIL, MVT::v2f64, Expand);
471 setOperationAction(ISD::FTRUNC, MVT::v2f64, Expand);
472 setOperationAction(ISD::FRINT, MVT::v2f64, Expand);
473 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Expand);
474 setOperationAction(ISD::FFLOOR, MVT::v2f64, Expand);
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000475 setOperationAction(ISD::FMA, MVT::v2f64, Expand);
Lang Hames591cdaf2012-03-29 21:56:11 +0000476
Stepan Dyatkovskiy46837402011-12-11 14:35:48 +0000477 setOperationAction(ISD::FSQRT, MVT::v4f32, Expand);
478 setOperationAction(ISD::FSIN, MVT::v4f32, Expand);
479 setOperationAction(ISD::FCOS, MVT::v4f32, Expand);
480 setOperationAction(ISD::FPOWI, MVT::v4f32, Expand);
481 setOperationAction(ISD::FPOW, MVT::v4f32, Expand);
482 setOperationAction(ISD::FLOG, MVT::v4f32, Expand);
483 setOperationAction(ISD::FLOG2, MVT::v4f32, Expand);
484 setOperationAction(ISD::FLOG10, MVT::v4f32, Expand);
485 setOperationAction(ISD::FEXP, MVT::v4f32, Expand);
486 setOperationAction(ISD::FEXP2, MVT::v4f32, Expand);
Craig Topper61d04572012-11-15 06:51:10 +0000487 setOperationAction(ISD::FCEIL, MVT::v4f32, Expand);
488 setOperationAction(ISD::FTRUNC, MVT::v4f32, Expand);
489 setOperationAction(ISD::FRINT, MVT::v4f32, Expand);
490 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Expand);
Craig Topper3e41a5b2012-09-08 04:58:43 +0000491 setOperationAction(ISD::FFLOOR, MVT::v4f32, Expand);
Bob Wilson194a2512009-09-15 23:55:57 +0000492
Arnold Schwaighofer99cba962013-03-02 19:38:33 +0000493 // Mark v2f32 intrinsics.
494 setOperationAction(ISD::FSQRT, MVT::v2f32, Expand);
495 setOperationAction(ISD::FSIN, MVT::v2f32, Expand);
496 setOperationAction(ISD::FCOS, MVT::v2f32, Expand);
497 setOperationAction(ISD::FPOWI, MVT::v2f32, Expand);
498 setOperationAction(ISD::FPOW, MVT::v2f32, Expand);
499 setOperationAction(ISD::FLOG, MVT::v2f32, Expand);
500 setOperationAction(ISD::FLOG2, MVT::v2f32, Expand);
501 setOperationAction(ISD::FLOG10, MVT::v2f32, Expand);
502 setOperationAction(ISD::FEXP, MVT::v2f32, Expand);
503 setOperationAction(ISD::FEXP2, MVT::v2f32, Expand);
504 setOperationAction(ISD::FCEIL, MVT::v2f32, Expand);
505 setOperationAction(ISD::FTRUNC, MVT::v2f32, Expand);
506 setOperationAction(ISD::FRINT, MVT::v2f32, Expand);
507 setOperationAction(ISD::FNEARBYINT, MVT::v2f32, Expand);
508 setOperationAction(ISD::FFLOOR, MVT::v2f32, Expand);
509
Bob Wilson6cc46572009-09-16 00:32:15 +0000510 // Neon does not support some operations on v1i64 and v2i64 types.
511 setOperationAction(ISD::MUL, MVT::v1i64, Expand);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000512 // Custom handling for some quad-vector types to detect VMULL.
513 setOperationAction(ISD::MUL, MVT::v8i16, Custom);
514 setOperationAction(ISD::MUL, MVT::v4i32, Custom);
515 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Nate Begemanfa62d502011-02-11 20:53:29 +0000516 // Custom handling for some vector types to avoid expensive expansions
517 setOperationAction(ISD::SDIV, MVT::v4i16, Custom);
518 setOperationAction(ISD::SDIV, MVT::v8i8, Custom);
519 setOperationAction(ISD::UDIV, MVT::v4i16, Custom);
520 setOperationAction(ISD::UDIV, MVT::v8i8, Custom);
Duncan Sandsf2641e12011-09-06 19:07:46 +0000521 setOperationAction(ISD::SETCC, MVT::v1i64, Expand);
522 setOperationAction(ISD::SETCC, MVT::v2i64, Expand);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000523 // Neon does not have single instruction SINT_TO_FP and UINT_TO_FP with
James Molloy547d4c02012-02-20 09:24:05 +0000524 // a destination type that is wider than the source, and nor does
525 // it have a FP_TO_[SU]INT instruction with a narrower destination than
526 // source.
Cameron Zwarich143f9ae2011-03-29 21:41:55 +0000527 setOperationAction(ISD::SINT_TO_FP, MVT::v4i16, Custom);
528 setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom);
James Molloy547d4c02012-02-20 09:24:05 +0000529 setOperationAction(ISD::FP_TO_UINT, MVT::v4i16, Custom);
530 setOperationAction(ISD::FP_TO_SINT, MVT::v4i16, Custom);
Bob Wilson6cc46572009-09-16 00:32:15 +0000531
Eli Friedmane6385e62012-11-15 22:44:27 +0000532 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Eli Friedman30834942012-11-17 01:52:46 +0000533 setOperationAction(ISD::FP_EXTEND, MVT::v2f64, Expand);
Eli Friedmane6385e62012-11-15 22:44:27 +0000534
Evan Chengb4eae132012-12-04 22:41:50 +0000535 // NEON does not have single instruction CTPOP for vectors with element
536 // types wider than 8-bits. However, custom lowering can leverage the
537 // v8i8/v16i8 vcnt instruction.
538 setOperationAction(ISD::CTPOP, MVT::v2i32, Custom);
539 setOperationAction(ISD::CTPOP, MVT::v4i32, Custom);
540 setOperationAction(ISD::CTPOP, MVT::v4i16, Custom);
541 setOperationAction(ISD::CTPOP, MVT::v8i16, Custom);
542
Jim Grosbach5f215872013-02-27 21:31:12 +0000543 // NEON only has FMA instructions as of VFP4.
544 if (!Subtarget->hasVFP4()) {
545 setOperationAction(ISD::FMA, MVT::v2f32, Expand);
546 setOperationAction(ISD::FMA, MVT::v4f32, Expand);
547 }
548
Bob Wilson06fce872011-02-07 17:43:21 +0000549 setTargetDAGCombine(ISD::INTRINSIC_VOID);
550 setTargetDAGCombine(ISD::INTRINSIC_W_CHAIN);
Bob Wilson2e076c42009-06-22 23:27:02 +0000551 setTargetDAGCombine(ISD::INTRINSIC_WO_CHAIN);
552 setTargetDAGCombine(ISD::SHL);
553 setTargetDAGCombine(ISD::SRL);
554 setTargetDAGCombine(ISD::SRA);
555 setTargetDAGCombine(ISD::SIGN_EXTEND);
556 setTargetDAGCombine(ISD::ZERO_EXTEND);
557 setTargetDAGCombine(ISD::ANY_EXTEND);
Bob Wilsonc6c13a32010-02-18 06:05:53 +0000558 setTargetDAGCombine(ISD::SELECT_CC);
Bob Wilsoncb6db982010-09-17 22:59:05 +0000559 setTargetDAGCombine(ISD::BUILD_VECTOR);
Bob Wilsonc7334a12010-10-27 20:38:28 +0000560 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Bob Wilson1a20c2a2010-12-21 06:43:19 +0000561 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
562 setTargetDAGCombine(ISD::STORE);
Chad Rosierfa8d8932011-06-24 19:23:04 +0000563 setTargetDAGCombine(ISD::FP_TO_SINT);
564 setTargetDAGCombine(ISD::FP_TO_UINT);
565 setTargetDAGCombine(ISD::FDIV);
Nadav Rotem097106b2011-10-15 20:03:12 +0000566
James Molloy547d4c02012-02-20 09:24:05 +0000567 // It is legal to extload from v4i8 to v4i16 or v4i32.
568 MVT Tys[6] = {MVT::v8i8, MVT::v4i8, MVT::v2i8,
569 MVT::v4i16, MVT::v2i16,
570 MVT::v2i32};
571 for (unsigned i = 0; i < 6; ++i) {
572 setLoadExtAction(ISD::EXTLOAD, Tys[i], Legal);
573 setLoadExtAction(ISD::ZEXTLOAD, Tys[i], Legal);
574 setLoadExtAction(ISD::SEXTLOAD, Tys[i], Legal);
575 }
Bob Wilson2e076c42009-06-22 23:27:02 +0000576 }
577
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +0000578 // ARM and Thumb2 support UMLAL/SMLAL.
579 if (!Subtarget->isThumb1Only())
580 setTargetDAGCombine(ISD::ADDC);
581
582
Evan Cheng6addd652007-05-18 00:19:34 +0000583 computeRegisterProperties();
Evan Cheng10043e22007-01-19 07:51:42 +0000584
585 // ARM does not have f32 extending load.
Owen Anderson9f944592009-08-11 20:47:22 +0000586 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000587
Duncan Sands95d46ef2008-01-23 20:39:46 +0000588 // ARM does not have i1 sign extending load.
Owen Anderson9f944592009-08-11 20:47:22 +0000589 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Duncan Sands95d46ef2008-01-23 20:39:46 +0000590
Evan Cheng10043e22007-01-19 07:51:42 +0000591 // ARM supports all 4 flavors of integer indexed load / store.
Evan Cheng84c6cda2009-07-02 07:28:31 +0000592 if (!Subtarget->isThumb1Only()) {
593 for (unsigned im = (unsigned)ISD::PRE_INC;
594 im != (unsigned)ISD::LAST_INDEXED_MODE; ++im) {
Owen Anderson9f944592009-08-11 20:47:22 +0000595 setIndexedLoadAction(im, MVT::i1, Legal);
596 setIndexedLoadAction(im, MVT::i8, Legal);
597 setIndexedLoadAction(im, MVT::i16, Legal);
598 setIndexedLoadAction(im, MVT::i32, Legal);
599 setIndexedStoreAction(im, MVT::i1, Legal);
600 setIndexedStoreAction(im, MVT::i8, Legal);
601 setIndexedStoreAction(im, MVT::i16, Legal);
602 setIndexedStoreAction(im, MVT::i32, Legal);
Evan Cheng84c6cda2009-07-02 07:28:31 +0000603 }
Evan Cheng10043e22007-01-19 07:51:42 +0000604 }
605
Louis Gerbarg3342bf12014-05-09 17:02:49 +0000606 setOperationAction(ISD::SADDO, MVT::i32, Custom);
607 setOperationAction(ISD::UADDO, MVT::i32, Custom);
608 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
609 setOperationAction(ISD::USUBO, MVT::i32, Custom);
610
Evan Cheng10043e22007-01-19 07:51:42 +0000611 // i64 operation support.
Eric Christopherc721b0db2011-04-19 18:49:19 +0000612 setOperationAction(ISD::MUL, MVT::i64, Expand);
613 setOperationAction(ISD::MULHU, MVT::i32, Expand);
Evan Chengb24e51e2009-07-07 01:17:28 +0000614 if (Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000615 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
616 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000617 }
Jim Grosbachcf1464d2011-07-01 21:12:19 +0000618 if (Subtarget->isThumb1Only() || !Subtarget->hasV6Ops()
619 || (Subtarget->isThumb2() && !Subtarget->hasThumb2DSP()))
Eric Christopherc721b0db2011-04-19 18:49:19 +0000620 setOperationAction(ISD::MULHS, MVT::i32, Expand);
621
Jim Grosbach5d994042009-10-31 19:38:01 +0000622 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
Jim Grosbach624fcb22009-10-31 21:00:56 +0000623 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +0000624 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000625 setOperationAction(ISD::SRL, MVT::i64, Custom);
626 setOperationAction(ISD::SRA, MVT::i64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000627
Evan Chenge8916542011-08-30 01:34:54 +0000628 if (!Subtarget->isThumb1Only()) {
629 // FIXME: We should do this for Thumb1 as well.
630 setOperationAction(ISD::ADDC, MVT::i32, Custom);
631 setOperationAction(ISD::ADDE, MVT::i32, Custom);
632 setOperationAction(ISD::SUBC, MVT::i32, Custom);
633 setOperationAction(ISD::SUBE, MVT::i32, Custom);
634 }
635
Evan Cheng10043e22007-01-19 07:51:42 +0000636 // ARM does not have ROTL.
Owen Anderson9f944592009-08-11 20:47:22 +0000637 setOperationAction(ISD::ROTL, MVT::i32, Expand);
Jim Grosbach8546ec92010-01-18 19:58:49 +0000638 setOperationAction(ISD::CTTZ, MVT::i32, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000639 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
David Goodwinaa294c52009-06-26 20:47:43 +0000640 if (!Subtarget->hasV5TOps() || Subtarget->isThumb1Only())
Owen Anderson9f944592009-08-11 20:47:22 +0000641 setOperationAction(ISD::CTLZ, MVT::i32, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000642
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000643 // These just redirect to CTTZ and CTLZ on ARM.
644 setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i32 , Expand);
645 setOperationAction(ISD::CTLZ_ZERO_UNDEF , MVT::i32 , Expand);
646
Tim Northoverbc933082013-05-23 19:11:20 +0000647 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Custom);
648
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000649 // Only ARMv6 has BSWAP.
650 if (!Subtarget->hasV6Ops())
Owen Anderson9f944592009-08-11 20:47:22 +0000651 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Lauro Ramos Venancio25d40522007-03-16 22:54:16 +0000652
Bob Wilsone8a549c2012-09-29 21:43:49 +0000653 if (!(Subtarget->hasDivide() && Subtarget->isThumb2()) &&
654 !(Subtarget->hasDivideInARMMode() && !Subtarget->isThumb())) {
655 // These are expanded into libcalls if the cpu doesn't have HW divider.
Jim Grosbach92d999002010-05-05 20:44:35 +0000656 setOperationAction(ISD::SDIV, MVT::i32, Expand);
657 setOperationAction(ISD::UDIV, MVT::i32, Expand);
658 }
Renato Golin87610692013-07-16 09:32:17 +0000659
660 // FIXME: Also set divmod for SREM on EABI
Owen Anderson9f944592009-08-11 20:47:22 +0000661 setOperationAction(ISD::SREM, MVT::i32, Expand);
662 setOperationAction(ISD::UREM, MVT::i32, Expand);
Renato Golin87610692013-07-16 09:32:17 +0000663 // Register based DivRem for AEABI (RTABI 4.2)
664 if (Subtarget->isTargetAEABI()) {
665 setLibcallName(RTLIB::SDIVREM_I8, "__aeabi_idivmod");
666 setLibcallName(RTLIB::SDIVREM_I16, "__aeabi_idivmod");
667 setLibcallName(RTLIB::SDIVREM_I32, "__aeabi_idivmod");
668 setLibcallName(RTLIB::SDIVREM_I64, "__aeabi_ldivmod");
669 setLibcallName(RTLIB::UDIVREM_I8, "__aeabi_uidivmod");
670 setLibcallName(RTLIB::UDIVREM_I16, "__aeabi_uidivmod");
671 setLibcallName(RTLIB::UDIVREM_I32, "__aeabi_uidivmod");
672 setLibcallName(RTLIB::UDIVREM_I64, "__aeabi_uldivmod");
673
674 setLibcallCallingConv(RTLIB::SDIVREM_I8, CallingConv::ARM_AAPCS);
675 setLibcallCallingConv(RTLIB::SDIVREM_I16, CallingConv::ARM_AAPCS);
676 setLibcallCallingConv(RTLIB::SDIVREM_I32, CallingConv::ARM_AAPCS);
677 setLibcallCallingConv(RTLIB::SDIVREM_I64, CallingConv::ARM_AAPCS);
678 setLibcallCallingConv(RTLIB::UDIVREM_I8, CallingConv::ARM_AAPCS);
679 setLibcallCallingConv(RTLIB::UDIVREM_I16, CallingConv::ARM_AAPCS);
680 setLibcallCallingConv(RTLIB::UDIVREM_I32, CallingConv::ARM_AAPCS);
681 setLibcallCallingConv(RTLIB::UDIVREM_I64, CallingConv::ARM_AAPCS);
682
683 setOperationAction(ISD::SDIVREM, MVT::i32, Custom);
684 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
685 } else {
686 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
687 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
688 }
Bob Wilson7117a912009-03-20 22:42:55 +0000689
Owen Anderson9f944592009-08-11 20:47:22 +0000690 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
691 setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
692 setOperationAction(ISD::GLOBAL_OFFSET_TABLE, MVT::i32, Custom);
693 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
Bob Wilson1cf0b032009-10-30 05:45:42 +0000694 setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000695
Evan Cheng74d92c12011-04-08 21:37:21 +0000696 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Evan Cheng2fa5a7e2010-05-11 07:26:32 +0000697
Evan Cheng10043e22007-01-19 07:51:42 +0000698 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +0000699 setOperationAction(ISD::VASTART, MVT::Other, Custom);
700 setOperationAction(ISD::VAARG, MVT::Other, Expand);
701 setOperationAction(ISD::VACOPY, MVT::Other, Expand);
702 setOperationAction(ISD::VAEND, MVT::Other, Expand);
703 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
704 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000705
Tim Northoverd6a729b2014-01-06 14:28:05 +0000706 if (!Subtarget->isTargetMachO()) {
707 // Non-MachO platforms may return values in these registers via the
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000708 // personality function.
Bill Wendling05d6f2f2012-02-13 23:47:16 +0000709 setExceptionPointerRegister(ARM::R0);
710 setExceptionSelectorRegister(ARM::R1);
711 }
Anton Korobeynikovf3a62312011-01-24 22:38:45 +0000712
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000713 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
714 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
715 else
716 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
717
Evan Cheng6e809de2010-08-11 06:22:01 +0000718 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
719 // the default expansion.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000720 if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000721 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
722 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000723 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000724
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000725 // On v8, we have particularly efficient implementations of atomic fences
726 // if they can be combined with nearby atomic loads and stores.
727 if (!Subtarget->hasV8Ops()) {
728 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
729 setInsertFencesForAtomic(true);
730 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000731 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000732 // If there's anything we can use as a barrier, go through custom lowering
733 // for ATOMIC_FENCE.
734 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
735 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
736
Jim Grosbach6860bb72010-06-18 22:35:32 +0000737 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000738 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000739 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000740 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000741 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000742 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000743 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000744 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000745 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000746 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000747 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000748 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000749 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000750 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
751 // Unordered/Monotonic case.
752 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
753 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000754 }
Evan Cheng10043e22007-01-19 07:51:42 +0000755
Evan Cheng21acf9f2010-11-04 05:19:35 +0000756 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000757
Eli Friedman8cfa7712010-06-26 04:36:50 +0000758 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
759 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000760 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
761 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000762 }
Owen Anderson9f944592009-08-11 20:47:22 +0000763 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000764
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000765 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
766 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000767 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000768 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000769 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000770 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
771 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000772
773 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000774 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbach31984832010-07-07 00:07:57 +0000775 if (Subtarget->isTargetDarwin()) {
776 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
777 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall7d84ece2011-05-29 19:50:32 +0000778 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbach31984832010-07-07 00:07:57 +0000779 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000780
Owen Anderson9f944592009-08-11 20:47:22 +0000781 setOperationAction(ISD::SETCC, MVT::i32, Expand);
782 setOperationAction(ISD::SETCC, MVT::f32, Expand);
783 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000784 setOperationAction(ISD::SELECT, MVT::i32, Custom);
785 setOperationAction(ISD::SELECT, MVT::f32, Custom);
786 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000787 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
788 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
789 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000790
Owen Anderson9f944592009-08-11 20:47:22 +0000791 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
792 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
793 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
794 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
795 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000796
Dan Gohman482732a2007-10-11 23:21:31 +0000797 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000798 setOperationAction(ISD::FSIN, MVT::f64, Expand);
799 setOperationAction(ISD::FSIN, MVT::f32, Expand);
800 setOperationAction(ISD::FCOS, MVT::f32, Expand);
801 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000802 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
803 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000804 setOperationAction(ISD::FREM, MVT::f64, Expand);
805 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000806 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
807 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000808 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
809 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000810 }
Owen Anderson9f944592009-08-11 20:47:22 +0000811 setOperationAction(ISD::FPOW, MVT::f64, Expand);
812 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000813
Evan Chengd0007f32012-04-10 21:40:28 +0000814 if (!Subtarget->hasVFP4()) {
815 setOperationAction(ISD::FMA, MVT::f64, Expand);
816 setOperationAction(ISD::FMA, MVT::f32, Expand);
817 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000818
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000819 // Various VFP goodness
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000820 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilsone4191e72010-03-19 22:51:32 +0000821 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
822 if (Subtarget->hasVFP2()) {
823 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
824 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
825 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
826 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
827 }
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000828 // Special handling for half-precision FP.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000829 if (!Subtarget->hasFP16()) {
830 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
831 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000832 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000833 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000834
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000835 // Combine sin / cos into one node or libcall if possible.
836 if (Subtarget->hasSinCos()) {
837 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
838 setLibcallName(RTLIB::SINCOS_F64, "sincos");
839 if (Subtarget->getTargetTriple().getOS() == Triple::IOS) {
840 // For iOS, we don't want to the normal expansion of a libcall to
841 // sincos. We want to issue a libcall to __sincos_stret.
842 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
843 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
844 }
845 }
Evan Cheng10043e22007-01-19 07:51:42 +0000846
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000847 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000848 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000849 setTargetDAGCombine(ISD::ADD);
850 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000851 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000852 setTargetDAGCombine(ISD::AND);
853 setTargetDAGCombine(ISD::OR);
854 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000855
Evan Chengf258a152012-02-23 02:58:19 +0000856 if (Subtarget->hasV6Ops())
857 setTargetDAGCombine(ISD::SRL);
858
Evan Cheng10043e22007-01-19 07:51:42 +0000859 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +0000860
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000861 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
862 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +0000863 setSchedulingPreference(Sched::RegPressure);
864 else
865 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +0000866
Evan Cheng3ae2b792011-01-06 06:52:41 +0000867 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000868 MaxStoresPerMemset = 8;
869 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
870 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
871 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
872 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
873 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengb71233f2010-06-26 01:52:05 +0000874
Rafael Espindolaa76eccf2010-07-11 04:01:49 +0000875 // On ARM arguments smaller than 4 bytes are extended, so all arguments
876 // are at least 4 bytes aligned.
877 setMinStackArgumentAlignment(4);
878
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000879 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000880 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000881
Eli Friedman2518f832011-05-06 20:34:06 +0000882 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +0000883}
884
Andrew Trick43f25632011-01-19 02:35:27 +0000885// FIXME: It might make sense to define the representative register class as the
886// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
887// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
888// SPR's representative would be DPR_VFP2. This should work well if register
889// pressure tracking were modified such that a register use would increment the
890// pressure of the register class's representative and all of it's super
891// classes' representatives transitively. We have not implemented this because
892// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000893// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +0000894// and extractions.
Evan Chenga77f3d32010-07-21 06:09:07 +0000895std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000896ARMTargetLowering::findRepresentativeClass(MVT VT) const{
Craig Topper062a2ba2014-04-25 05:30:21 +0000897 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +0000898 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000899 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +0000900 default:
Evan Chenga77f3d32010-07-21 06:09:07 +0000901 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng28590382010-07-21 23:53:58 +0000902 // Use DPR as representative register class for all floating point
903 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
904 // the cost is 1 for both f32 and f64.
905 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +0000906 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +0000907 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +0000908 // When NEON is used for SP, only half of the register file is available
909 // because operations that define both SP and DP results will be constrained
910 // to the VFP2 class (D0-D15). We currently model this constraint prior to
911 // coalescing by double-counting the SP regs. See the FIXME above.
912 if (Subtarget->useNEONForSinglePrecisionFP())
913 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000914 break;
915 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
916 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +0000917 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000918 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000919 break;
920 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000921 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000922 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +0000923 break;
924 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000925 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000926 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +0000927 break;
Evan Cheng10f99a32010-07-19 22:15:08 +0000928 }
Evan Chenga77f3d32010-07-21 06:09:07 +0000929 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +0000930}
931
Evan Cheng10043e22007-01-19 07:51:42 +0000932const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
933 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000934 default: return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +0000935 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +0000936 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +0000937 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
938 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +0000939 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +0000940 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
941 case ARMISD::tCALL: return "ARMISD::tCALL";
942 case ARMISD::BRCOND: return "ARMISD::BRCOND";
943 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +0000944 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +0000945 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +0000946 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +0000947 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
948 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +0000949 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +0000950 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +0000951 case ARMISD::CMPFP: return "ARMISD::CMPFP";
952 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +0000953 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +0000954 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +0000955
Evan Cheng10043e22007-01-19 07:51:42 +0000956 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +0000957
Jim Grosbach8546ec92010-01-18 19:58:49 +0000958 case ARMISD::RBIT: return "ARMISD::RBIT";
959
Bob Wilsone4191e72010-03-19 22:51:32 +0000960 case ARMISD::FTOSI: return "ARMISD::FTOSI";
961 case ARMISD::FTOUI: return "ARMISD::FTOUI";
962 case ARMISD::SITOF: return "ARMISD::SITOF";
963 case ARMISD::UITOF: return "ARMISD::UITOF";
964
Evan Cheng10043e22007-01-19 07:51:42 +0000965 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
966 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
967 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +0000968
Evan Chenge8916542011-08-30 01:34:54 +0000969 case ARMISD::ADDC: return "ARMISD::ADDC";
970 case ARMISD::ADDE: return "ARMISD::ADDE";
971 case ARMISD::SUBC: return "ARMISD::SUBC";
972 case ARMISD::SUBE: return "ARMISD::SUBE";
973
Bob Wilson22806742010-09-22 22:09:21 +0000974 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
975 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +0000976
Evan Chengec6d7c92009-10-28 06:55:03 +0000977 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
978 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
979
Dale Johannesend679ff72010-06-03 21:09:53 +0000980 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +0000981
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +0000982 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +0000983
Evan Chengb972e562009-08-07 00:34:42 +0000984 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
985
Bob Wilson7ed59712010-10-30 00:54:37 +0000986 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +0000987
Evan Cheng8740ee32010-11-03 06:34:55 +0000988 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
989
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +0000990 case ARMISD::WIN__CHKSTK: return "ARMISD:::WIN__CHKSTK";
991
Bob Wilson2e076c42009-06-22 23:27:02 +0000992 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +0000993 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +0000994 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +0000995 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
996 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +0000997 case ARMISD::VCGEU: return "ARMISD::VCGEU";
998 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +0000999 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
1000 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +00001001 case ARMISD::VCGTU: return "ARMISD::VCGTU";
1002 case ARMISD::VTST: return "ARMISD::VTST";
1003
1004 case ARMISD::VSHL: return "ARMISD::VSHL";
1005 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1006 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001007 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1008 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1009 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1010 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1011 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1012 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1013 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1014 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1015 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1016 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1017 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1018 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
1019 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1020 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001021 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001022 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001023 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001024 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001025 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001026 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001027 case ARMISD::VREV64: return "ARMISD::VREV64";
1028 case ARMISD::VREV32: return "ARMISD::VREV32";
1029 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001030 case ARMISD::VZIP: return "ARMISD::VZIP";
1031 case ARMISD::VUZP: return "ARMISD::VUZP";
1032 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001033 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1034 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001035 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1036 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001037 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1038 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001039 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilsonc6c13a32010-02-18 06:05:53 +00001040 case ARMISD::FMAX: return "ARMISD::FMAX";
1041 case ARMISD::FMIN: return "ARMISD::FMIN";
Joey Goulye3dd6842013-08-23 12:01:13 +00001042 case ARMISD::VMAXNM: return "ARMISD::VMAX";
1043 case ARMISD::VMINNM: return "ARMISD::VMIN";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001044 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001045 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1046 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001047 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilson2d790df2010-11-28 06:51:26 +00001048 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1049 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1050 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001051 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1052 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1053 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1054 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1055 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1056 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1057 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1058 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1059 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1060 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1061 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1062 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1063 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1064 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1065 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1066 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1067 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001068 }
1069}
1070
Matt Arsenault758659232013-05-18 00:21:46 +00001071EVT ARMTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Duncan Sandsf2641e12011-09-06 19:07:46 +00001072 if (!VT.isVector()) return getPointerTy();
1073 return VT.changeVectorElementTypeToInteger();
1074}
1075
Evan Cheng4cad68e2010-05-15 02:18:07 +00001076/// getRegClassFor - Return the register class that should be used for the
1077/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001078const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001079 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1080 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1081 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001082 if (Subtarget->hasNEON()) {
1083 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001084 return &ARM::QQPRRegClass;
1085 if (VT == MVT::v8i64)
1086 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001087 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001088 return TargetLowering::getRegClassFor(VT);
1089}
1090
Eric Christopher84bdfd82010-07-21 22:26:11 +00001091// Create a fast isel object.
1092FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001093ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1094 const TargetLibraryInfo *libInfo) const {
1095 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001096}
1097
Anton Korobeynikov19edda02010-07-24 21:52:08 +00001098/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1099/// be used for loads / stores from the global.
1100unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1101 return (Subtarget->isThumb1Only() ? 127 : 4095);
1102}
1103
Evan Cheng4401f882010-05-20 23:26:43 +00001104Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001105 unsigned NumVals = N->getNumValues();
1106 if (!NumVals)
1107 return Sched::RegPressure;
1108
1109 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001110 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001111 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001112 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001113 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001114 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001115 }
Evan Chengbf914992010-05-28 23:25:23 +00001116
1117 if (!N->isMachineOpcode())
1118 return Sched::RegPressure;
1119
1120 // Load are scheduled for latency even if there instruction itinerary
1121 // is not available.
1122 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001123 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001124
Evan Cheng6cc775f2011-06-28 19:10:37 +00001125 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001126 return Sched::RegPressure;
1127 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001128 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001129 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001130
Evan Cheng4401f882010-05-20 23:26:43 +00001131 return Sched::RegPressure;
1132}
1133
Evan Cheng10043e22007-01-19 07:51:42 +00001134//===----------------------------------------------------------------------===//
1135// Lowering Code
1136//===----------------------------------------------------------------------===//
1137
Evan Cheng10043e22007-01-19 07:51:42 +00001138/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1139static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1140 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001141 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001142 case ISD::SETNE: return ARMCC::NE;
1143 case ISD::SETEQ: return ARMCC::EQ;
1144 case ISD::SETGT: return ARMCC::GT;
1145 case ISD::SETGE: return ARMCC::GE;
1146 case ISD::SETLT: return ARMCC::LT;
1147 case ISD::SETLE: return ARMCC::LE;
1148 case ISD::SETUGT: return ARMCC::HI;
1149 case ISD::SETUGE: return ARMCC::HS;
1150 case ISD::SETULT: return ARMCC::LO;
1151 case ISD::SETULE: return ARMCC::LS;
1152 }
1153}
1154
Bob Wilsona2e83332009-09-09 23:14:54 +00001155/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1156static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001157 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001158 CondCode2 = ARMCC::AL;
1159 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001160 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001161 case ISD::SETEQ:
1162 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1163 case ISD::SETGT:
1164 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1165 case ISD::SETGE:
1166 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1167 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001168 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001169 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1170 case ISD::SETO: CondCode = ARMCC::VC; break;
1171 case ISD::SETUO: CondCode = ARMCC::VS; break;
1172 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1173 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1174 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1175 case ISD::SETLT:
1176 case ISD::SETULT: CondCode = ARMCC::LT; break;
1177 case ISD::SETLE:
1178 case ISD::SETULE: CondCode = ARMCC::LE; break;
1179 case ISD::SETNE:
1180 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1181 }
Evan Cheng10043e22007-01-19 07:51:42 +00001182}
1183
Bob Wilsona4c22902009-04-17 19:07:39 +00001184//===----------------------------------------------------------------------===//
1185// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001186//===----------------------------------------------------------------------===//
1187
1188#include "ARMGenCallingConv.inc"
1189
Oliver Stannardc24f2172014-05-09 14:01:47 +00001190/// getEffectiveCallingConv - Get the effective calling convention, taking into
1191/// account presence of floating point hardware and calling convention
1192/// limitations, such as support for variadic functions.
1193CallingConv::ID
1194ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1195 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001196 switch (CC) {
1197 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001198 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001199 case CallingConv::ARM_AAPCS:
1200 case CallingConv::ARM_APCS:
1201 case CallingConv::GHC:
1202 return CC;
1203 case CallingConv::ARM_AAPCS_VFP:
1204 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1205 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001206 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001207 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001208 else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001209 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1210 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001211 return CallingConv::ARM_AAPCS_VFP;
1212 else
1213 return CallingConv::ARM_AAPCS;
1214 case CallingConv::Fast:
1215 if (!Subtarget->isAAPCS_ABI()) {
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001216 if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001217 return CallingConv::Fast;
1218 return CallingConv::ARM_APCS;
Oliver Stannardb5e596f2014-06-13 08:33:03 +00001219 } else if (Subtarget->hasVFP2() && !Subtarget->isThumb1Only() && !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001220 return CallingConv::ARM_AAPCS_VFP;
1221 else
1222 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001223 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001224}
1225
1226/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1227/// CallingConvention.
1228CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1229 bool Return,
1230 bool isVarArg) const {
1231 switch (getEffectiveCallingConv(CC, isVarArg)) {
1232 default:
1233 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001234 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001235 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001236 case CallingConv::ARM_AAPCS:
1237 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1238 case CallingConv::ARM_AAPCS_VFP:
1239 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1240 case CallingConv::Fast:
1241 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001242 case CallingConv::GHC:
1243 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001244 }
1245}
1246
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001247/// LowerCallResult - Lower the result values of a call into the
1248/// appropriate copies out of appropriate physical registers.
1249SDValue
1250ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001251 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001252 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001253 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001254 SmallVectorImpl<SDValue> &InVals,
1255 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001256
Bob Wilsona4c22902009-04-17 19:07:39 +00001257 // Assign locations to each value returned by this call.
1258 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001259 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1260 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001261 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001262 CCAssignFnForNode(CallConv, /* Return*/ true,
1263 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001264
1265 // Copy all of the result registers out of their specified physreg.
1266 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1267 CCValAssign VA = RVLocs[i];
1268
Stephen Linb8bd2322013-04-20 05:14:40 +00001269 // Pass 'this' value directly from the argument to return value, to avoid
1270 // reg unit interference
1271 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001272 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1273 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001274 InVals.push_back(ThisVal);
1275 continue;
1276 }
1277
Bob Wilson0041bd32009-04-25 00:33:20 +00001278 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001279 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001280 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001281 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001282 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001283 Chain = Lo.getValue(1);
1284 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001285 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001286 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001287 InFlag);
1288 Chain = Hi.getValue(1);
1289 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001290 if (!Subtarget->isLittle())
1291 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001292 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001293
Owen Anderson9f944592009-08-11 20:47:22 +00001294 if (VA.getLocVT() == MVT::v2f64) {
1295 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1296 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1297 DAG.getConstant(0, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001298
1299 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001300 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001301 Chain = Lo.getValue(1);
1302 InFlag = Lo.getValue(2);
1303 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001304 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001305 Chain = Hi.getValue(1);
1306 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001307 if (!Subtarget->isLittle())
1308 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001309 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001310 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1311 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001312 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001313 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001314 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1315 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001316 Chain = Val.getValue(1);
1317 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001318 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001319
1320 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001321 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001322 case CCValAssign::Full: break;
1323 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001324 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001325 break;
1326 }
1327
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001328 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001329 }
1330
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001331 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001332}
1333
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001334/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001335SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001336ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1337 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001338 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001339 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001340 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001341 unsigned LocMemOffset = VA.getLocMemOffset();
1342 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1343 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilsona4c22902009-04-17 19:07:39 +00001344 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner886250c2010-09-21 18:51:21 +00001345 MachinePointerInfo::getStack(LocMemOffset),
David Greene0d0149f2010-02-15 16:55:24 +00001346 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001347}
1348
Andrew Trickef9de2a2013-05-25 02:42:55 +00001349void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001350 SDValue Chain, SDValue &Arg,
1351 RegsToPassVector &RegsToPass,
1352 CCValAssign &VA, CCValAssign &NextVA,
1353 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001354 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001355 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001356
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001357 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001358 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001359 unsigned id = Subtarget->isLittle() ? 0 : 1;
1360 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001361
1362 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001363 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001364 else {
1365 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001366 if (!StackPtr.getNode())
Bob Wilson2e076c42009-06-22 23:27:02 +00001367 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1368
Christian Pirkerb5728192014-05-08 14:06:24 +00001369 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001370 dl, DAG, NextVA,
1371 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001372 }
1373}
1374
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001375/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001376/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1377/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001378SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001379ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001380 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001381 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001382 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001383 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1384 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1385 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001386 SDValue Chain = CLI.Chain;
1387 SDValue Callee = CLI.Callee;
1388 bool &isTailCall = CLI.IsTailCall;
1389 CallingConv::ID CallConv = CLI.CallConv;
1390 bool doesNotRet = CLI.DoesNotReturn;
1391 bool isVarArg = CLI.IsVarArg;
1392
Dale Johannesend679ff72010-06-03 21:09:53 +00001393 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001394 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1395 bool isThisReturn = false;
1396 bool isSibCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001397
Bob Wilson8decdc42011-10-07 17:17:49 +00001398 // Disable tail calls if they're not supported.
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001399 if (!Subtarget->supportsTailCall() || MF.getTarget().Options.DisableTailCalls)
Bob Wilson3c9ed762010-08-13 22:43:33 +00001400 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001401
Dale Johannesend679ff72010-06-03 21:09:53 +00001402 if (isTailCall) {
1403 // Check if it's really possible to do a tail call.
1404 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001405 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001406 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001407 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1408 report_fatal_error("failed to perform tail call elimination on a call "
1409 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001410 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1411 // detected sibcalls.
1412 if (isTailCall) {
1413 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001414 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001415 }
1416 }
Evan Cheng10043e22007-01-19 07:51:42 +00001417
Bob Wilsona4c22902009-04-17 19:07:39 +00001418 // Analyze operands of the call, assigning locations to each operand.
1419 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001420 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1421 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001422 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001423 CCAssignFnForNode(CallConv, /* Return*/ false,
1424 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001425
Bob Wilsona4c22902009-04-17 19:07:39 +00001426 // Get a count of how many bytes are to be pushed on the stack.
1427 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001428
Dale Johannesend679ff72010-06-03 21:09:53 +00001429 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001430 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001431 NumBytes = 0;
1432
Evan Cheng10043e22007-01-19 07:51:42 +00001433 // Adjust the stack pointer for the new arguments...
1434 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001435 if (!isSibCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00001436 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
1437 dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001438
Jim Grosbach6ad4bcb2010-02-24 01:43:03 +00001439 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +00001440
Bob Wilson2e076c42009-06-22 23:27:02 +00001441 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001442 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001443
Bob Wilsona4c22902009-04-17 19:07:39 +00001444 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001445 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001446 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1447 i != e;
1448 ++i, ++realArgIdx) {
1449 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001450 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001451 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001452 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001453
Bob Wilsona4c22902009-04-17 19:07:39 +00001454 // Promote the value if needed.
1455 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001456 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001457 case CCValAssign::Full: break;
1458 case CCValAssign::SExt:
1459 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1460 break;
1461 case CCValAssign::ZExt:
1462 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1463 break;
1464 case CCValAssign::AExt:
1465 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1466 break;
1467 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001468 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001469 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001470 }
1471
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001472 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001473 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001474 if (VA.getLocVT() == MVT::v2f64) {
1475 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1476 DAG.getConstant(0, MVT::i32));
1477 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1478 DAG.getConstant(1, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001479
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001480 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001481 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1482
1483 VA = ArgLocs[++i]; // skip ahead to next loc
1484 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001485 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001486 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1487 } else {
1488 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001489
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001490 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1491 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001492 }
1493 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001494 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001495 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001496 }
1497 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001498 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1499 assert(VA.getLocVT() == MVT::i32 &&
1500 "unexpected calling convention register assignment");
1501 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001502 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001503 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001504 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001505 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001506 } else if (isByVal) {
1507 assert(VA.isMemLoc());
1508 unsigned offset = 0;
1509
1510 // True if this byval aggregate will be split between registers
1511 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001512 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
1513 unsigned CurByValIdx = CCInfo.getInRegsParamsProceed();
1514
1515 if (CurByValIdx < ByValArgsCount) {
1516
1517 unsigned RegBegin, RegEnd;
1518 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1519
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001520 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1521 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001522 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001523 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1524 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1525 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1526 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001527 false, false, false,
1528 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001529 MemOpChains.push_back(Load.getValue(1));
1530 RegsToPass.push_back(std::make_pair(j, Load));
1531 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001532
1533 // If parameter size outsides register area, "offset" value
1534 // helps us to calculate stack slot for remained part properly.
1535 offset = RegEnd - RegBegin;
1536
1537 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001538 }
1539
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001540 if (Flags.getByValSize() > 4*offset) {
Manman Ren9f911162012-06-01 02:44:42 +00001541 unsigned LocMemOffset = VA.getLocMemOffset();
1542 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1543 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1544 StkPtrOff);
1545 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1546 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1547 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1548 MVT::i32);
Manman Rene8735522012-06-01 19:33:18 +00001549 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001550
Manman Ren9f911162012-06-01 02:44:42 +00001551 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001552 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001553 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001554 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001555 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001556 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001557 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001558
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001559 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1560 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001561 }
Evan Cheng10043e22007-01-19 07:51:42 +00001562 }
1563
1564 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001565 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001566
1567 // Build a sequence of copy-to-reg nodes chained together with token chain
1568 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001569 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001570 // Tail call byval lowering might overwrite argument registers so in case of
1571 // tail call optimization the copies to registers are lowered later.
1572 if (!isTailCall)
1573 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1574 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1575 RegsToPass[i].second, InFlag);
1576 InFlag = Chain.getValue(1);
1577 }
Evan Cheng10043e22007-01-19 07:51:42 +00001578
Dale Johannesend679ff72010-06-03 21:09:53 +00001579 // For tail calls lower the arguments to the 'real' stack slot.
1580 if (isTailCall) {
1581 // Force all the incoming stack arguments to be loaded from the stack
1582 // before any new outgoing arguments are stored to the stack, because the
1583 // outgoing stack slots may alias the incoming argument stack slots, and
1584 // the alias isn't otherwise explicit. This is slightly more conservative
1585 // than necessary, because it means that each store effectively depends
1586 // on every argument instead of just those arguments it would clobber.
1587
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001588 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001589 InFlag = SDValue();
1590 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1591 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1592 RegsToPass[i].second, InFlag);
1593 InFlag = Chain.getValue(1);
1594 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001595 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001596 }
1597
Bill Wendling24c79f22008-09-16 21:48:12 +00001598 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1599 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1600 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001601 bool isDirect = false;
1602 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001603 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001604 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001605
1606 if (EnableARMLongCalls) {
Saleem Abdulrasool90386ad2014-06-07 20:29:27 +00001607 assert((Subtarget->isTargetWindows() ||
1608 getTargetMachine().getRelocationModel() == Reloc::Static) &&
1609 "long-calls with non-static relocation model!");
Jim Grosbach32bb3622010-04-14 22:28:31 +00001610 // Handle a global address or an external symbol. If it's not one of
1611 // those, the target's already in a register, so we don't need to do
1612 // anything extra.
1613 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001614 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001615 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001616 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001617 ARMConstantPoolValue *CPV =
1618 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1619
Jim Grosbach32bb3622010-04-14 22:28:31 +00001620 // Get the address of the callee into a register
1621 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1622 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1623 Callee = DAG.getLoad(getPointerTy(), dl,
1624 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001625 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001626 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001627 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1628 const char *Sym = S->getSymbol();
1629
1630 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001631 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001632 ARMConstantPoolValue *CPV =
1633 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1634 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001635 // Get the address of the callee into a register
1636 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1637 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1638 Callee = DAG.getLoad(getPointerTy(), dl,
1639 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001640 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001641 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001642 }
1643 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001644 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001645 isDirect = true;
Chris Lattner55452c22009-07-15 04:12:33 +00001646 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Tim Northoverd6a729b2014-01-06 14:28:05 +00001647 bool isStub = (isExt && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001648 getTargetMachine().getRelocationModel() != Reloc::Static;
1649 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc3c949b42007-06-19 21:05:09 +00001650 // ARM call to a local ARM function is predicable.
Evan Chengf128bdc2010-06-16 07:35:02 +00001651 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001652 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001653 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001654 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Tim Northover72360d22013-12-02 10:35:41 +00001655 Callee = DAG.getNode(ARMISD::WrapperPIC, dl, getPointerTy(),
1656 DAG.getTargetGlobalAddress(GV, dl, getPointerTy()));
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001657 } else {
1658 // On ELF targets for PIC code, direct calls should go through the PLT
1659 unsigned OpFlags = 0;
1660 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001661 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001662 OpFlags = ARMII::MO_PLT;
1663 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1664 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001665 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001666 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001667 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001668 getTargetMachine().getRelocationModel() != Reloc::Static;
1669 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng83f35172007-01-30 20:37:08 +00001670 // tBX takes a register source operand.
1671 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001672 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001673 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001674 ARMConstantPoolValue *CPV =
1675 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1676 ARMPCLabelIndex, 4);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001677 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001678 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen021052a2009-02-04 20:06:27 +00001679 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Chengcdbb70c2009-10-31 03:39:36 +00001680 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001681 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001682 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00001683 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson7117a912009-03-20 22:42:55 +00001684 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen021052a2009-02-04 20:06:27 +00001685 getPointerTy(), Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001686 } else {
1687 unsigned OpFlags = 0;
1688 // On ELF targets for PIC code, direct calls should go through the PLT
1689 if (Subtarget->isTargetELF() &&
1690 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1691 OpFlags = ARMII::MO_PLT;
1692 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1693 }
Evan Cheng10043e22007-01-19 07:51:42 +00001694 }
1695
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001696 // FIXME: handle tail calls differently.
1697 unsigned CallOpc;
Tim Northoverdee86042013-12-02 14:46:26 +00001698 bool HasMinSizeAttr = Subtarget->isMinSize();
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001699 if (Subtarget->isThumb()) {
1700 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001701 CallOpc = ARMISD::CALL_NOLINK;
1702 else
1703 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1704 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001705 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001706 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001707 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet8e1fe842012-11-02 21:32:17 +00001708 // Emit regular call when code size is the priority
1709 !HasMinSizeAttr)
Evan Cheng65f9d192012-02-28 18:51:51 +00001710 // "mov lr, pc; b _foo" to avoid confusing the RSP
1711 CallOpc = ARMISD::CALL_NOLINK;
1712 else
1713 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001714 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001715
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001716 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001717 Ops.push_back(Chain);
1718 Ops.push_back(Callee);
1719
1720 // Add argument registers to the end of the list so that they are known live
1721 // into the call.
1722 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1723 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1724 RegsToPass[i].second.getValueType()));
1725
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001726 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001727 if (!isTailCall) {
1728 const uint32_t *Mask;
1729 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1730 const ARMBaseRegisterInfo *ARI = static_cast<const ARMBaseRegisterInfo*>(TRI);
1731 if (isThisReturn) {
1732 // For 'this' returns, use the R0-preserving mask if applicable
1733 Mask = ARI->getThisReturnPreservedMask(CallConv);
1734 if (!Mask) {
1735 // Set isThisReturn to false if the calling convention is not one that
1736 // allows 'returned' to be modeled in this way, so LowerCallResult does
1737 // not try to pass 'this' straight through
1738 isThisReturn = false;
1739 Mask = ARI->getCallPreservedMask(CallConv);
1740 }
1741 } else
Stephen Linff7fcee2013-06-26 21:42:14 +00001742 Mask = ARI->getCallPreservedMask(CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001743
Matthias Braunc22630e2013-10-04 16:52:54 +00001744 assert(Mask && "Missing call preserved mask for calling convention");
1745 Ops.push_back(DAG.getRegisterMask(Mask));
1746 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001747
Gabor Greiff304a7a2008-08-28 21:40:38 +00001748 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001749 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001750
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001751 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001752 if (isTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001753 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Dale Johannesend679ff72010-06-03 21:09:53 +00001754
Duncan Sands739a0542008-07-02 17:40:58 +00001755 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001756 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001757 InFlag = Chain.getValue(1);
1758
Chris Lattner27539552008-10-11 22:08:30 +00001759 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001760 DAG.getIntPtrConstant(0, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001761 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001762 InFlag = Chain.getValue(1);
1763
Bob Wilsona4c22902009-04-17 19:07:39 +00001764 // Handle result values, copying them out of physregs into vregs that we
1765 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001766 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001767 InVals, isThisReturn,
1768 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001769}
1770
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001771/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001772/// on the stack. Remember the next parameter register to allocate,
1773/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001774/// this.
1775void
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001776ARMTargetLowering::HandleByVal(
1777 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001778 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1779 assert((State->getCallOrPrologue() == Prologue ||
1780 State->getCallOrPrologue() == Call) &&
1781 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001782
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001783 if ((ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001784 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1785 unsigned AlignInRegs = Align / 4;
1786 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1787 for (unsigned i = 0; i < Waste; ++i)
1788 reg = State->AllocateReg(GPRArgRegs, 4);
1789 }
1790 if (reg != 0) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001791 unsigned excess = 4 * (ARM::R4 - reg);
1792
1793 // Special case when NSAA != SP and parameter size greater than size of
1794 // all remained GPR regs. In that case we can't split parameter, we must
1795 // send it to stack. We also must set NCRN to R4, so waste all
1796 // remained registers.
Oliver Stannardd55e1152014-03-05 15:25:27 +00001797 const unsigned NSAAOffset = State->getNextStackOffset();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001798 if (Subtarget->isAAPCS_ABI() && NSAAOffset != 0 && size > excess) {
1799 while (State->AllocateReg(GPRArgRegs, 4))
1800 ;
1801 return;
1802 }
1803
1804 // First register for byval parameter is the first register that wasn't
1805 // allocated before this method call, so it would be "reg".
1806 // If parameter is small enough to be saved in range [reg, r4), then
1807 // the end (first after last) register would be reg + param-size-in-regs,
1808 // else parameter would be splitted between registers and stack,
1809 // end register would be r4 in this case.
1810 unsigned ByValRegBegin = reg;
Stepan Dyatkovskiy2703bca2013-05-08 14:51:27 +00001811 unsigned ByValRegEnd = (size < excess) ? reg + size/4 : (unsigned)ARM::R4;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001812 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1813 // Note, first register is allocated in the beginning of function already,
1814 // allocate remained amount of registers we need.
1815 for (unsigned i = reg+1; i != ByValRegEnd; ++i)
1816 State->AllocateReg(GPRArgRegs, 4);
Oliver Stannardd55e1152014-03-05 15:25:27 +00001817 // A byval parameter that is split between registers and memory needs its
1818 // size truncated here.
1819 // In the case where the entire structure fits in registers, we set the
1820 // size in memory to zero.
1821 if (size < excess)
1822 size = 0;
1823 else
1824 size -= excess;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001825 }
1826 }
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001827}
1828
Dale Johannesend679ff72010-06-03 21:09:53 +00001829/// MatchingStackOffset - Return true if the given stack call argument is
1830/// already available in the same position (relatively) of the caller's
1831/// incoming argument stack.
1832static
1833bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1834 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00001835 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001836 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1837 int FI = INT_MAX;
1838 if (Arg.getOpcode() == ISD::CopyFromReg) {
1839 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001840 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00001841 return false;
1842 MachineInstr *Def = MRI->getVRegDef(VR);
1843 if (!Def)
1844 return false;
1845 if (!Flags.isByVal()) {
1846 if (!TII->isLoadFromStackSlot(Def, FI))
1847 return false;
1848 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00001849 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00001850 }
1851 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1852 if (Flags.isByVal())
1853 // ByVal argument is passed in as a pointer but it's now being
1854 // dereferenced. e.g.
1855 // define @foo(%struct.X* %A) {
1856 // tail call @bar(%struct.X* byval %A)
1857 // }
1858 return false;
1859 SDValue Ptr = Ld->getBasePtr();
1860 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1861 if (!FINode)
1862 return false;
1863 FI = FINode->getIndex();
1864 } else
1865 return false;
1866
1867 assert(FI != INT_MAX);
1868 if (!MFI->isFixedObjectIndex(FI))
1869 return false;
1870 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1871}
1872
1873/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1874/// for tail call optimization. Targets which want to do tail call
1875/// optimization should implement this function.
1876bool
1877ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1878 CallingConv::ID CalleeCC,
1879 bool isVarArg,
1880 bool isCalleeStructRet,
1881 bool isCallerStructRet,
1882 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001883 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00001884 const SmallVectorImpl<ISD::InputArg> &Ins,
1885 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00001886 const Function *CallerF = DAG.getMachineFunction().getFunction();
1887 CallingConv::ID CallerCC = CallerF->getCallingConv();
1888 bool CCMatch = CallerCC == CalleeCC;
1889
1890 // Look for obvious safe cases to perform tail call optimization that do not
1891 // require ABI changes. This is what gcc calls sibcall.
1892
Jim Grosbache3864cc2010-06-16 23:45:49 +00001893 // Do not sibcall optimize vararg calls unless the call site is not passing
1894 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00001895 if (isVarArg && !Outs.empty())
1896 return false;
1897
Tim Northoverd8407452013-10-01 14:33:28 +00001898 // Exception-handling functions need a special set of instructions to indicate
1899 // a return to the hardware. Tail-calling another function would probably
1900 // break this.
1901 if (CallerF->hasFnAttribute("interrupt"))
1902 return false;
1903
Dale Johannesend679ff72010-06-03 21:09:53 +00001904 // Also avoid sibcall optimization if either caller or callee uses struct
1905 // return semantics.
1906 if (isCalleeStructRet || isCallerStructRet)
1907 return false;
1908
Dale Johannesend24c66b2010-06-23 18:52:34 +00001909 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach3840c902011-07-08 20:18:11 +00001910 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1911 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1912 // support in the assembler and linker to be used. This would need to be
1913 // fixed to fully support tail calls in Thumb1.
1914 //
Dale Johannesene2289282010-07-08 01:18:23 +00001915 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1916 // LR. This means if we need to reload LR, it takes an extra instructions,
1917 // which outweighs the value of the tail call; but here we don't know yet
1918 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach535d3b42010-09-08 03:54:02 +00001919 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesene2289282010-07-08 01:18:23 +00001920 // emitEpilogue if LR is used.
Dale Johannesene2289282010-07-08 01:18:23 +00001921
1922 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1923 // but we need to make sure there are enough registers; the only valid
1924 // registers are the 4 used for parameters. We don't currently do this
1925 // case.
Evan Chengd4b08732010-11-30 23:55:39 +00001926 if (Subtarget->isThumb1Only())
1927 return false;
Dale Johannesen3ac52b32010-06-18 18:13:11 +00001928
Dale Johannesend679ff72010-06-03 21:09:53 +00001929 // If the calling conventions do not match, then we'd better make sure the
1930 // results are returned in the same way as what the caller expects.
1931 if (!CCMatch) {
1932 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwarich89019782011-06-10 20:59:24 +00001933 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1934 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001935 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1936
1937 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwarich89019782011-06-10 20:59:24 +00001938 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1939 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001940 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1941
1942 if (RVLocs1.size() != RVLocs2.size())
1943 return false;
1944 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1945 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1946 return false;
1947 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1948 return false;
1949 if (RVLocs1[i].isRegLoc()) {
1950 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1951 return false;
1952 } else {
1953 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1954 return false;
1955 }
1956 }
1957 }
1958
Manman Ren7e48b252012-10-12 23:39:43 +00001959 // If Caller's vararg or byval argument has been split between registers and
1960 // stack, do not perform tail call, since part of the argument is in caller's
1961 // local frame.
1962 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
1963 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001964 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00001965 return false;
1966
Dale Johannesend679ff72010-06-03 21:09:53 +00001967 // If the callee takes no arguments then go on to check the results of the
1968 // call.
1969 if (!Outs.empty()) {
1970 // Check if stack adjustment is needed. For now, do not do this if any
1971 // argument is passed on the stack.
1972 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001973 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1974 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001975 CCInfo.AnalyzeCallOperands(Outs,
1976 CCAssignFnForNode(CalleeCC, false, isVarArg));
1977 if (CCInfo.getNextStackOffset()) {
1978 MachineFunction &MF = DAG.getMachineFunction();
1979
1980 // Check if the arguments are already laid out in the right way as
1981 // the caller's fixed stack objects.
1982 MachineFrameInfo *MFI = MF.getFrameInfo();
1983 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topper07720d82012-03-25 23:49:58 +00001984 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001985 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1986 i != e;
1987 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001988 CCValAssign &VA = ArgLocs[i];
1989 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001990 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001991 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00001992 if (VA.getLocInfo() == CCValAssign::Indirect)
1993 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001994 if (VA.needsCustom()) {
1995 // f64 and vector types are split into multiple registers or
1996 // register/stack-slot combinations. The types will not match
1997 // the registers; give up on memory f64 refs until we figure
1998 // out what to do about this.
1999 if (!VA.isRegLoc())
2000 return false;
2001 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00002002 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00002003 if (RegVT == MVT::v2f64) {
2004 if (!ArgLocs[++i].isRegLoc())
2005 return false;
2006 if (!ArgLocs[++i].isRegLoc())
2007 return false;
2008 }
2009 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002010 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2011 MFI, MRI, TII))
2012 return false;
2013 }
2014 }
2015 }
2016 }
2017
2018 return true;
2019}
2020
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002021bool
2022ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2023 MachineFunction &MF, bool isVarArg,
2024 const SmallVectorImpl<ISD::OutputArg> &Outs,
2025 LLVMContext &Context) const {
2026 SmallVector<CCValAssign, 16> RVLocs;
2027 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context);
2028 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2029 isVarArg));
2030}
2031
Tim Northoverd8407452013-10-01 14:33:28 +00002032static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2033 SDLoc DL, SelectionDAG &DAG) {
2034 const MachineFunction &MF = DAG.getMachineFunction();
2035 const Function *F = MF.getFunction();
2036
2037 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2038
2039 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2040 // version of the "preferred return address". These offsets affect the return
2041 // instruction if this is a return from PL1 without hypervisor extensions.
2042 // IRQ/FIQ: +4 "subs pc, lr, #4"
2043 // SWI: 0 "subs pc, lr, #0"
2044 // ABORT: +4 "subs pc, lr, #4"
2045 // UNDEF: +4/+2 "subs pc, lr, #0"
2046 // UNDEF varies depending on where the exception came from ARM or Thumb
2047 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2048
2049 int64_t LROffset;
2050 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2051 IntKind == "ABORT")
2052 LROffset = 4;
2053 else if (IntKind == "SWI" || IntKind == "UNDEF")
2054 LROffset = 0;
2055 else
2056 report_fatal_error("Unsupported interrupt attribute. If present, value "
2057 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2058
2059 RetOps.insert(RetOps.begin() + 1, DAG.getConstant(LROffset, MVT::i32, false));
2060
Craig Topper48d114b2014-04-26 18:35:24 +00002061 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002062}
2063
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002064SDValue
2065ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002066 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002067 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002068 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002069 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002070
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002071 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002072 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002073
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002074 // CCState - Info about the registers and stack slots.
Cameron Zwarich89019782011-06-10 20:59:24 +00002075 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2076 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002077
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002078 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002079 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2080 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002081
Bob Wilsona4c22902009-04-17 19:07:39 +00002082 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002083 SmallVector<SDValue, 4> RetOps;
2084 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002085 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002086
2087 // Copy the result values into the output registers.
2088 for (unsigned i = 0, realRVLocIdx = 0;
2089 i != RVLocs.size();
2090 ++i, ++realRVLocIdx) {
2091 CCValAssign &VA = RVLocs[i];
2092 assert(VA.isRegLoc() && "Can only return in registers!");
2093
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002094 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002095
2096 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002097 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002098 case CCValAssign::Full: break;
2099 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002100 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002101 break;
2102 }
2103
Bob Wilsona4c22902009-04-17 19:07:39 +00002104 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002105 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002106 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002107 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2108 DAG.getConstant(0, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002109 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002110 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002111
Christian Pirkerb5728192014-05-08 14:06:24 +00002112 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2113 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2114 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002115 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002116 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002117 VA = RVLocs[++i]; // skip ahead to next loc
2118 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002119 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2120 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002121 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002122 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002123 VA = RVLocs[++i]; // skip ahead to next loc
2124
2125 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002126 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2127 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002128 }
2129 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2130 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002131 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002132 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002133 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2134 fmrrd.getValue(isLittleEndian ? 0 : 1),
2135 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002136 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002137 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002138 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002139 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2140 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002141 Flag);
2142 } else
2143 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2144
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002145 // Guarantee that all emitted copies are
2146 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002147 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002148 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002149 }
2150
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002151 // Update chain and glue.
2152 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002153 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002154 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002155
Tim Northoverd8407452013-10-01 14:33:28 +00002156 // CPUs which aren't M-class use a special sequence to return from
2157 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2158 // though we use "subs pc, lr, #N").
2159 //
2160 // M-class CPUs actually use a normal return sequence with a special
2161 // (hardware-provided) value in LR, so the normal code path works.
2162 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2163 !Subtarget->isMClass()) {
2164 if (Subtarget->isThumb1Only())
2165 report_fatal_error("interrupt attribute is not supported in Thumb1");
2166 return LowerInterruptReturn(RetOps, dl, DAG);
2167 }
2168
Craig Topper48d114b2014-04-26 18:35:24 +00002169 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002170}
2171
Evan Chengf8bad082012-04-10 01:51:00 +00002172bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002173 if (N->getNumValues() != 1)
2174 return false;
2175 if (!N->hasNUsesOfValue(1, 0))
2176 return false;
2177
Evan Chengf8bad082012-04-10 01:51:00 +00002178 SDValue TCChain = Chain;
2179 SDNode *Copy = *N->use_begin();
2180 if (Copy->getOpcode() == ISD::CopyToReg) {
2181 // If the copy has a glue operand, we conservatively assume it isn't safe to
2182 // perform a tail call.
2183 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2184 return false;
2185 TCChain = Copy->getOperand(0);
2186 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2187 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002188 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002189 SmallPtrSet<SDNode*, 2> Copies;
2190 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002191 UI != UE; ++UI) {
2192 if (UI->getOpcode() != ISD::CopyToReg)
2193 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002194 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002195 }
Evan Chengf8bad082012-04-10 01:51:00 +00002196 if (Copies.size() > 2)
2197 return false;
2198
2199 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2200 UI != UE; ++UI) {
2201 SDValue UseChain = UI->getOperand(0);
2202 if (Copies.count(UseChain.getNode()))
2203 // Second CopyToReg
2204 Copy = *UI;
2205 else
2206 // First CopyToReg
2207 TCChain = UseChain;
2208 }
2209 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002210 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002211 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002212 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002213 Copy = *Copy->use_begin();
2214 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002215 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002216 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002217 } else {
2218 return false;
2219 }
2220
Evan Cheng419ea282010-12-01 22:59:46 +00002221 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002222 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2223 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002224 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2225 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002226 return false;
2227 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002228 }
2229
Evan Chengf8bad082012-04-10 01:51:00 +00002230 if (!HasRet)
2231 return false;
2232
2233 Chain = TCChain;
2234 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002235}
2236
Evan Cheng0663f232011-03-21 01:19:09 +00002237bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002238 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002239 return false;
2240
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00002241 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng0663f232011-03-21 01:19:09 +00002242 return false;
2243
2244 return !Subtarget->isThumb1Only();
2245}
2246
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002247// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2248// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2249// one of the above mentioned nodes. It has to be wrapped because otherwise
2250// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2251// be used to form addressing mode. These wrapped nodes will be selected
2252// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002253static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002254 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002255 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002256 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002257 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002258 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002259 if (CP->isMachineConstantPoolEntry())
2260 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2261 CP->getAlignment());
2262 else
2263 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2264 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002265 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002266}
2267
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002268unsigned ARMTargetLowering::getJumpTableEncoding() const {
2269 return MachineJumpTableInfo::EK_Inline;
2270}
2271
Dan Gohman21cea8a2010-04-17 15:26:15 +00002272SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2273 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002274 MachineFunction &MF = DAG.getMachineFunction();
2275 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2276 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002277 SDLoc DL(Op);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002278 EVT PtrVT = getPointerTy();
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002279 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002280 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2281 SDValue CPAddr;
2282 if (RelocM == Reloc::Static) {
2283 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2284 } else {
2285 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002286 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002287 ARMConstantPoolValue *CPV =
2288 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2289 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002290 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2291 }
2292 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2293 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002294 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002295 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002296 if (RelocM == Reloc::Static)
2297 return Result;
Evan Cheng408aa562009-11-06 22:24:13 +00002298 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002299 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002300}
2301
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002302// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002303SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002304ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002305 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002306 SDLoc dl(GA);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002307 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002308 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002309 MachineFunction &MF = DAG.getMachineFunction();
2310 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002311 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002312 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002313 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2314 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002315 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002316 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002317 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattner7727d052010-09-21 06:44:06 +00002318 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002319 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002320 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002321
Evan Cheng408aa562009-11-06 22:24:13 +00002322 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002323 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002324
2325 // call __tls_get_addr.
2326 ArgListTy Args;
2327 ArgListEntry Entry;
2328 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002329 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002330 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002331
Dale Johannesen555a3752009-01-30 23:10:59 +00002332 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002333 TargetLowering::CallLoweringInfo CLI(DAG);
2334 CLI.setDebugLoc(dl).setChain(Chain)
2335 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
2336 DAG.getExternalSymbol("__tls_get_addr", PtrVT), &Args, 0);
2337
Justin Holewinskiaa583972012-05-25 16:35:28 +00002338 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002339 return CallResult.first;
2340}
2341
2342// Lower ISD::GlobalTLSAddress using the "initial exec" or
2343// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002344SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002345ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002346 SelectionDAG &DAG,
2347 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002348 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002349 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002350 SDValue Offset;
2351 SDValue Chain = DAG.getEntryNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002352 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002353 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002354 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002355
Hans Wennborgaea41202012-05-04 09:40:39 +00002356 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002357 MachineFunction &MF = DAG.getMachineFunction();
2358 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002359 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002360 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002361 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2362 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002363 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2364 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2365 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002366 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002367 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002368 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002369 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002370 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002371 Chain = Offset.getValue(1);
2372
Evan Cheng408aa562009-11-06 22:24:13 +00002373 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002374 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002375
Evan Chengcdbb70c2009-10-31 03:39:36 +00002376 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002377 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002378 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002379 } else {
2380 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002381 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002382 ARMConstantPoolValue *CPV =
2383 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002384 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002385 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002386 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002387 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002388 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002389 }
2390
2391 // The address of the thread local variable is the add of the thread
2392 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002393 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002394}
2395
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002396SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002397ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002398 // TODO: implement the "local dynamic" model
2399 assert(Subtarget->isTargetELF() &&
2400 "TLS not implemented for non-ELF targets");
2401 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgaea41202012-05-04 09:40:39 +00002402
2403 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2404
2405 switch (model) {
2406 case TLSModel::GeneralDynamic:
2407 case TLSModel::LocalDynamic:
2408 return LowerToTLSGeneralDynamicModel(GA, DAG);
2409 case TLSModel::InitialExec:
2410 case TLSModel::LocalExec:
2411 return LowerToTLSExecModels(GA, DAG, model);
2412 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002413 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002414}
2415
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002416SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002417 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002418 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002419 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002420 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002421 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002422 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002423 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002424 ARMConstantPoolConstant::Create(GV,
2425 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002426 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002427 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson7117a912009-03-20 22:42:55 +00002428 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002429 CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002430 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002431 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002432 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002433 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002434 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002435 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002436 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002437 MachinePointerInfo::getGOT(),
2438 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002439 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002440 }
2441
2442 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002443 // pair. This is always cheaper.
2444 if (Subtarget->useMovt()) {
Evan Cheng68aec142011-01-19 02:16:49 +00002445 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002446 // FIXME: Once remat is capable of dealing with instructions with register
2447 // operands, expand this into two nodes.
2448 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2449 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002450 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002451 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2452 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2453 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2454 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002455 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002456 }
2457}
2458
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002459SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002460 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002461 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002462 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002463 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002464 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002465
Tim Northover72360d22013-12-02 10:35:41 +00002466 if (Subtarget->useMovt())
Evan Cheng68aec142011-01-19 02:16:49 +00002467 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002468
Tim Northover72360d22013-12-02 10:35:41 +00002469 // FIXME: Once remat is capable of dealing with instructions with register
2470 // operands, expand this into multiple nodes
2471 unsigned Wrapper =
2472 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002473
Tim Northover72360d22013-12-02 10:35:41 +00002474 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2475 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002476
Evan Cheng1b389522009-09-03 07:04:02 +00002477 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002478 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2479 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002480 return Result;
2481}
2482
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002483SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2484 SelectionDAG &DAG) const {
2485 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
2486 assert(Subtarget->useMovt() && "Windows on ARM expects to use movw/movt");
2487
2488 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2489 EVT PtrVT = getPointerTy();
2490 SDLoc DL(Op);
2491
2492 ++NumMovwMovt;
2493
2494 // FIXME: Once remat is capable of dealing with instructions with register
2495 // operands, expand this into two nodes.
2496 return DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2497 DAG.getTargetGlobalAddress(GV, DL, PtrVT));
2498}
2499
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002500SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002501 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002502 assert(Subtarget->isTargetELF() &&
2503 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002504 MachineFunction &MF = DAG.getMachineFunction();
2505 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002506 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002507 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002508 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002509 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002510 ARMConstantPoolValue *CPV =
2511 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2512 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002513 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002514 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002515 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002516 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002517 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00002518 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002519 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002520}
2521
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002522SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002523ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002524 SDLoc dl(Op);
Jim Grosbachfaa3abb2010-05-27 23:49:24 +00002525 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002526 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2527 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002528 Op.getOperand(1), Val);
2529}
2530
2531SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002532ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002533 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002534 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2535 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2536}
2537
2538SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002539ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002540 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002541 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002542 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002543 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002544 default: return SDValue(); // Don't custom lower most intrinsics.
Jim Grosbach07393ba2014-06-16 21:55:30 +00002545 case Intrinsic::arm_rbit: {
2546 assert(Op.getOperand(0).getValueType() == MVT::i32 &&
2547 "RBIT intrinsic must have i32 type!");
2548 return DAG.getNode(ARMISD::RBIT, dl, MVT::i32, Op.getOperand(0));
2549 }
Bob Wilson17f88782009-08-04 00:25:01 +00002550 case Intrinsic::arm_thread_pointer: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002551 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson17f88782009-08-04 00:25:01 +00002552 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2553 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002554 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002555 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002556 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002557 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002558 EVT PtrVT = getPointerTy();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002559 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2560 SDValue CPAddr;
2561 unsigned PCAdj = (RelocM != Reloc::PIC_)
2562 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002563 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002564 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2565 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002566 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002567 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002568 SDValue Result =
Evan Chengcdbb70c2009-10-31 03:39:36 +00002569 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002570 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002571 false, false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002572
2573 if (RelocM == Reloc::PIC_) {
Evan Cheng408aa562009-11-06 22:24:13 +00002574 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002575 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2576 }
2577 return Result;
2578 }
Evan Cheng18381b42011-03-29 23:06:19 +00002579 case Intrinsic::arm_neon_vmulls:
2580 case Intrinsic::arm_neon_vmullu: {
2581 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2582 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002583 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002584 Op.getOperand(1), Op.getOperand(2));
2585 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002586 }
2587}
2588
Eli Friedman30a49e92011-08-03 21:06:02 +00002589static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2590 const ARMSubtarget *Subtarget) {
2591 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002592 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002593 if (!Subtarget->hasDataBarrier()) {
2594 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2595 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2596 // here.
2597 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002598 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002599 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002600 DAG.getConstant(0, MVT::i32));
2601 }
2602
Tim Northover36b24172013-07-03 09:20:36 +00002603 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2604 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
2605 unsigned Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002606 if (Subtarget->isMClass()) {
2607 // Only a full system barrier exists in the M-class architectures.
2608 Domain = ARM_MB::SY;
2609 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002610 // Swift happens to implement ISHST barriers in a way that's compatible with
2611 // Release semantics but weaker than ISH so we'd be fools not to use
2612 // it. Beware: other processors probably don't!
2613 Domain = ARM_MB::ISHST;
2614 }
2615
Joey Gouly926d3f52013-09-05 15:35:24 +00002616 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
2617 DAG.getConstant(Intrinsic::arm_dmb, MVT::i32),
Tim Northover36b24172013-07-03 09:20:36 +00002618 DAG.getConstant(Domain, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002619}
2620
Evan Cheng8740ee32010-11-03 06:34:55 +00002621static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2622 const ARMSubtarget *Subtarget) {
2623 // ARM pre v5TE and Thumb1 does not have preload instructions.
2624 if (!(Subtarget->isThumb2() ||
2625 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2626 // Just preserve the chain.
2627 return Op.getOperand(0);
2628
Andrew Trickef9de2a2013-05-25 02:42:55 +00002629 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002630 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2631 if (!isRead &&
2632 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2633 // ARMv7 with MP extension has PLDW.
2634 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002635
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002636 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2637 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002638 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002639 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002640 isData = ~isData & 1;
2641 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002642
2643 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng21acf9f2010-11-04 05:19:35 +00002644 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2645 DAG.getConstant(isData, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002646}
2647
Dan Gohman31ae5862010-04-17 14:41:14 +00002648static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2649 MachineFunction &MF = DAG.getMachineFunction();
2650 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2651
Evan Cheng10043e22007-01-19 07:51:42 +00002652 // vastart just stores the address of the VarArgsFrameIndex slot into the
2653 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002654 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002655 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002656 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002657 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002658 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2659 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002660}
2661
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002662SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002663ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2664 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002665 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002666 MachineFunction &MF = DAG.getMachineFunction();
2667 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2668
Craig Topper760b1342012-02-22 05:59:10 +00002669 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002670 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002671 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002672 else
Craig Topperc7242e02012-04-20 07:30:17 +00002673 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002674
2675 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002676 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002677 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002678
2679 SDValue ArgValue2;
2680 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002681 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002682 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002683
2684 // Create load node to retrieve arguments from the stack.
2685 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Chengcdbb70c2009-10-31 03:39:36 +00002686 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002687 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002688 false, false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002689 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002690 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002691 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002692 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002693 if (!Subtarget->isLittle())
2694 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002695 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002696}
2697
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002698void
2699ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002700 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002701 unsigned ArgSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002702 unsigned &ArgRegsSize,
2703 unsigned &ArgRegsSaveSize)
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002704 const {
2705 unsigned NumGPRs;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002706 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2707 unsigned RBegin, REnd;
2708 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2709 NumGPRs = REnd - RBegin;
2710 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002711 unsigned int firstUnalloced;
2712 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2713 sizeof(GPRArgRegs) /
2714 sizeof(GPRArgRegs[0]));
2715 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2716 }
2717
2718 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002719 ArgRegsSize = NumGPRs * 4;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002720
2721 // If parameter is split between stack and GPRs...
Mark Seabornbe266aa2014-02-16 18:59:48 +00002722 if (NumGPRs && Align > 4 &&
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002723 (ArgRegsSize < ArgSize ||
2724 InRegsParamRecordIdx >= CCInfo.getInRegsParamsCount())) {
Mark Seabornbe266aa2014-02-16 18:59:48 +00002725 // Add padding for part of param recovered from GPRs. For example,
2726 // if Align == 8, its last byte must be at address K*8 - 1.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002727 // We need to do it, since remained (stack) part of parameter has
2728 // stack alignment, and we need to "attach" "GPRs head" without gaps
2729 // to it:
2730 // Stack:
2731 // |---- 8 bytes block ----| |---- 8 bytes block ----| |---- 8 bytes...
2732 // [ [padding] [GPRs head] ] [ Tail passed via stack ....
2733 //
2734 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2735 unsigned Padding =
Mark Seabornbe266aa2014-02-16 18:59:48 +00002736 OffsetToAlignment(ArgRegsSize + AFI->getArgRegsSaveSize(), Align);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002737 ArgRegsSaveSize = ArgRegsSize + Padding;
2738 } else
2739 // We don't need to extend regs save size for byval parameters if they
2740 // are passed via GPRs only.
2741 ArgRegsSaveSize = ArgRegsSize;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002742}
2743
2744// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002745// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002746// byval). Either way, we allocate stack slots adjacent to the data
2747// provided by our caller, and store the unallocated registers there.
2748// If this is a variadic function, the va_list pointer will begin with
2749// these values; otherwise, this reassembles a (byval) structure that
2750// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002751// Return: The frame index registers were stored into.
2752int
2753ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002754 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002755 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002756 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002757 unsigned OffsetFromOrigArg,
2758 unsigned ArgOffset,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002759 unsigned ArgSize,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002760 bool ForceMutable,
2761 unsigned ByValStoreOffset,
2762 unsigned TotalArgRegsSaveSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002763
2764 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002765 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002766 // Setup first unallocated register as first byval register;
2767 // eat all remained registers
2768 // (these two actions are performed by HandleByVal method).
2769 // Then, here, we initialize stack frame with
2770 // "store-reg" instructions.
2771 // Case #2. Var-args function, that doesn't contain byval parameters.
2772 // The same: eat all remained unallocated registers,
2773 // initialize stack frame.
2774
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002775 MachineFunction &MF = DAG.getMachineFunction();
2776 MachineFrameInfo *MFI = MF.getFrameInfo();
2777 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002778 unsigned firstRegToSaveIndex, lastRegToSaveIndex;
2779 unsigned RBegin, REnd;
2780 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2781 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2782 firstRegToSaveIndex = RBegin - ARM::R0;
2783 lastRegToSaveIndex = REnd - ARM::R0;
2784 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002785 firstRegToSaveIndex = CCInfo.getFirstUnallocated
Craig Topper58713212013-07-15 04:27:47 +00002786 (GPRArgRegs, array_lengthof(GPRArgRegs));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002787 lastRegToSaveIndex = 4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002788 }
2789
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002790 unsigned ArgRegsSize, ArgRegsSaveSize;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002791 computeRegArea(CCInfo, MF, InRegsParamRecordIdx, ArgSize,
2792 ArgRegsSize, ArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002793
2794 // Store any by-val regs to their spots on the stack so that they may be
2795 // loaded by deferencing the result of formal parameter pointer or va_next.
2796 // Note: once stack area for byval/varargs registers
2797 // was initialized, it can't be initialized again.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002798 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002799 unsigned Padding = ArgRegsSaveSize - ArgRegsSize;
2800
2801 if (Padding) {
2802 assert(AFI->getStoredByValParamsPadding() == 0 &&
2803 "The only parameter may be padded.");
2804 AFI->setStoredByValParamsPadding(Padding);
2805 }
2806
Oliver Stannardd55e1152014-03-05 15:25:27 +00002807 int FrameIndex = MFI->CreateFixedObject(ArgRegsSaveSize,
2808 Padding +
2809 ByValStoreOffset -
2810 (int64_t)TotalArgRegsSaveSize,
2811 false);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002812 SDValue FIN = DAG.getFrameIndex(FrameIndex, getPointerTy());
Oliver Stannardd55e1152014-03-05 15:25:27 +00002813 if (Padding) {
2814 MFI->CreateFixedObject(Padding,
2815 ArgOffset + ByValStoreOffset -
2816 (int64_t)ArgRegsSaveSize,
2817 false);
2818 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002819
2820 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002821 for (unsigned i = 0; firstRegToSaveIndex < lastRegToSaveIndex;
2822 ++firstRegToSaveIndex, ++i) {
Craig Topper760b1342012-02-22 05:59:10 +00002823 const TargetRegisterClass *RC;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002824 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002825 RC = &ARM::tGPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002826 else
Craig Topperc7242e02012-04-20 07:30:17 +00002827 RC = &ARM::GPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002828
2829 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2830 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2831 SDValue Store =
2832 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002833 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002834 false, false, 0);
2835 MemOps.push_back(Store);
2836 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2837 DAG.getConstant(4, getPointerTy()));
2838 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002839
2840 AFI->setArgRegsSaveSize(ArgRegsSaveSize + AFI->getArgRegsSaveSize());
2841
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002842 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002843 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002844 return FrameIndex;
Oliver Stannardd55e1152014-03-05 15:25:27 +00002845 } else {
2846 if (ArgSize == 0) {
2847 // We cannot allocate a zero-byte object for the first variadic argument,
2848 // so just make up a size.
2849 ArgSize = 4;
2850 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002851 // This will point to the next argument passed via stack.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002852 return MFI->CreateFixedObject(
Oliver Stannardd55e1152014-03-05 15:25:27 +00002853 ArgSize, ArgOffset, !ForceMutable);
2854 }
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002855}
2856
2857// Setup stack frame, the va_list pointer will start from.
2858void
2859ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002860 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002861 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002862 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002863 bool ForceMutable) const {
2864 MachineFunction &MF = DAG.getMachineFunction();
2865 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2866
2867 // Try to store any remaining integer argument regs
2868 // to their spots on the stack so that they may be loaded by deferencing
2869 // the result of va_next.
2870 // If there is no regs to be stored, just point address after last
2871 // argument passed via stack.
2872 int FrameIndex =
Craig Topper062a2ba2014-04-25 05:30:21 +00002873 StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
2874 CCInfo.getInRegsParamsCount(), 0, ArgOffset, 0, ForceMutable,
2875 0, TotalArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002876
2877 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002878}
2879
Bob Wilson2e076c42009-06-22 23:27:02 +00002880SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002881ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002882 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002883 const SmallVectorImpl<ISD::InputArg>
2884 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002885 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002886 SmallVectorImpl<SDValue> &InVals)
2887 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00002888 MachineFunction &MF = DAG.getMachineFunction();
2889 MachineFrameInfo *MFI = MF.getFrameInfo();
2890
Bob Wilsona4c22902009-04-17 19:07:39 +00002891 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2892
2893 // Assign locations to all of the incoming arguments.
2894 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00002895 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2896 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002897 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002898 CCAssignFnForNode(CallConv, /* Return*/ false,
2899 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00002900
Bob Wilsona4c22902009-04-17 19:07:39 +00002901 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002902 int lastInsIndex = -1;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002903 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002904 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2905 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002906
2907 // Initially ArgRegsSaveSize is zero.
2908 // Then we increase this value each time we meet byval parameter.
2909 // We also increase this value in case of varargs function.
2910 AFI->setArgRegsSaveSize(0);
2911
Oliver Stannardd55e1152014-03-05 15:25:27 +00002912 unsigned ByValStoreOffset = 0;
2913 unsigned TotalArgRegsSaveSize = 0;
2914 unsigned ArgRegsSaveSizeMaxAlign = 4;
2915
2916 // Calculate the amount of stack space that we need to allocate to store
2917 // byval and variadic arguments that are passed in registers.
2918 // We need to know this before we allocate the first byval or variadic
2919 // argument, as they will be allocated a stack slot below the CFA (Canonical
2920 // Frame Address, the stack pointer at entry to the function).
2921 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2922 CCValAssign &VA = ArgLocs[i];
2923 if (VA.isMemLoc()) {
2924 int index = VA.getValNo();
2925 if (index != lastInsIndex) {
2926 ISD::ArgFlagsTy Flags = Ins[index].Flags;
2927 if (Flags.isByVal()) {
2928 unsigned ExtraArgRegsSize;
2929 unsigned ExtraArgRegsSaveSize;
2930 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsProceed(),
2931 Flags.getByValSize(),
2932 ExtraArgRegsSize, ExtraArgRegsSaveSize);
2933
2934 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
2935 if (Flags.getByValAlign() > ArgRegsSaveSizeMaxAlign)
2936 ArgRegsSaveSizeMaxAlign = Flags.getByValAlign();
2937 CCInfo.nextInRegsParam();
2938 }
2939 lastInsIndex = index;
2940 }
2941 }
2942 }
2943 CCInfo.rewindByValRegsInfo();
2944 lastInsIndex = -1;
2945 if (isVarArg) {
2946 unsigned ExtraArgRegsSize;
2947 unsigned ExtraArgRegsSaveSize;
2948 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsCount(), 0,
2949 ExtraArgRegsSize, ExtraArgRegsSaveSize);
2950 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
2951 }
2952 // If the arg regs save area contains N-byte aligned values, the
2953 // bottom of it must be at least N-byte aligned.
2954 TotalArgRegsSaveSize = RoundUpToAlignment(TotalArgRegsSaveSize, ArgRegsSaveSizeMaxAlign);
2955 TotalArgRegsSaveSize = std::min(TotalArgRegsSaveSize, 16U);
2956
Bob Wilsona4c22902009-04-17 19:07:39 +00002957 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2958 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002959 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
2960 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002961 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00002962 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002963 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00002964
Bob Wilsona4c22902009-04-17 19:07:39 +00002965 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002966 // f64 and vector types are split up into multiple registers or
2967 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00002968 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002969 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002970 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00002971 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00002972 SDValue ArgValue2;
2973 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00002974 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson699bdf72010-04-13 22:03:22 +00002975 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2976 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002977 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002978 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00002979 } else {
2980 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2981 Chain, DAG, dl);
2982 }
Owen Anderson9f944592009-08-11 20:47:22 +00002983 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2984 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00002985 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson9f944592009-08-11 20:47:22 +00002986 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00002987 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2988 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002989 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00002990
Bob Wilson2e076c42009-06-22 23:27:02 +00002991 } else {
Craig Topper760b1342012-02-22 05:59:10 +00002992 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002993
Owen Anderson9f944592009-08-11 20:47:22 +00002994 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00002995 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00002996 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00002997 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00002998 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00002999 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00003000 else if (RegVT == MVT::i32)
Craig Topperc7242e02012-04-20 07:30:17 +00003001 RC = AFI->isThumb1OnlyFunction() ?
3002 (const TargetRegisterClass*)&ARM::tGPRRegClass :
3003 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00003004 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00003005 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00003006
3007 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00003008 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003009 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00003010 }
3011
3012 // If this is an 8 or 16-bit value, it is really passed promoted
3013 // to 32 bits. Insert an assert[sz]ext to capture this, then
3014 // truncate to the right size.
3015 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003016 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003017 case CCValAssign::Full: break;
3018 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003019 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003020 break;
3021 case CCValAssign::SExt:
3022 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3023 DAG.getValueType(VA.getValVT()));
3024 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3025 break;
3026 case CCValAssign::ZExt:
3027 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3028 DAG.getValueType(VA.getValVT()));
3029 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3030 break;
3031 }
3032
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003033 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003034
3035 } else { // VA.isRegLoc()
3036
3037 // sanity check
3038 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003039 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003040
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003041 int index = ArgLocs[i].getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003042
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003043 // Some Ins[] entries become multiple ArgLoc[] entries.
3044 // Process them only once.
3045 if (index != lastInsIndex)
3046 {
3047 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003048 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003049 // This can be changed with more analysis.
3050 // In case of tail call optimization mark all arguments mutable.
3051 // Since they could be overwritten by lowering of arguments in case of
3052 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003053 if (Flags.isByVal()) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003054 unsigned CurByValIndex = CCInfo.getInRegsParamsProceed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003055
3056 ByValStoreOffset = RoundUpToAlignment(ByValStoreOffset, Flags.getByValAlign());
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003057 int FrameIndex = StoreByValRegs(
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003058 CCInfo, DAG, dl, Chain, CurOrigArg,
3059 CurByValIndex,
3060 Ins[VA.getValNo()].PartOffset,
3061 VA.getLocMemOffset(),
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003062 Flags.getByValSize(),
Oliver Stannardd55e1152014-03-05 15:25:27 +00003063 true /*force mutable frames*/,
3064 ByValStoreOffset,
3065 TotalArgRegsSaveSize);
3066 ByValStoreOffset += Flags.getByValSize();
3067 ByValStoreOffset = std::min(ByValStoreOffset, 16U);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003068 InVals.push_back(DAG.getFrameIndex(FrameIndex, getPointerTy()));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003069 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003070 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003071 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003072 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003073 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003074
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003075 // Create load nodes to retrieve arguments from the stack.
3076 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3077 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3078 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003079 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003080 }
3081 lastInsIndex = index;
3082 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003083 }
3084 }
3085
3086 // varargs
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003087 if (isVarArg)
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003088 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003089 CCInfo.getNextStackOffset(),
3090 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003091
Oliver Stannardb14c6252014-04-02 16:10:33 +00003092 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3093
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003094 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003095}
3096
3097/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003098static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003099 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003100 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003101 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003102 // Maybe this has already been legalized into the constant pool?
3103 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003104 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003105 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003106 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003107 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003108 }
3109 }
3110 return false;
3111}
3112
Evan Cheng10043e22007-01-19 07:51:42 +00003113/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3114/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003115SDValue
3116ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003117 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003118 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003119 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003120 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003121 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003122 // Constant does not fit, try adjusting it by one?
3123 switch (CC) {
3124 default: break;
3125 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003126 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003127 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003128 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003129 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003130 }
3131 break;
3132 case ISD::SETULT:
3133 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003134 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003135 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003136 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003137 }
3138 break;
3139 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003140 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003141 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003142 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003143 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003144 }
3145 break;
3146 case ISD::SETULE:
3147 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003148 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003149 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003150 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003151 }
3152 break;
3153 }
3154 }
3155 }
3156
3157 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003158 ARMISD::NodeType CompareType;
3159 switch (CondCode) {
3160 default:
3161 CompareType = ARMISD::CMP;
3162 break;
3163 case ARMCC::EQ:
3164 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003165 // Uses only Z Flag
3166 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003167 break;
3168 }
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003169 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003170 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003171}
3172
3173/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003174SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003175ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003176 SDLoc dl) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003177 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003178 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003179 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003180 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003181 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3182 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003183}
3184
Bob Wilson45acbd02011-03-08 01:17:20 +00003185/// duplicateCmp - Glue values can have only one use, so this function
3186/// duplicates a comparison node.
3187SDValue
3188ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3189 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003190 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003191 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3192 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3193
3194 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3195 Cmp = Cmp.getOperand(0);
3196 Opc = Cmp.getOpcode();
3197 if (Opc == ARMISD::CMPFP)
3198 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3199 else {
3200 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3201 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3202 }
3203 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3204}
3205
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003206std::pair<SDValue, SDValue>
3207ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3208 SDValue &ARMcc) const {
3209 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3210
3211 SDValue Value, OverflowCmp;
3212 SDValue LHS = Op.getOperand(0);
3213 SDValue RHS = Op.getOperand(1);
3214
3215
3216 // FIXME: We are currently always generating CMPs because we don't support
3217 // generating CMN through the backend. This is not as good as the natural
3218 // CMP case because it causes a register dependency and cannot be folded
3219 // later.
3220
3221 switch (Op.getOpcode()) {
3222 default:
3223 llvm_unreachable("Unknown overflow instruction!");
3224 case ISD::SADDO:
3225 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3226 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3227 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3228 break;
3229 case ISD::UADDO:
3230 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3231 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3232 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3233 break;
3234 case ISD::SSUBO:
3235 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3236 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3237 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3238 break;
3239 case ISD::USUBO:
3240 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3241 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3242 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3243 break;
3244 } // switch (...)
3245
3246 return std::make_pair(Value, OverflowCmp);
3247}
3248
3249
3250SDValue
3251ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3252 // Let legalize expand this if it isn't a legal type yet.
3253 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3254 return SDValue();
3255
3256 SDValue Value, OverflowCmp;
3257 SDValue ARMcc;
3258 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3259 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3260 // We use 0 and 1 as false and true values.
3261 SDValue TVal = DAG.getConstant(1, MVT::i32);
3262 SDValue FVal = DAG.getConstant(0, MVT::i32);
3263 EVT VT = Op.getValueType();
3264
3265 SDValue Overflow = DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, TVal, FVal,
3266 ARMcc, CCR, OverflowCmp);
3267
3268 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
3269 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), VTs, Value, Overflow);
3270}
3271
3272
Bill Wendling6a981312010-08-11 08:43:16 +00003273SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3274 SDValue Cond = Op.getOperand(0);
3275 SDValue SelectTrue = Op.getOperand(1);
3276 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003277 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003278 unsigned Opc = Cond.getOpcode();
3279
3280 if (Cond.getResNo() == 1 &&
3281 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3282 Opc == ISD::USUBO)) {
3283 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3284 return SDValue();
3285
3286 SDValue Value, OverflowCmp;
3287 SDValue ARMcc;
3288 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3289 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3290 EVT VT = Op.getValueType();
3291
3292 return DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, SelectTrue, SelectFalse,
3293 ARMcc, CCR, OverflowCmp);
3294
3295 }
Bill Wendling6a981312010-08-11 08:43:16 +00003296
3297 // Convert:
3298 //
3299 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3300 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3301 //
3302 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3303 const ConstantSDNode *CMOVTrue =
3304 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3305 const ConstantSDNode *CMOVFalse =
3306 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3307
3308 if (CMOVTrue && CMOVFalse) {
3309 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3310 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3311
3312 SDValue True;
3313 SDValue False;
3314 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3315 True = SelectTrue;
3316 False = SelectFalse;
3317 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3318 True = SelectFalse;
3319 False = SelectTrue;
3320 }
3321
3322 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003323 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003324 SDValue ARMcc = Cond.getOperand(2);
3325 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003326 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003327 assert(True.getValueType() == VT);
3328 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendling6a981312010-08-11 08:43:16 +00003329 }
3330 }
3331 }
3332
Dan Gohmand4a77c42012-02-24 00:09:36 +00003333 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3334 // undefined bits before doing a full-word comparison with zero.
3335 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
3336 DAG.getConstant(1, Cond.getValueType()));
3337
Bill Wendling6a981312010-08-11 08:43:16 +00003338 return DAG.getSelectCC(dl, Cond,
3339 DAG.getConstant(0, Cond.getValueType()),
3340 SelectTrue, SelectFalse, ISD::SETNE);
3341}
3342
Joey Gouly881eab52013-08-22 15:29:11 +00003343static ISD::CondCode getInverseCCForVSEL(ISD::CondCode CC) {
3344 if (CC == ISD::SETNE)
3345 return ISD::SETEQ;
Weiming Zhao63871d22013-12-18 22:25:17 +00003346 return ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003347}
3348
3349static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3350 bool &swpCmpOps, bool &swpVselOps) {
3351 // Start by selecting the GE condition code for opcodes that return true for
3352 // 'equality'
3353 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3354 CC == ISD::SETULE)
3355 CondCode = ARMCC::GE;
3356
3357 // and GT for opcodes that return false for 'equality'.
3358 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3359 CC == ISD::SETULT)
3360 CondCode = ARMCC::GT;
3361
3362 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3363 // to swap the compare operands.
3364 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3365 CC == ISD::SETULT)
3366 swpCmpOps = true;
3367
3368 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3369 // If we have an unordered opcode, we need to swap the operands to the VSEL
3370 // instruction (effectively negating the condition).
3371 //
3372 // This also has the effect of swapping which one of 'less' or 'greater'
3373 // returns true, so we also swap the compare operands. It also switches
3374 // whether we return true for 'equality', so we compensate by picking the
3375 // opposite condition code to our original choice.
3376 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3377 CC == ISD::SETUGT) {
3378 swpCmpOps = !swpCmpOps;
3379 swpVselOps = !swpVselOps;
3380 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3381 }
3382
3383 // 'ordered' is 'anything but unordered', so use the VS condition code and
3384 // swap the VSEL operands.
3385 if (CC == ISD::SETO) {
3386 CondCode = ARMCC::VS;
3387 swpVselOps = true;
3388 }
3389
3390 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3391 // code and swap the VSEL operands.
3392 if (CC == ISD::SETUNE) {
3393 CondCode = ARMCC::EQ;
3394 swpVselOps = true;
3395 }
3396}
3397
Dan Gohman21cea8a2010-04-17 15:26:15 +00003398SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003399 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003400 SDValue LHS = Op.getOperand(0);
3401 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003402 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003403 SDValue TrueVal = Op.getOperand(2);
3404 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003405 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003406
Owen Anderson9f944592009-08-11 20:47:22 +00003407 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003408 // Try to generate VSEL on ARMv8.
3409 // The VSEL instruction can't use all the usual ARM condition
3410 // codes: it only has two bits to select the condition code, so it's
3411 // constrained to use only GE, GT, VS and EQ.
3412 //
3413 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3414 // swap the operands of the previous compare instruction (effectively
3415 // inverting the compare condition, swapping 'less' and 'greater') and
3416 // sometimes need to swap the operands to the VSEL (which inverts the
3417 // condition in the sense of firing whenever the previous condition didn't)
Joey Goulyccd04892013-09-13 13:46:57 +00003418 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003419 TrueVal.getValueType() == MVT::f64)) {
3420 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3421 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3422 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
3423 CC = getInverseCCForVSEL(CC);
3424 std::swap(TrueVal, FalseVal);
3425 }
3426 }
3427
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003428 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003429 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003430 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Joey Gouly881eab52013-08-22 15:29:11 +00003431 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3432 Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003433 }
3434
3435 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003436 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003437
Joey Gouly881eab52013-08-22 15:29:11 +00003438 // Try to generate VSEL on ARMv8.
Joey Goulyccd04892013-09-13 13:46:57 +00003439 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003440 TrueVal.getValueType() == MVT::f64)) {
Joey Goulye3dd6842013-08-23 12:01:13 +00003441 // We can select VMAXNM/VMINNM from a compare followed by a select with the
3442 // same operands, as follows:
3443 // c = fcmp [ogt, olt, ugt, ult] a, b
3444 // select c, a, b
3445 // We only do this in unsafe-fp-math, because signed zeros and NaNs are
3446 // handled differently than the original code sequence.
3447 if (getTargetMachine().Options.UnsafeFPMath && LHS == TrueVal &&
3448 RHS == FalseVal) {
3449 if (CC == ISD::SETOGT || CC == ISD::SETUGT)
3450 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal);
3451 if (CC == ISD::SETOLT || CC == ISD::SETULT)
3452 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal);
3453 }
3454
Joey Gouly881eab52013-08-22 15:29:11 +00003455 bool swpCmpOps = false;
3456 bool swpVselOps = false;
3457 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3458
3459 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3460 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3461 if (swpCmpOps)
3462 std::swap(LHS, RHS);
3463 if (swpVselOps)
3464 std::swap(TrueVal, FalseVal);
3465 }
3466 }
3467
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003468 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3469 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003470 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00003471 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003472 ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003473 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003474 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003475 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003476 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson7117a912009-03-20 22:42:55 +00003477 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003478 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Cheng10043e22007-01-19 07:51:42 +00003479 }
3480 return Result;
3481}
3482
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003483/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3484/// to morph to an integer compare sequence.
3485static bool canChangeToInt(SDValue Op, bool &SeenZero,
3486 const ARMSubtarget *Subtarget) {
3487 SDNode *N = Op.getNode();
3488 if (!N->hasOneUse())
3489 // Otherwise it requires moving the value from fp to integer registers.
3490 return false;
3491 if (!N->getNumValues())
3492 return false;
3493 EVT VT = Op.getValueType();
3494 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3495 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3496 // vmrs are very slow, e.g. cortex-a8.
3497 return false;
3498
3499 if (isFloatingPointZero(Op)) {
3500 SeenZero = true;
3501 return true;
3502 }
3503 return ISD::isNormalLoad(N);
3504}
3505
3506static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3507 if (isFloatingPointZero(Op))
3508 return DAG.getConstant(0, MVT::i32);
3509
3510 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003511 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003512 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003513 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003514 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003515
3516 llvm_unreachable("Unknown VFP cmp argument!");
3517}
3518
3519static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3520 SDValue &RetVal1, SDValue &RetVal2) {
3521 if (isFloatingPointZero(Op)) {
3522 RetVal1 = DAG.getConstant(0, MVT::i32);
3523 RetVal2 = DAG.getConstant(0, MVT::i32);
3524 return;
3525 }
3526
3527 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3528 SDValue Ptr = Ld->getBasePtr();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003529 RetVal1 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003530 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003531 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003532 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003533 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003534
3535 EVT PtrType = Ptr.getValueType();
3536 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003537 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003538 PtrType, Ptr, DAG.getConstant(4, PtrType));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003539 RetVal2 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003540 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003541 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003542 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003543 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003544 return;
3545 }
3546
3547 llvm_unreachable("Unknown VFP cmp argument!");
3548}
3549
3550/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3551/// f32 and even f64 comparisons to integer ones.
3552SDValue
3553ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3554 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003555 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003556 SDValue LHS = Op.getOperand(2);
3557 SDValue RHS = Op.getOperand(3);
3558 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003559 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003560
Evan Chengd12af5d2012-03-01 23:27:13 +00003561 bool LHSSeenZero = false;
3562 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3563 bool RHSSeenZero = false;
3564 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3565 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003566 // If unsafe fp math optimization is enabled and there are no other uses of
3567 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003568 // to an integer comparison.
3569 if (CC == ISD::SETOEQ)
3570 CC = ISD::SETEQ;
3571 else if (CC == ISD::SETUNE)
3572 CC = ISD::SETNE;
3573
Evan Chengd12af5d2012-03-01 23:27:13 +00003574 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003575 SDValue ARMcc;
3576 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003577 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3578 bitcastf32Toi32(LHS, DAG), Mask);
3579 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3580 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003581 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3582 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3583 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3584 Chain, Dest, ARMcc, CCR, Cmp);
3585 }
3586
3587 SDValue LHS1, LHS2;
3588 SDValue RHS1, RHS2;
3589 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3590 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003591 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3592 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003593 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3594 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003595 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003596 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003597 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003598 }
3599
3600 return SDValue();
3601}
3602
3603SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3604 SDValue Chain = Op.getOperand(0);
3605 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3606 SDValue LHS = Op.getOperand(2);
3607 SDValue RHS = Op.getOperand(3);
3608 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003609 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003610
Owen Anderson9f944592009-08-11 20:47:22 +00003611 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003612 SDValue ARMcc;
3613 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003614 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003615 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003616 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003617 }
3618
Owen Anderson9f944592009-08-11 20:47:22 +00003619 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003620
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003621 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003622 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3623 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3624 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3625 if (Result.getNode())
3626 return Result;
3627 }
3628
Evan Cheng10043e22007-01-19 07:51:42 +00003629 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003630 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003631
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003632 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3633 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003634 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003635 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003636 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003637 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003638 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003639 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3640 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003641 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003642 }
3643 return Res;
3644}
3645
Dan Gohman21cea8a2010-04-17 15:26:15 +00003646SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003647 SDValue Chain = Op.getOperand(0);
3648 SDValue Table = Op.getOperand(1);
3649 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003650 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003651
Owen Anderson53aa7a92009-08-10 22:56:29 +00003652 EVT PTy = getPointerTy();
Evan Cheng10043e22007-01-19 07:51:42 +00003653 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3654 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3f17aee2009-07-14 18:44:34 +00003655 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003656 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson9f944592009-08-11 20:47:22 +00003657 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chengc8bed032009-07-28 20:53:24 +00003658 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3659 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003660 if (Subtarget->isThumb2()) {
3661 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3662 // which does another jump to the destination. This also makes it easier
3663 // to translate it to TBB / TBH later.
3664 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003665 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Chengc6d70ae2009-07-29 02:18:14 +00003666 Addr, Op.getOperand(2), JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003667 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003668 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003669 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattner7727d052010-09-21 06:44:06 +00003670 MachinePointerInfo::getJumpTable(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003671 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003672 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003673 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson9f944592009-08-11 20:47:22 +00003674 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003675 } else {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003676 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003677 MachinePointerInfo::getJumpTable(),
3678 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003679 Chain = Addr.getValue(1);
Owen Anderson9f944592009-08-11 20:47:22 +00003680 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003681 }
Evan Cheng10043e22007-01-19 07:51:42 +00003682}
3683
Eli Friedman2d4055b2011-11-09 23:36:02 +00003684static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003685 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003686 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003687
James Molloy547d4c02012-02-20 09:24:05 +00003688 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3689 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3690 return Op;
3691 return DAG.UnrollVectorOp(Op.getNode());
3692 }
3693
3694 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3695 "Invalid type for custom lowering!");
3696 if (VT != MVT::v4i16)
3697 return DAG.UnrollVectorOp(Op.getNode());
3698
3699 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3700 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003701}
3702
Bob Wilsone4191e72010-03-19 22:51:32 +00003703static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003704 EVT VT = Op.getValueType();
3705 if (VT.isVector())
3706 return LowerVectorFP_TO_INT(Op, DAG);
3707
Andrew Trickef9de2a2013-05-25 02:42:55 +00003708 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003709 unsigned Opc;
3710
3711 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003712 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003713 case ISD::FP_TO_SINT:
3714 Opc = ARMISD::FTOSI;
3715 break;
3716 case ISD::FP_TO_UINT:
3717 Opc = ARMISD::FTOUI;
3718 break;
3719 }
3720 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peck527da1b2010-11-23 03:31:01 +00003721 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003722}
3723
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003724static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3725 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003726 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003727
Eli Friedman2d4055b2011-11-09 23:36:02 +00003728 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3729 if (VT.getVectorElementType() == MVT::f32)
3730 return Op;
3731 return DAG.UnrollVectorOp(Op.getNode());
3732 }
3733
Duncan Sandsa41634e2011-08-12 14:54:45 +00003734 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3735 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003736 if (VT != MVT::v4f32)
3737 return DAG.UnrollVectorOp(Op.getNode());
3738
3739 unsigned CastOpc;
3740 unsigned Opc;
3741 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003742 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003743 case ISD::SINT_TO_FP:
3744 CastOpc = ISD::SIGN_EXTEND;
3745 Opc = ISD::SINT_TO_FP;
3746 break;
3747 case ISD::UINT_TO_FP:
3748 CastOpc = ISD::ZERO_EXTEND;
3749 Opc = ISD::UINT_TO_FP;
3750 break;
3751 }
3752
3753 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3754 return DAG.getNode(Opc, dl, VT, Op);
3755}
3756
Bob Wilsone4191e72010-03-19 22:51:32 +00003757static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3758 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003759 if (VT.isVector())
3760 return LowerVectorINT_TO_FP(Op, DAG);
3761
Andrew Trickef9de2a2013-05-25 02:42:55 +00003762 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003763 unsigned Opc;
3764
3765 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003766 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003767 case ISD::SINT_TO_FP:
3768 Opc = ARMISD::SITOF;
3769 break;
3770 case ISD::UINT_TO_FP:
3771 Opc = ARMISD::UITOF;
3772 break;
3773 }
3774
Wesley Peck527da1b2010-11-23 03:31:01 +00003775 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilsone4191e72010-03-19 22:51:32 +00003776 return DAG.getNode(Opc, dl, VT, Op);
3777}
3778
Evan Cheng25f93642010-07-08 02:08:50 +00003779SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00003780 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003781 SDValue Tmp0 = Op.getOperand(0);
3782 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003783 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003784 EVT VT = Op.getValueType();
3785 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00003786 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3787 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3788 bool UseNEON = !InGPR && Subtarget->hasNEON();
3789
3790 if (UseNEON) {
3791 // Use VBSL to copy the sign bit.
3792 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3793 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3794 DAG.getTargetConstant(EncodedVal, MVT::i32));
3795 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3796 if (VT == MVT::f64)
3797 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3798 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3799 DAG.getConstant(32, MVT::i32));
3800 else /*if (VT == MVT::f32)*/
3801 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3802 if (SrcVT == MVT::f32) {
3803 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3804 if (VT == MVT::f64)
3805 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3806 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3807 DAG.getConstant(32, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00003808 } else if (VT == MVT::f32)
3809 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3810 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3811 DAG.getConstant(32, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00003812 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3813 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3814
3815 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3816 MVT::i32);
3817 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3818 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3819 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00003820
Evan Chengd6b641e2011-02-23 02:24:55 +00003821 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3822 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3823 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00003824 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00003825 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3826 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3827 DAG.getConstant(0, MVT::i32));
3828 } else {
3829 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3830 }
3831
3832 return Res;
3833 }
Evan Cheng2da1c952011-02-11 02:28:55 +00003834
3835 // Bitcast operand 1 to i32.
3836 if (SrcVT == MVT::f64)
3837 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00003838 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00003839 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3840
Evan Chengd6b641e2011-02-23 02:24:55 +00003841 // Or in the signbit with integer operations.
3842 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3843 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3844 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3845 if (VT == MVT::f32) {
3846 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3847 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3848 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3849 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00003850 }
3851
Evan Chengd6b641e2011-02-23 02:24:55 +00003852 // f64: Or the high part with signbit and then combine two parts.
3853 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00003854 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00003855 SDValue Lo = Tmp0.getValue(0);
3856 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3857 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3858 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00003859}
3860
Evan Cheng168ced92010-05-22 01:47:14 +00003861SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3862 MachineFunction &MF = DAG.getMachineFunction();
3863 MachineFrameInfo *MFI = MF.getFrameInfo();
3864 MFI->setReturnAddressIsTaken(true);
3865
Bill Wendling908bf812014-01-06 00:43:20 +00003866 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003867 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003868
Evan Cheng168ced92010-05-22 01:47:14 +00003869 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003870 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00003871 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3872 if (Depth) {
3873 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3874 SDValue Offset = DAG.getConstant(4, MVT::i32);
3875 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3876 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003877 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00003878 }
3879
3880 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00003881 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00003882 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3883}
3884
Dan Gohman21cea8a2010-04-17 15:26:15 +00003885SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00003886 const ARMBaseRegisterInfo &ARI =
3887 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
3888 MachineFunction &MF = DAG.getMachineFunction();
3889 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003890 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00003891
Owen Anderson53aa7a92009-08-10 22:56:29 +00003892 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003893 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003894 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00003895 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003896 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3897 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00003898 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3899 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003900 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003901 return FrameAddr;
3902}
3903
Renato Golinc7aea402014-05-06 16:51:25 +00003904// FIXME? Maybe this could be a TableGen attribute on some registers and
3905// this table could be generated automatically from RegInfo.
Hal Finkelf0e086a2014-05-11 19:29:07 +00003906unsigned ARMTargetLowering::getRegisterByName(const char* RegName,
3907 EVT VT) const {
Renato Golinc7aea402014-05-06 16:51:25 +00003908 unsigned Reg = StringSwitch<unsigned>(RegName)
3909 .Case("sp", ARM::SP)
3910 .Default(0);
3911 if (Reg)
3912 return Reg;
3913 report_fatal_error("Invalid register name global variable");
3914}
3915
Wesley Peck527da1b2010-11-23 03:31:01 +00003916/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00003917/// expand a bit convert where either the source or destination type is i64 to
3918/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3919/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3920/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00003921static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00003922 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003923 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003924 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00003925
Bob Wilson59b70ea2010-04-17 05:30:19 +00003926 // This function is only supposed to be called for i64 types, either as the
3927 // source or destination of the bit convert.
3928 EVT SrcVT = Op.getValueType();
3929 EVT DstVT = N->getValueType(0);
3930 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00003931 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00003932
Bob Wilson59b70ea2010-04-17 05:30:19 +00003933 // Turn i64->f64 into VMOVDRR.
3934 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00003935 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3936 DAG.getConstant(0, MVT::i32));
3937 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3938 DAG.getConstant(1, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00003939 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00003940 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00003941 }
Bob Wilson7117a912009-03-20 22:42:55 +00003942
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00003943 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00003944 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00003945 SDValue Cvt;
Christian Pirker6692e7c2014-05-14 16:59:44 +00003946 if (TLI.isBigEndian() && SrcVT.isVector() &&
3947 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00003948 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3949 DAG.getVTList(MVT::i32, MVT::i32),
3950 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
3951 else
3952 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3953 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00003954 // Merge the pieces into a single i64 value.
3955 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3956 }
Bob Wilson7117a912009-03-20 22:42:55 +00003957
Bob Wilson59b70ea2010-04-17 05:30:19 +00003958 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00003959}
3960
Bob Wilson2e076c42009-06-22 23:27:02 +00003961/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00003962/// Zero vectors are used to represent vector negation and in those cases
3963/// will be implemented with the NEON VNEG instruction. However, VNEG does
3964/// not support i64 elements, so sometimes the zero vectors will need to be
3965/// explicitly constructed. Regardless, use a canonical VMOV to create the
3966/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003967static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003968 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00003969 // The canonical modified immediate encoding of a zero vector is....0!
3970 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3971 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3972 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00003973 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00003974}
3975
Jim Grosbach624fcb22009-10-31 21:00:56 +00003976/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3977/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00003978SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3979 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00003980 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3981 EVT VT = Op.getValueType();
3982 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003983 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00003984 SDValue ShOpLo = Op.getOperand(0);
3985 SDValue ShOpHi = Op.getOperand(1);
3986 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003987 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003988 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00003989
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003990 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3991
Jim Grosbach624fcb22009-10-31 21:00:56 +00003992 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3993 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3994 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3995 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3996 DAG.getConstant(VTBits, MVT::i32));
3997 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3998 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003999 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004000
4001 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4002 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004003 ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00004004 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004005 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00004006 CCR, Cmp);
4007
4008 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004009 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00004010}
4011
Jim Grosbach5d994042009-10-31 19:38:01 +00004012/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4013/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004014SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4015 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004016 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4017 EVT VT = Op.getValueType();
4018 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004019 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004020 SDValue ShOpLo = Op.getOperand(0);
4021 SDValue ShOpHi = Op.getOperand(1);
4022 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004023 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004024
4025 assert(Op.getOpcode() == ISD::SHL_PARTS);
4026 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4027 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4028 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4029 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4030 DAG.getConstant(VTBits, MVT::i32));
4031 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4032 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4033
4034 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4035 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4036 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004037 ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004038 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004039 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004040 CCR, Cmp);
4041
4042 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004043 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004044}
4045
Jim Grosbach535d3b42010-09-08 03:54:02 +00004046SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004047 SelectionDAG &DAG) const {
4048 // The rounding mode is in bits 23:22 of the FPSCR.
4049 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4050 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4051 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004052 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004053 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
4054 DAG.getConstant(Intrinsic::arm_get_fpscr,
4055 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004056 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemanb69b1822010-08-03 21:31:55 +00004057 DAG.getConstant(1U << 22, MVT::i32));
4058 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
4059 DAG.getConstant(22, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004060 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemanb69b1822010-08-03 21:31:55 +00004061 DAG.getConstant(3, MVT::i32));
4062}
4063
Jim Grosbach8546ec92010-01-18 19:58:49 +00004064static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4065 const ARMSubtarget *ST) {
4066 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004067 SDLoc dl(N);
Jim Grosbach8546ec92010-01-18 19:58:49 +00004068
4069 if (!ST->hasV6T2Ops())
4070 return SDValue();
4071
4072 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
4073 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4074}
4075
Evan Chengb4eae132012-12-04 22:41:50 +00004076/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4077/// for each 16-bit element from operand, repeated. The basic idea is to
4078/// leverage vcnt to get the 8-bit counts, gather and add the results.
4079///
4080/// Trace for v4i16:
4081/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4082/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4083/// 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 +00004084/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004085/// [b0 b1 b2 b3 b4 b5 b6 b7]
4086/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4087/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4088/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4089static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4090 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004091 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004092
4093 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4094 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4095 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4096 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4097 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4098 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4099}
4100
4101/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4102/// bit-count for each 16-bit element from the operand. We need slightly
4103/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4104/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004105///
Evan Chengb4eae132012-12-04 22:41:50 +00004106/// Trace for v4i16:
4107/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4108/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4109/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4110/// v4i16:Extracted = [k0 k1 k2 k3 ]
4111static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4112 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004113 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004114
4115 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4116 if (VT.is64BitVector()) {
4117 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4118 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
4119 DAG.getIntPtrConstant(0));
4120 } else {
4121 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
4122 BitCounts, DAG.getIntPtrConstant(0));
4123 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4124 }
4125}
4126
4127/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4128/// bit-count for each 32-bit element from the operand. The idea here is
4129/// to split the vector into 16-bit elements, leverage the 16-bit count
4130/// routine, and then combine the results.
4131///
4132/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4133/// input = [v0 v1 ] (vi: 32-bit elements)
4134/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4135/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004136/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004137/// [k0 k1 k2 k3 ]
4138/// N1 =+[k1 k0 k3 k2 ]
4139/// [k0 k2 k1 k3 ]
4140/// N2 =+[k1 k3 k0 k2 ]
4141/// [k0 k2 k1 k3 ]
4142/// Extended =+[k1 k3 k0 k2 ]
4143/// [k0 k2 ]
4144/// Extracted=+[k1 k3 ]
4145///
4146static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4147 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004148 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004149
4150 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4151
4152 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4153 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4154 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4155 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4156 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4157
4158 if (VT.is64BitVector()) {
4159 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4160 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
4161 DAG.getIntPtrConstant(0));
4162 } else {
4163 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
4164 DAG.getIntPtrConstant(0));
4165 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4166 }
4167}
4168
4169static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4170 const ARMSubtarget *ST) {
4171 EVT VT = N->getValueType(0);
4172
4173 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004174 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4175 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004176 "Unexpected type for custom ctpop lowering");
4177
4178 if (VT.getVectorElementType() == MVT::i32)
4179 return lowerCTPOP32BitElements(N, DAG);
4180 else
4181 return lowerCTPOP16BitElements(N, DAG);
4182}
4183
Bob Wilson2e076c42009-06-22 23:27:02 +00004184static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4185 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004186 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004187 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004188
Bob Wilson7d471332010-11-18 21:16:28 +00004189 if (!VT.isVector())
4190 return SDValue();
4191
Bob Wilson2e076c42009-06-22 23:27:02 +00004192 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004193 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004194
Bob Wilson7d471332010-11-18 21:16:28 +00004195 // Left shifts translate directly to the vshiftu intrinsic.
4196 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004197 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilson7d471332010-11-18 21:16:28 +00004198 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
4199 N->getOperand(0), N->getOperand(1));
4200
4201 assert((N->getOpcode() == ISD::SRA ||
4202 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4203
4204 // NEON uses the same intrinsics for both left and right shifts. For
4205 // right shifts, the shift amounts are negative, so negate the vector of
4206 // shift amounts.
4207 EVT ShiftVT = N->getOperand(1).getValueType();
4208 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4209 getZeroVector(ShiftVT, DAG, dl),
4210 N->getOperand(1));
4211 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4212 Intrinsic::arm_neon_vshifts :
4213 Intrinsic::arm_neon_vshiftu);
4214 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4215 DAG.getConstant(vshiftInt, MVT::i32),
4216 N->getOperand(0), NegatedCount);
4217}
4218
4219static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4220 const ARMSubtarget *ST) {
4221 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004222 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004223
Eli Friedman682d8c12009-08-22 03:13:10 +00004224 // We can get here for a node like i32 = ISD::SHL i32, i64
4225 if (VT != MVT::i64)
4226 return SDValue();
4227
4228 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004229 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004230
Chris Lattnerf81d5882007-11-24 07:07:01 +00004231 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4232 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004233 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004234 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004235
Chris Lattnerf81d5882007-11-24 07:07:01 +00004236 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004237 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004238
Chris Lattnerf81d5882007-11-24 07:07:01 +00004239 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004240 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004241 DAG.getConstant(0, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004242 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004243 DAG.getConstant(1, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004244
Chris Lattnerf81d5882007-11-24 07:07:01 +00004245 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4246 // captures the result into a carry flag.
4247 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004248 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004249
Chris Lattnerf81d5882007-11-24 07:07:01 +00004250 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004251 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004252
Chris Lattnerf81d5882007-11-24 07:07:01 +00004253 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004254 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004255}
4256
Bob Wilson2e076c42009-06-22 23:27:02 +00004257static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4258 SDValue TmpOp0, TmpOp1;
4259 bool Invert = false;
4260 bool Swap = false;
4261 unsigned Opc = 0;
4262
4263 SDValue Op0 = Op.getOperand(0);
4264 SDValue Op1 = Op.getOperand(1);
4265 SDValue CC = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004266 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004267 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004268 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004269
4270 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
4271 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004272 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004273 case ISD::SETUNE:
4274 case ISD::SETNE: Invert = true; // Fallthrough
4275 case ISD::SETOEQ:
4276 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4277 case ISD::SETOLT:
4278 case ISD::SETLT: Swap = true; // Fallthrough
4279 case ISD::SETOGT:
4280 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4281 case ISD::SETOLE:
4282 case ISD::SETLE: Swap = true; // Fallthrough
4283 case ISD::SETOGE:
4284 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4285 case ISD::SETUGE: Swap = true; // Fallthrough
4286 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4287 case ISD::SETUGT: Swap = true; // Fallthrough
4288 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4289 case ISD::SETUEQ: Invert = true; // Fallthrough
4290 case ISD::SETONE:
4291 // Expand this to (OLT | OGT).
4292 TmpOp0 = Op0;
4293 TmpOp1 = Op1;
4294 Opc = ISD::OR;
4295 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4296 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
4297 break;
4298 case ISD::SETUO: Invert = true; // Fallthrough
4299 case ISD::SETO:
4300 // Expand this to (OLT | OGE).
4301 TmpOp0 = Op0;
4302 TmpOp1 = Op1;
4303 Opc = ISD::OR;
4304 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4305 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
4306 break;
4307 }
4308 } else {
4309 // Integer comparisons.
4310 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004311 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004312 case ISD::SETNE: Invert = true;
4313 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4314 case ISD::SETLT: Swap = true;
4315 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4316 case ISD::SETLE: Swap = true;
4317 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4318 case ISD::SETULT: Swap = true;
4319 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4320 case ISD::SETULE: Swap = true;
4321 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4322 }
4323
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004324 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004325 if (Opc == ARMISD::VCEQ) {
4326
4327 SDValue AndOp;
4328 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4329 AndOp = Op0;
4330 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4331 AndOp = Op1;
4332
4333 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004334 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004335 AndOp = AndOp.getOperand(0);
4336
4337 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4338 Opc = ARMISD::VTST;
Wesley Peck527da1b2010-11-23 03:31:01 +00004339 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
4340 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004341 Invert = !Invert;
4342 }
4343 }
4344 }
4345
4346 if (Swap)
4347 std::swap(Op0, Op1);
4348
Owen Andersonc7baee32010-11-08 23:21:22 +00004349 // If one of the operands is a constant vector zero, attempt to fold the
4350 // comparison to a specialized compare-against-zero form.
4351 SDValue SingleOp;
4352 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4353 SingleOp = Op0;
4354 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4355 if (Opc == ARMISD::VCGE)
4356 Opc = ARMISD::VCLEZ;
4357 else if (Opc == ARMISD::VCGT)
4358 Opc = ARMISD::VCLTZ;
4359 SingleOp = Op1;
4360 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004361
Owen Andersonc7baee32010-11-08 23:21:22 +00004362 SDValue Result;
4363 if (SingleOp.getNode()) {
4364 switch (Opc) {
4365 case ARMISD::VCEQ:
4366 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
4367 case ARMISD::VCGE:
4368 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
4369 case ARMISD::VCLEZ:
4370 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
4371 case ARMISD::VCGT:
4372 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
4373 case ARMISD::VCLTZ:
4374 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
4375 default:
4376 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4377 }
4378 } else {
4379 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4380 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004381
4382 if (Invert)
4383 Result = DAG.getNOT(dl, Result, VT);
4384
4385 return Result;
4386}
4387
Bob Wilson5b2b5042010-06-14 22:19:57 +00004388/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4389/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004390/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004391static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4392 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Andersona4076922010-11-05 21:57:54 +00004393 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004394 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004395
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004396 // SplatBitSize is set to the smallest size that splats the vector, so a
4397 // zero vector will always have SplatBitSize == 8. However, NEON modified
4398 // immediate instructions others than VMOV do not support the 8-bit encoding
4399 // of a zero vector, and the default encoding of zero is supposed to be the
4400 // 32-bit version.
4401 if (SplatBits == 0)
4402 SplatBitSize = 32;
4403
Bob Wilson2e076c42009-06-22 23:27:02 +00004404 switch (SplatBitSize) {
4405 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004406 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004407 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004408 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004409 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004410 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004411 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004412 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004413 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004414
4415 case 16:
4416 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004417 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004418 if ((SplatBits & ~0xff) == 0) {
4419 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004420 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004421 Imm = SplatBits;
4422 break;
4423 }
4424 if ((SplatBits & ~0xff00) == 0) {
4425 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004426 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004427 Imm = SplatBits >> 8;
4428 break;
4429 }
4430 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004431
4432 case 32:
4433 // NEON's 32-bit VMOV supports splat values where:
4434 // * only one byte is nonzero, or
4435 // * the least significant byte is 0xff and the second byte is nonzero, or
4436 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004437 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004438 if ((SplatBits & ~0xff) == 0) {
4439 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004440 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004441 Imm = SplatBits;
4442 break;
4443 }
4444 if ((SplatBits & ~0xff00) == 0) {
4445 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004446 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004447 Imm = SplatBits >> 8;
4448 break;
4449 }
4450 if ((SplatBits & ~0xff0000) == 0) {
4451 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004452 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004453 Imm = SplatBits >> 16;
4454 break;
4455 }
4456 if ((SplatBits & ~0xff000000) == 0) {
4457 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004458 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004459 Imm = SplatBits >> 24;
4460 break;
4461 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004462
Owen Andersona4076922010-11-05 21:57:54 +00004463 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4464 if (type == OtherModImm) return SDValue();
4465
Bob Wilson2e076c42009-06-22 23:27:02 +00004466 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004467 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4468 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004469 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004470 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004471 break;
4472 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004473
4474 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004475 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4476 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004477 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004478 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004479 break;
4480 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004481
4482 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4483 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4484 // VMOV.I32. A (very) minor optimization would be to replicate the value
4485 // and fall through here to test for a valid 64-bit splat. But, then the
4486 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004487 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004488
4489 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004490 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004491 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004492 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004493 uint64_t BitMask = 0xff;
4494 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004495 unsigned ImmMask = 1;
4496 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004497 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004498 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004499 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004500 Imm |= ImmMask;
4501 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004502 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004503 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004504 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004505 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004506 }
Bob Wilson6eae5202010-06-11 21:34:50 +00004507 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004508 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004509 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004510 break;
4511 }
4512
Bob Wilson6eae5202010-06-11 21:34:50 +00004513 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004514 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004515 }
4516
Bob Wilsona3f19012010-07-13 21:16:48 +00004517 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4518 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004519}
4520
Lang Hames591cdaf2012-03-29 21:56:11 +00004521SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4522 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004523 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004524 return SDValue();
4525
Tim Northoverf79c3a52013-08-20 08:57:11 +00004526 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004527 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004528
4529 // Try splatting with a VMOV.f32...
4530 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004531 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4532
Lang Hames591cdaf2012-03-29 21:56:11 +00004533 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004534 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4535 // We have code in place to select a valid ConstantFP already, no need to
4536 // do any mangling.
4537 return Op;
4538 }
4539
4540 // It's a float and we are trying to use NEON operations where
4541 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004542 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004543 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4544 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4545 NewVal);
4546 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4547 DAG.getConstant(0, MVT::i32));
4548 }
4549
Tim Northoverf79c3a52013-08-20 08:57:11 +00004550 // The rest of our options are NEON only, make sure that's allowed before
4551 // proceeding..
4552 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4553 return SDValue();
4554
Lang Hames591cdaf2012-03-29 21:56:11 +00004555 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004556 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4557
4558 // It wouldn't really be worth bothering for doubles except for one very
4559 // important value, which does happen to match: 0.0. So make sure we don't do
4560 // anything stupid.
4561 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4562 return SDValue();
4563
4564 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
4565 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4566 false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004567 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004568 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004569 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4570 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004571 if (IsDouble)
4572 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4573
4574 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004575 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4576 VecConstant);
4577 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4578 DAG.getConstant(0, MVT::i32));
4579 }
4580
4581 // Finally, try a VMVN.i32
Tim Northoverf79c3a52013-08-20 08:57:11 +00004582 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4583 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004584 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004585 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004586 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004587
4588 if (IsDouble)
4589 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4590
4591 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004592 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4593 VecConstant);
4594 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4595 DAG.getConstant(0, MVT::i32));
4596 }
4597
4598 return SDValue();
4599}
4600
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004601// check if an VEXT instruction can handle the shuffle mask when the
4602// vector sources of the shuffle are the same.
4603static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4604 unsigned NumElts = VT.getVectorNumElements();
4605
4606 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4607 if (M[0] < 0)
4608 return false;
4609
4610 Imm = M[0];
4611
4612 // If this is a VEXT shuffle, the immediate value is the index of the first
4613 // element. The other shuffle indices must be the successive elements after
4614 // the first one.
4615 unsigned ExpectedElt = Imm;
4616 for (unsigned i = 1; i < NumElts; ++i) {
4617 // Increment the expected index. If it wraps around, just follow it
4618 // back to index zero and keep going.
4619 ++ExpectedElt;
4620 if (ExpectedElt == NumElts)
4621 ExpectedElt = 0;
4622
4623 if (M[i] < 0) continue; // ignore UNDEF indices
4624 if (ExpectedElt != static_cast<unsigned>(M[i]))
4625 return false;
4626 }
4627
4628 return true;
4629}
4630
Lang Hames591cdaf2012-03-29 21:56:11 +00004631
Benjamin Kramer339ced42012-01-15 13:16:05 +00004632static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004633 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004634 unsigned NumElts = VT.getVectorNumElements();
4635 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004636
4637 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4638 if (M[0] < 0)
4639 return false;
4640
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004641 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004642
4643 // If this is a VEXT shuffle, the immediate value is the index of the first
4644 // element. The other shuffle indices must be the successive elements after
4645 // the first one.
4646 unsigned ExpectedElt = Imm;
4647 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004648 // Increment the expected index. If it wraps around, it may still be
4649 // a VEXT but the source vectors must be swapped.
4650 ExpectedElt += 1;
4651 if (ExpectedElt == NumElts * 2) {
4652 ExpectedElt = 0;
4653 ReverseVEXT = true;
4654 }
4655
Bob Wilson411dfad2010-08-17 05:54:34 +00004656 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004657 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004658 return false;
4659 }
4660
4661 // Adjust the index value if the source operands will be swapped.
4662 if (ReverseVEXT)
4663 Imm -= NumElts;
4664
Bob Wilson32cd8552009-08-19 17:03:43 +00004665 return true;
4666}
4667
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004668/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4669/// instruction with the specified blocksize. (The order of the elements
4670/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00004671static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004672 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4673 "Only possible block sizes for VREV are: 16, 32, 64");
4674
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004675 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00004676 if (EltSz == 64)
4677 return false;
4678
4679 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004680 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00004681 // If the first shuffle index is UNDEF, be optimistic.
4682 if (M[0] < 0)
4683 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004684
4685 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4686 return false;
4687
4688 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004689 if (M[i] < 0) continue; // ignore UNDEF indices
4690 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004691 return false;
4692 }
4693
4694 return true;
4695}
4696
Benjamin Kramer339ced42012-01-15 13:16:05 +00004697static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00004698 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4699 // range, then 0 is placed into the resulting vector. So pretty much any mask
4700 // of 8 elements can work here.
4701 return VT == MVT::v8i8 && M.size() == 8;
4702}
4703
Benjamin Kramer339ced42012-01-15 13:16:05 +00004704static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004705 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4706 if (EltSz == 64)
4707 return false;
4708
Bob Wilsona7062312009-08-21 20:54:19 +00004709 unsigned NumElts = VT.getVectorNumElements();
4710 WhichResult = (M[0] == 0 ? 0 : 1);
4711 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004712 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4713 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsona7062312009-08-21 20:54:19 +00004714 return false;
4715 }
4716 return true;
4717}
4718
Bob Wilson0bbd3072009-12-03 06:40:55 +00004719/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4720/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4721/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004722static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004723 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4724 if (EltSz == 64)
4725 return false;
4726
4727 unsigned NumElts = VT.getVectorNumElements();
4728 WhichResult = (M[0] == 0 ? 0 : 1);
4729 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004730 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4731 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004732 return false;
4733 }
4734 return true;
4735}
4736
Benjamin Kramer339ced42012-01-15 13:16:05 +00004737static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004738 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4739 if (EltSz == 64)
4740 return false;
4741
Bob Wilsona7062312009-08-21 20:54:19 +00004742 unsigned NumElts = VT.getVectorNumElements();
4743 WhichResult = (M[0] == 0 ? 0 : 1);
4744 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004745 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsona7062312009-08-21 20:54:19 +00004746 if ((unsigned) M[i] != 2 * i + WhichResult)
4747 return false;
4748 }
4749
4750 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004751 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004752 return false;
4753
4754 return true;
4755}
4756
Bob Wilson0bbd3072009-12-03 06:40:55 +00004757/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4758/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4759/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00004760static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004761 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4762 if (EltSz == 64)
4763 return false;
4764
4765 unsigned Half = VT.getVectorNumElements() / 2;
4766 WhichResult = (M[0] == 0 ? 0 : 1);
4767 for (unsigned j = 0; j != 2; ++j) {
4768 unsigned Idx = WhichResult;
4769 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004770 int MIdx = M[i + j * Half];
4771 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson0bbd3072009-12-03 06:40:55 +00004772 return false;
4773 Idx += 2;
4774 }
4775 }
4776
4777 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4778 if (VT.is64BitVector() && EltSz == 32)
4779 return false;
4780
4781 return true;
4782}
4783
Benjamin Kramer339ced42012-01-15 13:16:05 +00004784static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004785 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4786 if (EltSz == 64)
4787 return false;
4788
Bob Wilsona7062312009-08-21 20:54:19 +00004789 unsigned NumElts = VT.getVectorNumElements();
4790 WhichResult = (M[0] == 0 ? 0 : 1);
4791 unsigned Idx = WhichResult * NumElts / 2;
4792 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004793 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4794 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsona7062312009-08-21 20:54:19 +00004795 return false;
4796 Idx += 1;
4797 }
4798
4799 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004800 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004801 return false;
4802
4803 return true;
4804}
4805
Bob Wilson0bbd3072009-12-03 06:40:55 +00004806/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4807/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4808/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004809static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004810 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4811 if (EltSz == 64)
4812 return false;
4813
4814 unsigned NumElts = VT.getVectorNumElements();
4815 WhichResult = (M[0] == 0 ? 0 : 1);
4816 unsigned Idx = WhichResult * NumElts / 2;
4817 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004818 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4819 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004820 return false;
4821 Idx += 1;
4822 }
4823
4824 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4825 if (VT.is64BitVector() && EltSz == 32)
4826 return false;
4827
4828 return true;
4829}
4830
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00004831/// \return true if this is a reverse operation on an vector.
4832static bool isReverseMask(ArrayRef<int> M, EVT VT) {
4833 unsigned NumElts = VT.getVectorNumElements();
4834 // Make sure the mask has the right size.
4835 if (NumElts != M.size())
4836 return false;
4837
4838 // Look for <15, ..., 3, -1, 1, 0>.
4839 for (unsigned i = 0; i != NumElts; ++i)
4840 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
4841 return false;
4842
4843 return true;
4844}
4845
Dale Johannesen2bff5052010-07-29 20:10:08 +00004846// If N is an integer constant that can be moved into a register in one
4847// instruction, return an SDValue of such a constant (will become a MOV
4848// instruction). Otherwise return null.
4849static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004850 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00004851 uint64_t Val;
4852 if (!isa<ConstantSDNode>(N))
4853 return SDValue();
4854 Val = cast<ConstantSDNode>(N)->getZExtValue();
4855
4856 if (ST->isThumb1Only()) {
4857 if (Val <= 255 || ~Val <= 255)
4858 return DAG.getConstant(Val, MVT::i32);
4859 } else {
4860 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4861 return DAG.getConstant(Val, MVT::i32);
4862 }
4863 return SDValue();
4864}
4865
Bob Wilson2e076c42009-06-22 23:27:02 +00004866// If this is a case we can't handle, return null and let the default
4867// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00004868SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4869 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00004870 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004871 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004872 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004873
4874 APInt SplatBits, SplatUndef;
4875 unsigned SplatBitSize;
4876 bool HasAnyUndefs;
4877 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00004878 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00004879 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00004880 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00004881 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00004882 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00004883 DAG, VmovVT, VT.is128BitVector(),
4884 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00004885 if (Val.getNode()) {
4886 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00004887 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00004888 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00004889
4890 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00004891 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004892 Val = isNEONModifiedImm(NegatedImm,
4893 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00004894 DAG, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00004895 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00004896 if (Val.getNode()) {
4897 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00004898 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00004899 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00004900
4901 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00004902 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00004903 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00004904 if (ImmVal != -1) {
4905 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4906 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4907 }
4908 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00004909 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00004910 }
4911
Bob Wilson91fdf682010-05-22 00:23:12 +00004912 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00004913 //
4914 // As an optimisation, even if more than one value is used it may be more
4915 // profitable to splat with one value then change some lanes.
4916 //
4917 // Heuristically we decide to do this if the vector has a "dominant" value,
4918 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00004919 unsigned NumElts = VT.getVectorNumElements();
4920 bool isOnlyLowElement = true;
4921 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00004922 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00004923 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00004924
4925 // Map of the number of times a particular SDValue appears in the
4926 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00004927 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00004928 SDValue Value;
4929 for (unsigned i = 0; i < NumElts; ++i) {
4930 SDValue V = Op.getOperand(i);
4931 if (V.getOpcode() == ISD::UNDEF)
4932 continue;
4933 if (i > 0)
4934 isOnlyLowElement = false;
4935 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4936 isConstant = false;
4937
James Molloy49bdbce2012-09-06 09:55:02 +00004938 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00004939 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00004940
James Molloy49bdbce2012-09-06 09:55:02 +00004941 // Is this value dominant? (takes up more than half of the lanes)
4942 if (++Count > (NumElts / 2)) {
4943 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00004944 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00004945 }
Bob Wilson91fdf682010-05-22 00:23:12 +00004946 }
James Molloy49bdbce2012-09-06 09:55:02 +00004947 if (ValueCounts.size() != 1)
4948 usesOnlyOneValue = false;
4949 if (!Value.getNode() && ValueCounts.size() > 0)
4950 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00004951
James Molloy49bdbce2012-09-06 09:55:02 +00004952 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00004953 return DAG.getUNDEF(VT);
4954
Quentin Colombet0f2fe742013-07-23 22:34:47 +00004955 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
4956 // Keep going if we are hitting this case.
4957 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00004958 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4959
Dale Johannesen2bff5052010-07-29 20:10:08 +00004960 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4961
Dale Johannesen710a2d92010-10-19 20:00:17 +00004962 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4963 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00004964 if (hasDominantValue && EltSize <= 32) {
4965 if (!isConstant) {
4966 SDValue N;
4967
4968 // If we are VDUPing a value that comes directly from a vector, that will
4969 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00004970 // just use VDUPLANE. We can only do this if the lane being extracted
4971 // is at a constant index, as the VDUP from lane instructions only have
4972 // constant-index forms.
4973 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4974 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00004975 // We need to create a new undef vector to use for the VDUPLANE if the
4976 // size of the vector from which we get the value is different than the
4977 // size of the vector that we need to create. We will insert the element
4978 // such that the register coalescer will remove unnecessary copies.
4979 if (VT != Value->getOperand(0).getValueType()) {
4980 ConstantSDNode *constIndex;
4981 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
4982 assert(constIndex && "The index is not a constant!");
4983 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
4984 VT.getVectorNumElements();
4985 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
4986 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
4987 Value, DAG.getConstant(index, MVT::i32)),
4988 DAG.getConstant(index, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00004989 } else
Silviu Barangab1409702012-10-15 09:41:32 +00004990 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00004991 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00004992 } else
James Molloy49bdbce2012-09-06 09:55:02 +00004993 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4994
4995 if (!usesOnlyOneValue) {
4996 // The dominant value was splatted as 'N', but we now have to insert
4997 // all differing elements.
4998 for (unsigned I = 0; I < NumElts; ++I) {
4999 if (Op.getOperand(I) == Value)
5000 continue;
5001 SmallVector<SDValue, 3> Ops;
5002 Ops.push_back(N);
5003 Ops.push_back(Op.getOperand(I));
5004 Ops.push_back(DAG.getConstant(I, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00005005 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00005006 }
5007 }
5008 return N;
5009 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00005010 if (VT.getVectorElementType().isFloatingPoint()) {
5011 SmallVector<SDValue, 8> Ops;
5012 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005013 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005014 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005015 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005016 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005017 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5018 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005019 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005020 }
James Molloy49bdbce2012-09-06 09:55:02 +00005021 if (usesOnlyOneValue) {
5022 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5023 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005024 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005025 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005026 }
5027
5028 // If all elements are constants and the case above didn't get hit, fall back
5029 // to the default expansion, which will generate a load from the constant
5030 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005031 if (isConstant)
5032 return SDValue();
5033
Bob Wilson6f2b8962011-01-07 21:37:30 +00005034 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5035 if (NumElts >= 4) {
5036 SDValue shuffle = ReconstructShuffle(Op, DAG);
5037 if (shuffle != SDValue())
5038 return shuffle;
5039 }
5040
Bob Wilson91fdf682010-05-22 00:23:12 +00005041 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005042 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5043 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005044 if (EltSize >= 32) {
5045 // Do the expansion with floating-point types, since that is what the VFP
5046 // registers are defined to use, and since i64 is not legal.
5047 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5048 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005049 SmallVector<SDValue, 8> Ops;
5050 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005051 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005052 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005053 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005054 }
5055
Jim Grosbach24e102a2013-07-08 18:18:52 +00005056 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5057 // know the default expansion would otherwise fall back on something even
5058 // worse. For a vector with one or two non-undef values, that's
5059 // scalar_to_vector for the elements followed by a shuffle (provided the
5060 // shuffle is valid for the target) and materialization element by element
5061 // on the stack followed by a load for everything else.
5062 if (!isConstant && !usesOnlyOneValue) {
5063 SDValue Vec = DAG.getUNDEF(VT);
5064 for (unsigned i = 0 ; i < NumElts; ++i) {
5065 SDValue V = Op.getOperand(i);
5066 if (V.getOpcode() == ISD::UNDEF)
5067 continue;
5068 SDValue LaneIdx = DAG.getConstant(i, MVT::i32);
5069 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5070 }
5071 return Vec;
5072 }
5073
Bob Wilson2e076c42009-06-22 23:27:02 +00005074 return SDValue();
5075}
5076
Bob Wilson6f2b8962011-01-07 21:37:30 +00005077// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005078// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005079SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5080 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005081 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005082 EVT VT = Op.getValueType();
5083 unsigned NumElts = VT.getVectorNumElements();
5084
5085 SmallVector<SDValue, 2> SourceVecs;
5086 SmallVector<unsigned, 2> MinElts;
5087 SmallVector<unsigned, 2> MaxElts;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005088
Bob Wilson6f2b8962011-01-07 21:37:30 +00005089 for (unsigned i = 0; i < NumElts; ++i) {
5090 SDValue V = Op.getOperand(i);
5091 if (V.getOpcode() == ISD::UNDEF)
5092 continue;
5093 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5094 // A shuffle can only come from building a vector from various
5095 // elements of other vectors.
5096 return SDValue();
Eli Friedman74d1da52011-10-14 23:58:49 +00005097 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
5098 VT.getVectorElementType()) {
5099 // This code doesn't know how to handle shuffles where the vector
5100 // element types do not match (this happens because type legalization
5101 // promotes the return type of EXTRACT_VECTOR_ELT).
5102 // FIXME: It might be appropriate to extend this code to handle
5103 // mismatched types.
5104 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005105 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005106
Bob Wilson6f2b8962011-01-07 21:37:30 +00005107 // Record this extraction against the appropriate vector if possible...
5108 SDValue SourceVec = V.getOperand(0);
Jim Grosbach6df755c2012-07-25 17:02:47 +00005109 // If the element number isn't a constant, we can't effectively
5110 // analyze what's going on.
5111 if (!isa<ConstantSDNode>(V.getOperand(1)))
5112 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005113 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5114 bool FoundSource = false;
5115 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
5116 if (SourceVecs[j] == SourceVec) {
5117 if (MinElts[j] > EltNo)
5118 MinElts[j] = EltNo;
5119 if (MaxElts[j] < EltNo)
5120 MaxElts[j] = EltNo;
5121 FoundSource = true;
5122 break;
5123 }
5124 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005125
Bob Wilson6f2b8962011-01-07 21:37:30 +00005126 // Or record a new source if not...
5127 if (!FoundSource) {
5128 SourceVecs.push_back(SourceVec);
5129 MinElts.push_back(EltNo);
5130 MaxElts.push_back(EltNo);
5131 }
5132 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005133
Bob Wilson6f2b8962011-01-07 21:37:30 +00005134 // Currently only do something sane when at most two source vectors
5135 // involved.
5136 if (SourceVecs.size() > 2)
5137 return SDValue();
5138
5139 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
5140 int VEXTOffsets[2] = {0, 0};
Andrew Trick5eb0a302011-01-19 02:26:13 +00005141
Bob Wilson6f2b8962011-01-07 21:37:30 +00005142 // This loop extracts the usage patterns of the source vectors
5143 // and prepares appropriate SDValues for a shuffle if possible.
5144 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
5145 if (SourceVecs[i].getValueType() == VT) {
5146 // No VEXT necessary
5147 ShuffleSrcs[i] = SourceVecs[i];
5148 VEXTOffsets[i] = 0;
5149 continue;
5150 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
5151 // It probably isn't worth padding out a smaller vector just to
5152 // break it down again in a shuffle.
5153 return SDValue();
5154 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005155
Bob Wilson6f2b8962011-01-07 21:37:30 +00005156 // Since only 64-bit and 128-bit vectors are legal on ARM and
5157 // we've eliminated the other cases...
Bob Wilson3fa9c062011-01-07 23:40:46 +00005158 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
5159 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick5eb0a302011-01-19 02:26:13 +00005160
Bob Wilson6f2b8962011-01-07 21:37:30 +00005161 if (MaxElts[i] - MinElts[i] >= NumElts) {
5162 // Span too large for a VEXT to cope
5163 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005164 }
5165
Bob Wilson6f2b8962011-01-07 21:37:30 +00005166 if (MinElts[i] >= NumElts) {
5167 // The extraction can just take the second half
5168 VEXTOffsets[i] = NumElts;
Eric Christopher2af95512011-01-14 23:50:53 +00005169 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5170 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005171 DAG.getIntPtrConstant(NumElts));
5172 } else if (MaxElts[i] < NumElts) {
5173 // The extraction can just take the first half
5174 VEXTOffsets[i] = 0;
Eric Christopher2af95512011-01-14 23:50:53 +00005175 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5176 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005177 DAG.getIntPtrConstant(0));
5178 } else {
5179 // An actual VEXT is needed
5180 VEXTOffsets[i] = MinElts[i];
Eric Christopher2af95512011-01-14 23:50:53 +00005181 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5182 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005183 DAG.getIntPtrConstant(0));
Eric Christopher2af95512011-01-14 23:50:53 +00005184 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5185 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005186 DAG.getIntPtrConstant(NumElts));
5187 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
5188 DAG.getConstant(VEXTOffsets[i], MVT::i32));
5189 }
5190 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005191
Bob Wilson6f2b8962011-01-07 21:37:30 +00005192 SmallVector<int, 8> Mask;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005193
Bob Wilson6f2b8962011-01-07 21:37:30 +00005194 for (unsigned i = 0; i < NumElts; ++i) {
5195 SDValue Entry = Op.getOperand(i);
5196 if (Entry.getOpcode() == ISD::UNDEF) {
5197 Mask.push_back(-1);
5198 continue;
5199 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005200
Bob Wilson6f2b8962011-01-07 21:37:30 +00005201 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher2af95512011-01-14 23:50:53 +00005202 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
5203 .getOperand(1))->getSExtValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005204 if (ExtractVec == SourceVecs[0]) {
5205 Mask.push_back(ExtractElt - VEXTOffsets[0]);
5206 } else {
5207 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
5208 }
5209 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005210
Bob Wilson6f2b8962011-01-07 21:37:30 +00005211 // Final check before we try to produce nonsense...
5212 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher2af95512011-01-14 23:50:53 +00005213 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
5214 &Mask[0]);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005215
Bob Wilson6f2b8962011-01-07 21:37:30 +00005216 return SDValue();
5217}
5218
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005219/// isShuffleMaskLegal - Targets can use this to indicate that they only
5220/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5221/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5222/// are assumed to be legal.
5223bool
5224ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5225 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005226 if (VT.getVectorNumElements() == 4 &&
5227 (VT.is128BitVector() || VT.is64BitVector())) {
5228 unsigned PFIndexes[4];
5229 for (unsigned i = 0; i != 4; ++i) {
5230 if (M[i] < 0)
5231 PFIndexes[i] = 8;
5232 else
5233 PFIndexes[i] = M[i];
5234 }
5235
5236 // Compute the index in the perfect shuffle table.
5237 unsigned PFTableIndex =
5238 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5239 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5240 unsigned Cost = (PFEntry >> 30);
5241
5242 if (Cost <= 4)
5243 return true;
5244 }
5245
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005246 bool ReverseVEXT;
Bob Wilsona7062312009-08-21 20:54:19 +00005247 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005248
Bob Wilson846bd792010-06-07 23:53:38 +00005249 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5250 return (EltSize >= 32 ||
5251 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005252 isVREVMask(M, VT, 64) ||
5253 isVREVMask(M, VT, 32) ||
5254 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005255 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005256 isVTBLMask(M, VT) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005257 isVTRNMask(M, VT, WhichResult) ||
5258 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson0bbd3072009-12-03 06:40:55 +00005259 isVZIPMask(M, VT, WhichResult) ||
5260 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
5261 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005262 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
5263 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005264}
5265
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005266/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5267/// the specified operations to build the shuffle.
5268static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5269 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005270 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005271 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5272 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5273 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5274
5275 enum {
5276 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5277 OP_VREV,
5278 OP_VDUP0,
5279 OP_VDUP1,
5280 OP_VDUP2,
5281 OP_VDUP3,
5282 OP_VEXT1,
5283 OP_VEXT2,
5284 OP_VEXT3,
5285 OP_VUZPL, // VUZP, left result
5286 OP_VUZPR, // VUZP, right result
5287 OP_VZIPL, // VZIP, left result
5288 OP_VZIPR, // VZIP, right result
5289 OP_VTRNL, // VTRN, left result
5290 OP_VTRNR // VTRN, right result
5291 };
5292
5293 if (OpNum == OP_COPY) {
5294 if (LHSID == (1*9+2)*9+3) return LHS;
5295 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5296 return RHS;
5297 }
5298
5299 SDValue OpLHS, OpRHS;
5300 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5301 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5302 EVT VT = OpLHS.getValueType();
5303
5304 switch (OpNum) {
5305 default: llvm_unreachable("Unknown shuffle opcode!");
5306 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005307 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005308 if (VT.getVectorElementType() == MVT::i32 ||
5309 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005310 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5311 // vrev <4 x i16> -> VREV32
5312 if (VT.getVectorElementType() == MVT::i16)
5313 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5314 // vrev <4 x i8> -> VREV16
5315 assert(VT.getVectorElementType() == MVT::i8);
5316 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005317 case OP_VDUP0:
5318 case OP_VDUP1:
5319 case OP_VDUP2:
5320 case OP_VDUP3:
5321 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005322 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005323 case OP_VEXT1:
5324 case OP_VEXT2:
5325 case OP_VEXT3:
5326 return DAG.getNode(ARMISD::VEXT, dl, VT,
5327 OpLHS, OpRHS,
5328 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
5329 case OP_VUZPL:
5330 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005331 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005332 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5333 case OP_VZIPL:
5334 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005335 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005336 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5337 case OP_VTRNL:
5338 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005339 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5340 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005341 }
5342}
5343
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005344static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005345 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005346 SelectionDAG &DAG) {
5347 // Check to see if we can use the VTBL instruction.
5348 SDValue V1 = Op.getOperand(0);
5349 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005350 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005351
5352 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005353 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005354 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
5355 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
5356
5357 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5358 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005359 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005360
Owen Anderson77aa2662011-04-05 21:48:57 +00005361 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005362 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005363}
5364
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005365static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5366 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005367 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005368 SDValue OpLHS = Op.getOperand(0);
5369 EVT VT = OpLHS.getValueType();
5370
5371 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5372 "Expect an v8i16/v16i8 type");
5373 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5374 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5375 // extract the first 8 bytes into the top double word and the last 8 bytes
5376 // into the bottom double word. The v8i16 case is similar.
5377 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5378 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
5379 DAG.getConstant(ExtractNum, MVT::i32));
5380}
5381
Bob Wilson2e076c42009-06-22 23:27:02 +00005382static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005383 SDValue V1 = Op.getOperand(0);
5384 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005385 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005386 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005387 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005388
Bob Wilsonc6800b52009-08-13 02:13:04 +00005389 // Convert shuffles that are directly supported on NEON to target-specific
5390 // DAG nodes, instead of keeping them as shuffles and matching them again
5391 // during code selection. This is more efficient and avoids the possibility
5392 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005393 // FIXME: floating-point vectors should be canonicalized to integer vectors
5394 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005395 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005396
Bob Wilson846bd792010-06-07 23:53:38 +00005397 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5398 if (EltSize <= 32) {
5399 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5400 int Lane = SVN->getSplatIndex();
5401 // If this is undef splat, generate it via "just" vdup, if possible.
5402 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005403
Dan Gohman198b7ff2011-11-03 21:49:52 +00005404 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005405 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5406 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5407 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005408 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5409 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5410 // reaches it).
5411 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5412 !isa<ConstantSDNode>(V1.getOperand(0))) {
5413 bool IsScalarToVector = true;
5414 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5415 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5416 IsScalarToVector = false;
5417 break;
5418 }
5419 if (IsScalarToVector)
5420 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5421 }
Bob Wilson846bd792010-06-07 23:53:38 +00005422 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
5423 DAG.getConstant(Lane, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005424 }
Bob Wilson846bd792010-06-07 23:53:38 +00005425
5426 bool ReverseVEXT;
5427 unsigned Imm;
5428 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5429 if (ReverseVEXT)
5430 std::swap(V1, V2);
5431 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
5432 DAG.getConstant(Imm, MVT::i32));
5433 }
5434
5435 if (isVREVMask(ShuffleMask, VT, 64))
5436 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5437 if (isVREVMask(ShuffleMask, VT, 32))
5438 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5439 if (isVREVMask(ShuffleMask, VT, 16))
5440 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5441
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005442 if (V2->getOpcode() == ISD::UNDEF &&
5443 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5444 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
5445 DAG.getConstant(Imm, MVT::i32));
5446 }
5447
Bob Wilson846bd792010-06-07 23:53:38 +00005448 // Check for Neon shuffles that modify both input vectors in place.
5449 // If both results are used, i.e., if there are two shuffles with the same
5450 // source operands and with masks corresponding to both results of one of
5451 // these operations, DAG memoization will ensure that a single node is
5452 // used for both shuffles.
5453 unsigned WhichResult;
5454 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5455 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5456 V1, V2).getValue(WhichResult);
5457 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5458 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5459 V1, V2).getValue(WhichResult);
5460 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5461 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5462 V1, V2).getValue(WhichResult);
5463
5464 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5465 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5466 V1, V1).getValue(WhichResult);
5467 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5468 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5469 V1, V1).getValue(WhichResult);
5470 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5471 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5472 V1, V1).getValue(WhichResult);
Bob Wilsoncce31f62009-08-14 05:08:32 +00005473 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005474
Bob Wilsona7062312009-08-21 20:54:19 +00005475 // If the shuffle is not directly supported and it has 4 elements, use
5476 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005477 unsigned NumElts = VT.getVectorNumElements();
5478 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005479 unsigned PFIndexes[4];
5480 for (unsigned i = 0; i != 4; ++i) {
5481 if (ShuffleMask[i] < 0)
5482 PFIndexes[i] = 8;
5483 else
5484 PFIndexes[i] = ShuffleMask[i];
5485 }
5486
5487 // Compute the index in the perfect shuffle table.
5488 unsigned PFTableIndex =
5489 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005490 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5491 unsigned Cost = (PFEntry >> 30);
5492
5493 if (Cost <= 4)
5494 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5495 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00005496
Bob Wilsond8a9a042010-06-04 00:04:02 +00005497 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00005498 if (EltSize >= 32) {
5499 // Do the expansion with floating-point types, since that is what the VFP
5500 // registers are defined to use, and since i64 is not legal.
5501 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5502 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005503 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5504 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005505 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00005506 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00005507 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00005508 Ops.push_back(DAG.getUNDEF(EltVT));
5509 else
5510 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5511 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5512 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
5513 MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00005514 }
Craig Topper48d114b2014-04-26 18:35:24 +00005515 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005516 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00005517 }
5518
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005519 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5520 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
5521
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005522 if (VT == MVT::v8i8) {
5523 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
5524 if (NewOp.getNode())
5525 return NewOp;
5526 }
5527
Bob Wilson6f34e272009-08-14 05:16:33 +00005528 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005529}
5530
Eli Friedmana5e244c2011-10-24 23:08:52 +00005531static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5532 // INSERT_VECTOR_ELT is legal only for immediate indexes.
5533 SDValue Lane = Op.getOperand(2);
5534 if (!isa<ConstantSDNode>(Lane))
5535 return SDValue();
5536
5537 return Op;
5538}
5539
Bob Wilson2e076c42009-06-22 23:27:02 +00005540static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00005541 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00005542 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00005543 if (!isa<ConstantSDNode>(Lane))
5544 return SDValue();
5545
5546 SDValue Vec = Op.getOperand(0);
5547 if (Op.getValueType() == MVT::i32 &&
5548 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005549 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00005550 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5551 }
5552
5553 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00005554}
5555
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005556static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5557 // The only time a CONCAT_VECTORS operation can have legal types is when
5558 // two 64-bit vectors are concatenated to a 128-bit vector.
5559 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5560 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005561 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005562 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005563 SDValue Op0 = Op.getOperand(0);
5564 SDValue Op1 = Op.getOperand(1);
5565 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005566 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005567 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005568 DAG.getIntPtrConstant(0));
5569 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005570 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005571 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005572 DAG.getIntPtrConstant(1));
Wesley Peck527da1b2010-11-23 03:31:01 +00005573 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005574}
5575
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005576/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5577/// element has been zero/sign-extended, depending on the isSigned parameter,
5578/// from an integer type half its size.
5579static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5580 bool isSigned) {
5581 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5582 EVT VT = N->getValueType(0);
5583 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5584 SDNode *BVN = N->getOperand(0).getNode();
5585 if (BVN->getValueType(0) != MVT::v4i32 ||
5586 BVN->getOpcode() != ISD::BUILD_VECTOR)
5587 return false;
5588 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5589 unsigned HiElt = 1 - LoElt;
5590 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5591 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5592 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5593 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5594 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5595 return false;
5596 if (isSigned) {
5597 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5598 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5599 return true;
5600 } else {
5601 if (Hi0->isNullValue() && Hi1->isNullValue())
5602 return true;
5603 }
5604 return false;
5605 }
5606
5607 if (N->getOpcode() != ISD::BUILD_VECTOR)
5608 return false;
5609
5610 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5611 SDNode *Elt = N->getOperand(i).getNode();
5612 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5613 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5614 unsigned HalfSize = EltSize / 2;
5615 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005616 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005617 return false;
5618 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005619 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005620 return false;
5621 }
5622 continue;
5623 }
5624 return false;
5625 }
5626
5627 return true;
5628}
5629
5630/// isSignExtended - Check if a node is a vector value that is sign-extended
5631/// or a constant BUILD_VECTOR with sign-extended elements.
5632static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5633 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5634 return true;
5635 if (isExtendedBUILD_VECTOR(N, DAG, true))
5636 return true;
5637 return false;
5638}
5639
5640/// isZeroExtended - Check if a node is a vector value that is zero-extended
5641/// or a constant BUILD_VECTOR with zero-extended elements.
5642static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5643 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5644 return true;
5645 if (isExtendedBUILD_VECTOR(N, DAG, false))
5646 return true;
5647 return false;
5648}
5649
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005650static EVT getExtensionTo64Bits(const EVT &OrigVT) {
5651 if (OrigVT.getSizeInBits() >= 64)
5652 return OrigVT;
5653
5654 assert(OrigVT.isSimple() && "Expecting a simple value type");
5655
5656 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
5657 switch (OrigSimpleTy) {
5658 default: llvm_unreachable("Unexpected Vector Type");
5659 case MVT::v2i8:
5660 case MVT::v2i16:
5661 return MVT::v2i32;
5662 case MVT::v4i8:
5663 return MVT::v4i16;
5664 }
5665}
5666
Sebastian Popa204f722012-11-30 19:08:04 +00005667/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5668/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5669/// We insert the required extension here to get the vector to fill a D register.
5670static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5671 const EVT &OrigTy,
5672 const EVT &ExtTy,
5673 unsigned ExtOpcode) {
5674 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5675 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5676 // 64-bits we need to insert a new extension so that it will be 64-bits.
5677 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5678 if (OrigTy.getSizeInBits() >= 64)
5679 return N;
5680
5681 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005682 EVT NewVT = getExtensionTo64Bits(OrigTy);
5683
Andrew Trickef9de2a2013-05-25 02:42:55 +00005684 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00005685}
5686
5687/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5688/// does not do any sign/zero extension. If the original vector is less
5689/// than 64 bits, an appropriate extension will be added after the load to
5690/// reach a total size of 64 bits. We have to add the extension separately
5691/// because ARM does not have a sign/zero extending load for vectors.
5692static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005693 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
5694
5695 // The load already has the right type.
5696 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005697 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00005698 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5699 LD->isNonTemporal(), LD->isInvariant(),
5700 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005701
5702 // We need to create a zextload/sextload. We cannot just create a load
5703 // followed by a zext/zext node because LowerMUL is also run during normal
5704 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005705 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005706 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
5707 LD->getMemoryVT(), LD->isVolatile(),
5708 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00005709}
5710
5711/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5712/// extending load, or BUILD_VECTOR with extended elements, return the
5713/// unextended value. The unextended vector should be 64 bits so that it can
5714/// be used as an operand to a VMULL instruction. If the original vector size
5715/// before extension is less than 64 bits we add a an extension to resize
5716/// the vector to 64 bits.
5717static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00005718 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00005719 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5720 N->getOperand(0)->getValueType(0),
5721 N->getValueType(0),
5722 N->getOpcode());
5723
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005724 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00005725 return SkipLoadExtensionForVMULL(LD, DAG);
5726
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005727 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5728 // have been legalized as a BITCAST from v4i32.
5729 if (N->getOpcode() == ISD::BITCAST) {
5730 SDNode *BVN = N->getOperand(0).getNode();
5731 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5732 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5733 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00005734 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005735 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5736 }
5737 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5738 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5739 EVT VT = N->getValueType(0);
5740 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5741 unsigned NumElts = VT.getVectorNumElements();
5742 MVT TruncVT = MVT::getIntegerVT(EltSize);
5743 SmallVector<SDValue, 8> Ops;
5744 for (unsigned i = 0; i != NumElts; ++i) {
5745 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5746 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00005747 // Element types smaller than 32 bits are not legal, so use i32 elements.
5748 // The values are implicitly truncated so sext vs. zext doesn't matter.
5749 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005750 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005751 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topper48d114b2014-04-26 18:35:24 +00005752 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00005753}
5754
Evan Chenge2086e72011-03-29 01:56:09 +00005755static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5756 unsigned Opcode = N->getOpcode();
5757 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5758 SDNode *N0 = N->getOperand(0).getNode();
5759 SDNode *N1 = N->getOperand(1).getNode();
5760 return N0->hasOneUse() && N1->hasOneUse() &&
5761 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5762 }
5763 return false;
5764}
5765
5766static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5767 unsigned Opcode = N->getOpcode();
5768 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5769 SDNode *N0 = N->getOperand(0).getNode();
5770 SDNode *N1 = N->getOperand(1).getNode();
5771 return N0->hasOneUse() && N1->hasOneUse() &&
5772 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
5773 }
5774 return false;
5775}
5776
Bob Wilson38ab35a2010-09-01 23:50:19 +00005777static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
5778 // Multiplications are only custom-lowered for 128-bit vectors so that
5779 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
5780 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00005781 assert(VT.is128BitVector() && VT.isInteger() &&
5782 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00005783 SDNode *N0 = Op.getOperand(0).getNode();
5784 SDNode *N1 = Op.getOperand(1).getNode();
5785 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00005786 bool isMLA = false;
5787 bool isN0SExt = isSignExtended(N0, DAG);
5788 bool isN1SExt = isSignExtended(N1, DAG);
5789 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00005790 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00005791 else {
5792 bool isN0ZExt = isZeroExtended(N0, DAG);
5793 bool isN1ZExt = isZeroExtended(N1, DAG);
5794 if (isN0ZExt && isN1ZExt)
5795 NewOpc = ARMISD::VMULLu;
5796 else if (isN1SExt || isN1ZExt) {
5797 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5798 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5799 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5800 NewOpc = ARMISD::VMULLs;
5801 isMLA = true;
5802 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5803 NewOpc = ARMISD::VMULLu;
5804 isMLA = true;
5805 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5806 std::swap(N0, N1);
5807 NewOpc = ARMISD::VMULLu;
5808 isMLA = true;
5809 }
5810 }
5811
5812 if (!NewOpc) {
5813 if (VT == MVT::v2i64)
5814 // Fall through to expand this. It is not legal.
5815 return SDValue();
5816 else
5817 // Other vector multiplications are legal.
5818 return Op;
5819 }
5820 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005821
5822 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005823 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00005824 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00005825 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005826 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00005827 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005828 assert(Op0.getValueType().is64BitVector() &&
5829 Op1.getValueType().is64BitVector() &&
5830 "unexpected types for extended operands to VMULL");
5831 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
5832 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005833
Evan Chenge2086e72011-03-29 01:56:09 +00005834 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
5835 // isel lowering to take advantage of no-stall back to back vmul + vmla.
5836 // vmull q0, d4, d6
5837 // vmlal q0, d5, d6
5838 // is faster than
5839 // vaddl q0, d4, d5
5840 // vmovl q1, d6
5841 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00005842 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
5843 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005844 EVT Op1VT = Op1.getValueType();
5845 return DAG.getNode(N0->getOpcode(), DL, VT,
5846 DAG.getNode(NewOpc, DL, VT,
5847 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
5848 DAG.getNode(NewOpc, DL, VT,
5849 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00005850}
5851
Owen Anderson77aa2662011-04-05 21:48:57 +00005852static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005853LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005854 // Convert to float
5855 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
5856 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
5857 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
5858 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
5859 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
5860 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
5861 // Get reciprocal estimate.
5862 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00005863 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005864 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
5865 // Because char has a smaller range than uchar, we can actually get away
5866 // without any newton steps. This requires that we use a weird bias
5867 // of 0xb000, however (again, this has been exhaustively tested).
5868 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
5869 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
5870 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
5871 Y = DAG.getConstant(0xb000, MVT::i32);
5872 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
5873 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
5874 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
5875 // Convert back to short.
5876 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
5877 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
5878 return X;
5879}
5880
Owen Anderson77aa2662011-04-05 21:48:57 +00005881static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005882LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005883 SDValue N2;
5884 // Convert to float.
5885 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5886 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5887 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5888 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5889 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5890 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005891
Nate Begemanfa62d502011-02-11 20:53:29 +00005892 // Use reciprocal estimate and one refinement step.
5893 // float4 recip = vrecpeq_f32(yf);
5894 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00005895 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005896 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005897 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005898 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5899 N1, N2);
5900 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5901 // Because short has a smaller range than ushort, we can actually get away
5902 // with only a single newton step. This requires that we use a weird bias
5903 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005904 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00005905 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5906 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005907 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00005908 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5909 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5910 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5911 // Convert back to integer and return.
5912 // return vmovn_s32(vcvt_s32_f32(result));
5913 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5914 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5915 return N0;
5916}
5917
5918static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5919 EVT VT = Op.getValueType();
5920 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5921 "unexpected type for custom-lowering ISD::SDIV");
5922
Andrew Trickef9de2a2013-05-25 02:42:55 +00005923 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00005924 SDValue N0 = Op.getOperand(0);
5925 SDValue N1 = Op.getOperand(1);
5926 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00005927
Nate Begemanfa62d502011-02-11 20:53:29 +00005928 if (VT == MVT::v8i8) {
5929 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5930 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005931
Nate Begemanfa62d502011-02-11 20:53:29 +00005932 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5933 DAG.getIntPtrConstant(4));
5934 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00005935 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00005936 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5937 DAG.getIntPtrConstant(0));
5938 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5939 DAG.getIntPtrConstant(0));
5940
5941 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5942 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5943
5944 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5945 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00005946
Nate Begemanfa62d502011-02-11 20:53:29 +00005947 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5948 return N0;
5949 }
5950 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5951}
5952
5953static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5954 EVT VT = Op.getValueType();
5955 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5956 "unexpected type for custom-lowering ISD::UDIV");
5957
Andrew Trickef9de2a2013-05-25 02:42:55 +00005958 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00005959 SDValue N0 = Op.getOperand(0);
5960 SDValue N1 = Op.getOperand(1);
5961 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00005962
Nate Begemanfa62d502011-02-11 20:53:29 +00005963 if (VT == MVT::v8i8) {
5964 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5965 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005966
Nate Begemanfa62d502011-02-11 20:53:29 +00005967 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5968 DAG.getIntPtrConstant(4));
5969 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00005970 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00005971 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5972 DAG.getIntPtrConstant(0));
5973 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5974 DAG.getIntPtrConstant(0));
Owen Anderson77aa2662011-04-05 21:48:57 +00005975
Nate Begemanfa62d502011-02-11 20:53:29 +00005976 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5977 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00005978
Nate Begemanfa62d502011-02-11 20:53:29 +00005979 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5980 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00005981
5982 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begemanfa62d502011-02-11 20:53:29 +00005983 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5984 N0);
5985 return N0;
5986 }
Owen Anderson77aa2662011-04-05 21:48:57 +00005987
Nate Begemanfa62d502011-02-11 20:53:29 +00005988 // v4i16 sdiv ... Convert to float.
5989 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5990 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5991 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5992 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5993 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005994 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00005995
5996 // Use reciprocal estimate and two refinement steps.
5997 // float4 recip = vrecpeq_f32(yf);
5998 // recip *= vrecpsq_f32(yf, recip);
5999 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00006000 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006001 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00006002 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006003 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006004 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006005 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00006006 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00006007 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006008 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006009 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
6010 // Simply multiplying by the reciprocal estimate can leave us a few ulps
6011 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6012 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006013 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006014 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6015 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
6016 N1 = DAG.getConstant(2, MVT::i32);
6017 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6018 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6019 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6020 // Convert back to integer and return.
6021 // return vmovn_u32(vcvt_s32_f32(result));
6022 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6023 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6024 return N0;
6025}
6026
Evan Chenge8916542011-08-30 01:34:54 +00006027static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6028 EVT VT = Op.getNode()->getValueType(0);
6029 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6030
6031 unsigned Opc;
6032 bool ExtraOp = false;
6033 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006034 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006035 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6036 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6037 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6038 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6039 }
6040
6041 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006042 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006043 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006044 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006045 Op.getOperand(1), Op.getOperand(2));
6046}
6047
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006048SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6049 assert(Subtarget->isTargetDarwin());
6050
6051 // For iOS, we want to call an alternative entry point: __sincos_stret,
6052 // return values are passed via sret.
6053 SDLoc dl(Op);
6054 SDValue Arg = Op.getOperand(0);
6055 EVT ArgVT = Arg.getValueType();
6056 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
6057
6058 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
6059 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6060
6061 // Pair of floats / doubles used to pass the result.
6062 StructType *RetTy = StructType::get(ArgTy, ArgTy, NULL);
6063
6064 // Create stack object for sret.
6065 const uint64_t ByteSize = TLI.getDataLayout()->getTypeAllocSize(RetTy);
6066 const unsigned StackAlign = TLI.getDataLayout()->getPrefTypeAlignment(RetTy);
6067 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6068 SDValue SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
6069
6070 ArgListTy Args;
6071 ArgListEntry Entry;
6072
6073 Entry.Node = SRet;
6074 Entry.Ty = RetTy->getPointerTo();
6075 Entry.isSExt = false;
6076 Entry.isZExt = false;
6077 Entry.isSRet = true;
6078 Args.push_back(Entry);
6079
6080 Entry.Node = Arg;
6081 Entry.Ty = ArgTy;
6082 Entry.isSExt = false;
6083 Entry.isZExt = false;
6084 Args.push_back(Entry);
6085
6086 const char *LibcallName = (ArgVT == MVT::f64)
6087 ? "__sincos_stret" : "__sincosf_stret";
6088 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
6089
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006090 TargetLowering::CallLoweringInfo CLI(DAG);
6091 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
6092 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), Callee,
6093 &Args, 0)
6094 .setDiscardResult();
6095
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006096 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6097
6098 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6099 MachinePointerInfo(), false, false, false, 0);
6100
6101 // Address of cos field.
6102 SDValue Add = DAG.getNode(ISD::ADD, dl, getPointerTy(), SRet,
6103 DAG.getIntPtrConstant(ArgVT.getStoreSize()));
6104 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6105 MachinePointerInfo(), false, false, false, 0);
6106
6107 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6108 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6109 LoadSin.getValue(0), LoadCos.getValue(0));
6110}
6111
Eli Friedman10f9ce22011-09-15 22:26:18 +00006112static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006113 // Monotonic load/store is legal for all targets
6114 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6115 return Op;
6116
Alp Tokercb402912014-01-24 17:20:08 +00006117 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006118 // dmb or equivalent available.
6119 return SDValue();
6120}
6121
Tim Northoverbc933082013-05-23 19:11:20 +00006122static void ReplaceREADCYCLECOUNTER(SDNode *N,
6123 SmallVectorImpl<SDValue> &Results,
6124 SelectionDAG &DAG,
6125 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006126 SDLoc DL(N);
Tim Northoverbc933082013-05-23 19:11:20 +00006127 SDValue Cycles32, OutChain;
6128
6129 if (Subtarget->hasPerfMon()) {
6130 // Under Power Management extensions, the cycle-count is:
6131 // mrc p15, #0, <Rt>, c9, c13, #0
6132 SDValue Ops[] = { N->getOperand(0), // Chain
6133 DAG.getConstant(Intrinsic::arm_mrc, MVT::i32),
6134 DAG.getConstant(15, MVT::i32),
6135 DAG.getConstant(0, MVT::i32),
6136 DAG.getConstant(9, MVT::i32),
6137 DAG.getConstant(13, MVT::i32),
6138 DAG.getConstant(0, MVT::i32)
6139 };
6140
6141 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00006142 DAG.getVTList(MVT::i32, MVT::Other), Ops);
Tim Northoverbc933082013-05-23 19:11:20 +00006143 OutChain = Cycles32.getValue(1);
6144 } else {
6145 // Intrinsic is defined to return 0 on unsupported platforms. Technically
6146 // there are older ARM CPUs that have implementation-specific ways of
6147 // obtaining this information (FIXME!).
6148 Cycles32 = DAG.getConstant(0, MVT::i32);
6149 OutChain = DAG.getEntryNode();
6150 }
6151
6152
6153 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64,
6154 Cycles32, DAG.getConstant(0, MVT::i32));
6155 Results.push_back(Cycles64);
6156 Results.push_back(OutChain);
6157}
6158
Dan Gohman21cea8a2010-04-17 15:26:15 +00006159SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006160 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006161 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Cheng10043e22007-01-19 07:51:42 +00006162 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006163 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006164 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006165 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6166 default: llvm_unreachable("unknown object format");
6167 case Triple::COFF:
6168 return LowerGlobalAddressWindows(Op, DAG);
6169 case Triple::ELF:
6170 return LowerGlobalAddressELF(Op, DAG);
6171 case Triple::MachO:
6172 return LowerGlobalAddressDarwin(Op, DAG);
6173 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006174 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006175 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006176 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6177 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006178 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006179 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006180 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006181 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006182 case ISD::SINT_TO_FP:
6183 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6184 case ISD::FP_TO_SINT:
6185 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006186 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006187 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006188 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006189 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006190 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006191 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006192 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6193 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006194 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006195 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006196 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006197 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng15b80e42009-11-12 07:13:11 +00006198 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006199 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006200 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach8546ec92010-01-18 19:58:49 +00006201 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006202 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006203 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006204 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006205 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006206 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006207 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006208 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006209 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006210 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006211 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanfa62d502011-02-11 20:53:29 +00006212 case ISD::SDIV: return LowerSDIV(Op, DAG);
6213 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006214 case ISD::ADDC:
6215 case ISD::ADDE:
6216 case ISD::SUBC:
6217 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006218 case ISD::SADDO:
6219 case ISD::UADDO:
6220 case ISD::SSUBO:
6221 case ISD::USUBO:
6222 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006223 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006224 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006225 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006226 case ISD::SDIVREM:
6227 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00006228 case ISD::DYNAMIC_STACKALLOC:
6229 if (Subtarget->getTargetTriple().isWindowsItaniumEnvironment())
6230 return LowerDYNAMIC_STACKALLOC(Op, DAG);
6231 llvm_unreachable("Don't know how to custom lower this!");
Evan Cheng10043e22007-01-19 07:51:42 +00006232 }
Evan Cheng10043e22007-01-19 07:51:42 +00006233}
6234
Duncan Sands6ed40142008-12-01 11:39:25 +00006235/// ReplaceNodeResults - Replace the results of node with an illegal result
6236/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006237void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
6238 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006239 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006240 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006241 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006242 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006243 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peck527da1b2010-11-23 03:31:01 +00006244 case ISD::BITCAST:
6245 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006246 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006247 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006248 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006249 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006250 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006251 case ISD::READCYCLECOUNTER:
6252 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6253 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00006254 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006255 if (Res.getNode())
6256 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006257}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006258
Evan Cheng10043e22007-01-19 07:51:42 +00006259//===----------------------------------------------------------------------===//
6260// ARM Scheduler Hooks
6261//===----------------------------------------------------------------------===//
6262
Bill Wendling030b58e2011-10-06 22:18:16 +00006263/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6264/// registers the function context.
6265void ARMTargetLowering::
6266SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6267 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendling374ee192011-10-03 21:25:38 +00006268 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6269 DebugLoc dl = MI->getDebugLoc();
6270 MachineFunction *MF = MBB->getParent();
6271 MachineRegisterInfo *MRI = &MF->getRegInfo();
6272 MachineConstantPool *MCP = MF->getConstantPool();
6273 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6274 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006275
Bill Wendling374ee192011-10-03 21:25:38 +00006276 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006277 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006278
Bill Wendling374ee192011-10-03 21:25:38 +00006279 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006280 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006281 ARMConstantPoolValue *CPV =
6282 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6283 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6284
Craig Topperc7242e02012-04-20 07:30:17 +00006285 const TargetRegisterClass *TRC = isThumb ?
6286 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6287 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006288
Bill Wendling030b58e2011-10-06 22:18:16 +00006289 // Grab constant pool and fixed stack memory operands.
6290 MachineMemOperand *CPMMO =
6291 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6292 MachineMemOperand::MOLoad, 4, 4);
6293
6294 MachineMemOperand *FIMMOSt =
6295 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6296 MachineMemOperand::MOStore, 4, 4);
6297
6298 // Load the address of the dispatch MBB into the jump buffer.
6299 if (isThumb2) {
6300 // Incoming value: jbuf
6301 // ldr.n r5, LCPI1_1
6302 // orr r5, r5, #1
6303 // add r5, pc
6304 // str r5, [$jbuf, #+4] ; &jbuf[1]
6305 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6306 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6307 .addConstantPoolIndex(CPI)
6308 .addMemOperand(CPMMO));
6309 // Set the low bit because of thumb mode.
6310 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6311 AddDefaultCC(
6312 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6313 .addReg(NewVReg1, RegState::Kill)
6314 .addImm(0x01)));
6315 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6316 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6317 .addReg(NewVReg2, RegState::Kill)
6318 .addImm(PCLabelId);
6319 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6320 .addReg(NewVReg3, RegState::Kill)
6321 .addFrameIndex(FI)
6322 .addImm(36) // &jbuf[1] :: pc
6323 .addMemOperand(FIMMOSt));
6324 } else if (isThumb) {
6325 // Incoming value: jbuf
6326 // ldr.n r1, LCPI1_4
6327 // add r1, pc
6328 // mov r2, #1
6329 // orrs r1, r2
6330 // add r2, $jbuf, #+4 ; &jbuf[1]
6331 // str r1, [r2]
6332 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6333 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6334 .addConstantPoolIndex(CPI)
6335 .addMemOperand(CPMMO));
6336 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6337 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6338 .addReg(NewVReg1, RegState::Kill)
6339 .addImm(PCLabelId);
6340 // Set the low bit because of thumb mode.
6341 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6342 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6343 .addReg(ARM::CPSR, RegState::Define)
6344 .addImm(1));
6345 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6346 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6347 .addReg(ARM::CPSR, RegState::Define)
6348 .addReg(NewVReg2, RegState::Kill)
6349 .addReg(NewVReg3, RegState::Kill));
6350 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6351 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
6352 .addFrameIndex(FI)
6353 .addImm(36)); // &jbuf[1] :: pc
6354 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6355 .addReg(NewVReg4, RegState::Kill)
6356 .addReg(NewVReg5, RegState::Kill)
6357 .addImm(0)
6358 .addMemOperand(FIMMOSt));
6359 } else {
6360 // Incoming value: jbuf
6361 // ldr r1, LCPI1_1
6362 // add r1, pc, r1
6363 // str r1, [$jbuf, #+4] ; &jbuf[1]
6364 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6365 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6366 .addConstantPoolIndex(CPI)
6367 .addImm(0)
6368 .addMemOperand(CPMMO));
6369 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6370 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6371 .addReg(NewVReg1, RegState::Kill)
6372 .addImm(PCLabelId));
6373 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6374 .addReg(NewVReg2, RegState::Kill)
6375 .addFrameIndex(FI)
6376 .addImm(36) // &jbuf[1] :: pc
6377 .addMemOperand(FIMMOSt));
6378 }
6379}
6380
6381MachineBasicBlock *ARMTargetLowering::
6382EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
6383 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6384 DebugLoc dl = MI->getDebugLoc();
6385 MachineFunction *MF = MBB->getParent();
6386 MachineRegisterInfo *MRI = &MF->getRegInfo();
6387 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6388 MachineFrameInfo *MFI = MF->getFrameInfo();
6389 int FI = MFI->getFunctionContextIndex();
6390
Craig Topperc7242e02012-04-20 07:30:17 +00006391 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
6392 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen691ae332012-05-20 06:38:47 +00006393 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00006394
Bill Wendling362c1b02011-10-06 21:29:56 +00006395 // Get a mapping of the call site numbers to all of the landing pads they're
6396 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00006397 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6398 unsigned MaxCSNum = 0;
6399 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00006400 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6401 ++BB) {
Bill Wendling202803e2011-10-05 00:02:33 +00006402 if (!BB->isLandingPad()) continue;
6403
6404 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6405 // pad.
6406 for (MachineBasicBlock::iterator
6407 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6408 if (!II->isEHLabel()) continue;
6409
6410 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006411 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006412
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006413 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6414 for (SmallVectorImpl<unsigned>::iterator
6415 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6416 CSI != CSE; ++CSI) {
6417 CallSiteNumToLPad[*CSI].push_back(BB);
6418 MaxCSNum = std::max(MaxCSNum, *CSI);
6419 }
Bill Wendling202803e2011-10-05 00:02:33 +00006420 break;
6421 }
6422 }
6423
6424 // Get an ordered list of the machine basic blocks for the jump table.
6425 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00006426 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00006427 LPadList.reserve(CallSiteNumToLPad.size());
6428 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6429 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6430 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006431 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00006432 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00006433 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6434 }
Bill Wendling202803e2011-10-05 00:02:33 +00006435 }
6436
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006437 assert(!LPadList.empty() &&
6438 "No landing pad destinations for the dispatch jump table!");
6439
Bill Wendling362c1b02011-10-06 21:29:56 +00006440 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00006441 MachineJumpTableInfo *JTI =
6442 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6443 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6444 unsigned UId = AFI->createJumpTableUId();
Chad Rosier96603432013-03-01 18:30:38 +00006445 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00006446
Bill Wendling362c1b02011-10-06 21:29:56 +00006447 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00006448
6449 // Shove the dispatch's address into the return slot in the function context.
6450 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6451 DispatchBB->setIsLandingPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00006452
Bill Wendling324be982011-10-05 00:39:32 +00006453 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006454 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00006455 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006456 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00006457 else
6458 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6459
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006460 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00006461 DispatchBB->addSuccessor(TrapBB);
6462
6463 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6464 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00006465
Bill Wendling510fbcd2011-10-17 21:32:56 +00006466 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00006467 MF->insert(MF->end(), DispatchBB);
6468 MF->insert(MF->end(), DispContBB);
6469 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00006470
Bill Wendling030b58e2011-10-06 22:18:16 +00006471 // Insert code into the entry block that creates and registers the function
6472 // context.
6473 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6474
Bill Wendling030b58e2011-10-06 22:18:16 +00006475 MachineMemOperand *FIMMOLd =
Bill Wendling362c1b02011-10-06 21:29:56 +00006476 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendlingb3d46782011-10-06 23:37:36 +00006477 MachineMemOperand::MOLoad |
6478 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00006479
Chad Rosier1ec8e402012-11-06 23:05:24 +00006480 MachineInstrBuilder MIB;
6481 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6482
6483 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6484 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6485
6486 // Add a register mask with no preserved registers. This results in all
6487 // registers being marked as clobbered.
6488 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00006489
Bill Wendling85833f72011-10-18 22:49:07 +00006490 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00006491 if (Subtarget->isThumb2()) {
6492 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6493 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6494 .addFrameIndex(FI)
6495 .addImm(4)
6496 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006497
Bill Wendling85833f72011-10-18 22:49:07 +00006498 if (NumLPads < 256) {
6499 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6500 .addReg(NewVReg1)
6501 .addImm(LPadList.size()));
6502 } else {
6503 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6504 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006505 .addImm(NumLPads & 0xFFFF));
6506
6507 unsigned VReg2 = VReg1;
6508 if ((NumLPads & 0xFFFF0000) != 0) {
6509 VReg2 = MRI->createVirtualRegister(TRC);
6510 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6511 .addReg(VReg1)
6512 .addImm(NumLPads >> 16));
6513 }
6514
Bill Wendling85833f72011-10-18 22:49:07 +00006515 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6516 .addReg(NewVReg1)
6517 .addReg(VReg2));
6518 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006519
Bill Wendling5626c662011-10-06 22:53:00 +00006520 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6521 .addMBB(TrapBB)
6522 .addImm(ARMCC::HI)
6523 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00006524
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006525 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6526 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006527 .addJumpTableIndex(MJTI)
6528 .addImm(UId));
Bill Wendling202803e2011-10-05 00:02:33 +00006529
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006530 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006531 AddDefaultCC(
6532 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006533 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6534 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00006535 .addReg(NewVReg1)
6536 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6537
6538 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006539 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00006540 .addReg(NewVReg1)
Bill Wendling5626c662011-10-06 22:53:00 +00006541 .addJumpTableIndex(MJTI)
6542 .addImm(UId);
6543 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00006544 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6545 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6546 .addFrameIndex(FI)
6547 .addImm(1)
6548 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00006549
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006550 if (NumLPads < 256) {
6551 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6552 .addReg(NewVReg1)
6553 .addImm(NumLPads));
6554 } else {
6555 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00006556 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6557 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6558
6559 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006560 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006561 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006562 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006563 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006564
6565 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6566 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6567 .addReg(VReg1, RegState::Define)
6568 .addConstantPoolIndex(Idx));
6569 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6570 .addReg(NewVReg1)
6571 .addReg(VReg1));
6572 }
6573
Bill Wendlingb3d46782011-10-06 23:37:36 +00006574 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6575 .addMBB(TrapBB)
6576 .addImm(ARMCC::HI)
6577 .addReg(ARM::CPSR);
6578
6579 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6580 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6581 .addReg(ARM::CPSR, RegState::Define)
6582 .addReg(NewVReg1)
6583 .addImm(2));
6584
6585 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00006586 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendlingb3d46782011-10-06 23:37:36 +00006587 .addJumpTableIndex(MJTI)
6588 .addImm(UId));
6589
6590 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6591 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6592 .addReg(ARM::CPSR, RegState::Define)
6593 .addReg(NewVReg2, RegState::Kill)
6594 .addReg(NewVReg3));
6595
6596 MachineMemOperand *JTMMOLd =
6597 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6598 MachineMemOperand::MOLoad, 4, 4);
6599
6600 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6601 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6602 .addReg(NewVReg4, RegState::Kill)
6603 .addImm(0)
6604 .addMemOperand(JTMMOLd));
6605
Chad Rosier96603432013-03-01 18:30:38 +00006606 unsigned NewVReg6 = NewVReg5;
6607 if (RelocM == Reloc::PIC_) {
6608 NewVReg6 = MRI->createVirtualRegister(TRC);
6609 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6610 .addReg(ARM::CPSR, RegState::Define)
6611 .addReg(NewVReg5, RegState::Kill)
6612 .addReg(NewVReg3));
6613 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00006614
6615 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6616 .addReg(NewVReg6, RegState::Kill)
6617 .addJumpTableIndex(MJTI)
6618 .addImm(UId);
Bill Wendling5626c662011-10-06 22:53:00 +00006619 } else {
6620 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6621 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6622 .addFrameIndex(FI)
6623 .addImm(4)
6624 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00006625
Bill Wendling4969dcd2011-10-18 22:52:20 +00006626 if (NumLPads < 256) {
6627 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6628 .addReg(NewVReg1)
6629 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00006630 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00006631 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6632 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006633 .addImm(NumLPads & 0xFFFF));
6634
6635 unsigned VReg2 = VReg1;
6636 if ((NumLPads & 0xFFFF0000) != 0) {
6637 VReg2 = MRI->createVirtualRegister(TRC);
6638 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6639 .addReg(VReg1)
6640 .addImm(NumLPads >> 16));
6641 }
6642
Bill Wendling4969dcd2011-10-18 22:52:20 +00006643 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6644 .addReg(NewVReg1)
6645 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00006646 } else {
6647 MachineConstantPool *ConstantPool = MF->getConstantPool();
6648 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6649 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6650
6651 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006652 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006653 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006654 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006655 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6656
6657 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6658 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6659 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00006660 .addConstantPoolIndex(Idx)
6661 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00006662 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6663 .addReg(NewVReg1)
6664 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00006665 }
6666
Bill Wendling5626c662011-10-06 22:53:00 +00006667 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6668 .addMBB(TrapBB)
6669 .addImm(ARMCC::HI)
6670 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00006671
Bill Wendling973c8172011-10-18 22:11:18 +00006672 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006673 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00006674 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006675 .addReg(NewVReg1)
6676 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00006677 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6678 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006679 .addJumpTableIndex(MJTI)
6680 .addImm(UId));
6681
6682 MachineMemOperand *JTMMOLd =
6683 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6684 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00006685 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006686 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00006687 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6688 .addReg(NewVReg3, RegState::Kill)
6689 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006690 .addImm(0)
6691 .addMemOperand(JTMMOLd));
6692
Chad Rosier96603432013-03-01 18:30:38 +00006693 if (RelocM == Reloc::PIC_) {
6694 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
6695 .addReg(NewVReg5, RegState::Kill)
6696 .addReg(NewVReg4)
6697 .addJumpTableIndex(MJTI)
6698 .addImm(UId);
6699 } else {
6700 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
6701 .addReg(NewVReg5, RegState::Kill)
6702 .addJumpTableIndex(MJTI)
6703 .addImm(UId);
6704 }
Bill Wendling5626c662011-10-06 22:53:00 +00006705 }
Bill Wendling202803e2011-10-05 00:02:33 +00006706
Bill Wendling324be982011-10-05 00:39:32 +00006707 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006708 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00006709 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006710 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6711 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006712 if (SeenMBBs.insert(CurMBB))
Bill Wendling883ec972011-10-07 23:18:02 +00006713 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006714 }
6715
Bill Wendling26d27802011-10-17 05:25:09 +00006716 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00006717 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00006718 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling883ec972011-10-07 23:18:02 +00006719 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6720 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6721 MachineBasicBlock *BB = *I;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006722
6723 // Remove the landing pad successor from the invoke block and replace it
6724 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00006725 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6726 BB->succ_end());
6727 while (!Successors.empty()) {
6728 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling883ec972011-10-07 23:18:02 +00006729 if (SMBB->isLandingPad()) {
6730 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00006731 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006732 }
6733 }
6734
6735 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006736
6737 // Find the invoke call and mark all of the callee-saved registers as
6738 // 'implicit defined' so that they're spilled. This prevents code from
6739 // moving instructions to before the EH block, where they will never be
6740 // executed.
6741 for (MachineBasicBlock::reverse_iterator
6742 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00006743 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006744
6745 DenseMap<unsigned, bool> DefRegs;
6746 for (MachineInstr::mop_iterator
6747 OI = II->operands_begin(), OE = II->operands_end();
6748 OI != OE; ++OI) {
6749 if (!OI->isReg()) continue;
6750 DefRegs[OI->getReg()] = true;
6751 }
6752
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00006753 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006754
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006755 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00006756 unsigned Reg = SavedRegs[i];
6757 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00006758 !ARM::tGPRRegClass.contains(Reg) &&
6759 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006760 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006761 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006762 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006763 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006764 continue;
6765 if (!DefRegs[Reg])
6766 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006767 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006768
6769 break;
6770 }
Bill Wendling883ec972011-10-07 23:18:02 +00006771 }
Bill Wendling324be982011-10-05 00:39:32 +00006772
Bill Wendling617075f2011-10-18 18:30:49 +00006773 // Mark all former landing pads as non-landing pads. The dispatch is the only
6774 // landing pad now.
6775 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6776 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6777 (*I)->setIsLandingPad(false);
6778
Bill Wendling324be982011-10-05 00:39:32 +00006779 // The instruction is gone now.
6780 MI->eraseFromParent();
6781
Bill Wendling374ee192011-10-03 21:25:38 +00006782 return MBB;
6783}
6784
Evan Cheng0cc4ad92010-07-13 19:27:42 +00006785static
6786MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6787 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6788 E = MBB->succ_end(); I != E; ++I)
6789 if (*I != Succ)
6790 return *I;
6791 llvm_unreachable("Expecting a BB with two successors!");
6792}
6793
Manman Renb504f492013-10-29 22:27:32 +00006794/// Return the load opcode for a given load size. If load size >= 8,
6795/// neon opcode will be returned.
6796static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
6797 if (LdSize >= 8)
6798 return LdSize == 16 ? ARM::VLD1q32wb_fixed
6799 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
6800 if (IsThumb1)
6801 return LdSize == 4 ? ARM::tLDRi
6802 : LdSize == 2 ? ARM::tLDRHi
6803 : LdSize == 1 ? ARM::tLDRBi : 0;
6804 if (IsThumb2)
6805 return LdSize == 4 ? ARM::t2LDR_POST
6806 : LdSize == 2 ? ARM::t2LDRH_POST
6807 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
6808 return LdSize == 4 ? ARM::LDR_POST_IMM
6809 : LdSize == 2 ? ARM::LDRH_POST
6810 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
6811}
6812
6813/// Return the store opcode for a given store size. If store size >= 8,
6814/// neon opcode will be returned.
6815static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
6816 if (StSize >= 8)
6817 return StSize == 16 ? ARM::VST1q32wb_fixed
6818 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
6819 if (IsThumb1)
6820 return StSize == 4 ? ARM::tSTRi
6821 : StSize == 2 ? ARM::tSTRHi
6822 : StSize == 1 ? ARM::tSTRBi : 0;
6823 if (IsThumb2)
6824 return StSize == 4 ? ARM::t2STR_POST
6825 : StSize == 2 ? ARM::t2STRH_POST
6826 : StSize == 1 ? ARM::t2STRB_POST : 0;
6827 return StSize == 4 ? ARM::STR_POST_IMM
6828 : StSize == 2 ? ARM::STRH_POST
6829 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
6830}
6831
6832/// Emit a post-increment load operation with given size. The instructions
6833/// will be added to BB at Pos.
6834static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
6835 const TargetInstrInfo *TII, DebugLoc dl,
6836 unsigned LdSize, unsigned Data, unsigned AddrIn,
6837 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6838 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
6839 assert(LdOpc != 0 && "Should have a load opcode");
6840 if (LdSize >= 8) {
6841 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6842 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6843 .addImm(0));
6844 } else if (IsThumb1) {
6845 // load + update AddrIn
6846 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6847 .addReg(AddrIn).addImm(0));
6848 MachineInstrBuilder MIB =
6849 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6850 MIB = AddDefaultT1CC(MIB);
6851 MIB.addReg(AddrIn).addImm(LdSize);
6852 AddDefaultPred(MIB);
6853 } else if (IsThumb2) {
6854 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6855 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6856 .addImm(LdSize));
6857 } else { // arm
6858 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6859 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6860 .addReg(0).addImm(LdSize));
6861 }
6862}
6863
6864/// Emit a post-increment store operation with given size. The instructions
6865/// will be added to BB at Pos.
6866static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
6867 const TargetInstrInfo *TII, DebugLoc dl,
6868 unsigned StSize, unsigned Data, unsigned AddrIn,
6869 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6870 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
6871 assert(StOpc != 0 && "Should have a store opcode");
6872 if (StSize >= 8) {
6873 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6874 .addReg(AddrIn).addImm(0).addReg(Data));
6875 } else if (IsThumb1) {
6876 // store + update AddrIn
6877 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
6878 .addReg(AddrIn).addImm(0));
6879 MachineInstrBuilder MIB =
6880 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6881 MIB = AddDefaultT1CC(MIB);
6882 MIB.addReg(AddrIn).addImm(StSize);
6883 AddDefaultPred(MIB);
6884 } else if (IsThumb2) {
6885 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6886 .addReg(Data).addReg(AddrIn).addImm(StSize));
6887 } else { // arm
6888 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6889 .addReg(Data).addReg(AddrIn).addReg(0)
6890 .addImm(StSize));
6891 }
6892}
6893
David Peixottoc32e24a2013-10-17 19:49:22 +00006894MachineBasicBlock *
6895ARMTargetLowering::EmitStructByval(MachineInstr *MI,
6896 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00006897 // This pseudo instruction has 3 operands: dst, src, size
6898 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6899 // Otherwise, we will generate unrolled scalar copies.
6900 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6901 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6902 MachineFunction::iterator It = BB;
6903 ++It;
6904
6905 unsigned dest = MI->getOperand(0).getReg();
6906 unsigned src = MI->getOperand(1).getReg();
6907 unsigned SizeVal = MI->getOperand(2).getImm();
6908 unsigned Align = MI->getOperand(3).getImm();
6909 DebugLoc dl = MI->getDebugLoc();
6910
Manman Rene8735522012-06-01 19:33:18 +00006911 MachineFunction *MF = BB->getParent();
6912 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00006913 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00006914 const TargetRegisterClass *TRC = nullptr;
6915 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00006916
6917 bool IsThumb1 = Subtarget->isThumb1Only();
6918 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00006919
6920 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00006921 UnitSize = 1;
6922 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00006923 UnitSize = 2;
6924 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00006925 // Check whether we can use NEON instructions.
Bill Wendling698e84f2012-12-30 10:32:01 +00006926 if (!MF->getFunction()->getAttributes().
6927 hasAttribute(AttributeSet::FunctionIndex,
6928 Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00006929 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00006930 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00006931 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00006932 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00006933 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00006934 }
6935 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00006936 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00006937 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00006938 }
Manman Ren6e1fd462012-06-18 22:23:48 +00006939
David Peixottob0653e532013-10-24 16:39:36 +00006940 // Select the correct opcode and register class for unit size load/store
6941 bool IsNeon = UnitSize >= 8;
6942 TRC = (IsThumb1 || IsThumb2) ? (const TargetRegisterClass *)&ARM::tGPRRegClass
6943 : (const TargetRegisterClass *)&ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00006944 if (IsNeon)
David Peixottob0653e532013-10-24 16:39:36 +00006945 VecTRC = UnitSize == 16
6946 ? (const TargetRegisterClass *)&ARM::DPairRegClass
6947 : UnitSize == 8
6948 ? (const TargetRegisterClass *)&ARM::DPRRegClass
Craig Topper062a2ba2014-04-25 05:30:21 +00006949 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00006950
Manman Rene8735522012-06-01 19:33:18 +00006951 unsigned BytesLeft = SizeVal % UnitSize;
6952 unsigned LoopSize = SizeVal - BytesLeft;
6953
6954 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6955 // Use LDR and STR to copy.
6956 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6957 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6958 unsigned srcIn = src;
6959 unsigned destIn = dest;
6960 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00006961 unsigned srcOut = MRI.createVirtualRegister(TRC);
6962 unsigned destOut = MRI.createVirtualRegister(TRC);
6963 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00006964 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
6965 IsThumb1, IsThumb2);
6966 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
6967 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00006968 srcIn = srcOut;
6969 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00006970 }
6971
6972 // Handle the leftover bytes with LDRB and STRB.
6973 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6974 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00006975 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00006976 unsigned srcOut = MRI.createVirtualRegister(TRC);
6977 unsigned destOut = MRI.createVirtualRegister(TRC);
6978 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00006979 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
6980 IsThumb1, IsThumb2);
6981 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
6982 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00006983 srcIn = srcOut;
6984 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00006985 }
6986 MI->eraseFromParent(); // The instruction is gone now.
6987 return BB;
6988 }
6989
6990 // Expand the pseudo op to a loop.
6991 // thisMBB:
6992 // ...
6993 // movw varEnd, # --> with thumb2
6994 // movt varEnd, #
6995 // ldrcp varEnd, idx --> without thumb2
6996 // fallthrough --> loopMBB
6997 // loopMBB:
6998 // PHI varPhi, varEnd, varLoop
6999 // PHI srcPhi, src, srcLoop
7000 // PHI destPhi, dst, destLoop
7001 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7002 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
7003 // subs varLoop, varPhi, #UnitSize
7004 // bne loopMBB
7005 // fallthrough --> exitMBB
7006 // exitMBB:
7007 // epilogue to handle left-over bytes
7008 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7009 // [destOut] = STRB_POST(scratch, destLoop, 1)
7010 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7011 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
7012 MF->insert(It, loopMBB);
7013 MF->insert(It, exitMBB);
7014
7015 // Transfer the remainder of BB and its successor edges to exitMBB.
7016 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007017 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007018 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7019
7020 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007021 unsigned varEnd = MRI.createVirtualRegister(TRC);
7022 if (IsThumb2) {
7023 unsigned Vtmp = varEnd;
7024 if ((LoopSize & 0xFFFF0000) != 0)
7025 Vtmp = MRI.createVirtualRegister(TRC);
7026 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), Vtmp)
7027 .addImm(LoopSize & 0xFFFF));
7028
7029 if ((LoopSize & 0xFFFF0000) != 0)
7030 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
7031 .addReg(Vtmp).addImm(LoopSize >> 16));
7032 } else {
7033 MachineConstantPool *ConstantPool = MF->getConstantPool();
7034 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7035 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7036
7037 // MachineConstantPool wants an explicit alignment.
7038 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
7039 if (Align == 0)
7040 Align = getDataLayout()->getTypeAllocSize(C->getType());
7041 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7042
7043 if (IsThumb1)
7044 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7045 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7046 else
7047 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7048 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7049 }
Manman Rene8735522012-06-01 19:33:18 +00007050 BB->addSuccessor(loopMBB);
7051
7052 // Generate the loop body:
7053 // varPhi = PHI(varLoop, varEnd)
7054 // srcPhi = PHI(srcLoop, src)
7055 // destPhi = PHI(destLoop, dst)
7056 MachineBasicBlock *entryBB = BB;
7057 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007058 unsigned varLoop = MRI.createVirtualRegister(TRC);
7059 unsigned varPhi = MRI.createVirtualRegister(TRC);
7060 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7061 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7062 unsigned destLoop = MRI.createVirtualRegister(TRC);
7063 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007064
7065 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7066 .addReg(varLoop).addMBB(loopMBB)
7067 .addReg(varEnd).addMBB(entryBB);
7068 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7069 .addReg(srcLoop).addMBB(loopMBB)
7070 .addReg(src).addMBB(entryBB);
7071 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7072 .addReg(destLoop).addMBB(loopMBB)
7073 .addReg(dest).addMBB(entryBB);
7074
7075 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7076 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007077 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007078 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7079 IsThumb1, IsThumb2);
7080 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7081 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007082
7083 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007084 if (IsThumb1) {
7085 MachineInstrBuilder MIB =
7086 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7087 MIB = AddDefaultT1CC(MIB);
7088 MIB.addReg(varPhi).addImm(UnitSize);
7089 AddDefaultPred(MIB);
7090 } else {
7091 MachineInstrBuilder MIB =
7092 BuildMI(*BB, BB->end(), dl,
7093 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7094 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7095 MIB->getOperand(5).setReg(ARM::CPSR);
7096 MIB->getOperand(5).setIsDef(true);
7097 }
7098 BuildMI(*BB, BB->end(), dl,
7099 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7100 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007101
7102 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7103 BB->addSuccessor(loopMBB);
7104 BB->addSuccessor(exitMBB);
7105
7106 // Add epilogue to handle BytesLeft.
7107 BB = exitMBB;
7108 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007109
7110 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7111 // [destOut] = STRB_POST(scratch, destLoop, 1)
7112 unsigned srcIn = srcLoop;
7113 unsigned destIn = destLoop;
7114 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007115 unsigned srcOut = MRI.createVirtualRegister(TRC);
7116 unsigned destOut = MRI.createVirtualRegister(TRC);
7117 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007118 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7119 IsThumb1, IsThumb2);
7120 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7121 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007122 srcIn = srcOut;
7123 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007124 }
7125
7126 MI->eraseFromParent(); // The instruction is gone now.
7127 return BB;
7128}
7129
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007130MachineBasicBlock *
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007131ARMTargetLowering::EmitLowered__chkstk(MachineInstr *MI,
7132 MachineBasicBlock *MBB) const {
7133 const TargetMachine &TM = getTargetMachine();
7134 const TargetInstrInfo &TII = *TM.getInstrInfo();
7135 DebugLoc DL = MI->getDebugLoc();
7136
7137 assert(Subtarget->isTargetWindows() &&
7138 "__chkstk is only supported on Windows");
7139 assert(Subtarget->isThumb2() && "Windows on ARM requires Thumb-2 mode");
7140
7141 // __chkstk takes the number of words to allocate on the stack in R4, and
7142 // returns the stack adjustment in number of bytes in R4. This will not
7143 // clober any other registers (other than the obvious lr).
7144 //
7145 // Although, technically, IP should be considered a register which may be
7146 // clobbered, the call itself will not touch it. Windows on ARM is a pure
7147 // thumb-2 environment, so there is no interworking required. As a result, we
7148 // do not expect a veneer to be emitted by the linker, clobbering IP.
7149 //
Alp Toker1d099d92014-06-19 19:41:26 +00007150 // Each module receives its own copy of __chkstk, so no import thunk is
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007151 // required, again, ensuring that IP is not clobbered.
7152 //
7153 // Finally, although some linkers may theoretically provide a trampoline for
7154 // out of range calls (which is quite common due to a 32M range limitation of
7155 // branches for Thumb), we can generate the long-call version via
7156 // -mcmodel=large, alleviating the need for the trampoline which may clobber
7157 // IP.
7158
7159 switch (TM.getCodeModel()) {
7160 case CodeModel::Small:
7161 case CodeModel::Medium:
7162 case CodeModel::Default:
7163 case CodeModel::Kernel:
7164 BuildMI(*MBB, MI, DL, TII.get(ARM::tBL))
7165 .addImm((unsigned)ARMCC::AL).addReg(0)
7166 .addExternalSymbol("__chkstk")
7167 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7168 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7169 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7170 break;
7171 case CodeModel::Large:
7172 case CodeModel::JITDefault: {
7173 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
7174 unsigned Reg = MRI.createVirtualRegister(&ARM::rGPRRegClass);
7175
7176 BuildMI(*MBB, MI, DL, TII.get(ARM::t2MOVi32imm), Reg)
7177 .addExternalSymbol("__chkstk");
7178 BuildMI(*MBB, MI, DL, TII.get(ARM::tBLXr))
7179 .addImm((unsigned)ARMCC::AL).addReg(0)
7180 .addReg(Reg, RegState::Kill)
7181 .addReg(ARM::R4, RegState::Implicit | RegState::Kill)
7182 .addReg(ARM::R4, RegState::Implicit | RegState::Define)
7183 .addReg(ARM::R12, RegState::Implicit | RegState::Define | RegState::Dead);
7184 break;
7185 }
7186 }
7187
7188 AddDefaultCC(AddDefaultPred(BuildMI(*MBB, MI, DL, TII.get(ARM::t2SUBrr),
7189 ARM::SP)
7190 .addReg(ARM::SP, RegState::Define)
7191 .addReg(ARM::R4, RegState::Kill)));
7192
7193 MI->eraseFromParent();
7194 return MBB;
7195}
7196
7197MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007198ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007199 MachineBasicBlock *BB) const {
Evan Cheng10043e22007-01-19 07:51:42 +00007200 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007201 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007202 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007203 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007204 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007205 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007206 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007207 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007208 // The Thumb2 pre-indexed stores have the same MI operands, they just
7209 // define them differently in the .td files from the isel patterns, so
7210 // they need pseudos.
7211 case ARM::t2STR_preidx:
7212 MI->setDesc(TII->get(ARM::t2STR_PRE));
7213 return BB;
7214 case ARM::t2STRB_preidx:
7215 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7216 return BB;
7217 case ARM::t2STRH_preidx:
7218 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7219 return BB;
7220
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007221 case ARM::STRi_preidx:
7222 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007223 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007224 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7225 // Decode the offset.
7226 unsigned Offset = MI->getOperand(4).getImm();
7227 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7228 Offset = ARM_AM::getAM2Offset(Offset);
7229 if (isSub)
7230 Offset = -Offset;
7231
Jim Grosbachf402f692011-08-12 21:02:34 +00007232 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007233 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007234 .addOperand(MI->getOperand(0)) // Rn_wb
7235 .addOperand(MI->getOperand(1)) // Rt
7236 .addOperand(MI->getOperand(2)) // Rn
7237 .addImm(Offset) // offset (skip GPR==zero_reg)
7238 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007239 .addOperand(MI->getOperand(6))
7240 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007241 MI->eraseFromParent();
7242 return BB;
7243 }
7244 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007245 case ARM::STRBr_preidx:
7246 case ARM::STRH_preidx: {
7247 unsigned NewOpc;
7248 switch (MI->getOpcode()) {
7249 default: llvm_unreachable("unexpected opcode!");
7250 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7251 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7252 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7253 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007254 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7255 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7256 MIB.addOperand(MI->getOperand(i));
7257 MI->eraseFromParent();
7258 return BB;
7259 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007260
Evan Chengbb2af352009-08-12 05:17:19 +00007261 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007262 // To "insert" a SELECT_CC instruction, we actually have to insert the
7263 // diamond control-flow pattern. The incoming instruction knows the
7264 // destination vreg to set, the condition code register to branch on, the
7265 // true/false values to select between, and a branch opcode to use.
7266 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007267 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007268 ++It;
7269
7270 // thisMBB:
7271 // ...
7272 // TrueVal = ...
7273 // cmpTY ccX, r1, r2
7274 // bCC copy1MBB
7275 // fallthrough --> copy0MBB
7276 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007277 MachineFunction *F = BB->getParent();
7278 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7279 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007280 F->insert(It, copy0MBB);
7281 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007282
7283 // Transfer the remainder of BB and its successor edges to sinkMBB.
7284 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007285 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007286 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7287
Dan Gohmanf4f04102010-07-06 15:49:48 +00007288 BB->addSuccessor(copy0MBB);
7289 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007290
Dan Gohman34396292010-07-06 20:24:04 +00007291 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7292 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7293
Evan Cheng10043e22007-01-19 07:51:42 +00007294 // copy0MBB:
7295 // %FalseValue = ...
7296 // # fallthrough to sinkMBB
7297 BB = copy0MBB;
7298
7299 // Update machine-CFG edges
7300 BB->addSuccessor(sinkMBB);
7301
7302 // sinkMBB:
7303 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7304 // ...
7305 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007306 BuildMI(*BB, BB->begin(), dl,
7307 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007308 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7309 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7310
Dan Gohman34396292010-07-06 20:24:04 +00007311 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007312 return BB;
7313 }
Evan Chengb972e562009-08-07 00:34:42 +00007314
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007315 case ARM::BCCi64:
7316 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007317 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007318 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007319
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007320 // Compare both parts that make up the double comparison separately for
7321 // equality.
7322 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7323
7324 unsigned LHS1 = MI->getOperand(1).getReg();
7325 unsigned LHS2 = MI->getOperand(2).getReg();
7326 if (RHSisZero) {
7327 AddDefaultPred(BuildMI(BB, dl,
7328 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7329 .addReg(LHS1).addImm(0));
7330 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7331 .addReg(LHS2).addImm(0)
7332 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7333 } else {
7334 unsigned RHS1 = MI->getOperand(3).getReg();
7335 unsigned RHS2 = MI->getOperand(4).getReg();
7336 AddDefaultPred(BuildMI(BB, dl,
7337 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7338 .addReg(LHS1).addReg(RHS1));
7339 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7340 .addReg(LHS2).addReg(RHS2)
7341 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7342 }
7343
7344 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7345 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7346 if (MI->getOperand(0).getImm() == ARMCC::NE)
7347 std::swap(destMBB, exitMBB);
7348
7349 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7350 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007351 if (isThumb2)
7352 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7353 else
7354 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007355
7356 MI->eraseFromParent(); // The pseudo instruction is gone now.
7357 return BB;
7358 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007359
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007360 case ARM::Int_eh_sjlj_setjmp:
7361 case ARM::Int_eh_sjlj_setjmp_nofp:
7362 case ARM::tInt_eh_sjlj_setjmp:
7363 case ARM::t2Int_eh_sjlj_setjmp:
7364 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7365 EmitSjLjDispatchBlock(MI, BB);
7366 return BB;
7367
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007368 case ARM::ABS:
7369 case ARM::t2ABS: {
7370 // To insert an ABS instruction, we have to insert the
7371 // diamond control-flow pattern. The incoming instruction knows the
7372 // source vreg to test against 0, the destination vreg to set,
7373 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007374 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007375 // It transforms
7376 // V1 = ABS V0
7377 // into
7378 // V2 = MOVS V0
7379 // BCC (branch to SinkBB if V0 >= 0)
7380 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007381 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007382 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7383 MachineFunction::iterator BBI = BB;
7384 ++BBI;
7385 MachineFunction *Fn = BB->getParent();
7386 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7387 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7388 Fn->insert(BBI, RSBBB);
7389 Fn->insert(BBI, SinkBB);
7390
7391 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7392 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7393 bool isThumb2 = Subtarget->isThumb2();
7394 MachineRegisterInfo &MRI = Fn->getRegInfo();
7395 // In Thumb mode S must not be specified if source register is the SP or
7396 // PC and if destination register is the SP, so restrict register class
Craig Topperc7242e02012-04-20 07:30:17 +00007397 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
7398 (const TargetRegisterClass*)&ARM::rGPRRegClass :
7399 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007400
7401 // Transfer the remainder of BB and its successor edges to sinkMBB.
7402 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007403 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007404 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7405
7406 BB->addSuccessor(RSBBB);
7407 BB->addSuccessor(SinkBB);
7408
7409 // fall through to SinkMBB
7410 RSBBB->addSuccessor(SinkBB);
7411
Manman Rene0763c72012-06-15 21:32:12 +00007412 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00007413 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00007414 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7415 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007416
7417 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00007418 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007419 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7420 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7421
7422 // insert rsbri in RSBBB
7423 // Note: BCC and rsbri will be converted into predicated rsbmi
7424 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00007425 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007426 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Rene0763c72012-06-15 21:32:12 +00007427 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007428 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7429
Andrew Trick3f07c422011-10-18 18:40:53 +00007430 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007431 // reuse ABSDstReg to not change uses of ABS instruction
7432 BuildMI(*SinkBB, SinkBB->begin(), dl,
7433 TII->get(ARM::PHI), ABSDstReg)
7434 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00007435 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007436
7437 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00007438 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007439
7440 // return last added BB
7441 return SinkBB;
7442 }
Manman Rene8735522012-06-01 19:33:18 +00007443 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00007444 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00007445 return EmitStructByval(MI, BB);
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +00007446 case ARM::WIN__CHKSTK:
7447 return EmitLowered__chkstk(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00007448 }
7449}
7450
Evan Chenge6fba772011-08-30 19:09:48 +00007451void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7452 SDNode *Node) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007453 if (!MI->hasPostISelHook()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007454 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
7455 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
7456 return;
7457 }
7458
Evan Cheng7f8e5632011-12-07 07:15:52 +00007459 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00007460 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7461 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7462 // operand is still set to noreg. If needed, set the optional operand's
7463 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00007464 //
Andrew Trick88b24502011-10-18 19:18:52 +00007465 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00007466
Andrew Trick924123a2011-09-21 02:20:46 +00007467 // Rename pseudo opcodes.
7468 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7469 if (NewOpc) {
7470 const ARMBaseInstrInfo *TII =
7471 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick88b24502011-10-18 19:18:52 +00007472 MCID = &TII->get(NewOpc);
7473
7474 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7475 "converted opcode should be the same except for cc_out");
7476
7477 MI->setDesc(*MCID);
7478
7479 // Add the optional cc_out operand
7480 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00007481 }
Andrew Trick88b24502011-10-18 19:18:52 +00007482 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00007483
7484 // Any ARM instruction that sets the 's' bit should specify an optional
7485 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00007486 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007487 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007488 return;
7489 }
Andrew Trick924123a2011-09-21 02:20:46 +00007490 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7491 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007492 bool definesCPSR = false;
7493 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00007494 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00007495 i != e; ++i) {
7496 const MachineOperand &MO = MI->getOperand(i);
7497 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7498 definesCPSR = true;
7499 if (MO.isDead())
7500 deadCPSR = true;
7501 MI->RemoveOperand(i);
7502 break;
Evan Chenge6fba772011-08-30 19:09:48 +00007503 }
7504 }
Andrew Trick8586e622011-09-20 03:17:40 +00007505 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00007506 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007507 return;
7508 }
7509 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00007510 if (deadCPSR) {
7511 assert(!MI->getOperand(ccOutIdx).getReg() &&
7512 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00007513 return;
Andrew Trick924123a2011-09-21 02:20:46 +00007514 }
Andrew Trick8586e622011-09-20 03:17:40 +00007515
Andrew Trick924123a2011-09-21 02:20:46 +00007516 // If this instruction was defined with an optional CPSR def and its dag node
7517 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007518 MachineOperand &MO = MI->getOperand(ccOutIdx);
7519 MO.setReg(ARM::CPSR);
7520 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00007521}
7522
Evan Cheng10043e22007-01-19 07:51:42 +00007523//===----------------------------------------------------------------------===//
7524// ARM Optimization Hooks
7525//===----------------------------------------------------------------------===//
7526
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007527// Helper function that checks if N is a null or all ones constant.
7528static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7529 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7530 if (!C)
7531 return false;
7532 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7533}
7534
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007535// Return true if N is conditionally 0 or all ones.
7536// Detects these expressions where cc is an i1 value:
7537//
7538// (select cc 0, y) [AllOnes=0]
7539// (select cc y, 0) [AllOnes=0]
7540// (zext cc) [AllOnes=0]
7541// (sext cc) [AllOnes=0/1]
7542// (select cc -1, y) [AllOnes=1]
7543// (select cc y, -1) [AllOnes=1]
7544//
7545// Invert is set when N is the null/all ones constant when CC is false.
7546// OtherOp is set to the alternative value of N.
7547static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7548 SDValue &CC, bool &Invert,
7549 SDValue &OtherOp,
7550 SelectionDAG &DAG) {
7551 switch (N->getOpcode()) {
7552 default: return false;
7553 case ISD::SELECT: {
7554 CC = N->getOperand(0);
7555 SDValue N1 = N->getOperand(1);
7556 SDValue N2 = N->getOperand(2);
7557 if (isZeroOrAllOnes(N1, AllOnes)) {
7558 Invert = false;
7559 OtherOp = N2;
7560 return true;
7561 }
7562 if (isZeroOrAllOnes(N2, AllOnes)) {
7563 Invert = true;
7564 OtherOp = N1;
7565 return true;
7566 }
7567 return false;
7568 }
7569 case ISD::ZERO_EXTEND:
7570 // (zext cc) can never be the all ones value.
7571 if (AllOnes)
7572 return false;
7573 // Fall through.
7574 case ISD::SIGN_EXTEND: {
7575 EVT VT = N->getValueType(0);
7576 CC = N->getOperand(0);
7577 if (CC.getValueType() != MVT::i1)
7578 return false;
7579 Invert = !AllOnes;
7580 if (AllOnes)
7581 // When looking for an AllOnes constant, N is an sext, and the 'other'
7582 // value is 0.
7583 OtherOp = DAG.getConstant(0, VT);
7584 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7585 // When looking for a 0 constant, N can be zext or sext.
7586 OtherOp = DAG.getConstant(1, VT);
7587 else
7588 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7589 return true;
7590 }
7591 }
7592}
7593
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007594// Combine a constant select operand into its use:
7595//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007596// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7597// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7598// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7599// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7600// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007601//
7602// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007603// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007604//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007605// Also recognize sext/zext from i1:
7606//
7607// (add (zext cc), x) -> (select cc (add x, 1), x)
7608// (add (sext cc), x) -> (select cc (add x, -1), x)
7609//
7610// These transformations eventually create predicated instructions.
7611//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007612// @param N The node to transform.
7613// @param Slct The N operand that is a select.
7614// @param OtherOp The other N operand (x above).
7615// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007616// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007617// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00007618static
7619SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007620 TargetLowering::DAGCombinerInfo &DCI,
7621 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00007622 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007623 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007624 SDValue NonConstantVal;
7625 SDValue CCOp;
7626 bool SwapSelectOps;
7627 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7628 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007629 return SDValue();
7630
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007631 // Slct is now know to be the desired identity constant when CC is true.
7632 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007633 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007634 OtherOp, NonConstantVal);
7635 // Unless SwapSelectOps says CC should be false.
7636 if (SwapSelectOps)
7637 std::swap(TrueVal, FalseVal);
7638
Andrew Trickef9de2a2013-05-25 02:42:55 +00007639 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007640 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00007641}
7642
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007643// Attempt combineSelectAndUse on each operand of a commutative operator N.
7644static
7645SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7646 TargetLowering::DAGCombinerInfo &DCI) {
7647 SDValue N0 = N->getOperand(0);
7648 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007649 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007650 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7651 if (Result.getNode())
7652 return Result;
7653 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007654 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007655 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7656 if (Result.getNode())
7657 return Result;
7658 }
7659 return SDValue();
7660}
7661
Eric Christopher1b8b94192011-06-29 21:10:36 +00007662// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00007663// (only after legalization).
7664static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7665 TargetLowering::DAGCombinerInfo &DCI,
7666 const ARMSubtarget *Subtarget) {
7667
7668 // Only perform optimization if after legalize, and if NEON is available. We
7669 // also expected both operands to be BUILD_VECTORs.
7670 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7671 || N0.getOpcode() != ISD::BUILD_VECTOR
7672 || N1.getOpcode() != ISD::BUILD_VECTOR)
7673 return SDValue();
7674
7675 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7676 EVT VT = N->getValueType(0);
7677 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7678 return SDValue();
7679
7680 // Check that the vector operands are of the right form.
7681 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7682 // operands, where N is the size of the formed vector.
7683 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7684 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007685
7686 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00007687 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00007688 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00007689 SDValue Vec = N0->getOperand(0)->getOperand(0);
7690 SDNode *V = Vec.getNode();
7691 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00007692
Eric Christopher1b8b94192011-06-29 21:10:36 +00007693 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007694 // check to see if each of their operands are an EXTRACT_VECTOR with
7695 // the same vector and appropriate index.
7696 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7697 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7698 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00007699
Tanya Lattnere9e67052011-06-14 23:48:48 +00007700 SDValue ExtVec0 = N0->getOperand(i);
7701 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007702
Tanya Lattnere9e67052011-06-14 23:48:48 +00007703 // First operand is the vector, verify its the same.
7704 if (V != ExtVec0->getOperand(0).getNode() ||
7705 V != ExtVec1->getOperand(0).getNode())
7706 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00007707
Tanya Lattnere9e67052011-06-14 23:48:48 +00007708 // Second is the constant, verify its correct.
7709 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7710 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00007711
Tanya Lattnere9e67052011-06-14 23:48:48 +00007712 // For the constant, we want to see all the even or all the odd.
7713 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7714 || C1->getZExtValue() != nextIndex+1)
7715 return SDValue();
7716
7717 // Increment index.
7718 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007719 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00007720 return SDValue();
7721 }
7722
7723 // Create VPADDL node.
7724 SelectionDAG &DAG = DCI.DAG;
7725 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00007726
7727 // Build operand list.
7728 SmallVector<SDValue, 8> Ops;
7729 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7730 TLI.getPointerTy()));
7731
7732 // Input is the vector.
7733 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007734
Tanya Lattnere9e67052011-06-14 23:48:48 +00007735 // Get widened type and narrowed type.
7736 MVT widenType;
7737 unsigned numElem = VT.getVectorNumElements();
Silviu Barangaa3106e62014-04-03 10:44:27 +00007738
7739 EVT inputLaneType = Vec.getValueType().getVectorElementType();
7740 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00007741 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7742 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7743 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7744 default:
Craig Toppere55c5562012-02-07 02:50:20 +00007745 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00007746 }
7747
Craig Topper48d114b2014-04-26 18:35:24 +00007748 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00007749 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
7750 return DAG.getNode(ExtOp, SDLoc(N), VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00007751}
7752
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007753static SDValue findMUL_LOHI(SDValue V) {
7754 if (V->getOpcode() == ISD::UMUL_LOHI ||
7755 V->getOpcode() == ISD::SMUL_LOHI)
7756 return V;
7757 return SDValue();
7758}
7759
7760static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7761 TargetLowering::DAGCombinerInfo &DCI,
7762 const ARMSubtarget *Subtarget) {
7763
7764 if (Subtarget->isThumb1Only()) return SDValue();
7765
7766 // Only perform the checks after legalize when the pattern is available.
7767 if (DCI.isBeforeLegalize()) return SDValue();
7768
7769 // Look for multiply add opportunities.
7770 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7771 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7772 // a glue link from the first add to the second add.
7773 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7774 // a S/UMLAL instruction.
7775 // loAdd UMUL_LOHI
7776 // \ / :lo \ :hi
7777 // \ / \ [no multiline comment]
7778 // ADDC | hiAdd
7779 // \ :glue / /
7780 // \ / /
7781 // ADDE
7782 //
7783 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7784 SDValue AddcOp0 = AddcNode->getOperand(0);
7785 SDValue AddcOp1 = AddcNode->getOperand(1);
7786
7787 // Check if the two operands are from the same mul_lohi node.
7788 if (AddcOp0.getNode() == AddcOp1.getNode())
7789 return SDValue();
7790
7791 assert(AddcNode->getNumValues() == 2 &&
7792 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00007793 "Expect ADDC with two result values. First: i32");
7794
7795 // Check that we have a glued ADDC node.
7796 if (AddcNode->getValueType(1) != MVT::Glue)
7797 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007798
7799 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7800 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7801 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7802 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7803 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7804 return SDValue();
7805
7806 // Look for the glued ADDE.
7807 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00007808 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007809 return SDValue();
7810
7811 // Make sure it is really an ADDE.
7812 if (AddeNode->getOpcode() != ISD::ADDE)
7813 return SDValue();
7814
7815 assert(AddeNode->getNumOperands() == 3 &&
7816 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7817 "ADDE node has the wrong inputs");
7818
7819 // Check for the triangle shape.
7820 SDValue AddeOp0 = AddeNode->getOperand(0);
7821 SDValue AddeOp1 = AddeNode->getOperand(1);
7822
7823 // Make sure that the ADDE operands are not coming from the same node.
7824 if (AddeOp0.getNode() == AddeOp1.getNode())
7825 return SDValue();
7826
7827 // Find the MUL_LOHI node walking up ADDE's operands.
7828 bool IsLeftOperandMUL = false;
7829 SDValue MULOp = findMUL_LOHI(AddeOp0);
7830 if (MULOp == SDValue())
7831 MULOp = findMUL_LOHI(AddeOp1);
7832 else
7833 IsLeftOperandMUL = true;
7834 if (MULOp == SDValue())
7835 return SDValue();
7836
7837 // Figure out the right opcode.
7838 unsigned Opc = MULOp->getOpcode();
7839 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7840
7841 // Figure out the high and low input values to the MLAL node.
7842 SDValue* HiMul = &MULOp;
Craig Topper062a2ba2014-04-25 05:30:21 +00007843 SDValue* HiAdd = nullptr;
7844 SDValue* LoMul = nullptr;
7845 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007846
7847 if (IsLeftOperandMUL)
7848 HiAdd = &AddeOp1;
7849 else
7850 HiAdd = &AddeOp0;
7851
7852
7853 if (AddcOp0->getOpcode() == Opc) {
7854 LoMul = &AddcOp0;
7855 LowAdd = &AddcOp1;
7856 }
7857 if (AddcOp1->getOpcode() == Opc) {
7858 LoMul = &AddcOp1;
7859 LowAdd = &AddcOp0;
7860 }
7861
Craig Topper062a2ba2014-04-25 05:30:21 +00007862 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007863 return SDValue();
7864
7865 if (LoMul->getNode() != HiMul->getNode())
7866 return SDValue();
7867
7868 // Create the merged node.
7869 SelectionDAG &DAG = DCI.DAG;
7870
7871 // Build operand list.
7872 SmallVector<SDValue, 8> Ops;
7873 Ops.push_back(LoMul->getOperand(0));
7874 Ops.push_back(LoMul->getOperand(1));
7875 Ops.push_back(*LowAdd);
7876 Ops.push_back(*HiAdd);
7877
Andrew Trickef9de2a2013-05-25 02:42:55 +00007878 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00007879 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007880
7881 // Replace the ADDs' nodes uses by the MLA node's values.
7882 SDValue HiMLALResult(MLALNode.getNode(), 1);
7883 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7884
7885 SDValue LoMLALResult(MLALNode.getNode(), 0);
7886 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7887
7888 // Return original node to notify the driver to stop replacing.
7889 SDValue resNode(AddcNode, 0);
7890 return resNode;
7891}
7892
7893/// PerformADDCCombine - Target-specific dag combine transform from
7894/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7895static SDValue PerformADDCCombine(SDNode *N,
7896 TargetLowering::DAGCombinerInfo &DCI,
7897 const ARMSubtarget *Subtarget) {
7898
7899 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7900
7901}
7902
Bob Wilson728eb292010-07-29 20:34:14 +00007903/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7904/// operands N0 and N1. This is a helper for PerformADDCombine that is
7905/// called with the default operands, and if that fails, with commuted
7906/// operands.
7907static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007908 TargetLowering::DAGCombinerInfo &DCI,
7909 const ARMSubtarget *Subtarget){
7910
7911 // Attempt to create vpaddl for this add.
7912 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7913 if (Result.getNode())
7914 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007915
Chris Lattner4147f082009-03-12 06:52:53 +00007916 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007917 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00007918 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7919 if (Result.getNode()) return Result;
7920 }
Chris Lattner4147f082009-03-12 06:52:53 +00007921 return SDValue();
7922}
7923
Bob Wilson728eb292010-07-29 20:34:14 +00007924/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7925///
7926static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007927 TargetLowering::DAGCombinerInfo &DCI,
7928 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00007929 SDValue N0 = N->getOperand(0);
7930 SDValue N1 = N->getOperand(1);
7931
7932 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007933 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00007934 if (Result.getNode())
7935 return Result;
7936
7937 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007938 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00007939}
7940
Chris Lattner4147f082009-03-12 06:52:53 +00007941/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00007942///
Chris Lattner4147f082009-03-12 06:52:53 +00007943static SDValue PerformSUBCombine(SDNode *N,
7944 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00007945 SDValue N0 = N->getOperand(0);
7946 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00007947
Chris Lattner4147f082009-03-12 06:52:53 +00007948 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007949 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00007950 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7951 if (Result.getNode()) return Result;
7952 }
Bob Wilson7117a912009-03-20 22:42:55 +00007953
Chris Lattner4147f082009-03-12 06:52:53 +00007954 return SDValue();
7955}
7956
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007957/// PerformVMULCombine
7958/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7959/// special multiplier accumulator forwarding.
7960/// vmul d3, d0, d2
7961/// vmla d3, d1, d2
7962/// is faster than
7963/// vadd d3, d0, d1
7964/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00007965// However, for (A + B) * (A + B),
7966// vadd d2, d0, d1
7967// vmul d3, d0, d2
7968// vmla d3, d1, d2
7969// is slower than
7970// vadd d2, d0, d1
7971// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007972static SDValue PerformVMULCombine(SDNode *N,
7973 TargetLowering::DAGCombinerInfo &DCI,
7974 const ARMSubtarget *Subtarget) {
7975 if (!Subtarget->hasVMLxForwarding())
7976 return SDValue();
7977
7978 SelectionDAG &DAG = DCI.DAG;
7979 SDValue N0 = N->getOperand(0);
7980 SDValue N1 = N->getOperand(1);
7981 unsigned Opcode = N0.getOpcode();
7982 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7983 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00007984 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007985 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7986 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7987 return SDValue();
7988 std::swap(N0, N1);
7989 }
7990
Weiming Zhao2052f482013-09-25 23:12:06 +00007991 if (N0 == N1)
7992 return SDValue();
7993
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007994 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007995 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007996 SDValue N00 = N0->getOperand(0);
7997 SDValue N01 = N0->getOperand(1);
7998 return DAG.getNode(Opcode, DL, VT,
7999 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
8000 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
8001}
8002
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008003static SDValue PerformMULCombine(SDNode *N,
8004 TargetLowering::DAGCombinerInfo &DCI,
8005 const ARMSubtarget *Subtarget) {
8006 SelectionDAG &DAG = DCI.DAG;
8007
8008 if (Subtarget->isThumb1Only())
8009 return SDValue();
8010
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008011 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8012 return SDValue();
8013
8014 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00008015 if (VT.is64BitVector() || VT.is128BitVector())
8016 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008017 if (VT != MVT::i32)
8018 return SDValue();
8019
8020 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8021 if (!C)
8022 return SDValue();
8023
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008024 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008025 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008026
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008027 ShiftAmt = ShiftAmt & (32 - 1);
8028 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008029 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008030
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008031 SDValue Res;
8032 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008033
8034 if (MulAmt >= 0) {
8035 if (isPowerOf2_32(MulAmt - 1)) {
8036 // (mul x, 2^N + 1) => (add (shl x, N), x)
8037 Res = DAG.getNode(ISD::ADD, DL, VT,
8038 V,
8039 DAG.getNode(ISD::SHL, DL, VT,
8040 V,
8041 DAG.getConstant(Log2_32(MulAmt - 1),
8042 MVT::i32)));
8043 } else if (isPowerOf2_32(MulAmt + 1)) {
8044 // (mul x, 2^N - 1) => (sub (shl x, N), x)
8045 Res = DAG.getNode(ISD::SUB, DL, VT,
8046 DAG.getNode(ISD::SHL, DL, VT,
8047 V,
8048 DAG.getConstant(Log2_32(MulAmt + 1),
8049 MVT::i32)),
8050 V);
8051 } else
8052 return SDValue();
8053 } else {
8054 uint64_t MulAmtAbs = -MulAmt;
8055 if (isPowerOf2_32(MulAmtAbs + 1)) {
8056 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
8057 Res = DAG.getNode(ISD::SUB, DL, VT,
8058 V,
8059 DAG.getNode(ISD::SHL, DL, VT,
8060 V,
8061 DAG.getConstant(Log2_32(MulAmtAbs + 1),
8062 MVT::i32)));
8063 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
8064 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
8065 Res = DAG.getNode(ISD::ADD, DL, VT,
8066 V,
8067 DAG.getNode(ISD::SHL, DL, VT,
8068 V,
8069 DAG.getConstant(Log2_32(MulAmtAbs-1),
8070 MVT::i32)));
8071 Res = DAG.getNode(ISD::SUB, DL, VT,
8072 DAG.getConstant(0, MVT::i32),Res);
8073
8074 } else
8075 return SDValue();
8076 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008077
8078 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00008079 Res = DAG.getNode(ISD::SHL, DL, VT,
8080 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008081
8082 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00008083 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00008084 return SDValue();
8085}
8086
Owen Anderson30c48922010-11-05 19:27:46 +00008087static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008088 TargetLowering::DAGCombinerInfo &DCI,
8089 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008090
Owen Anderson30c48922010-11-05 19:27:46 +00008091 // Attempt to use immediate-form VBIC
8092 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008093 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008094 EVT VT = N->getValueType(0);
8095 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008096
Tanya Lattner266792a2011-04-07 15:24:20 +00008097 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8098 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008099
Owen Anderson30c48922010-11-05 19:27:46 +00008100 APInt SplatBits, SplatUndef;
8101 unsigned SplatBitSize;
8102 bool HasAnyUndefs;
8103 if (BVN &&
8104 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8105 if (SplatBitSize <= 64) {
8106 EVT VbicVT;
8107 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8108 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00008109 DAG, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008110 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008111 if (Val.getNode()) {
8112 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008113 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008114 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008115 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008116 }
8117 }
8118 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008119
Evan Chenge87681c2012-02-23 01:19:06 +00008120 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008121 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8122 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8123 if (Result.getNode())
8124 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008125 }
8126
Owen Anderson30c48922010-11-05 19:27:46 +00008127 return SDValue();
8128}
8129
Jim Grosbach11013ed2010-07-16 23:05:05 +00008130/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8131static SDValue PerformORCombine(SDNode *N,
8132 TargetLowering::DAGCombinerInfo &DCI,
8133 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008134 // Attempt to use immediate-form VORR
8135 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008136 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008137 EVT VT = N->getValueType(0);
8138 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008139
Tanya Lattner266792a2011-04-07 15:24:20 +00008140 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8141 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008142
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008143 APInt SplatBits, SplatUndef;
8144 unsigned SplatBitSize;
8145 bool HasAnyUndefs;
8146 if (BVN && Subtarget->hasNEON() &&
8147 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8148 if (SplatBitSize <= 64) {
8149 EVT VorrVT;
8150 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8151 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00008152 DAG, VorrVT, VT.is128BitVector(),
8153 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008154 if (Val.getNode()) {
8155 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008156 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008157 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008158 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008159 }
8160 }
8161 }
8162
Evan Chenge87681c2012-02-23 01:19:06 +00008163 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008164 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8165 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8166 if (Result.getNode())
8167 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008168 }
8169
Nadav Rotem3a94c542012-08-13 18:52:44 +00008170 // The code below optimizes (or (and X, Y), Z).
8171 // The AND operand needs to have a single user to make these optimizations
8172 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008173 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008174 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008175 return SDValue();
8176 SDValue N1 = N->getOperand(1);
8177
8178 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8179 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8180 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8181 APInt SplatUndef;
8182 unsigned SplatBitSize;
8183 bool HasAnyUndefs;
8184
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008185 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008186 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008187 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8188 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008189 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008190 HasAnyUndefs) && !HasAnyUndefs) {
8191 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8192 HasAnyUndefs) && !HasAnyUndefs) {
8193 // Ensure that the bit width of the constants are the same and that
8194 // the splat arguments are logical inverses as per the pattern we
8195 // are trying to simplify.
8196 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8197 SplatBits0 == ~SplatBits1) {
8198 // Canonicalize the vector type to make instruction selection
8199 // simpler.
8200 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8201 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8202 N0->getOperand(1),
8203 N0->getOperand(0),
8204 N1->getOperand(0));
8205 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8206 }
8207 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008208 }
8209 }
8210
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008211 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8212 // reasonable.
8213
Jim Grosbach11013ed2010-07-16 23:05:05 +00008214 // BFI is only available on V6T2+
8215 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8216 return SDValue();
8217
Andrew Trickef9de2a2013-05-25 02:42:55 +00008218 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008219 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008220 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008221 //
8222 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008223 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008224 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008225 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008226 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008227 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008228
Jim Grosbach11013ed2010-07-16 23:05:05 +00008229 if (VT != MVT::i32)
8230 return SDValue();
8231
Evan Cheng2e51bb42010-12-13 20:32:54 +00008232 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008233
Jim Grosbach11013ed2010-07-16 23:05:05 +00008234 // The value and the mask need to be constants so we can verify this is
8235 // actually a bitfield set. If the mask is 0xffff, we can do better
8236 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008237 SDValue MaskOp = N0.getOperand(1);
8238 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8239 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008240 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008241 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008242 if (Mask == 0xffff)
8243 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008244 SDValue Res;
8245 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008246 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8247 if (N1C) {
8248 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008249 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008250 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008251
Evan Cheng34345752010-12-11 04:11:38 +00008252 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008253 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008254
Evan Cheng2e51bb42010-12-13 20:32:54 +00008255 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Cheng34345752010-12-11 04:11:38 +00008256 DAG.getConstant(Val, MVT::i32),
8257 DAG.getConstant(Mask, MVT::i32));
8258
8259 // Do not add new nodes to DAG combiner worklist.
8260 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008261 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008262 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008263 } else if (N1.getOpcode() == ISD::AND) {
8264 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008265 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8266 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008267 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008268 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008269
Eric Christopherd5530962011-03-26 01:21:03 +00008270 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8271 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008272 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008273 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008274 // The pack halfword instruction works better for masks that fit it,
8275 // so use that when it's available.
8276 if (Subtarget->hasT2ExtractPack() &&
8277 (Mask == 0xffff || Mask == 0xffff0000))
8278 return SDValue();
8279 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008280 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008281 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopherd5530962011-03-26 01:21:03 +00008282 DAG.getConstant(amt, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008283 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008284 DAG.getConstant(Mask, MVT::i32));
8285 // Do not add new nodes to DAG combiner worklist.
8286 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008287 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008288 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008289 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008290 // The pack halfword instruction works better for masks that fit it,
8291 // so use that when it's available.
8292 if (Subtarget->hasT2ExtractPack() &&
8293 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8294 return SDValue();
8295 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008296 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008297 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008298 DAG.getConstant(lsb, MVT::i32));
8299 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopherd5530962011-03-26 01:21:03 +00008300 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008301 // Do not add new nodes to DAG combiner worklist.
8302 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008303 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008304 }
8305 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008306
Evan Cheng2e51bb42010-12-13 20:32:54 +00008307 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8308 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8309 ARM::isBitFieldInvertedMask(~Mask)) {
8310 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8311 // where lsb(mask) == #shamt and masked bits of B are known zero.
8312 SDValue ShAmt = N00.getOperand(1);
8313 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008314 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008315 if (ShAmtC != LSB)
8316 return SDValue();
8317
8318 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8319 DAG.getConstant(~Mask, MVT::i32));
8320
8321 // Do not add new nodes to DAG combiner worklist.
8322 DCI.CombineTo(N, Res, false);
8323 }
8324
Jim Grosbach11013ed2010-07-16 23:05:05 +00008325 return SDValue();
8326}
8327
Evan Chenge87681c2012-02-23 01:19:06 +00008328static SDValue PerformXORCombine(SDNode *N,
8329 TargetLowering::DAGCombinerInfo &DCI,
8330 const ARMSubtarget *Subtarget) {
8331 EVT VT = N->getValueType(0);
8332 SelectionDAG &DAG = DCI.DAG;
8333
8334 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8335 return SDValue();
8336
8337 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008338 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8339 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8340 if (Result.getNode())
8341 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008342 }
8343
8344 return SDValue();
8345}
8346
Evan Cheng6d02d902011-06-15 01:12:31 +00008347/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8348/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00008349static SDValue PerformBFICombine(SDNode *N,
8350 TargetLowering::DAGCombinerInfo &DCI) {
8351 SDValue N1 = N->getOperand(1);
8352 if (N1.getOpcode() == ISD::AND) {
8353 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8354 if (!N11C)
8355 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008356 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008357 unsigned LSB = countTrailingZeros(~InvMask);
8358 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Evan Cheng6d02d902011-06-15 01:12:31 +00008359 unsigned Mask = (1 << Width)-1;
Evan Chengc1778132010-12-14 03:22:07 +00008360 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008361 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008362 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00008363 N->getOperand(0), N1.getOperand(0),
8364 N->getOperand(2));
8365 }
8366 return SDValue();
8367}
8368
Bob Wilson22806742010-09-22 22:09:21 +00008369/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8370/// ARMISD::VMOVRRD.
8371static SDValue PerformVMOVRRDCombine(SDNode *N,
8372 TargetLowering::DAGCombinerInfo &DCI) {
8373 // vmovrrd(vmovdrr x, y) -> x,y
8374 SDValue InDouble = N->getOperand(0);
8375 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
8376 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008377
8378 // vmovrrd(load f64) -> (load i32), (load i32)
8379 SDNode *InNode = InDouble.getNode();
8380 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8381 InNode->getValueType(0) == MVT::f64 &&
8382 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8383 !cast<LoadSDNode>(InNode)->isVolatile()) {
8384 // TODO: Should this be done for non-FrameIndex operands?
8385 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8386
8387 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008388 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008389 SDValue BasePtr = LD->getBasePtr();
8390 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8391 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008392 LD->isNonTemporal(), LD->isInvariant(),
8393 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008394
8395 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8396 DAG.getConstant(4, MVT::i32));
8397 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8398 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008399 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008400 std::min(4U, LD->getAlignment() / 2));
8401
8402 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
Christian Pirker762b2c62014-06-01 09:30:52 +00008403 if (DCI.DAG.getTargetLoweringInfo().isBigEndian())
8404 std::swap (NewLD1, NewLD2);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008405 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
8406 DCI.RemoveFromWorklist(LD);
8407 DAG.DeleteNode(LD);
8408 return Result;
8409 }
8410
Bob Wilson22806742010-09-22 22:09:21 +00008411 return SDValue();
8412}
8413
8414/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8415/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8416static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8417 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8418 SDValue Op0 = N->getOperand(0);
8419 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00008420 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008421 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008422 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008423 Op1 = Op1.getOperand(0);
8424 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8425 Op0.getNode() == Op1.getNode() &&
8426 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008427 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00008428 N->getValueType(0), Op0.getOperand(0));
8429 return SDValue();
8430}
8431
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008432/// PerformSTORECombine - Target-specific dag combine xforms for
8433/// ISD::STORE.
8434static SDValue PerformSTORECombine(SDNode *N,
8435 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008436 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008437 if (St->isVolatile())
8438 return SDValue();
8439
Andrew Trickbc325162012-07-18 18:34:24 +00008440 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosiere0e38f62012-04-09 20:32:02 +00008441 // pack all of the elements in one place. Next, store to memory in fewer
8442 // chunks.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008443 SDValue StVal = St->getValue();
Chad Rosiere0e38f62012-04-09 20:32:02 +00008444 EVT VT = StVal.getValueType();
8445 if (St->isTruncatingStore() && VT.isVector()) {
8446 SelectionDAG &DAG = DCI.DAG;
8447 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8448 EVT StVT = St->getMemoryVT();
8449 unsigned NumElems = VT.getVectorNumElements();
8450 assert(StVT != VT && "Cannot truncate to the same type");
8451 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
8452 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
8453
8454 // From, To sizes and ElemCount must be pow of two
8455 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
8456
8457 // We are going to use the original vector elt for storing.
8458 // Accumulated smaller vector elements must be a multiple of the store size.
8459 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
8460
8461 unsigned SizeRatio = FromEltSz / ToEltSz;
8462 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
8463
8464 // Create a type on which we perform the shuffle.
8465 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
8466 NumElems*SizeRatio);
8467 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
8468
Andrew Trickef9de2a2013-05-25 02:42:55 +00008469 SDLoc DL(St);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008470 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
8471 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
Christian Pirker2cc1cf02014-06-16 09:17:30 +00008472 for (unsigned i = 0; i < NumElems; ++i)
8473 ShuffleVec[i] = TLI.isBigEndian() ? (i+1) * SizeRatio - 1 : i * SizeRatio;
Chad Rosiere0e38f62012-04-09 20:32:02 +00008474
8475 // Can't shuffle using an illegal type.
8476 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
8477
8478 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
8479 DAG.getUNDEF(WideVec.getValueType()),
8480 ShuffleVec.data());
8481 // At this point all of the data is stored at the bottom of the
8482 // register. We now need to save it to mem.
8483
8484 // Find the largest store unit
8485 MVT StoreType = MVT::i8;
8486 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
8487 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
8488 MVT Tp = (MVT::SimpleValueType)tp;
8489 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
8490 StoreType = Tp;
8491 }
8492 // Didn't find a legal store type.
8493 if (!TLI.isTypeLegal(StoreType))
8494 return SDValue();
8495
8496 // Bitcast the original vector into a vector of store-size units
8497 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
8498 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
8499 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
8500 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
8501 SmallVector<SDValue, 8> Chains;
8502 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
8503 TLI.getPointerTy());
8504 SDValue BasePtr = St->getBasePtr();
8505
8506 // Perform one or more big stores into memory.
8507 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
8508 for (unsigned I = 0; I < E; I++) {
8509 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
8510 StoreType, ShuffWide,
8511 DAG.getIntPtrConstant(I));
8512 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
8513 St->getPointerInfo(), St->isVolatile(),
8514 St->isNonTemporal(), St->getAlignment());
8515 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
8516 Increment);
8517 Chains.push_back(Ch);
8518 }
Craig Topper48d114b2014-04-26 18:35:24 +00008519 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008520 }
8521
8522 if (!ISD::isNormalStore(St))
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008523 return SDValue();
8524
Chad Rosier99cbde92012-04-09 19:38:15 +00008525 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8526 // ARM stores of arguments in the same cache line.
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008527 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier99cbde92012-04-09 19:38:15 +00008528 StVal.getNode()->hasOneUse()) {
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008529 SelectionDAG &DAG = DCI.DAG;
Christian Pirkerb5728192014-05-08 14:06:24 +00008530 bool isBigEndian = DAG.getTargetLoweringInfo().isBigEndian();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008531 SDLoc DL(St);
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008532 SDValue BasePtr = St->getBasePtr();
8533 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
Christian Pirkerb5728192014-05-08 14:06:24 +00008534 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
8535 BasePtr, St->getPointerInfo(), St->isVolatile(),
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008536 St->isNonTemporal(), St->getAlignment());
8537
8538 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8539 DAG.getConstant(4, MVT::i32));
Christian Pirkerb5728192014-05-08 14:06:24 +00008540 return DAG.getStore(NewST1.getValue(0), DL,
8541 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008542 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8543 St->isNonTemporal(),
8544 std::min(4U, St->getAlignment() / 2));
8545 }
8546
8547 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008548 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8549 return SDValue();
8550
Chad Rosier99cbde92012-04-09 19:38:15 +00008551 // Bitcast an i64 store extracted from a vector to f64.
8552 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008553 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008554 SDLoc dl(StVal);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008555 SDValue IntVec = StVal.getOperand(0);
8556 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8557 IntVec.getValueType().getVectorNumElements());
8558 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8559 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8560 Vec, StVal.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008561 dl = SDLoc(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008562 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8563 // Make the DAGCombiner fold the bitcasts.
8564 DCI.AddToWorklist(Vec.getNode());
8565 DCI.AddToWorklist(ExtElt.getNode());
8566 DCI.AddToWorklist(V.getNode());
8567 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8568 St->getPointerInfo(), St->isVolatile(),
8569 St->isNonTemporal(), St->getAlignment(),
8570 St->getTBAAInfo());
8571}
8572
8573/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8574/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8575/// i64 vector to have f64 elements, since the value can then be loaded
8576/// directly into a VFP register.
8577static bool hasNormalLoadOperand(SDNode *N) {
8578 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8579 for (unsigned i = 0; i < NumElts; ++i) {
8580 SDNode *Elt = N->getOperand(i).getNode();
8581 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8582 return true;
8583 }
8584 return false;
8585}
8586
Bob Wilsoncb6db982010-09-17 22:59:05 +00008587/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8588/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008589static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8590 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilsoncb6db982010-09-17 22:59:05 +00008591 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8592 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8593 // into a pair of GPRs, which is fine when the value is used as a scalar,
8594 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008595 SelectionDAG &DAG = DCI.DAG;
8596 if (N->getNumOperands() == 2) {
8597 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8598 if (RV.getNode())
8599 return RV;
8600 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00008601
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008602 // Load i64 elements as f64 values so that type legalization does not split
8603 // them up into i32 values.
8604 EVT VT = N->getValueType(0);
8605 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8606 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008607 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008608 SmallVector<SDValue, 8> Ops;
8609 unsigned NumElts = VT.getVectorNumElements();
8610 for (unsigned i = 0; i < NumElts; ++i) {
8611 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8612 Ops.push_back(V);
8613 // Make the DAGCombiner fold the bitcast.
8614 DCI.AddToWorklist(V.getNode());
8615 }
8616 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00008617 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008618 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8619}
8620
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008621/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
8622static SDValue
8623PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8624 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
8625 // At that time, we may have inserted bitcasts from integer to float.
8626 // If these bitcasts have survived DAGCombine, change the lowering of this
8627 // BUILD_VECTOR in something more vector friendly, i.e., that does not
8628 // force to use floating point types.
8629
8630 // Make sure we can change the type of the vector.
8631 // This is possible iff:
8632 // 1. The vector is only used in a bitcast to a integer type. I.e.,
8633 // 1.1. Vector is used only once.
8634 // 1.2. Use is a bit convert to an integer type.
8635 // 2. The size of its operands are 32-bits (64-bits are not legal).
8636 EVT VT = N->getValueType(0);
8637 EVT EltVT = VT.getVectorElementType();
8638
8639 // Check 1.1. and 2.
8640 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
8641 return SDValue();
8642
8643 // By construction, the input type must be float.
8644 assert(EltVT == MVT::f32 && "Unexpected type!");
8645
8646 // Check 1.2.
8647 SDNode *Use = *N->use_begin();
8648 if (Use->getOpcode() != ISD::BITCAST ||
8649 Use->getValueType(0).isFloatingPoint())
8650 return SDValue();
8651
8652 // Check profitability.
8653 // Model is, if more than half of the relevant operands are bitcast from
8654 // i32, turn the build_vector into a sequence of insert_vector_elt.
8655 // Relevant operands are everything that is not statically
8656 // (i.e., at compile time) bitcasted.
8657 unsigned NumOfBitCastedElts = 0;
8658 unsigned NumElts = VT.getVectorNumElements();
8659 unsigned NumOfRelevantElts = NumElts;
8660 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
8661 SDValue Elt = N->getOperand(Idx);
8662 if (Elt->getOpcode() == ISD::BITCAST) {
8663 // Assume only bit cast to i32 will go away.
8664 if (Elt->getOperand(0).getValueType() == MVT::i32)
8665 ++NumOfBitCastedElts;
8666 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
8667 // Constants are statically casted, thus do not count them as
8668 // relevant operands.
8669 --NumOfRelevantElts;
8670 }
8671
8672 // Check if more than half of the elements require a non-free bitcast.
8673 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
8674 return SDValue();
8675
8676 SelectionDAG &DAG = DCI.DAG;
8677 // Create the new vector type.
8678 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
8679 // Check if the type is legal.
8680 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8681 if (!TLI.isTypeLegal(VecVT))
8682 return SDValue();
8683
8684 // Combine:
8685 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
8686 // => BITCAST INSERT_VECTOR_ELT
8687 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
8688 // (BITCAST EN), N.
8689 SDValue Vec = DAG.getUNDEF(VecVT);
8690 SDLoc dl(N);
8691 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
8692 SDValue V = N->getOperand(Idx);
8693 if (V.getOpcode() == ISD::UNDEF)
8694 continue;
8695 if (V.getOpcode() == ISD::BITCAST &&
8696 V->getOperand(0).getValueType() == MVT::i32)
8697 // Fold obvious case.
8698 V = V.getOperand(0);
8699 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00008700 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008701 // Make the DAGCombiner fold the bitcasts.
8702 DCI.AddToWorklist(V.getNode());
8703 }
8704 SDValue LaneIdx = DAG.getConstant(Idx, MVT::i32);
8705 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
8706 }
8707 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
8708 // Make the DAGCombiner fold the bitcasts.
8709 DCI.AddToWorklist(Vec.getNode());
8710 return Vec;
8711}
8712
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008713/// PerformInsertEltCombine - Target-specific dag combine xforms for
8714/// ISD::INSERT_VECTOR_ELT.
8715static SDValue PerformInsertEltCombine(SDNode *N,
8716 TargetLowering::DAGCombinerInfo &DCI) {
8717 // Bitcast an i64 load inserted into a vector to f64.
8718 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8719 EVT VT = N->getValueType(0);
8720 SDNode *Elt = N->getOperand(1).getNode();
8721 if (VT.getVectorElementType() != MVT::i64 ||
8722 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8723 return SDValue();
8724
8725 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008726 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008727 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8728 VT.getVectorNumElements());
8729 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8730 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8731 // Make the DAGCombiner fold the bitcasts.
8732 DCI.AddToWorklist(Vec.getNode());
8733 DCI.AddToWorklist(V.getNode());
8734 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8735 Vec, V, N->getOperand(2));
8736 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00008737}
8738
Bob Wilsonc7334a12010-10-27 20:38:28 +00008739/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8740/// ISD::VECTOR_SHUFFLE.
8741static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8742 // The LLVM shufflevector instruction does not require the shuffle mask
8743 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8744 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8745 // operands do not match the mask length, they are extended by concatenating
8746 // them with undef vectors. That is probably the right thing for other
8747 // targets, but for NEON it is better to concatenate two double-register
8748 // size vector operands into a single quad-register size vector. Do that
8749 // transformation here:
8750 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8751 // shuffle(concat(v1, v2), undef)
8752 SDValue Op0 = N->getOperand(0);
8753 SDValue Op1 = N->getOperand(1);
8754 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8755 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8756 Op0.getNumOperands() != 2 ||
8757 Op1.getNumOperands() != 2)
8758 return SDValue();
8759 SDValue Concat0Op1 = Op0.getOperand(1);
8760 SDValue Concat1Op1 = Op1.getOperand(1);
8761 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8762 Concat1Op1.getOpcode() != ISD::UNDEF)
8763 return SDValue();
8764 // Skip the transformation if any of the types are illegal.
8765 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8766 EVT VT = N->getValueType(0);
8767 if (!TLI.isTypeLegal(VT) ||
8768 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8769 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8770 return SDValue();
8771
Andrew Trickef9de2a2013-05-25 02:42:55 +00008772 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008773 Op0.getOperand(0), Op1.getOperand(0));
8774 // Translate the shuffle mask.
8775 SmallVector<int, 16> NewMask;
8776 unsigned NumElts = VT.getVectorNumElements();
8777 unsigned HalfElts = NumElts/2;
8778 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8779 for (unsigned n = 0; n < NumElts; ++n) {
8780 int MaskElt = SVN->getMaskElt(n);
8781 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00008782 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00008783 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00008784 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00008785 NewElt = HalfElts + MaskElt - NumElts;
8786 NewMask.push_back(NewElt);
8787 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00008788 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008789 DAG.getUNDEF(VT), NewMask.data());
8790}
8791
Bob Wilson06fce872011-02-07 17:43:21 +00008792/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8793/// NEON load/store intrinsics to merge base address updates.
8794static SDValue CombineBaseUpdate(SDNode *N,
8795 TargetLowering::DAGCombinerInfo &DCI) {
8796 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8797 return SDValue();
8798
8799 SelectionDAG &DAG = DCI.DAG;
8800 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8801 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8802 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8803 SDValue Addr = N->getOperand(AddrOpIdx);
8804
8805 // Search for a use of the address operand that is an increment.
8806 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8807 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8808 SDNode *User = *UI;
8809 if (User->getOpcode() != ISD::ADD ||
8810 UI.getUse().getResNo() != Addr.getResNo())
8811 continue;
8812
8813 // Check that the add is independent of the load/store. Otherwise, folding
8814 // it would create a cycle.
8815 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8816 continue;
8817
8818 // Find the new opcode for the updating load/store.
8819 bool isLoad = true;
8820 bool isLaneOp = false;
8821 unsigned NewOpc = 0;
8822 unsigned NumVecs = 0;
8823 if (isIntrinsic) {
8824 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8825 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00008826 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008827 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8828 NumVecs = 1; break;
8829 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8830 NumVecs = 2; break;
8831 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8832 NumVecs = 3; break;
8833 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8834 NumVecs = 4; break;
8835 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8836 NumVecs = 2; isLaneOp = true; break;
8837 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8838 NumVecs = 3; isLaneOp = true; break;
8839 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8840 NumVecs = 4; isLaneOp = true; break;
8841 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8842 NumVecs = 1; isLoad = false; break;
8843 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8844 NumVecs = 2; isLoad = false; break;
8845 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8846 NumVecs = 3; isLoad = false; break;
8847 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8848 NumVecs = 4; isLoad = false; break;
8849 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8850 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8851 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8852 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8853 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8854 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8855 }
8856 } else {
8857 isLaneOp = true;
8858 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00008859 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008860 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8861 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8862 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8863 }
8864 }
8865
8866 // Find the size of memory referenced by the load/store.
8867 EVT VecTy;
8868 if (isLoad)
8869 VecTy = N->getValueType(0);
Owen Anderson77aa2662011-04-05 21:48:57 +00008870 else
Bob Wilson06fce872011-02-07 17:43:21 +00008871 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8872 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8873 if (isLaneOp)
8874 NumBytes /= VecTy.getVectorNumElements();
8875
8876 // If the increment is a constant, it must match the memory ref size.
8877 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8878 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8879 uint64_t IncVal = CInc->getZExtValue();
8880 if (IncVal != NumBytes)
8881 continue;
8882 } else if (NumBytes >= 3 * 16) {
8883 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8884 // separate instructions that make it harder to use a non-constant update.
8885 continue;
8886 }
8887
8888 // Create the new updating load/store node.
8889 EVT Tys[6];
8890 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8891 unsigned n;
8892 for (n = 0; n < NumResultVecs; ++n)
8893 Tys[n] = VecTy;
8894 Tys[n++] = MVT::i32;
8895 Tys[n] = MVT::Other;
Craig Topperabb4ac72014-04-16 06:10:51 +00008896 SDVTList SDTys = DAG.getVTList(ArrayRef<EVT>(Tys, NumResultVecs+2));
Bob Wilson06fce872011-02-07 17:43:21 +00008897 SmallVector<SDValue, 8> Ops;
8898 Ops.push_back(N->getOperand(0)); // incoming chain
8899 Ops.push_back(N->getOperand(AddrOpIdx));
8900 Ops.push_back(Inc);
8901 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8902 Ops.push_back(N->getOperand(i));
8903 }
8904 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008905 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00008906 Ops, MemInt->getMemoryVT(),
Bob Wilson06fce872011-02-07 17:43:21 +00008907 MemInt->getMemOperand());
8908
8909 // Update the uses.
8910 std::vector<SDValue> NewResults;
8911 for (unsigned i = 0; i < NumResultVecs; ++i) {
8912 NewResults.push_back(SDValue(UpdN.getNode(), i));
8913 }
8914 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8915 DCI.CombineTo(N, NewResults);
8916 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8917
8918 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00008919 }
Bob Wilson06fce872011-02-07 17:43:21 +00008920 return SDValue();
8921}
8922
Bob Wilson2d790df2010-11-28 06:51:26 +00008923/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8924/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8925/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8926/// return true.
8927static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8928 SelectionDAG &DAG = DCI.DAG;
8929 EVT VT = N->getValueType(0);
8930 // vldN-dup instructions only support 64-bit vectors for N > 1.
8931 if (!VT.is64BitVector())
8932 return false;
8933
8934 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8935 SDNode *VLD = N->getOperand(0).getNode();
8936 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8937 return false;
8938 unsigned NumVecs = 0;
8939 unsigned NewOpc = 0;
8940 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8941 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8942 NumVecs = 2;
8943 NewOpc = ARMISD::VLD2DUP;
8944 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8945 NumVecs = 3;
8946 NewOpc = ARMISD::VLD3DUP;
8947 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8948 NumVecs = 4;
8949 NewOpc = ARMISD::VLD4DUP;
8950 } else {
8951 return false;
8952 }
8953
8954 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8955 // numbers match the load.
8956 unsigned VLDLaneNo =
8957 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8958 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8959 UI != UE; ++UI) {
8960 // Ignore uses of the chain result.
8961 if (UI.getUse().getResNo() == NumVecs)
8962 continue;
8963 SDNode *User = *UI;
8964 if (User->getOpcode() != ARMISD::VDUPLANE ||
8965 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8966 return false;
8967 }
8968
8969 // Create the vldN-dup node.
8970 EVT Tys[5];
8971 unsigned n;
8972 for (n = 0; n < NumVecs; ++n)
8973 Tys[n] = VT;
8974 Tys[n] = MVT::Other;
Craig Topperabb4ac72014-04-16 06:10:51 +00008975 SDVTList SDTys = DAG.getVTList(ArrayRef<EVT>(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00008976 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8977 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008978 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00008979 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00008980 VLDMemInt->getMemOperand());
8981
8982 // Update the uses.
8983 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8984 UI != UE; ++UI) {
8985 unsigned ResNo = UI.getUse().getResNo();
8986 // Ignore uses of the chain result.
8987 if (ResNo == NumVecs)
8988 continue;
8989 SDNode *User = *UI;
8990 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8991 }
8992
8993 // Now the vldN-lane intrinsic is dead except for its chain result.
8994 // Update uses of the chain.
8995 std::vector<SDValue> VLDDupResults;
8996 for (unsigned n = 0; n < NumVecs; ++n)
8997 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8998 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8999 DCI.CombineTo(VLD, VLDDupResults);
9000
9001 return true;
9002}
9003
Bob Wilson103a0dc2010-07-14 01:22:12 +00009004/// PerformVDUPLANECombine - Target-specific dag combine xforms for
9005/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00009006static SDValue PerformVDUPLANECombine(SDNode *N,
9007 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00009008 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009009
Bob Wilson2d790df2010-11-28 06:51:26 +00009010 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
9011 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
9012 if (CombineVLDDUP(N, DCI))
9013 return SDValue(N, 0);
9014
9015 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
9016 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00009017 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009018 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00009019 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00009020 return SDValue();
9021
9022 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
9023 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
9024 // The canonical VMOV for a zero vector uses a 32-bit element size.
9025 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
9026 unsigned EltBits;
9027 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
9028 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00009029 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009030 if (EltSize > VT.getVectorElementType().getSizeInBits())
9031 return SDValue();
9032
Andrew Trickef9de2a2013-05-25 02:42:55 +00009033 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00009034}
9035
Eric Christopher1b8b94192011-06-29 21:10:36 +00009036// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00009037// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
9038static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
9039{
Chad Rosier6b610b32011-06-28 17:26:57 +00009040 integerPart cN;
9041 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009042 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
9043 I != E; I++) {
9044 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
9045 if (!C)
9046 return false;
9047
Eric Christopher1b8b94192011-06-29 21:10:36 +00009048 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009049 APFloat APF = C->getValueAPF();
9050 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
9051 != APFloat::opOK || !isExact)
9052 return false;
9053
9054 c0 = (I == 0) ? cN : c0;
9055 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
9056 return false;
9057 }
9058 C = c0;
9059 return true;
9060}
9061
9062/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
9063/// can replace combinations of VMUL and VCVT (floating-point to integer)
9064/// when the VMUL has a constant operand that is a power of 2.
9065///
9066/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9067/// vmul.f32 d16, d17, d16
9068/// vcvt.s32.f32 d16, d16
9069/// becomes:
9070/// vcvt.s32.f32 d16, d16, #3
9071static SDValue PerformVCVTCombine(SDNode *N,
9072 TargetLowering::DAGCombinerInfo &DCI,
9073 const ARMSubtarget *Subtarget) {
9074 SelectionDAG &DAG = DCI.DAG;
9075 SDValue Op = N->getOperand(0);
9076
9077 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
9078 Op.getOpcode() != ISD::FMUL)
9079 return SDValue();
9080
9081 uint64_t C;
9082 SDValue N0 = Op->getOperand(0);
9083 SDValue ConstVec = Op->getOperand(1);
9084 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
9085
Eric Christopher1b8b94192011-06-29 21:10:36 +00009086 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00009087 !isConstVecPow2(ConstVec, isSigned, C))
9088 return SDValue();
9089
Tim Northover7cbc2152013-06-28 15:29:25 +00009090 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
9091 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
9092 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9093 // These instructions only exist converting from f32 to i32. We can handle
9094 // smaller integers by generating an extra truncate, but larger ones would
9095 // be lossy.
9096 return SDValue();
9097 }
9098
Chad Rosierfa8d8932011-06-24 19:23:04 +00009099 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9100 Intrinsic::arm_neon_vcvtfp2fxu;
Tim Northover7cbc2152013-06-28 15:29:25 +00009101 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9102 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
9103 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9104 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
9105 DAG.getConstant(Log2_64(C), MVT::i32));
9106
9107 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9108 FixConv = DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), FixConv);
9109
9110 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009111}
9112
9113/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9114/// can replace combinations of VCVT (integer to floating-point) and VDIV
9115/// when the VDIV has a constant operand that is a power of 2.
9116///
9117/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9118/// vcvt.f32.s32 d16, d16
9119/// vdiv.f32 d16, d17, d16
9120/// becomes:
9121/// vcvt.f32.s32 d16, d16, #3
9122static SDValue PerformVDIVCombine(SDNode *N,
9123 TargetLowering::DAGCombinerInfo &DCI,
9124 const ARMSubtarget *Subtarget) {
9125 SelectionDAG &DAG = DCI.DAG;
9126 SDValue Op = N->getOperand(0);
9127 unsigned OpOpcode = Op.getNode()->getOpcode();
9128
9129 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
9130 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9131 return SDValue();
9132
9133 uint64_t C;
9134 SDValue ConstVec = N->getOperand(1);
9135 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
9136
9137 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9138 !isConstVecPow2(ConstVec, isSigned, C))
9139 return SDValue();
9140
Tim Northover7cbc2152013-06-28 15:29:25 +00009141 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
9142 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
9143 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9144 // These instructions only exist converting from i32 to f32. We can handle
9145 // smaller integers by generating an extra extend, but larger ones would
9146 // be lossy.
9147 return SDValue();
9148 }
9149
9150 SDValue ConvInput = Op.getOperand(0);
9151 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9152 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9153 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
9154 SDLoc(N), NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9155 ConvInput);
9156
Eric Christopher1b8b94192011-06-29 21:10:36 +00009157 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00009158 Intrinsic::arm_neon_vcvtfxu2fp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009159 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
Chad Rosierfa8d8932011-06-24 19:23:04 +00009160 Op.getValueType(),
Eric Christopher1b8b94192011-06-29 21:10:36 +00009161 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Tim Northover7cbc2152013-06-28 15:29:25 +00009162 ConvInput, DAG.getConstant(Log2_64(C), MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00009163}
9164
9165/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00009166/// operand of a vector shift operation, where all the elements of the
9167/// build_vector must have the same constant integer value.
9168static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9169 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00009170 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00009171 Op = Op.getOperand(0);
9172 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9173 APInt SplatBits, SplatUndef;
9174 unsigned SplatBitSize;
9175 bool HasAnyUndefs;
9176 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9177 HasAnyUndefs, ElementBits) ||
9178 SplatBitSize > ElementBits)
9179 return false;
9180 Cnt = SplatBits.getSExtValue();
9181 return true;
9182}
9183
9184/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9185/// operand of a vector shift left operation. That value must be in the range:
9186/// 0 <= Value < ElementBits for a left shift; or
9187/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009188static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009189 assert(VT.isVector() && "vector shift count is not a vector type");
9190 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9191 if (! getVShiftImm(Op, ElementBits, Cnt))
9192 return false;
9193 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9194}
9195
9196/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9197/// operand of a vector shift right operation. For a shift opcode, the value
9198/// is positive, but for an intrinsic the value count must be negative. The
9199/// absolute value must be in the range:
9200/// 1 <= |Value| <= ElementBits for a right shift; or
9201/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009202static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00009203 int64_t &Cnt) {
9204 assert(VT.isVector() && "vector shift count is not a vector type");
9205 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9206 if (! getVShiftImm(Op, ElementBits, Cnt))
9207 return false;
9208 if (isIntrinsic)
9209 Cnt = -Cnt;
9210 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9211}
9212
9213/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9214static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9215 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9216 switch (IntNo) {
9217 default:
9218 // Don't do anything for most intrinsics.
9219 break;
9220
9221 // Vector shifts: check for immediate versions and lower them.
9222 // Note: This is done during DAG combining instead of DAG legalizing because
9223 // the build_vectors for 64-bit vector element shift counts are generally
9224 // not legal, and it is hard to see their values after they get legalized to
9225 // loads from a constant pool.
9226 case Intrinsic::arm_neon_vshifts:
9227 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009228 case Intrinsic::arm_neon_vrshifts:
9229 case Intrinsic::arm_neon_vrshiftu:
9230 case Intrinsic::arm_neon_vrshiftn:
9231 case Intrinsic::arm_neon_vqshifts:
9232 case Intrinsic::arm_neon_vqshiftu:
9233 case Intrinsic::arm_neon_vqshiftsu:
9234 case Intrinsic::arm_neon_vqshiftns:
9235 case Intrinsic::arm_neon_vqshiftnu:
9236 case Intrinsic::arm_neon_vqshiftnsu:
9237 case Intrinsic::arm_neon_vqrshiftns:
9238 case Intrinsic::arm_neon_vqrshiftnu:
9239 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009240 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009241 int64_t Cnt;
9242 unsigned VShiftOpc = 0;
9243
9244 switch (IntNo) {
9245 case Intrinsic::arm_neon_vshifts:
9246 case Intrinsic::arm_neon_vshiftu:
9247 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9248 VShiftOpc = ARMISD::VSHL;
9249 break;
9250 }
9251 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9252 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9253 ARMISD::VSHRs : ARMISD::VSHRu);
9254 break;
9255 }
9256 return SDValue();
9257
Bob Wilson2e076c42009-06-22 23:27:02 +00009258 case Intrinsic::arm_neon_vrshifts:
9259 case Intrinsic::arm_neon_vrshiftu:
9260 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9261 break;
9262 return SDValue();
9263
9264 case Intrinsic::arm_neon_vqshifts:
9265 case Intrinsic::arm_neon_vqshiftu:
9266 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9267 break;
9268 return SDValue();
9269
9270 case Intrinsic::arm_neon_vqshiftsu:
9271 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9272 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +00009273 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009274
Bob Wilson2e076c42009-06-22 23:27:02 +00009275 case Intrinsic::arm_neon_vrshiftn:
9276 case Intrinsic::arm_neon_vqshiftns:
9277 case Intrinsic::arm_neon_vqshiftnu:
9278 case Intrinsic::arm_neon_vqshiftnsu:
9279 case Intrinsic::arm_neon_vqrshiftns:
9280 case Intrinsic::arm_neon_vqrshiftnu:
9281 case Intrinsic::arm_neon_vqrshiftnsu:
9282 // Narrowing shifts require an immediate right shift.
9283 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9284 break;
Jim Grosbach84511e12010-06-02 21:53:11 +00009285 llvm_unreachable("invalid shift count for narrowing vector shift "
9286 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009287
9288 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00009289 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00009290 }
9291
9292 switch (IntNo) {
9293 case Intrinsic::arm_neon_vshifts:
9294 case Intrinsic::arm_neon_vshiftu:
9295 // Opcode already set above.
9296 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00009297 case Intrinsic::arm_neon_vrshifts:
9298 VShiftOpc = ARMISD::VRSHRs; break;
9299 case Intrinsic::arm_neon_vrshiftu:
9300 VShiftOpc = ARMISD::VRSHRu; break;
9301 case Intrinsic::arm_neon_vrshiftn:
9302 VShiftOpc = ARMISD::VRSHRN; break;
9303 case Intrinsic::arm_neon_vqshifts:
9304 VShiftOpc = ARMISD::VQSHLs; break;
9305 case Intrinsic::arm_neon_vqshiftu:
9306 VShiftOpc = ARMISD::VQSHLu; break;
9307 case Intrinsic::arm_neon_vqshiftsu:
9308 VShiftOpc = ARMISD::VQSHLsu; break;
9309 case Intrinsic::arm_neon_vqshiftns:
9310 VShiftOpc = ARMISD::VQSHRNs; break;
9311 case Intrinsic::arm_neon_vqshiftnu:
9312 VShiftOpc = ARMISD::VQSHRNu; break;
9313 case Intrinsic::arm_neon_vqshiftnsu:
9314 VShiftOpc = ARMISD::VQSHRNsu; break;
9315 case Intrinsic::arm_neon_vqrshiftns:
9316 VShiftOpc = ARMISD::VQRSHRNs; break;
9317 case Intrinsic::arm_neon_vqrshiftnu:
9318 VShiftOpc = ARMISD::VQRSHRNu; break;
9319 case Intrinsic::arm_neon_vqrshiftnsu:
9320 VShiftOpc = ARMISD::VQRSHRNsu; break;
9321 }
9322
Andrew Trickef9de2a2013-05-25 02:42:55 +00009323 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009324 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009325 }
9326
9327 case Intrinsic::arm_neon_vshiftins: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009328 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009329 int64_t Cnt;
9330 unsigned VShiftOpc = 0;
9331
9332 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9333 VShiftOpc = ARMISD::VSLI;
9334 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9335 VShiftOpc = ARMISD::VSRI;
9336 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009337 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009338 }
9339
Andrew Trickef9de2a2013-05-25 02:42:55 +00009340 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +00009341 N->getOperand(1), N->getOperand(2),
Owen Anderson9f944592009-08-11 20:47:22 +00009342 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009343 }
9344
9345 case Intrinsic::arm_neon_vqrshifts:
9346 case Intrinsic::arm_neon_vqrshiftu:
9347 // No immediate versions of these to check for.
9348 break;
9349 }
9350
9351 return SDValue();
9352}
9353
9354/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9355/// lowers them. As with the vector shift intrinsics, this is done during DAG
9356/// combining instead of DAG legalizing because the build_vectors for 64-bit
9357/// vector element shift counts are generally not legal, and it is hard to see
9358/// their values after they get legalized to loads from a constant pool.
9359static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9360 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009361 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +00009362 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9363 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9364 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9365 SDValue N1 = N->getOperand(1);
9366 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9367 SDValue N0 = N->getOperand(0);
9368 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9369 DAG.MaskedValueIsZero(N0.getOperand(0),
9370 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009371 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +00009372 }
9373 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009374
9375 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +00009376 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9377 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +00009378 return SDValue();
9379
9380 assert(ST->hasNEON() && "unexpected vector shift");
9381 int64_t Cnt;
9382
9383 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009384 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009385
9386 case ISD::SHL:
9387 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009388 return DAG.getNode(ARMISD::VSHL, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009389 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009390 break;
9391
9392 case ISD::SRA:
9393 case ISD::SRL:
9394 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9395 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9396 ARMISD::VSHRs : ARMISD::VSHRu);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009397 return DAG.getNode(VShiftOpc, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009398 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009399 }
9400 }
9401 return SDValue();
9402}
9403
9404/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9405/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9406static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9407 const ARMSubtarget *ST) {
9408 SDValue N0 = N->getOperand(0);
9409
9410 // Check for sign- and zero-extensions of vector extract operations of 8-
9411 // and 16-bit vector elements. NEON supports these directly. They are
9412 // handled during DAG combining because type legalization will promote them
9413 // to 32-bit types and it is messy to recognize the operations after that.
9414 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9415 SDValue Vec = N0.getOperand(0);
9416 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009417 EVT VT = N->getValueType(0);
9418 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009419 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9420
Owen Anderson9f944592009-08-11 20:47:22 +00009421 if (VT == MVT::i32 &&
9422 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +00009423 TLI.isTypeLegal(Vec.getValueType()) &&
9424 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009425
9426 unsigned Opc = 0;
9427 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009428 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009429 case ISD::SIGN_EXTEND:
9430 Opc = ARMISD::VGETLANEs;
9431 break;
9432 case ISD::ZERO_EXTEND:
9433 case ISD::ANY_EXTEND:
9434 Opc = ARMISD::VGETLANEu;
9435 break;
9436 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009437 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +00009438 }
9439 }
9440
9441 return SDValue();
9442}
9443
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009444/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9445/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9446static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9447 const ARMSubtarget *ST) {
9448 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng55f0c6b2010-07-15 22:07:12 +00009449 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009450 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9451 // a NaN; only do the transformation when it matches that behavior.
9452
9453 // For now only do this when using NEON for FP operations; if using VFP, it
9454 // is not obvious that the benefit outweighs the cost of switching to the
9455 // NEON pipeline.
9456 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9457 N->getValueType(0) != MVT::f32)
9458 return SDValue();
9459
9460 SDValue CondLHS = N->getOperand(0);
9461 SDValue CondRHS = N->getOperand(1);
9462 SDValue LHS = N->getOperand(2);
9463 SDValue RHS = N->getOperand(3);
9464 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9465
9466 unsigned Opcode = 0;
9467 bool IsReversed;
Bob Wilsonba8ac742010-02-24 22:15:53 +00009468 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009469 IsReversed = false; // x CC y ? x : y
Bob Wilsonba8ac742010-02-24 22:15:53 +00009470 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009471 IsReversed = true ; // x CC y ? y : x
9472 } else {
9473 return SDValue();
9474 }
9475
Bob Wilsonba8ac742010-02-24 22:15:53 +00009476 bool IsUnordered;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009477 switch (CC) {
9478 default: break;
9479 case ISD::SETOLT:
9480 case ISD::SETOLE:
9481 case ISD::SETLT:
9482 case ISD::SETLE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009483 case ISD::SETULT:
9484 case ISD::SETULE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009485 // If LHS is NaN, an ordered comparison will be false and the result will
9486 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9487 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9488 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9489 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9490 break;
9491 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9492 // will return -0, so vmin can only be used for unsafe math or if one of
9493 // the operands is known to be nonzero.
9494 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009495 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009496 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9497 break;
9498 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009499 break;
9500
9501 case ISD::SETOGT:
9502 case ISD::SETOGE:
9503 case ISD::SETGT:
9504 case ISD::SETGE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009505 case ISD::SETUGT:
9506 case ISD::SETUGE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009507 // If LHS is NaN, an ordered comparison will be false and the result will
9508 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9509 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9510 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9511 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9512 break;
9513 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9514 // will return +0, so vmax can only be used for unsafe math or if one of
9515 // the operands is known to be nonzero.
9516 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009517 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009518 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9519 break;
9520 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009521 break;
9522 }
9523
9524 if (!Opcode)
9525 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009526 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009527}
9528
Evan Chengf863e3f2011-07-13 00:42:17 +00009529/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9530SDValue
9531ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9532 SDValue Cmp = N->getOperand(4);
9533 if (Cmp.getOpcode() != ARMISD::CMPZ)
9534 // Only looking at EQ and NE cases.
9535 return SDValue();
9536
9537 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009538 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +00009539 SDValue LHS = Cmp.getOperand(0);
9540 SDValue RHS = Cmp.getOperand(1);
9541 SDValue FalseVal = N->getOperand(0);
9542 SDValue TrueVal = N->getOperand(1);
9543 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +00009544 ARMCC::CondCodes CC =
9545 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +00009546
9547 // Simplify
9548 // mov r1, r0
9549 // cmp r1, x
9550 // mov r0, y
9551 // moveq r0, x
9552 // to
9553 // cmp r0, x
9554 // movne r0, y
9555 //
9556 // mov r1, r0
9557 // cmp r1, x
9558 // mov r0, x
9559 // movne r0, y
9560 // to
9561 // cmp r0, x
9562 // movne r0, y
9563 /// FIXME: Turn this into a target neutral optimization?
9564 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +00009565 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +00009566 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9567 N->getOperand(3), Cmp);
9568 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9569 SDValue ARMcc;
9570 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9571 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9572 N->getOperand(3), NewCmp);
9573 }
9574
9575 if (Res.getNode()) {
9576 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00009577 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +00009578 // Capture demanded bits information that would be otherwise lost.
9579 if (KnownZero == 0xfffffffe)
9580 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9581 DAG.getValueType(MVT::i1));
9582 else if (KnownZero == 0xffffff00)
9583 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9584 DAG.getValueType(MVT::i8));
9585 else if (KnownZero == 0xffff0000)
9586 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9587 DAG.getValueType(MVT::i16));
9588 }
9589
9590 return Res;
9591}
9592
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009593SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +00009594 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009595 switch (N->getOpcode()) {
9596 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009597 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +00009598 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009599 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009600 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009601 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +00009602 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9603 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +00009604 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00009605 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson22806742010-09-22 22:09:21 +00009606 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009607 case ISD::STORE: return PerformSTORECombine(N, DCI);
9608 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
9609 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +00009610 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +00009611 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009612 case ISD::FP_TO_SINT:
9613 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9614 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009615 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00009616 case ISD::SHL:
9617 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009618 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00009619 case ISD::SIGN_EXTEND:
9620 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009621 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9622 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +00009623 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson06fce872011-02-07 17:43:21 +00009624 case ARMISD::VLD2DUP:
9625 case ARMISD::VLD3DUP:
9626 case ARMISD::VLD4DUP:
9627 return CombineBaseUpdate(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009628 case ARMISD::BUILD_VECTOR:
9629 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009630 case ISD::INTRINSIC_VOID:
9631 case ISD::INTRINSIC_W_CHAIN:
9632 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9633 case Intrinsic::arm_neon_vld1:
9634 case Intrinsic::arm_neon_vld2:
9635 case Intrinsic::arm_neon_vld3:
9636 case Intrinsic::arm_neon_vld4:
9637 case Intrinsic::arm_neon_vld2lane:
9638 case Intrinsic::arm_neon_vld3lane:
9639 case Intrinsic::arm_neon_vld4lane:
9640 case Intrinsic::arm_neon_vst1:
9641 case Intrinsic::arm_neon_vst2:
9642 case Intrinsic::arm_neon_vst3:
9643 case Intrinsic::arm_neon_vst4:
9644 case Intrinsic::arm_neon_vst2lane:
9645 case Intrinsic::arm_neon_vst3lane:
9646 case Intrinsic::arm_neon_vst4lane:
9647 return CombineBaseUpdate(N, DCI);
9648 default: break;
9649 }
9650 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009651 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009652 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009653}
9654
Evan Chengd42641c2011-02-02 01:06:55 +00009655bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9656 EVT VT) const {
9657 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9658}
9659
Matt Arsenault25793a32014-02-05 23:15:53 +00009660bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, unsigned,
9661 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009662 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +00009663 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009664
9665 switch (VT.getSimpleVT().SimpleTy) {
9666 default:
9667 return false;
9668 case MVT::i8:
9669 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009670 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009671 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +00009672 if (AllowsUnaligned) {
9673 if (Fast)
9674 *Fast = Subtarget->hasV7Ops();
9675 return true;
9676 }
9677 return false;
9678 }
Evan Chengeec6bc62012-08-15 17:44:53 +00009679 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009680 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009681 // For any little-endian targets with neon, we can support unaligned ld/st
9682 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +00009683 // A big-endian target may also explicitly support unaligned accesses
Evan Cheng79e2ca92012-12-10 23:21:26 +00009684 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
9685 if (Fast)
9686 *Fast = true;
9687 return true;
9688 }
9689 return false;
9690 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009691 }
9692}
9693
Lang Hames9929c422011-11-02 22:52:45 +00009694static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9695 unsigned AlignCheck) {
9696 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9697 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9698}
9699
9700EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9701 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00009702 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +00009703 bool MemcpyStrSrc,
9704 MachineFunction &MF) const {
9705 const Function *F = MF.getFunction();
9706
9707 // See if we can use NEON instructions for this...
Evan Cheng962711e2012-12-12 02:34:41 +00009708 if ((!IsMemset || ZeroMemset) &&
Evan Cheng79e2ca92012-12-10 23:21:26 +00009709 Subtarget->hasNEON() &&
Bill Wendling698e84f2012-12-30 10:32:01 +00009710 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
9711 Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009712 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +00009713 if (Size >= 16 &&
9714 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault25793a32014-02-05 23:15:53 +00009715 (allowsUnalignedMemoryAccesses(MVT::v2f64, 0, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009716 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +00009717 } else if (Size >= 8 &&
9718 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault25793a32014-02-05 23:15:53 +00009719 (allowsUnalignedMemoryAccesses(MVT::f64, 0, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009720 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +00009721 }
9722 }
9723
Lang Hamesb85fcd02011-11-08 18:56:23 +00009724 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +00009725 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009726 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +00009727 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009728 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +00009729
Lang Hames9929c422011-11-02 22:52:45 +00009730 // Let the target-independent logic figure it out.
9731 return MVT::Other;
9732}
9733
Evan Cheng9ec512d2012-12-06 19:13:27 +00009734bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
9735 if (Val.getOpcode() != ISD::LOAD)
9736 return false;
9737
9738 EVT VT1 = Val.getValueType();
9739 if (!VT1.isSimple() || !VT1.isInteger() ||
9740 !VT2.isSimple() || !VT2.isInteger())
9741 return false;
9742
9743 switch (VT1.getSimpleVT().SimpleTy) {
9744 default: break;
9745 case MVT::i1:
9746 case MVT::i8:
9747 case MVT::i16:
9748 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
9749 return true;
9750 }
9751
9752 return false;
9753}
9754
Tim Northovercc2e9032013-08-06 13:58:03 +00009755bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
9756 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
9757 return false;
9758
9759 if (!isTypeLegal(EVT::getEVT(Ty1)))
9760 return false;
9761
9762 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
9763
9764 // Assuming the caller doesn't have a zeroext or signext return parameter,
9765 // truncation all the way down to i1 is valid.
9766 return true;
9767}
9768
9769
Evan Chengdc49a8d2009-08-14 20:09:37 +00009770static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9771 if (V < 0)
9772 return false;
9773
9774 unsigned Scale = 1;
9775 switch (VT.getSimpleVT().SimpleTy) {
9776 default: return false;
9777 case MVT::i1:
9778 case MVT::i8:
9779 // Scale == 1;
9780 break;
9781 case MVT::i16:
9782 // Scale == 2;
9783 Scale = 2;
9784 break;
9785 case MVT::i32:
9786 // Scale == 4;
9787 Scale = 4;
9788 break;
9789 }
9790
9791 if ((V & (Scale - 1)) != 0)
9792 return false;
9793 V /= Scale;
9794 return V == (V & ((1LL << 5) - 1));
9795}
9796
9797static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9798 const ARMSubtarget *Subtarget) {
9799 bool isNeg = false;
9800 if (V < 0) {
9801 isNeg = true;
9802 V = - V;
9803 }
9804
9805 switch (VT.getSimpleVT().SimpleTy) {
9806 default: return false;
9807 case MVT::i1:
9808 case MVT::i8:
9809 case MVT::i16:
9810 case MVT::i32:
9811 // + imm12 or - imm8
9812 if (isNeg)
9813 return V == (V & ((1LL << 8) - 1));
9814 return V == (V & ((1LL << 12) - 1));
9815 case MVT::f32:
9816 case MVT::f64:
9817 // Same as ARM mode. FIXME: NEON?
9818 if (!Subtarget->hasVFP2())
9819 return false;
9820 if ((V & 3) != 0)
9821 return false;
9822 V >>= 2;
9823 return V == (V & ((1LL << 8) - 1));
9824 }
9825}
9826
Evan Cheng2150b922007-03-12 23:30:29 +00009827/// isLegalAddressImmediate - Return true if the integer value can be used
9828/// as the offset of the target addressing mode for load / store of the
9829/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009830static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009831 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +00009832 if (V == 0)
9833 return true;
9834
Evan Chengce5dfb62009-03-09 19:15:00 +00009835 if (!VT.isSimple())
9836 return false;
9837
Evan Chengdc49a8d2009-08-14 20:09:37 +00009838 if (Subtarget->isThumb1Only())
9839 return isLegalT1AddressImmediate(V, VT);
9840 else if (Subtarget->isThumb2())
9841 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +00009842
Evan Chengdc49a8d2009-08-14 20:09:37 +00009843 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +00009844 if (V < 0)
9845 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +00009846 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +00009847 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +00009848 case MVT::i1:
9849 case MVT::i8:
9850 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +00009851 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009852 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +00009853 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +00009854 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009855 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +00009856 case MVT::f32:
9857 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009858 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +00009859 return false;
Evan Chengbef131de2007-05-03 02:00:18 +00009860 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +00009861 return false;
9862 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009863 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +00009864 }
Evan Cheng10043e22007-01-19 07:51:42 +00009865}
9866
Evan Chengdc49a8d2009-08-14 20:09:37 +00009867bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9868 EVT VT) const {
9869 int Scale = AM.Scale;
9870 if (Scale < 0)
9871 return false;
9872
9873 switch (VT.getSimpleVT().SimpleTy) {
9874 default: return false;
9875 case MVT::i1:
9876 case MVT::i8:
9877 case MVT::i16:
9878 case MVT::i32:
9879 if (Scale == 1)
9880 return true;
9881 // r + r << imm
9882 Scale = Scale & ~1;
9883 return Scale == 2 || Scale == 4 || Scale == 8;
9884 case MVT::i64:
9885 // r + r
9886 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9887 return true;
9888 return false;
9889 case MVT::isVoid:
9890 // Note, we allow "void" uses (basically, uses that aren't loads or
9891 // stores), because arm allows folding a scale into many arithmetic
9892 // operations. This should be made more precise and revisited later.
9893
9894 // Allow r << imm, but the imm has to be a multiple of two.
9895 if (Scale & 1) return false;
9896 return isPowerOf2_32(Scale);
9897 }
9898}
9899
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009900/// isLegalAddressingMode - Return true if the addressing mode represented
9901/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson7117a912009-03-20 22:42:55 +00009902bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00009903 Type *Ty) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009904 EVT VT = getValueType(Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +00009905 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +00009906 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009907
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009908 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +00009909 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009910 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009911
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009912 switch (AM.Scale) {
9913 case 0: // no scale reg, must be "r+i" or "r", or "i".
9914 break;
9915 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009916 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009917 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +00009918 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009919 default:
Chris Lattner502c3f42007-04-13 06:50:55 +00009920 // ARM doesn't support any R+R*scale+imm addr modes.
9921 if (AM.BaseOffs)
9922 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009923
Bob Wilson866c1742009-04-08 17:55:28 +00009924 if (!VT.isSimple())
9925 return false;
9926
Evan Chengdc49a8d2009-08-14 20:09:37 +00009927 if (Subtarget->isThumb2())
9928 return isLegalT2ScaledAddressingMode(AM, VT);
9929
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009930 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +00009931 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009932 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +00009933 case MVT::i1:
9934 case MVT::i8:
9935 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009936 if (Scale < 0) Scale = -Scale;
9937 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009938 return true;
9939 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +00009940 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +00009941 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009942 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009943 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009944 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009945 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +00009946 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009947
Owen Anderson9f944592009-08-11 20:47:22 +00009948 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009949 // Note, we allow "void" uses (basically, uses that aren't loads or
9950 // stores), because arm allows folding a scale into many arithmetic
9951 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +00009952
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009953 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +00009954 if (Scale & 1) return false;
9955 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009956 }
Evan Cheng2150b922007-03-12 23:30:29 +00009957 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009958 return true;
Evan Cheng2150b922007-03-12 23:30:29 +00009959}
9960
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009961/// isLegalICmpImmediate - Return true if the specified immediate is legal
9962/// icmp immediate, that is the target has icmp instructions which can compare
9963/// a register against the immediate without having to materialize the
9964/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +00009965bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +00009966 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009967 if (!Subtarget->isThumb())
Chandler Carruth8a102c22012-04-06 20:10:52 +00009968 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009969 if (Subtarget->isThumb2())
Chandler Carruth8a102c22012-04-06 20:10:52 +00009970 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +00009971 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +00009972 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009973}
9974
Andrew Tricka22cdb72012-07-18 18:34:27 +00009975/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9976/// *or sub* immediate, that is the target has add or sub instructions which can
9977/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +00009978/// immediate into a register.
9979bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +00009980 // Same encoding for add/sub, just flip the sign.
9981 int64_t AbsImm = llvm::abs64(Imm);
9982 if (!Subtarget->isThumb())
9983 return ARM_AM::getSOImmVal(AbsImm) != -1;
9984 if (Subtarget->isThumb2())
9985 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9986 // Thumb1 only has 8-bit unsigned immediate.
9987 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +00009988}
9989
Owen Anderson53aa7a92009-08-10 22:56:29 +00009990static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +00009991 bool isSEXTLoad, SDValue &Base,
9992 SDValue &Offset, bool &isInc,
9993 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +00009994 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9995 return false;
9996
Owen Anderson9f944592009-08-11 20:47:22 +00009997 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +00009998 // AddressingMode 3
9999 Base = Ptr->getOperand(0);
10000 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010001 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010002 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010003 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010004 isInc = false;
10005 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10006 return true;
10007 }
10008 }
10009 isInc = (Ptr->getOpcode() == ISD::ADD);
10010 Offset = Ptr->getOperand(1);
10011 return true;
Owen Anderson9f944592009-08-11 20:47:22 +000010012 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +000010013 // AddressingMode 2
10014 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +000010015 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +000010016 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010017 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +000010018 isInc = false;
10019 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10020 Base = Ptr->getOperand(0);
10021 return true;
10022 }
10023 }
10024
10025 if (Ptr->getOpcode() == ISD::ADD) {
10026 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +000010027 ARM_AM::ShiftOpc ShOpcVal=
10028 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +000010029 if (ShOpcVal != ARM_AM::no_shift) {
10030 Base = Ptr->getOperand(1);
10031 Offset = Ptr->getOperand(0);
10032 } else {
10033 Base = Ptr->getOperand(0);
10034 Offset = Ptr->getOperand(1);
10035 }
10036 return true;
10037 }
10038
10039 isInc = (Ptr->getOpcode() == ISD::ADD);
10040 Base = Ptr->getOperand(0);
10041 Offset = Ptr->getOperand(1);
10042 return true;
10043 }
10044
Jim Grosbachd7cf55c2009-11-09 00:11:35 +000010045 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +000010046 return false;
10047}
10048
Owen Anderson53aa7a92009-08-10 22:56:29 +000010049static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +000010050 bool isSEXTLoad, SDValue &Base,
10051 SDValue &Offset, bool &isInc,
10052 SelectionDAG &DAG) {
10053 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
10054 return false;
10055
10056 Base = Ptr->getOperand(0);
10057 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
10058 int RHSC = (int)RHS->getZExtValue();
10059 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
10060 assert(Ptr->getOpcode() == ISD::ADD);
10061 isInc = false;
10062 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
10063 return true;
10064 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
10065 isInc = Ptr->getOpcode() == ISD::ADD;
10066 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
10067 return true;
10068 }
10069 }
10070
10071 return false;
10072}
10073
Evan Cheng10043e22007-01-19 07:51:42 +000010074/// getPreIndexedAddressParts - returns true by value, base pointer and
10075/// offset pointer and addressing mode by reference if the node's address
10076/// can be legally represented as pre-indexed load / store address.
10077bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010078ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
10079 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010080 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010081 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010082 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010083 return false;
10084
Owen Anderson53aa7a92009-08-10 22:56:29 +000010085 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010086 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010087 bool isSEXTLoad = false;
10088 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10089 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010090 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010091 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10092 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10093 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010094 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010095 } else
10096 return false;
10097
10098 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010099 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010100 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010101 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10102 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010103 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010104 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000010105 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000010106 if (!isLegal)
10107 return false;
10108
10109 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10110 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010111}
10112
10113/// getPostIndexedAddressParts - returns true by value, base pointer and
10114/// offset pointer and addressing mode by reference if this node can be
10115/// combined with a load / store to form a post-indexed load / store.
10116bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010117 SDValue &Base,
10118 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010119 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010120 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010121 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010122 return false;
10123
Owen Anderson53aa7a92009-08-10 22:56:29 +000010124 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010125 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010126 bool isSEXTLoad = false;
10127 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010128 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010129 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010130 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10131 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010132 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010133 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010134 } else
10135 return false;
10136
10137 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010138 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010139 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010140 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000010141 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010142 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010143 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10144 isInc, DAG);
10145 if (!isLegal)
10146 return false;
10147
Evan Chengf19384d2010-05-18 21:31:17 +000010148 if (Ptr != Base) {
10149 // Swap base ptr and offset to catch more post-index load / store when
10150 // it's legal. In Thumb2 mode, offset must be an immediate.
10151 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10152 !Subtarget->isThumb2())
10153 std::swap(Base, Offset);
10154
10155 // Post-indexed load / store update the base pointer.
10156 if (Ptr != Base)
10157 return false;
10158 }
10159
Evan Cheng84c6cda2009-07-02 07:28:31 +000010160 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10161 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010162}
10163
Jay Foada0653a32014-05-14 21:14:37 +000010164void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10165 APInt &KnownZero,
10166 APInt &KnownOne,
10167 const SelectionDAG &DAG,
10168 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000010169 unsigned BitWidth = KnownOne.getBitWidth();
10170 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000010171 switch (Op.getOpcode()) {
10172 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000010173 case ARMISD::ADDC:
10174 case ARMISD::ADDE:
10175 case ARMISD::SUBC:
10176 case ARMISD::SUBE:
10177 // These nodes' second result is a boolean
10178 if (Op.getResNo() == 0)
10179 break;
10180 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10181 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010182 case ARMISD::CMOV: {
10183 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000010184 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010185 if (KnownZero == 0 && KnownOne == 0) return;
10186
Dan Gohmanf990faf2008-02-13 00:35:47 +000010187 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000010188 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010189 KnownZero &= KnownZeroRHS;
10190 KnownOne &= KnownOneRHS;
10191 return;
10192 }
Tim Northover01b4aa92014-04-03 15:10:35 +000010193 case ISD::INTRINSIC_W_CHAIN: {
10194 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
10195 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
10196 switch (IntID) {
10197 default: return;
10198 case Intrinsic::arm_ldaex:
10199 case Intrinsic::arm_ldrex: {
10200 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
10201 unsigned MemBits = VT.getScalarType().getSizeInBits();
10202 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
10203 return;
10204 }
10205 }
10206 }
Evan Cheng10043e22007-01-19 07:51:42 +000010207 }
10208}
10209
10210//===----------------------------------------------------------------------===//
10211// ARM Inline Assembly Support
10212//===----------------------------------------------------------------------===//
10213
Evan Cheng078b0b02011-01-08 01:24:27 +000010214bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10215 // Looking for "rev" which is V6+.
10216 if (!Subtarget->hasV6Ops())
10217 return false;
10218
10219 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10220 std::string AsmStr = IA->getAsmString();
10221 SmallVector<StringRef, 4> AsmPieces;
10222 SplitString(AsmStr, AsmPieces, ";\n");
10223
10224 switch (AsmPieces.size()) {
10225 default: return false;
10226 case 1:
10227 AsmStr = AsmPieces[0];
10228 AsmPieces.clear();
10229 SplitString(AsmStr, AsmPieces, " \t,");
10230
10231 // rev $0, $1
10232 if (AsmPieces.size() == 3 &&
10233 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10234 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010235 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010236 if (Ty && Ty->getBitWidth() == 32)
10237 return IntrinsicLowering::LowerToByteSwap(CI);
10238 }
10239 break;
10240 }
10241
10242 return false;
10243}
10244
Evan Cheng10043e22007-01-19 07:51:42 +000010245/// getConstraintType - Given a constraint letter, return the type of
10246/// constraint it is for this target.
10247ARMTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +000010248ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
10249 if (Constraint.size() == 1) {
10250 switch (Constraint[0]) {
10251 default: break;
10252 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010253 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010254 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010255 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010256 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010257 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010258 // An address with a single base register. Due to the way we
10259 // currently handle addresses it is the same as an 'r' memory constraint.
10260 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010261 }
Eric Christophere256cd02011-06-21 22:10:57 +000010262 } else if (Constraint.size() == 2) {
10263 switch (Constraint[0]) {
10264 default: break;
10265 // All 'U+' constraints are addresses.
10266 case 'U': return C_Memory;
10267 }
Evan Cheng10043e22007-01-19 07:51:42 +000010268 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010269 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010270}
10271
John Thompsone8360b72010-10-29 17:29:13 +000010272/// Examine constraint type and operand type and determine a weight value.
10273/// This object must already have been set up with the operand type
10274/// and the current alternative constraint selected.
10275TargetLowering::ConstraintWeight
10276ARMTargetLowering::getSingleConstraintMatchWeight(
10277 AsmOperandInfo &info, const char *constraint) const {
10278 ConstraintWeight weight = CW_Invalid;
10279 Value *CallOperandVal = info.CallOperandVal;
10280 // If we don't have a value, we can't do a match,
10281 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010282 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010283 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010284 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010285 // Look at the constraint type.
10286 switch (*constraint) {
10287 default:
10288 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10289 break;
10290 case 'l':
10291 if (type->isIntegerTy()) {
10292 if (Subtarget->isThumb())
10293 weight = CW_SpecificReg;
10294 else
10295 weight = CW_Register;
10296 }
10297 break;
10298 case 'w':
10299 if (type->isFloatingPointTy())
10300 weight = CW_Register;
10301 break;
10302 }
10303 return weight;
10304}
10305
Eric Christophercf2007c2011-06-30 23:50:52 +000010306typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10307RCPair
Evan Cheng10043e22007-01-19 07:51:42 +000010308ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000010309 MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000010310 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010311 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000010312 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000010313 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010314 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010315 return RCPair(0U, &ARM::tGPRRegClass);
10316 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000010317 case 'h': // High regs or no regs.
10318 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010319 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000010320 break;
Chris Lattner6223e832007-04-02 17:24:08 +000010321 case 'r':
Craig Topperc7242e02012-04-20 07:30:17 +000010322 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010323 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000010324 if (VT == MVT::Other)
10325 break;
Owen Anderson9f944592009-08-11 20:47:22 +000010326 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010327 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000010328 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010329 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000010330 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010331 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010332 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010333 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000010334 if (VT == MVT::Other)
10335 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010336 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010337 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010338 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010339 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010340 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010341 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010342 break;
Eric Christopherc011d312011-07-01 00:30:46 +000010343 case 't':
10344 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010345 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000010346 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010347 }
10348 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000010349 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000010350 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000010351
Evan Cheng10043e22007-01-19 07:51:42 +000010352 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10353}
10354
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010355/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10356/// vector. If it is invalid, don't add anything to Ops.
10357void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010358 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010359 std::vector<SDValue>&Ops,
10360 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000010361 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010362
Eric Christopherde9399b2011-06-02 23:16:42 +000010363 // Currently only support length 1 constraints.
10364 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010365
Eric Christopherde9399b2011-06-02 23:16:42 +000010366 char ConstraintLetter = Constraint[0];
10367 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010368 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000010369 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010370 case 'I': case 'J': case 'K': case 'L':
10371 case 'M': case 'N': case 'O':
10372 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10373 if (!C)
10374 return;
10375
10376 int64_t CVal64 = C->getSExtValue();
10377 int CVal = (int) CVal64;
10378 // None of these constraints allow values larger than 32 bits. Check
10379 // that the value fits in an int.
10380 if (CVal != CVal64)
10381 return;
10382
Eric Christopherde9399b2011-06-02 23:16:42 +000010383 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000010384 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000010385 // Constant suitable for movw, must be between 0 and
10386 // 65535.
10387 if (Subtarget->hasV6T2Ops())
10388 if (CVal >= 0 && CVal <= 65535)
10389 break;
10390 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010391 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000010392 if (Subtarget->isThumb1Only()) {
10393 // This must be a constant between 0 and 255, for ADD
10394 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010395 if (CVal >= 0 && CVal <= 255)
10396 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010397 } else if (Subtarget->isThumb2()) {
10398 // A constant that can be used as an immediate value in a
10399 // data-processing instruction.
10400 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10401 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010402 } else {
10403 // A constant that can be used as an immediate value in a
10404 // data-processing instruction.
10405 if (ARM_AM::getSOImmVal(CVal) != -1)
10406 break;
10407 }
10408 return;
10409
10410 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000010411 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010412 // This must be a constant between -255 and -1, for negated ADD
10413 // immediates. This can be used in GCC with an "n" modifier that
10414 // prints the negated value, for use with SUB instructions. It is
10415 // not useful otherwise but is implemented for compatibility.
10416 if (CVal >= -255 && CVal <= -1)
10417 break;
10418 } else {
10419 // This must be a constant between -4095 and 4095. It is not clear
10420 // what this constraint is intended for. Implemented for
10421 // compatibility with GCC.
10422 if (CVal >= -4095 && CVal <= 4095)
10423 break;
10424 }
10425 return;
10426
10427 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000010428 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010429 // A 32-bit value where only one byte has a nonzero value. Exclude
10430 // zero to match GCC. This constraint is used by GCC internally for
10431 // constants that can be loaded with a move/shift combination.
10432 // It is not useful otherwise but is implemented for compatibility.
10433 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10434 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010435 } else if (Subtarget->isThumb2()) {
10436 // A constant whose bitwise inverse can be used as an immediate
10437 // value in a data-processing instruction. This can be used in GCC
10438 // with a "B" modifier that prints the inverted value, for use with
10439 // BIC and MVN instructions. It is not useful otherwise but is
10440 // implemented for compatibility.
10441 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10442 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010443 } else {
10444 // A constant whose bitwise inverse can be used as an immediate
10445 // value in a data-processing instruction. This can be used in GCC
10446 // with a "B" modifier that prints the inverted value, for use with
10447 // BIC and MVN instructions. It is not useful otherwise but is
10448 // implemented for compatibility.
10449 if (ARM_AM::getSOImmVal(~CVal) != -1)
10450 break;
10451 }
10452 return;
10453
10454 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000010455 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010456 // This must be a constant between -7 and 7,
10457 // for 3-operand ADD/SUB immediate instructions.
10458 if (CVal >= -7 && CVal < 7)
10459 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010460 } else if (Subtarget->isThumb2()) {
10461 // A constant whose negation can be used as an immediate value in a
10462 // data-processing instruction. This can be used in GCC with an "n"
10463 // modifier that prints the negated value, for use with SUB
10464 // instructions. It is not useful otherwise but is implemented for
10465 // compatibility.
10466 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10467 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010468 } else {
10469 // A constant whose negation can be used as an immediate value in a
10470 // data-processing instruction. This can be used in GCC with an "n"
10471 // modifier that prints the negated value, for use with SUB
10472 // instructions. It is not useful otherwise but is implemented for
10473 // compatibility.
10474 if (ARM_AM::getSOImmVal(-CVal) != -1)
10475 break;
10476 }
10477 return;
10478
10479 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000010480 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010481 // This must be a multiple of 4 between 0 and 1020, for
10482 // ADD sp + immediate.
10483 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10484 break;
10485 } else {
10486 // A power of two or a constant between 0 and 32. This is used in
10487 // GCC for the shift amount on shifted register operands, but it is
10488 // useful in general for any shift amounts.
10489 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10490 break;
10491 }
10492 return;
10493
10494 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000010495 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010496 // This must be a constant between 0 and 31, for shift amounts.
10497 if (CVal >= 0 && CVal <= 31)
10498 break;
10499 }
10500 return;
10501
10502 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000010503 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010504 // This must be a multiple of 4 between -508 and 508, for
10505 // ADD/SUB sp = sp + immediate.
10506 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10507 break;
10508 }
10509 return;
10510 }
10511 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10512 break;
10513 }
10514
10515 if (Result.getNode()) {
10516 Ops.push_back(Result);
10517 return;
10518 }
Dale Johannesence97d552010-06-25 21:55:36 +000010519 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010520}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010521
Renato Golin87610692013-07-16 09:32:17 +000010522SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
10523 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only");
10524 unsigned Opcode = Op->getOpcode();
10525 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
10526 "Invalid opcode for Div/Rem lowering");
10527 bool isSigned = (Opcode == ISD::SDIVREM);
10528 EVT VT = Op->getValueType(0);
10529 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
10530
10531 RTLIB::Libcall LC;
10532 switch (VT.getSimpleVT().SimpleTy) {
10533 default: llvm_unreachable("Unexpected request for libcall!");
10534 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
10535 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
10536 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
10537 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
10538 }
10539
10540 SDValue InChain = DAG.getEntryNode();
10541
10542 TargetLowering::ArgListTy Args;
10543 TargetLowering::ArgListEntry Entry;
10544 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
10545 EVT ArgVT = Op->getOperand(i).getValueType();
10546 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
10547 Entry.Node = Op->getOperand(i);
10548 Entry.Ty = ArgTy;
10549 Entry.isSExt = isSigned;
10550 Entry.isZExt = !isSigned;
10551 Args.push_back(Entry);
10552 }
10553
10554 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
10555 getPointerTy());
10556
10557 Type *RetTy = (Type*)StructType::get(Ty, Ty, NULL);
10558
10559 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010560 TargetLowering::CallLoweringInfo CLI(DAG);
10561 CLI.setDebugLoc(dl).setChain(InChain)
10562 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, &Args, 0)
10563 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000010564
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010565 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000010566 return CallInfo.first;
10567}
10568
Saleem Abdulrasoolabac6e92014-06-09 20:18:42 +000010569SDValue
10570ARMTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
10571 assert(Subtarget->isTargetWindows() && "unsupported target platform");
10572 SDLoc DL(Op);
10573
10574 // Get the inputs.
10575 SDValue Chain = Op.getOperand(0);
10576 SDValue Size = Op.getOperand(1);
10577
10578 SDValue Words = DAG.getNode(ISD::SRL, DL, MVT::i32, Size,
10579 DAG.getConstant(2, MVT::i32));
10580
10581 SDValue Flag;
10582 Chain = DAG.getCopyToReg(Chain, DL, ARM::R4, Words, Flag);
10583 Flag = Chain.getValue(1);
10584
10585 SDVTList NodeTys = DAG.getVTList(MVT::i32, MVT::Glue);
10586 Chain = DAG.getNode(ARMISD::WIN__CHKSTK, DL, NodeTys, Chain, Flag);
10587
10588 SDValue NewSP = DAG.getCopyFromReg(Chain, DL, ARM::SP, MVT::i32);
10589 Chain = NewSP.getValue(1);
10590
10591 SDValue Ops[2] = { NewSP, Chain };
10592 return DAG.getMergeValues(Ops, DL);
10593}
10594
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010595bool
10596ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10597 // The ARM target isn't yet aware of offsets.
10598 return false;
10599}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010600
Jim Grosbach11013ed2010-07-16 23:05:05 +000010601bool ARM::isBitFieldInvertedMask(unsigned v) {
10602 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010603 return false;
10604
Jim Grosbach11013ed2010-07-16 23:05:05 +000010605 // there can be 1's on either or both "outsides", all the "inside"
10606 // bits must be 0's
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010607 unsigned TO = CountTrailingOnes_32(v);
10608 unsigned LO = CountLeadingOnes_32(v);
10609 v = (v >> TO) << TO;
10610 v = (v << LO) >> LO;
10611 return v == 0;
Jim Grosbach11013ed2010-07-16 23:05:05 +000010612}
10613
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010614/// isFPImmLegal - Returns true if the target can instruction select the
10615/// specified FP immediate natively. If false, the legalizer will
10616/// materialize the FP immediate as a load from a constant pool.
10617bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10618 if (!Subtarget->hasVFP3())
10619 return false;
10620 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010621 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010622 if (VT == MVT::f64)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010623 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010624 return false;
10625}
Bob Wilson5549d492010-09-21 17:56:22 +000010626
Wesley Peck527da1b2010-11-23 03:31:01 +000010627/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000010628/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10629/// specified in the intrinsic calls.
10630bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10631 const CallInst &I,
10632 unsigned Intrinsic) const {
10633 switch (Intrinsic) {
10634 case Intrinsic::arm_neon_vld1:
10635 case Intrinsic::arm_neon_vld2:
10636 case Intrinsic::arm_neon_vld3:
10637 case Intrinsic::arm_neon_vld4:
10638 case Intrinsic::arm_neon_vld2lane:
10639 case Intrinsic::arm_neon_vld3lane:
10640 case Intrinsic::arm_neon_vld4lane: {
10641 Info.opc = ISD::INTRINSIC_W_CHAIN;
10642 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010643 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010644 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10645 Info.ptrVal = I.getArgOperand(0);
10646 Info.offset = 0;
10647 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10648 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10649 Info.vol = false; // volatile loads with NEON intrinsics not supported
10650 Info.readMem = true;
10651 Info.writeMem = false;
10652 return true;
10653 }
10654 case Intrinsic::arm_neon_vst1:
10655 case Intrinsic::arm_neon_vst2:
10656 case Intrinsic::arm_neon_vst3:
10657 case Intrinsic::arm_neon_vst4:
10658 case Intrinsic::arm_neon_vst2lane:
10659 case Intrinsic::arm_neon_vst3lane:
10660 case Intrinsic::arm_neon_vst4lane: {
10661 Info.opc = ISD::INTRINSIC_VOID;
10662 // Conservatively set memVT to the entire set of vectors stored.
10663 unsigned NumElts = 0;
10664 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000010665 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000010666 if (!ArgTy->isVectorTy())
10667 break;
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010668 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010669 }
10670 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10671 Info.ptrVal = I.getArgOperand(0);
10672 Info.offset = 0;
10673 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10674 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10675 Info.vol = false; // volatile stores with NEON intrinsics not supported
10676 Info.readMem = false;
10677 Info.writeMem = true;
10678 return true;
10679 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010680 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010681 case Intrinsic::arm_ldrex: {
10682 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
10683 Info.opc = ISD::INTRINSIC_W_CHAIN;
10684 Info.memVT = MVT::getVT(PtrTy->getElementType());
10685 Info.ptrVal = I.getArgOperand(0);
10686 Info.offset = 0;
10687 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10688 Info.vol = true;
10689 Info.readMem = true;
10690 Info.writeMem = false;
10691 return true;
10692 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010693 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010694 case Intrinsic::arm_strex: {
10695 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
10696 Info.opc = ISD::INTRINSIC_W_CHAIN;
10697 Info.memVT = MVT::getVT(PtrTy->getElementType());
10698 Info.ptrVal = I.getArgOperand(1);
10699 Info.offset = 0;
10700 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10701 Info.vol = true;
10702 Info.readMem = false;
10703 Info.writeMem = true;
10704 return true;
10705 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010706 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010707 case Intrinsic::arm_strexd: {
10708 Info.opc = ISD::INTRINSIC_W_CHAIN;
10709 Info.memVT = MVT::i64;
10710 Info.ptrVal = I.getArgOperand(2);
10711 Info.offset = 0;
10712 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010713 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010714 Info.readMem = false;
10715 Info.writeMem = true;
10716 return true;
10717 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010718 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010719 case Intrinsic::arm_ldrexd: {
10720 Info.opc = ISD::INTRINSIC_W_CHAIN;
10721 Info.memVT = MVT::i64;
10722 Info.ptrVal = I.getArgOperand(0);
10723 Info.offset = 0;
10724 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010725 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010726 Info.readMem = true;
10727 Info.writeMem = false;
10728 return true;
10729 }
Bob Wilson5549d492010-09-21 17:56:22 +000010730 default:
10731 break;
10732 }
10733
10734 return false;
10735}
Juergen Ributzka659ce002014-01-28 01:20:14 +000010736
10737/// \brief Returns true if it is beneficial to convert a load of a constant
10738/// to just the constant itself.
10739bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
10740 Type *Ty) const {
10741 assert(Ty->isIntegerTy());
10742
10743 unsigned Bits = Ty->getPrimitiveSizeInBits();
10744 if (Bits == 0 || Bits > 32)
10745 return false;
10746 return true;
10747}
Tim Northover037f26f22014-04-17 18:22:47 +000010748
10749bool ARMTargetLowering::shouldExpandAtomicInIR(Instruction *Inst) const {
10750 // Loads and stores less than 64-bits are already atomic; ones above that
10751 // are doomed anyway, so defer to the default libcall and blame the OS when
Tim Northoverb45c3b72014-06-16 18:49:36 +000010752 // things go wrong. Cortex M doesn't have ldrexd/strexd though, so don't emit
10753 // anything for those.
10754 bool IsMClass = Subtarget->isMClass();
10755 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
10756 unsigned Size = SI->getValueOperand()->getType()->getPrimitiveSizeInBits();
10757 return Size == 64 && !IsMClass;
10758 } else if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
10759 return LI->getType()->getPrimitiveSizeInBits() == 64 && !IsMClass;
10760 }
Tim Northover037f26f22014-04-17 18:22:47 +000010761
Tim Northoverb45c3b72014-06-16 18:49:36 +000010762 // For the real atomic operations, we have ldrex/strex up to 32 bits,
10763 // and up to 64 bits on the non-M profiles
10764 unsigned AtomicLimit = IsMClass ? 32 : 64;
10765 return Inst->getType()->getPrimitiveSizeInBits() <= AtomicLimit;
Tim Northover037f26f22014-04-17 18:22:47 +000010766}
10767
10768Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
10769 AtomicOrdering Ord) const {
10770 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
10771 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
10772 bool IsAcquire =
10773 Ord == Acquire || Ord == AcquireRelease || Ord == SequentiallyConsistent;
10774
10775 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
10776 // intrinsic must return {i32, i32} and we have to recombine them into a
10777 // single i64 here.
10778 if (ValTy->getPrimitiveSizeInBits() == 64) {
10779 Intrinsic::ID Int =
10780 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
10781 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
10782
10783 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
10784 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
10785
10786 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
10787 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000010788 if (!Subtarget->isLittle())
10789 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000010790 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
10791 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
10792 return Builder.CreateOr(
10793 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
10794 }
10795
10796 Type *Tys[] = { Addr->getType() };
10797 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
10798 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
10799
10800 return Builder.CreateTruncOrBitCast(
10801 Builder.CreateCall(Ldrex, Addr),
10802 cast<PointerType>(Addr->getType())->getElementType());
10803}
10804
10805Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
10806 Value *Addr,
10807 AtomicOrdering Ord) const {
10808 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
10809 bool IsRelease =
10810 Ord == Release || Ord == AcquireRelease || Ord == SequentiallyConsistent;
10811
10812 // Since the intrinsics must have legal type, the i64 intrinsics take two
10813 // parameters: "i32, i32". We must marshal Val into the appropriate form
10814 // before the call.
10815 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
10816 Intrinsic::ID Int =
10817 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
10818 Function *Strex = Intrinsic::getDeclaration(M, Int);
10819 Type *Int32Ty = Type::getInt32Ty(M->getContext());
10820
10821 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
10822 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000010823 if (!Subtarget->isLittle())
10824 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000010825 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
10826 return Builder.CreateCall3(Strex, Lo, Hi, Addr);
10827 }
10828
10829 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
10830 Type *Tys[] = { Addr->getType() };
10831 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
10832
10833 return Builder.CreateCall2(
10834 Strex, Builder.CreateZExtOrBitCast(
10835 Val, Strex->getFunctionType()->getParamType(0)),
10836 Addr);
10837}
Oliver Stannardc24f2172014-05-09 14:01:47 +000010838
10839enum HABaseType {
10840 HA_UNKNOWN = 0,
10841 HA_FLOAT,
10842 HA_DOUBLE,
10843 HA_VECT64,
10844 HA_VECT128
10845};
10846
10847static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
10848 uint64_t &Members) {
10849 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
10850 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
10851 uint64_t SubMembers = 0;
10852 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
10853 return false;
10854 Members += SubMembers;
10855 }
10856 } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
10857 uint64_t SubMembers = 0;
10858 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
10859 return false;
10860 Members += SubMembers * AT->getNumElements();
10861 } else if (Ty->isFloatTy()) {
10862 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
10863 return false;
10864 Members = 1;
10865 Base = HA_FLOAT;
10866 } else if (Ty->isDoubleTy()) {
10867 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
10868 return false;
10869 Members = 1;
10870 Base = HA_DOUBLE;
10871 } else if (const VectorType *VT = dyn_cast<VectorType>(Ty)) {
10872 Members = 1;
10873 switch (Base) {
10874 case HA_FLOAT:
10875 case HA_DOUBLE:
10876 return false;
10877 case HA_VECT64:
10878 return VT->getBitWidth() == 64;
10879 case HA_VECT128:
10880 return VT->getBitWidth() == 128;
10881 case HA_UNKNOWN:
10882 switch (VT->getBitWidth()) {
10883 case 64:
10884 Base = HA_VECT64;
10885 return true;
10886 case 128:
10887 Base = HA_VECT128;
10888 return true;
10889 default:
10890 return false;
10891 }
10892 }
10893 }
10894
10895 return (Members > 0 && Members <= 4);
10896}
10897
10898/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate.
10899bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
10900 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000010901 if (getEffectiveCallingConv(CallConv, isVarArg) !=
10902 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000010903 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000010904
10905 HABaseType Base = HA_UNKNOWN;
10906 uint64_t Members = 0;
10907 bool result = isHomogeneousAggregate(Ty, Base, Members);
10908 DEBUG(dbgs() << "isHA: " << result << " "; Ty->dump(); dbgs() << "\n");
10909 return result;
Oliver Stannardc24f2172014-05-09 14:01:47 +000010910}