blob: 00d07e84067425c2293804b6789ec40d52d6a856 [file] [log] [blame]
Evan Cheng10043e22007-01-19 07:51:42 +00001//===-- ARMISelLowering.cpp - ARM DAG Lowering Implementation -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Cheng10043e22007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that ARM uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Craig Topper188ed9d2012-03-17 07:33:42 +000015#include "ARMISelLowering.h"
Eric Christopher1c069172010-09-10 22:42:06 +000016#include "ARMCallingConv.h"
Evan Cheng10043e22007-01-19 07:51:42 +000017#include "ARMConstantPoolValue.h"
Evan Cheng10043e22007-01-19 07:51:42 +000018#include "ARMMachineFunctionInfo.h"
Anton Korobeynikov9a232f42009-08-21 12:41:24 +000019#include "ARMPerfectShuffle.h"
Evan Cheng10043e22007-01-19 07:51:42 +000020#include "ARMSubtarget.h"
21#include "ARMTargetMachine.h"
Chris Lattner4e7dfaf2009-08-02 00:34:36 +000022#include "ARMTargetObjectFile.h"
Evan Chenga20cde32011-07-20 23:34:39 +000023#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/ADT/Statistic.h"
25#include "llvm/ADT/StringExtras.h"
Bob Wilsona4c22902009-04-17 19:07:39 +000026#include "llvm/CodeGen/CallingConvLower.h"
Evan Cheng078b0b02011-01-08 01:24:27 +000027#include "llvm/CodeGen/IntrinsicLowering.h"
Evan Cheng10043e22007-01-19 07:51:42 +000028#include "llvm/CodeGen/MachineBasicBlock.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Bill Wendling202803e2011-10-05 00:02:33 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng10043e22007-01-19 07:51:42 +000034#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/CallingConv.h"
36#include "llvm/IR/Constants.h"
37#include "llvm/IR/Function.h"
38#include "llvm/IR/GlobalValue.h"
Tim Northover037f26f22014-04-17 18:22:47 +000039#include "llvm/IR/IRBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000040#include "llvm/IR/Instruction.h"
41#include "llvm/IR/Instructions.h"
42#include "llvm/IR/Intrinsics.h"
43#include "llvm/IR/Type.h"
Bill Wendling46ffefc2010-03-09 02:46:12 +000044#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach32bb3622010-04-14 22:28:31 +000045#include "llvm/Support/CommandLine.h"
Oliver Stannardc24f2172014-05-09 14:01:47 +000046#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000047#include "llvm/Support/ErrorHandling.h"
Evan Cheng2150b922007-03-12 23:30:29 +000048#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000049#include "llvm/Target/TargetOptions.h"
David Peixottoc32e24a2013-10-17 19:49:22 +000050#include <utility>
Evan Cheng10043e22007-01-19 07:51:42 +000051using namespace llvm;
52
Chandler Carruth84e68b22014-04-22 02:41:26 +000053#define DEBUG_TYPE "arm-isel"
54
Dale Johannesend679ff72010-06-03 21:09:53 +000055STATISTIC(NumTailCalls, "Number of tail calls");
Evan Cheng68aec142011-01-19 02:16:49 +000056STATISTIC(NumMovwMovt, "Number of GAs materialized with movw + movt");
Manman Ren9f911162012-06-01 02:44:42 +000057STATISTIC(NumLoopByVals, "Number of loops generated for byval arguments");
Dale Johannesend679ff72010-06-03 21:09:53 +000058
Eric Christopher347f4c32010-12-15 23:47:29 +000059cl::opt<bool>
Jim Grosbach32bb3622010-04-14 22:28:31 +000060EnableARMLongCalls("arm-long-calls", cl::Hidden,
Evan Cheng25f93642010-07-08 02:08:50 +000061 cl::desc("Generate calls via indirect call instructions"),
Jim Grosbach32bb3622010-04-14 22:28:31 +000062 cl::init(false));
63
Evan Chengf128bdc2010-06-16 07:35:02 +000064static cl::opt<bool>
65ARMInterworking("arm-interworking", cl::Hidden,
66 cl::desc("Enable / disable ARM interworking (for debugging only)"),
67 cl::init(true));
68
Benjamin Kramer7ba71be2011-11-26 23:01:57 +000069namespace {
Cameron Zwarich89019782011-06-10 20:59:24 +000070 class ARMCCState : public CCState {
71 public:
72 ARMCCState(CallingConv::ID CC, bool isVarArg, MachineFunction &MF,
Craig Topperb94011f2013-07-14 04:42:23 +000073 const TargetMachine &TM, SmallVectorImpl<CCValAssign> &locs,
Cameron Zwarich89019782011-06-10 20:59:24 +000074 LLVMContext &C, ParmContext PC)
75 : CCState(CC, isVarArg, MF, TM, locs, C) {
76 assert(((PC == Call) || (PC == Prologue)) &&
77 "ARMCCState users must specify whether their context is call"
78 "or prologue generation.");
79 CallOrPrologue = PC;
80 }
81 };
82}
83
Stuart Hastings45fe3c32011-04-20 16:47:52 +000084// The APCS parameter registers.
Craig Topper840beec2014-04-04 05:16:06 +000085static const MCPhysReg GPRArgRegs[] = {
Stuart Hastings45fe3c32011-04-20 16:47:52 +000086 ARM::R0, ARM::R1, ARM::R2, ARM::R3
87};
88
Craig Topper4fa625f2012-08-12 03:16:37 +000089void ARMTargetLowering::addTypeForNEON(MVT VT, MVT PromotedLdStVT,
90 MVT PromotedBitwiseVT) {
Bob Wilson2e076c42009-06-22 23:27:02 +000091 if (VT != PromotedLdStVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +000092 setOperationAction(ISD::LOAD, VT, Promote);
93 AddPromotedToType (ISD::LOAD, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000094
Craig Topper4fa625f2012-08-12 03:16:37 +000095 setOperationAction(ISD::STORE, VT, Promote);
96 AddPromotedToType (ISD::STORE, VT, PromotedLdStVT);
Bob Wilson2e076c42009-06-22 23:27:02 +000097 }
98
Craig Topper4fa625f2012-08-12 03:16:37 +000099 MVT ElemTy = VT.getVectorElementType();
Owen Anderson9f944592009-08-11 20:47:22 +0000100 if (ElemTy != MVT::i64 && ElemTy != MVT::f64)
Craig Topper4fa625f2012-08-12 03:16:37 +0000101 setOperationAction(ISD::SETCC, VT, Custom);
102 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
103 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000104 if (ElemTy == MVT::i32) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000105 setOperationAction(ISD::SINT_TO_FP, VT, Custom);
106 setOperationAction(ISD::UINT_TO_FP, VT, Custom);
107 setOperationAction(ISD::FP_TO_SINT, VT, Custom);
108 setOperationAction(ISD::FP_TO_UINT, VT, Custom);
Eli Friedman2d4055b2011-11-09 23:36:02 +0000109 } else {
Craig Topper4fa625f2012-08-12 03:16:37 +0000110 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
111 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
112 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
113 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Bob Wilson5d8cfb22009-09-16 20:20:44 +0000114 }
Craig Topper4fa625f2012-08-12 03:16:37 +0000115 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
116 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
117 setOperationAction(ISD::CONCAT_VECTORS, VT, Legal);
118 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
119 setOperationAction(ISD::SELECT, VT, Expand);
120 setOperationAction(ISD::SELECT_CC, VT, Expand);
Jim Grosbach30af4422012-10-12 22:59:21 +0000121 setOperationAction(ISD::VSELECT, VT, Expand);
Craig Topper4fa625f2012-08-12 03:16:37 +0000122 setOperationAction(ISD::SIGN_EXTEND_INREG, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000123 if (VT.isInteger()) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000124 setOperationAction(ISD::SHL, VT, Custom);
125 setOperationAction(ISD::SRA, VT, Custom);
126 setOperationAction(ISD::SRL, VT, Custom);
Bob Wilson2e076c42009-06-22 23:27:02 +0000127 }
128
129 // Promote all bit-wise operations.
130 if (VT.isInteger() && VT != PromotedBitwiseVT) {
Craig Topper4fa625f2012-08-12 03:16:37 +0000131 setOperationAction(ISD::AND, VT, Promote);
132 AddPromotedToType (ISD::AND, VT, PromotedBitwiseVT);
133 setOperationAction(ISD::OR, VT, Promote);
134 AddPromotedToType (ISD::OR, VT, PromotedBitwiseVT);
135 setOperationAction(ISD::XOR, VT, Promote);
136 AddPromotedToType (ISD::XOR, VT, PromotedBitwiseVT);
Bob Wilson2e076c42009-06-22 23:27:02 +0000137 }
Bob Wilson4ed397c2009-09-16 00:17:28 +0000138
139 // Neon does not support vector divide/remainder operations.
Craig Topper4fa625f2012-08-12 03:16:37 +0000140 setOperationAction(ISD::SDIV, VT, Expand);
141 setOperationAction(ISD::UDIV, VT, Expand);
142 setOperationAction(ISD::FDIV, VT, Expand);
143 setOperationAction(ISD::SREM, VT, Expand);
144 setOperationAction(ISD::UREM, VT, Expand);
145 setOperationAction(ISD::FREM, VT, Expand);
Bob Wilson2e076c42009-06-22 23:27:02 +0000146}
147
Craig Topper4fa625f2012-08-12 03:16:37 +0000148void ARMTargetLowering::addDRTypeForNEON(MVT VT) {
Craig Topperc7242e02012-04-20 07:30:17 +0000149 addRegisterClass(VT, &ARM::DPRRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000150 addTypeForNEON(VT, MVT::f64, MVT::v2i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000151}
152
Craig Topper4fa625f2012-08-12 03:16:37 +0000153void ARMTargetLowering::addQRTypeForNEON(MVT VT) {
Jakob Stoklund Olesen20912062014-01-14 06:18:34 +0000154 addRegisterClass(VT, &ARM::DPairRegClass);
Owen Anderson9f944592009-08-11 20:47:22 +0000155 addTypeForNEON(VT, MVT::v2f64, MVT::v4i32);
Bob Wilson2e076c42009-06-22 23:27:02 +0000156}
157
Chris Lattner5e693ed2009-07-28 03:13:23 +0000158static TargetLoweringObjectFile *createTLOF(TargetMachine &TM) {
Tim Northoverd6a729b2014-01-06 14:28:05 +0000159 if (TM.getSubtarget<ARMSubtarget>().isTargetMachO())
Bill Wendlingbbcaa402010-03-15 21:09:38 +0000160 return new TargetLoweringObjectFileMachO();
Saleem Abdulrasool46fed302014-05-17 04:28:08 +0000161 if (TM.getSubtarget<ARMSubtarget>().isTargetWindows())
162 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)
Evan Cheng408aa562009-11-06 22:24:13 +0000167 : TargetLowering(TM, createTLOF(TM)) {
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
Evan Chengf7f97b42010-04-15 22:20:34 +0000713 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Evan Cheng6e809de2010-08-11 06:22:01 +0000714 // ARMv6 Thumb1 (except for CPUs that support dmb / dsb) and earlier use
715 // the default expansion.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000716 if (Subtarget->hasAnyDataBarrier() && !Subtarget->isThumb1Only()) {
Tim Northoverc882eb02014-04-03 11:44:58 +0000717 // ATOMIC_FENCE needs custom lowering; the others should have been expanded
718 // to ldrex/strex loops already.
Tim Northoverc7ea8042013-10-25 09:30:24 +0000719 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
Tim Northoverc882eb02014-04-03 11:44:58 +0000720
Amara Emersonb4ad2f32013-09-26 12:22:36 +0000721 // On v8, we have particularly efficient implementations of atomic fences
722 // if they can be combined with nearby atomic loads and stores.
723 if (!Subtarget->hasV8Ops()) {
724 // Automatically insert fences (dmb ist) around ATOMIC_SWAP etc.
725 setInsertFencesForAtomic(true);
726 }
Jim Grosbach6860bb72010-06-18 22:35:32 +0000727 } else {
Tim Northoverc7ea8042013-10-25 09:30:24 +0000728 // If there's anything we can use as a barrier, go through custom lowering
729 // for ATOMIC_FENCE.
730 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other,
731 Subtarget->hasAnyDataBarrier() ? Custom : Expand);
732
Jim Grosbach6860bb72010-06-18 22:35:32 +0000733 // Set them all for expansion, which will force libcalls.
Jim Grosbach6860bb72010-06-18 22:35:32 +0000734 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Expand);
Jim Grosbacha57c2882010-06-18 23:03:10 +0000735 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000736 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000737 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000738 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000739 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000740 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Expand);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000741 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000742 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000743 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000744 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Expand);
Jim Grosbachd4b733e2011-04-26 19:44:18 +0000745 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Expand);
Eli Friedmanba912e02011-09-15 22:18:49 +0000746 // Mark ATOMIC_LOAD and ATOMIC_STORE custom so we can handle the
747 // Unordered/Monotonic case.
748 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
749 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
Jim Grosbach6860bb72010-06-18 22:35:32 +0000750 }
Evan Cheng10043e22007-01-19 07:51:42 +0000751
Evan Cheng21acf9f2010-11-04 05:19:35 +0000752 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
Evan Cheng6f360422010-11-03 05:14:24 +0000753
Eli Friedman8cfa7712010-06-26 04:36:50 +0000754 // Requires SXTB/SXTH, available on v6 and up in both ARM and Thumb modes.
755 if (!Subtarget->hasV6Ops()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000756 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
757 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000758 }
Owen Anderson9f944592009-08-11 20:47:22 +0000759 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Evan Cheng10043e22007-01-19 07:51:42 +0000760
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000761 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
762 !Subtarget->isThumb1Only()) {
Bob Wilson6a4491b2010-01-19 22:56:26 +0000763 // Turn f64->i64 into VMOVRRD, i64 -> f64 to VMOVDRR
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000764 // iff target supports vfp2.
Wesley Peck527da1b2010-11-23 03:31:01 +0000765 setOperationAction(ISD::BITCAST, MVT::i64, Custom);
Nate Begemanb69b1822010-08-03 21:31:55 +0000766 setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
767 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000768
769 // We want to custom lower some of our intrinsics.
Owen Anderson9f944592009-08-11 20:47:22 +0000770 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Jim Grosbach31984832010-07-07 00:07:57 +0000771 if (Subtarget->isTargetDarwin()) {
772 setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
773 setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
John McCall7d84ece2011-05-29 19:50:32 +0000774 setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume");
Jim Grosbach31984832010-07-07 00:07:57 +0000775 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +0000776
Owen Anderson9f944592009-08-11 20:47:22 +0000777 setOperationAction(ISD::SETCC, MVT::i32, Expand);
778 setOperationAction(ISD::SETCC, MVT::f32, Expand);
779 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Bill Wendling6a981312010-08-11 08:43:16 +0000780 setOperationAction(ISD::SELECT, MVT::i32, Custom);
781 setOperationAction(ISD::SELECT, MVT::f32, Custom);
782 setOperationAction(ISD::SELECT, MVT::f64, Custom);
Owen Anderson9f944592009-08-11 20:47:22 +0000783 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
784 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
785 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000786
Owen Anderson9f944592009-08-11 20:47:22 +0000787 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
788 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
789 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
790 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
791 setOperationAction(ISD::BR_JT, MVT::Other, Custom);
Evan Cheng10043e22007-01-19 07:51:42 +0000792
Dan Gohman482732a2007-10-11 23:21:31 +0000793 // We don't support sin/cos/fmod/copysign/pow
Owen Anderson9f944592009-08-11 20:47:22 +0000794 setOperationAction(ISD::FSIN, MVT::f64, Expand);
795 setOperationAction(ISD::FSIN, MVT::f32, Expand);
796 setOperationAction(ISD::FCOS, MVT::f32, Expand);
797 setOperationAction(ISD::FCOS, MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +0000798 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
799 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +0000800 setOperationAction(ISD::FREM, MVT::f64, Expand);
801 setOperationAction(ISD::FREM, MVT::f32, Expand);
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000802 if (!TM.Options.UseSoftFloat && Subtarget->hasVFP2() &&
803 !Subtarget->isThumb1Only()) {
Owen Anderson9f944592009-08-11 20:47:22 +0000804 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
805 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng86e476b2008-04-01 01:50:16 +0000806 }
Owen Anderson9f944592009-08-11 20:47:22 +0000807 setOperationAction(ISD::FPOW, MVT::f64, Expand);
808 setOperationAction(ISD::FPOW, MVT::f32, Expand);
Bob Wilson7117a912009-03-20 22:42:55 +0000809
Evan Chengd0007f32012-04-10 21:40:28 +0000810 if (!Subtarget->hasVFP4()) {
811 setOperationAction(ISD::FMA, MVT::f64, Expand);
812 setOperationAction(ISD::FMA, MVT::f32, Expand);
813 }
Cameron Zwarichf03fa182011-07-08 21:39:21 +0000814
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000815 // Various VFP goodness
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000816 if (!TM.Options.UseSoftFloat && !Subtarget->isThumb1Only()) {
Bob Wilsone4191e72010-03-19 22:51:32 +0000817 // int <-> fp are custom expanded into bit_convert + ARMISD ops.
818 if (Subtarget->hasVFP2()) {
819 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
820 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
821 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
822 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
823 }
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000824 // Special handling for half-precision FP.
Anton Korobeynikov64578d52010-03-18 22:35:37 +0000825 if (!Subtarget->hasFP16()) {
826 setOperationAction(ISD::FP16_TO_FP32, MVT::f32, Expand);
827 setOperationAction(ISD::FP32_TO_FP16, MVT::i32, Expand);
Anton Korobeynikovd7fece32010-03-14 18:42:31 +0000828 }
Evan Cheng86e476b2008-04-01 01:50:16 +0000829 }
Jim Grosbach1a597112014-04-03 23:43:18 +0000830
Bob Wilsone7dde0c2013-11-03 06:14:38 +0000831 // Combine sin / cos into one node or libcall if possible.
832 if (Subtarget->hasSinCos()) {
833 setLibcallName(RTLIB::SINCOS_F32, "sincosf");
834 setLibcallName(RTLIB::SINCOS_F64, "sincos");
835 if (Subtarget->getTargetTriple().getOS() == Triple::IOS) {
836 // For iOS, we don't want to the normal expansion of a libcall to
837 // sincos. We want to issue a libcall to __sincos_stret.
838 setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
839 setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
840 }
841 }
Evan Cheng10043e22007-01-19 07:51:42 +0000842
Chris Lattnerf3f4ad92007-11-27 22:36:16 +0000843 // We have target-specific dag combine patterns for the following nodes:
Jim Grosbachd7cf55c2009-11-09 00:11:35 +0000844 // ARMISD::VMOVRRD - No need to call setTargetDAGCombine
Chris Lattner4147f082009-03-12 06:52:53 +0000845 setTargetDAGCombine(ISD::ADD);
846 setTargetDAGCombine(ISD::SUB);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +0000847 setTargetDAGCombine(ISD::MUL);
Jakob Stoklund Olesene45e22b2012-09-07 17:34:15 +0000848 setTargetDAGCombine(ISD::AND);
849 setTargetDAGCombine(ISD::OR);
850 setTargetDAGCombine(ISD::XOR);
Jim Grosbach11013ed2010-07-16 23:05:05 +0000851
Evan Chengf258a152012-02-23 02:58:19 +0000852 if (Subtarget->hasV6Ops())
853 setTargetDAGCombine(ISD::SRL);
854
Evan Cheng10043e22007-01-19 07:51:42 +0000855 setStackPointerRegisterToSaveRestore(ARM::SP);
Evan Cheng4401f882010-05-20 23:26:43 +0000856
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000857 if (TM.Options.UseSoftFloat || Subtarget->isThumb1Only() ||
858 !Subtarget->hasVFP2())
Evan Cheng34c26042010-05-21 00:43:17 +0000859 setSchedulingPreference(Sched::RegPressure);
860 else
861 setSchedulingPreference(Sched::Hybrid);
Dale Johannesen58698d22007-05-17 21:31:21 +0000862
Evan Cheng3ae2b792011-01-06 06:52:41 +0000863 //// temporary - rewrite interface to use type
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000864 MaxStoresPerMemset = 8;
865 MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
866 MaxStoresPerMemcpy = 4; // For @llvm.memcpy -> sequence of stores
867 MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
868 MaxStoresPerMemmove = 4; // For @llvm.memmove -> sequence of stores
869 MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 4 : 2;
Evan Chengb71233f2010-06-26 01:52:05 +0000870
Rafael Espindolaa76eccf2010-07-11 04:01:49 +0000871 // On ARM arguments smaller than 4 bytes are extended, so all arguments
872 // are at least 4 bytes aligned.
873 setMinStackArgumentAlignment(4);
874
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000875 // Prefer likely predicted branches to selects on out-of-order cores.
Jim Grosbach341ad3e2013-02-20 21:13:59 +0000876 PredictableSelectIsExpensive = Subtarget->isLikeA9();
Benjamin Kramere31f31e2012-05-05 12:49:14 +0000877
Eli Friedman2518f832011-05-06 20:34:06 +0000878 setMinFunctionAlignment(Subtarget->isThumb() ? 1 : 2);
Evan Cheng10043e22007-01-19 07:51:42 +0000879}
880
Andrew Trick43f25632011-01-19 02:35:27 +0000881// FIXME: It might make sense to define the representative register class as the
882// nearest super-register that has a non-null superset. For example, DPR_VFP2 is
883// a super-register of SPR, and DPR is a superset if DPR_VFP2. Consequently,
884// SPR's representative would be DPR_VFP2. This should work well if register
885// pressure tracking were modified such that a register use would increment the
886// pressure of the register class's representative and all of it's super
887// classes' representatives transitively. We have not implemented this because
888// of the difficulty prior to coalescing of modeling operand register classes
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000889// due to the common occurrence of cross class copies and subregister insertions
Andrew Trick43f25632011-01-19 02:35:27 +0000890// and extractions.
Evan Chenga77f3d32010-07-21 06:09:07 +0000891std::pair<const TargetRegisterClass*, uint8_t>
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000892ARMTargetLowering::findRepresentativeClass(MVT VT) const{
Craig Topper062a2ba2014-04-25 05:30:21 +0000893 const TargetRegisterClass *RRC = nullptr;
Evan Chenga77f3d32010-07-21 06:09:07 +0000894 uint8_t Cost = 1;
Patrik Hagglundf9eb1682012-12-19 11:30:36 +0000895 switch (VT.SimpleTy) {
Evan Cheng10f99a32010-07-19 22:15:08 +0000896 default:
Evan Chenga77f3d32010-07-21 06:09:07 +0000897 return TargetLowering::findRepresentativeClass(VT);
Evan Cheng28590382010-07-21 23:53:58 +0000898 // Use DPR as representative register class for all floating point
899 // and vector types. Since there are 32 SPR registers and 32 DPR registers so
900 // the cost is 1 for both f32 and f64.
901 case MVT::f32: case MVT::f64: case MVT::v8i8: case MVT::v4i16:
Evan Chenga77f3d32010-07-21 06:09:07 +0000902 case MVT::v2i32: case MVT::v1i64: case MVT::v2f32:
Craig Topperc7242e02012-04-20 07:30:17 +0000903 RRC = &ARM::DPRRegClass;
Andrew Trick43f25632011-01-19 02:35:27 +0000904 // When NEON is used for SP, only half of the register file is available
905 // because operations that define both SP and DP results will be constrained
906 // to the VFP2 class (D0-D15). We currently model this constraint prior to
907 // coalescing by double-counting the SP regs. See the FIXME above.
908 if (Subtarget->useNEONForSinglePrecisionFP())
909 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000910 break;
911 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
912 case MVT::v4f32: case MVT::v2f64:
Craig Topperc7242e02012-04-20 07:30:17 +0000913 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000914 Cost = 2;
Evan Chenga77f3d32010-07-21 06:09:07 +0000915 break;
916 case MVT::v4i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000917 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000918 Cost = 4;
Evan Chenga77f3d32010-07-21 06:09:07 +0000919 break;
920 case MVT::v8i64:
Craig Topperc7242e02012-04-20 07:30:17 +0000921 RRC = &ARM::DPRRegClass;
Evan Cheng28590382010-07-21 23:53:58 +0000922 Cost = 8;
Evan Chenga77f3d32010-07-21 06:09:07 +0000923 break;
Evan Cheng10f99a32010-07-19 22:15:08 +0000924 }
Evan Chenga77f3d32010-07-21 06:09:07 +0000925 return std::make_pair(RRC, Cost);
Evan Cheng10f99a32010-07-19 22:15:08 +0000926}
927
Evan Cheng10043e22007-01-19 07:51:42 +0000928const char *ARMTargetLowering::getTargetNodeName(unsigned Opcode) const {
929 switch (Opcode) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000930 default: return nullptr;
Evan Cheng10043e22007-01-19 07:51:42 +0000931 case ARMISD::Wrapper: return "ARMISD::Wrapper";
Evan Chengdfce83c2011-01-17 08:03:18 +0000932 case ARMISD::WrapperPIC: return "ARMISD::WrapperPIC";
Evan Cheng10043e22007-01-19 07:51:42 +0000933 case ARMISD::WrapperJT: return "ARMISD::WrapperJT";
934 case ARMISD::CALL: return "ARMISD::CALL";
Evan Chengc3c949b42007-06-19 21:05:09 +0000935 case ARMISD::CALL_PRED: return "ARMISD::CALL_PRED";
Evan Cheng10043e22007-01-19 07:51:42 +0000936 case ARMISD::CALL_NOLINK: return "ARMISD::CALL_NOLINK";
937 case ARMISD::tCALL: return "ARMISD::tCALL";
938 case ARMISD::BRCOND: return "ARMISD::BRCOND";
939 case ARMISD::BR_JT: return "ARMISD::BR_JT";
Evan Chengc6d70ae2009-07-29 02:18:14 +0000940 case ARMISD::BR2_JT: return "ARMISD::BR2_JT";
Evan Cheng10043e22007-01-19 07:51:42 +0000941 case ARMISD::RET_FLAG: return "ARMISD::RET_FLAG";
Tim Northoverd8407452013-10-01 14:33:28 +0000942 case ARMISD::INTRET_FLAG: return "ARMISD::INTRET_FLAG";
Evan Cheng10043e22007-01-19 07:51:42 +0000943 case ARMISD::PIC_ADD: return "ARMISD::PIC_ADD";
944 case ARMISD::CMP: return "ARMISD::CMP";
Bill Wendling4b796472012-06-11 08:07:26 +0000945 case ARMISD::CMN: return "ARMISD::CMN";
David Goodwindbf11ba2009-06-29 15:33:01 +0000946 case ARMISD::CMPZ: return "ARMISD::CMPZ";
Evan Cheng10043e22007-01-19 07:51:42 +0000947 case ARMISD::CMPFP: return "ARMISD::CMPFP";
948 case ARMISD::CMPFPw0: return "ARMISD::CMPFPw0";
Evan Cheng0cc4ad92010-07-13 19:27:42 +0000949 case ARMISD::BCC_i64: return "ARMISD::BCC_i64";
Evan Cheng10043e22007-01-19 07:51:42 +0000950 case ARMISD::FMSTAT: return "ARMISD::FMSTAT";
Evan Chenge87681c2012-02-23 01:19:06 +0000951
Evan Cheng10043e22007-01-19 07:51:42 +0000952 case ARMISD::CMOV: return "ARMISD::CMOV";
Bob Wilson7117a912009-03-20 22:42:55 +0000953
Jim Grosbach8546ec92010-01-18 19:58:49 +0000954 case ARMISD::RBIT: return "ARMISD::RBIT";
955
Bob Wilsone4191e72010-03-19 22:51:32 +0000956 case ARMISD::FTOSI: return "ARMISD::FTOSI";
957 case ARMISD::FTOUI: return "ARMISD::FTOUI";
958 case ARMISD::SITOF: return "ARMISD::SITOF";
959 case ARMISD::UITOF: return "ARMISD::UITOF";
960
Evan Cheng10043e22007-01-19 07:51:42 +0000961 case ARMISD::SRL_FLAG: return "ARMISD::SRL_FLAG";
962 case ARMISD::SRA_FLAG: return "ARMISD::SRA_FLAG";
963 case ARMISD::RRX: return "ARMISD::RRX";
Bob Wilson7117a912009-03-20 22:42:55 +0000964
Evan Chenge8916542011-08-30 01:34:54 +0000965 case ARMISD::ADDC: return "ARMISD::ADDC";
966 case ARMISD::ADDE: return "ARMISD::ADDE";
967 case ARMISD::SUBC: return "ARMISD::SUBC";
968 case ARMISD::SUBE: return "ARMISD::SUBE";
969
Bob Wilson22806742010-09-22 22:09:21 +0000970 case ARMISD::VMOVRRD: return "ARMISD::VMOVRRD";
971 case ARMISD::VMOVDRR: return "ARMISD::VMOVDRR";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +0000972
Evan Chengec6d7c92009-10-28 06:55:03 +0000973 case ARMISD::EH_SJLJ_SETJMP: return "ARMISD::EH_SJLJ_SETJMP";
974 case ARMISD::EH_SJLJ_LONGJMP:return "ARMISD::EH_SJLJ_LONGJMP";
975
Dale Johannesend679ff72010-06-03 21:09:53 +0000976 case ARMISD::TC_RETURN: return "ARMISD::TC_RETURN";
Jim Grosbach535d3b42010-09-08 03:54:02 +0000977
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +0000978 case ARMISD::THREAD_POINTER:return "ARMISD::THREAD_POINTER";
Bob Wilson2e076c42009-06-22 23:27:02 +0000979
Evan Chengb972e562009-08-07 00:34:42 +0000980 case ARMISD::DYN_ALLOC: return "ARMISD::DYN_ALLOC";
981
Bob Wilson7ed59712010-10-30 00:54:37 +0000982 case ARMISD::MEMBARRIER_MCR: return "ARMISD::MEMBARRIER_MCR";
Jim Grosbach53e88542009-12-10 00:11:09 +0000983
Evan Cheng8740ee32010-11-03 06:34:55 +0000984 case ARMISD::PRELOAD: return "ARMISD::PRELOAD";
985
Bob Wilson2e076c42009-06-22 23:27:02 +0000986 case ARMISD::VCEQ: return "ARMISD::VCEQ";
Bob Wilsonf268d032010-12-18 00:04:26 +0000987 case ARMISD::VCEQZ: return "ARMISD::VCEQZ";
Bob Wilson2e076c42009-06-22 23:27:02 +0000988 case ARMISD::VCGE: return "ARMISD::VCGE";
Bob Wilsonf268d032010-12-18 00:04:26 +0000989 case ARMISD::VCGEZ: return "ARMISD::VCGEZ";
990 case ARMISD::VCLEZ: return "ARMISD::VCLEZ";
Bob Wilson2e076c42009-06-22 23:27:02 +0000991 case ARMISD::VCGEU: return "ARMISD::VCGEU";
992 case ARMISD::VCGT: return "ARMISD::VCGT";
Bob Wilsonf268d032010-12-18 00:04:26 +0000993 case ARMISD::VCGTZ: return "ARMISD::VCGTZ";
994 case ARMISD::VCLTZ: return "ARMISD::VCLTZ";
Bob Wilson2e076c42009-06-22 23:27:02 +0000995 case ARMISD::VCGTU: return "ARMISD::VCGTU";
996 case ARMISD::VTST: return "ARMISD::VTST";
997
998 case ARMISD::VSHL: return "ARMISD::VSHL";
999 case ARMISD::VSHRs: return "ARMISD::VSHRs";
1000 case ARMISD::VSHRu: return "ARMISD::VSHRu";
Bob Wilson2e076c42009-06-22 23:27:02 +00001001 case ARMISD::VRSHRs: return "ARMISD::VRSHRs";
1002 case ARMISD::VRSHRu: return "ARMISD::VRSHRu";
1003 case ARMISD::VRSHRN: return "ARMISD::VRSHRN";
1004 case ARMISD::VQSHLs: return "ARMISD::VQSHLs";
1005 case ARMISD::VQSHLu: return "ARMISD::VQSHLu";
1006 case ARMISD::VQSHLsu: return "ARMISD::VQSHLsu";
1007 case ARMISD::VQSHRNs: return "ARMISD::VQSHRNs";
1008 case ARMISD::VQSHRNu: return "ARMISD::VQSHRNu";
1009 case ARMISD::VQSHRNsu: return "ARMISD::VQSHRNsu";
1010 case ARMISD::VQRSHRNs: return "ARMISD::VQRSHRNs";
1011 case ARMISD::VQRSHRNu: return "ARMISD::VQRSHRNu";
1012 case ARMISD::VQRSHRNsu: return "ARMISD::VQRSHRNsu";
1013 case ARMISD::VGETLANEu: return "ARMISD::VGETLANEu";
1014 case ARMISD::VGETLANEs: return "ARMISD::VGETLANEs";
Bob Wilsona3f19012010-07-13 21:16:48 +00001015 case ARMISD::VMOVIMM: return "ARMISD::VMOVIMM";
Bob Wilsonbad47f62010-07-14 06:31:50 +00001016 case ARMISD::VMVNIMM: return "ARMISD::VMVNIMM";
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00001017 case ARMISD::VMOVFPIMM: return "ARMISD::VMOVFPIMM";
Bob Wilsoneb54d512009-08-14 05:13:08 +00001018 case ARMISD::VDUP: return "ARMISD::VDUP";
Bob Wilsoncce31f62009-08-14 05:08:32 +00001019 case ARMISD::VDUPLANE: return "ARMISD::VDUPLANE";
Bob Wilson32cd8552009-08-19 17:03:43 +00001020 case ARMISD::VEXT: return "ARMISD::VEXT";
Bob Wilsonea3a4022009-08-12 22:31:50 +00001021 case ARMISD::VREV64: return "ARMISD::VREV64";
1022 case ARMISD::VREV32: return "ARMISD::VREV32";
1023 case ARMISD::VREV16: return "ARMISD::VREV16";
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00001024 case ARMISD::VZIP: return "ARMISD::VZIP";
1025 case ARMISD::VUZP: return "ARMISD::VUZP";
1026 case ARMISD::VTRN: return "ARMISD::VTRN";
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00001027 case ARMISD::VTBL1: return "ARMISD::VTBL1";
1028 case ARMISD::VTBL2: return "ARMISD::VTBL2";
Bob Wilson38ab35a2010-09-01 23:50:19 +00001029 case ARMISD::VMULLs: return "ARMISD::VMULLs";
1030 case ARMISD::VMULLu: return "ARMISD::VMULLu";
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00001031 case ARMISD::UMLAL: return "ARMISD::UMLAL";
1032 case ARMISD::SMLAL: return "ARMISD::SMLAL";
Bob Wilsond8a9a042010-06-04 00:04:02 +00001033 case ARMISD::BUILD_VECTOR: return "ARMISD::BUILD_VECTOR";
Bob Wilsonc6c13a32010-02-18 06:05:53 +00001034 case ARMISD::FMAX: return "ARMISD::FMAX";
1035 case ARMISD::FMIN: return "ARMISD::FMIN";
Joey Goulye3dd6842013-08-23 12:01:13 +00001036 case ARMISD::VMAXNM: return "ARMISD::VMAX";
1037 case ARMISD::VMINNM: return "ARMISD::VMIN";
Jim Grosbach6e3b5fa2010-07-17 01:50:57 +00001038 case ARMISD::BFI: return "ARMISD::BFI";
Bob Wilson62a6f7e2010-11-28 06:51:11 +00001039 case ARMISD::VORRIMM: return "ARMISD::VORRIMM";
1040 case ARMISD::VBICIMM: return "ARMISD::VBICIMM";
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00001041 case ARMISD::VBSL: return "ARMISD::VBSL";
Bob Wilson2d790df2010-11-28 06:51:26 +00001042 case ARMISD::VLD2DUP: return "ARMISD::VLD2DUP";
1043 case ARMISD::VLD3DUP: return "ARMISD::VLD3DUP";
1044 case ARMISD::VLD4DUP: return "ARMISD::VLD4DUP";
Bob Wilson06fce872011-02-07 17:43:21 +00001045 case ARMISD::VLD1_UPD: return "ARMISD::VLD1_UPD";
1046 case ARMISD::VLD2_UPD: return "ARMISD::VLD2_UPD";
1047 case ARMISD::VLD3_UPD: return "ARMISD::VLD3_UPD";
1048 case ARMISD::VLD4_UPD: return "ARMISD::VLD4_UPD";
1049 case ARMISD::VLD2LN_UPD: return "ARMISD::VLD2LN_UPD";
1050 case ARMISD::VLD3LN_UPD: return "ARMISD::VLD3LN_UPD";
1051 case ARMISD::VLD4LN_UPD: return "ARMISD::VLD4LN_UPD";
1052 case ARMISD::VLD2DUP_UPD: return "ARMISD::VLD2DUP_UPD";
1053 case ARMISD::VLD3DUP_UPD: return "ARMISD::VLD3DUP_UPD";
1054 case ARMISD::VLD4DUP_UPD: return "ARMISD::VLD4DUP_UPD";
1055 case ARMISD::VST1_UPD: return "ARMISD::VST1_UPD";
1056 case ARMISD::VST2_UPD: return "ARMISD::VST2_UPD";
1057 case ARMISD::VST3_UPD: return "ARMISD::VST3_UPD";
1058 case ARMISD::VST4_UPD: return "ARMISD::VST4_UPD";
1059 case ARMISD::VST2LN_UPD: return "ARMISD::VST2LN_UPD";
1060 case ARMISD::VST3LN_UPD: return "ARMISD::VST3LN_UPD";
1061 case ARMISD::VST4LN_UPD: return "ARMISD::VST4LN_UPD";
Evan Cheng10043e22007-01-19 07:51:42 +00001062 }
1063}
1064
Matt Arsenault758659232013-05-18 00:21:46 +00001065EVT ARMTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
Duncan Sandsf2641e12011-09-06 19:07:46 +00001066 if (!VT.isVector()) return getPointerTy();
1067 return VT.changeVectorElementTypeToInteger();
1068}
1069
Evan Cheng4cad68e2010-05-15 02:18:07 +00001070/// getRegClassFor - Return the register class that should be used for the
1071/// specified value type.
Patrik Hagglund5e6c3612012-12-13 06:34:11 +00001072const TargetRegisterClass *ARMTargetLowering::getRegClassFor(MVT VT) const {
Evan Cheng4cad68e2010-05-15 02:18:07 +00001073 // Map v4i64 to QQ registers but do not make the type legal. Similarly map
1074 // v8i64 to QQQQ registers. v4i64 and v8i64 are only used for REG_SEQUENCE to
1075 // load / store 4 to 8 consecutive D registers.
Evan Cheng3d214cd2010-05-15 02:20:21 +00001076 if (Subtarget->hasNEON()) {
1077 if (VT == MVT::v4i64)
Craig Topperc7242e02012-04-20 07:30:17 +00001078 return &ARM::QQPRRegClass;
1079 if (VT == MVT::v8i64)
1080 return &ARM::QQQQPRRegClass;
Evan Cheng3d214cd2010-05-15 02:20:21 +00001081 }
Evan Cheng4cad68e2010-05-15 02:18:07 +00001082 return TargetLowering::getRegClassFor(VT);
1083}
1084
Eric Christopher84bdfd82010-07-21 22:26:11 +00001085// Create a fast isel object.
1086FastISel *
Bob Wilson3e6fa462012-08-03 04:06:28 +00001087ARMTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
1088 const TargetLibraryInfo *libInfo) const {
1089 return ARM::createFastISel(funcInfo, libInfo);
Eric Christopher84bdfd82010-07-21 22:26:11 +00001090}
1091
Anton Korobeynikov19edda02010-07-24 21:52:08 +00001092/// getMaximalGlobalOffset - Returns the maximal possible offset which can
1093/// be used for loads / stores from the global.
1094unsigned ARMTargetLowering::getMaximalGlobalOffset() const {
1095 return (Subtarget->isThumb1Only() ? 127 : 4095);
1096}
1097
Evan Cheng4401f882010-05-20 23:26:43 +00001098Sched::Preference ARMTargetLowering::getSchedulingPreference(SDNode *N) const {
Evan Chengbf914992010-05-28 23:25:23 +00001099 unsigned NumVals = N->getNumValues();
1100 if (!NumVals)
1101 return Sched::RegPressure;
1102
1103 for (unsigned i = 0; i != NumVals; ++i) {
Evan Cheng4401f882010-05-20 23:26:43 +00001104 EVT VT = N->getValueType(i);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001105 if (VT == MVT::Glue || VT == MVT::Other)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001106 continue;
Evan Cheng4401f882010-05-20 23:26:43 +00001107 if (VT.isFloatingPoint() || VT.isVector())
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001108 return Sched::ILP;
Evan Cheng4401f882010-05-20 23:26:43 +00001109 }
Evan Chengbf914992010-05-28 23:25:23 +00001110
1111 if (!N->isMachineOpcode())
1112 return Sched::RegPressure;
1113
1114 // Load are scheduled for latency even if there instruction itinerary
1115 // is not available.
1116 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng6cc775f2011-06-28 19:10:37 +00001117 const MCInstrDesc &MCID = TII->get(N->getMachineOpcode());
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001118
Evan Cheng6cc775f2011-06-28 19:10:37 +00001119 if (MCID.getNumDefs() == 0)
Evan Cheng0c4c5ca2010-10-29 18:07:31 +00001120 return Sched::RegPressure;
1121 if (!Itins->isEmpty() &&
Evan Cheng6cc775f2011-06-28 19:10:37 +00001122 Itins->getOperandCycle(MCID.getSchedClass(), 0) > 2)
Dan Gohman4ed1afa2011-10-24 17:55:11 +00001123 return Sched::ILP;
Evan Chengbf914992010-05-28 23:25:23 +00001124
Evan Cheng4401f882010-05-20 23:26:43 +00001125 return Sched::RegPressure;
1126}
1127
Evan Cheng10043e22007-01-19 07:51:42 +00001128//===----------------------------------------------------------------------===//
1129// Lowering Code
1130//===----------------------------------------------------------------------===//
1131
Evan Cheng10043e22007-01-19 07:51:42 +00001132/// IntCCToARMCC - Convert a DAG integer condition code to an ARM CC
1133static ARMCC::CondCodes IntCCToARMCC(ISD::CondCode CC) {
1134 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001135 default: llvm_unreachable("Unknown condition code!");
Evan Cheng10043e22007-01-19 07:51:42 +00001136 case ISD::SETNE: return ARMCC::NE;
1137 case ISD::SETEQ: return ARMCC::EQ;
1138 case ISD::SETGT: return ARMCC::GT;
1139 case ISD::SETGE: return ARMCC::GE;
1140 case ISD::SETLT: return ARMCC::LT;
1141 case ISD::SETLE: return ARMCC::LE;
1142 case ISD::SETUGT: return ARMCC::HI;
1143 case ISD::SETUGE: return ARMCC::HS;
1144 case ISD::SETULT: return ARMCC::LO;
1145 case ISD::SETULE: return ARMCC::LS;
1146 }
1147}
1148
Bob Wilsona2e83332009-09-09 23:14:54 +00001149/// FPCCToARMCC - Convert a DAG fp condition code to an ARM CC.
1150static void FPCCToARMCC(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
Evan Cheng10043e22007-01-19 07:51:42 +00001151 ARMCC::CondCodes &CondCode2) {
Evan Cheng10043e22007-01-19 07:51:42 +00001152 CondCode2 = ARMCC::AL;
1153 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001154 default: llvm_unreachable("Unknown FP condition!");
Evan Cheng10043e22007-01-19 07:51:42 +00001155 case ISD::SETEQ:
1156 case ISD::SETOEQ: CondCode = ARMCC::EQ; break;
1157 case ISD::SETGT:
1158 case ISD::SETOGT: CondCode = ARMCC::GT; break;
1159 case ISD::SETGE:
1160 case ISD::SETOGE: CondCode = ARMCC::GE; break;
1161 case ISD::SETOLT: CondCode = ARMCC::MI; break;
Bob Wilsona2e83332009-09-09 23:14:54 +00001162 case ISD::SETOLE: CondCode = ARMCC::LS; break;
Evan Cheng10043e22007-01-19 07:51:42 +00001163 case ISD::SETONE: CondCode = ARMCC::MI; CondCode2 = ARMCC::GT; break;
1164 case ISD::SETO: CondCode = ARMCC::VC; break;
1165 case ISD::SETUO: CondCode = ARMCC::VS; break;
1166 case ISD::SETUEQ: CondCode = ARMCC::EQ; CondCode2 = ARMCC::VS; break;
1167 case ISD::SETUGT: CondCode = ARMCC::HI; break;
1168 case ISD::SETUGE: CondCode = ARMCC::PL; break;
1169 case ISD::SETLT:
1170 case ISD::SETULT: CondCode = ARMCC::LT; break;
1171 case ISD::SETLE:
1172 case ISD::SETULE: CondCode = ARMCC::LE; break;
1173 case ISD::SETNE:
1174 case ISD::SETUNE: CondCode = ARMCC::NE; break;
1175 }
Evan Cheng10043e22007-01-19 07:51:42 +00001176}
1177
Bob Wilsona4c22902009-04-17 19:07:39 +00001178//===----------------------------------------------------------------------===//
1179// Calling Convention Implementation
Bob Wilsona4c22902009-04-17 19:07:39 +00001180//===----------------------------------------------------------------------===//
1181
1182#include "ARMGenCallingConv.inc"
1183
Oliver Stannardc24f2172014-05-09 14:01:47 +00001184/// getEffectiveCallingConv - Get the effective calling convention, taking into
1185/// account presence of floating point hardware and calling convention
1186/// limitations, such as support for variadic functions.
1187CallingConv::ID
1188ARMTargetLowering::getEffectiveCallingConv(CallingConv::ID CC,
1189 bool isVarArg) const {
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001190 switch (CC) {
1191 default:
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001192 llvm_unreachable("Unsupported calling convention");
Oliver Stannardc24f2172014-05-09 14:01:47 +00001193 case CallingConv::ARM_AAPCS:
1194 case CallingConv::ARM_APCS:
1195 case CallingConv::GHC:
1196 return CC;
1197 case CallingConv::ARM_AAPCS_VFP:
1198 return isVarArg ? CallingConv::ARM_AAPCS : CallingConv::ARM_AAPCS_VFP;
1199 case CallingConv::C:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001200 if (!Subtarget->isAAPCS_ABI())
Oliver Stannardc24f2172014-05-09 14:01:47 +00001201 return CallingConv::ARM_APCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001202 else if (Subtarget->hasVFP2() &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001203 getTargetMachine().Options.FloatABIType == FloatABI::Hard &&
1204 !isVarArg)
Oliver Stannardc24f2172014-05-09 14:01:47 +00001205 return CallingConv::ARM_AAPCS_VFP;
1206 else
1207 return CallingConv::ARM_AAPCS;
1208 case CallingConv::Fast:
1209 if (!Subtarget->isAAPCS_ABI()) {
1210 if (Subtarget->hasVFP2() && !isVarArg)
1211 return CallingConv::Fast;
1212 return CallingConv::ARM_APCS;
1213 } else if (Subtarget->hasVFP2() && !isVarArg)
1214 return CallingConv::ARM_AAPCS_VFP;
1215 else
1216 return CallingConv::ARM_AAPCS;
Evan Cheng08dd8c82010-10-22 18:23:05 +00001217 }
Oliver Stannardc24f2172014-05-09 14:01:47 +00001218}
1219
1220/// CCAssignFnForNode - Selects the correct CCAssignFn for the given
1221/// CallingConvention.
1222CCAssignFn *ARMTargetLowering::CCAssignFnForNode(CallingConv::ID CC,
1223 bool Return,
1224 bool isVarArg) const {
1225 switch (getEffectiveCallingConv(CC, isVarArg)) {
1226 default:
1227 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001228 case CallingConv::ARM_APCS:
Evan Cheng08dd8c82010-10-22 18:23:05 +00001229 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS);
Oliver Stannardc24f2172014-05-09 14:01:47 +00001230 case CallingConv::ARM_AAPCS:
1231 return (Return ? RetCC_ARM_AAPCS : CC_ARM_AAPCS);
1232 case CallingConv::ARM_AAPCS_VFP:
1233 return (Return ? RetCC_ARM_AAPCS_VFP : CC_ARM_AAPCS_VFP);
1234 case CallingConv::Fast:
1235 return (Return ? RetFastCC_ARM_APCS : FastCC_ARM_APCS);
Eric Christopherb3322362012-08-03 00:05:53 +00001236 case CallingConv::GHC:
1237 return (Return ? RetCC_ARM_APCS : CC_ARM_APCS_GHC);
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001238 }
1239}
1240
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001241/// LowerCallResult - Lower the result values of a call into the
1242/// appropriate copies out of appropriate physical registers.
1243SDValue
1244ARMTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel68c5f472009-09-02 08:44:58 +00001245 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001246 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001247 SDLoc dl, SelectionDAG &DAG,
Stephen Linb8bd2322013-04-20 05:14:40 +00001248 SmallVectorImpl<SDValue> &InVals,
1249 bool isThisReturn, SDValue ThisVal) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001250
Bob Wilsona4c22902009-04-17 19:07:39 +00001251 // Assign locations to each value returned by this call.
1252 SmallVector<CCValAssign, 16> RVLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001253 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1254 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001255 CCInfo.AnalyzeCallResult(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001256 CCAssignFnForNode(CallConv, /* Return*/ true,
1257 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00001258
1259 // Copy all of the result registers out of their specified physreg.
1260 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1261 CCValAssign VA = RVLocs[i];
1262
Stephen Linb8bd2322013-04-20 05:14:40 +00001263 // Pass 'this' value directly from the argument to return value, to avoid
1264 // reg unit interference
1265 if (i == 0 && isThisReturn) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001266 assert(!VA.needsCustom() && VA.getLocVT() == MVT::i32 &&
1267 "unexpected return calling convention register assignment");
Stephen Linb8bd2322013-04-20 05:14:40 +00001268 InVals.push_back(ThisVal);
1269 continue;
1270 }
1271
Bob Wilson0041bd32009-04-25 00:33:20 +00001272 SDValue Val;
Bob Wilsona4c22902009-04-17 19:07:39 +00001273 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00001274 // Handle f64 or half of a v2f64.
Owen Anderson9f944592009-08-11 20:47:22 +00001275 SDValue Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsona4c22902009-04-17 19:07:39 +00001276 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001277 Chain = Lo.getValue(1);
1278 InFlag = Lo.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001279 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001280 SDValue Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32,
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001281 InFlag);
1282 Chain = Hi.getValue(1);
1283 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001284 if (!Subtarget->isLittle())
1285 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001286 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Bob Wilson2e076c42009-06-22 23:27:02 +00001287
Owen Anderson9f944592009-08-11 20:47:22 +00001288 if (VA.getLocVT() == MVT::v2f64) {
1289 SDValue Vec = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
1290 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1291 DAG.getConstant(0, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001292
1293 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001294 Lo = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001295 Chain = Lo.getValue(1);
1296 InFlag = Lo.getValue(2);
1297 VA = RVLocs[++i]; // skip ahead to next loc
Owen Anderson9f944592009-08-11 20:47:22 +00001298 Hi = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), MVT::i32, InFlag);
Bob Wilson2e076c42009-06-22 23:27:02 +00001299 Chain = Hi.getValue(1);
1300 InFlag = Hi.getValue(2);
Christian Pirkerb5728192014-05-08 14:06:24 +00001301 if (!Subtarget->isLittle())
1302 std::swap (Lo, Hi);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001303 Val = DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Owen Anderson9f944592009-08-11 20:47:22 +00001304 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Vec, Val,
1305 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00001306 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001307 } else {
Bob Wilson0041bd32009-04-25 00:33:20 +00001308 Val = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), VA.getLocVT(),
1309 InFlag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00001310 Chain = Val.getValue(1);
1311 InFlag = Val.getValue(2);
Bob Wilsona4c22902009-04-17 19:07:39 +00001312 }
Bob Wilson0041bd32009-04-25 00:33:20 +00001313
1314 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001315 default: llvm_unreachable("Unknown loc info!");
Bob Wilson0041bd32009-04-25 00:33:20 +00001316 case CCValAssign::Full: break;
1317 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001318 Val = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), Val);
Bob Wilson0041bd32009-04-25 00:33:20 +00001319 break;
1320 }
1321
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001322 InVals.push_back(Val);
Bob Wilsona4c22902009-04-17 19:07:39 +00001323 }
1324
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001325 return Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00001326}
1327
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001328/// LowerMemOpCallTo - Store the argument to the stack.
Bob Wilsona4c22902009-04-17 19:07:39 +00001329SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001330ARMTargetLowering::LowerMemOpCallTo(SDValue Chain,
1331 SDValue StackPtr, SDValue Arg,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001332 SDLoc dl, SelectionDAG &DAG,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001333 const CCValAssign &VA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001334 ISD::ArgFlagsTy Flags) const {
Bob Wilsona4c22902009-04-17 19:07:39 +00001335 unsigned LocMemOffset = VA.getLocMemOffset();
1336 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1337 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Bob Wilsona4c22902009-04-17 19:07:39 +00001338 return DAG.getStore(Chain, dl, Arg, PtrOff,
Chris Lattner886250c2010-09-21 18:51:21 +00001339 MachinePointerInfo::getStack(LocMemOffset),
David Greene0d0149f2010-02-15 16:55:24 +00001340 false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00001341}
1342
Andrew Trickef9de2a2013-05-25 02:42:55 +00001343void ARMTargetLowering::PassF64ArgInRegs(SDLoc dl, SelectionDAG &DAG,
Bob Wilson2e076c42009-06-22 23:27:02 +00001344 SDValue Chain, SDValue &Arg,
1345 RegsToPassVector &RegsToPass,
1346 CCValAssign &VA, CCValAssign &NextVA,
1347 SDValue &StackPtr,
Craig Topperb94011f2013-07-14 04:42:23 +00001348 SmallVectorImpl<SDValue> &MemOpChains,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001349 ISD::ArgFlagsTy Flags) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00001350
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00001351 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00001352 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00001353 unsigned id = Subtarget->isLittle() ? 0 : 1;
1354 RegsToPass.push_back(std::make_pair(VA.getLocReg(), fmrrd.getValue(id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001355
1356 if (NextVA.isRegLoc())
Christian Pirkerb5728192014-05-08 14:06:24 +00001357 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), fmrrd.getValue(1-id)));
Bob Wilson2e076c42009-06-22 23:27:02 +00001358 else {
1359 assert(NextVA.isMemLoc());
Craig Topper062a2ba2014-04-25 05:30:21 +00001360 if (!StackPtr.getNode())
Bob Wilson2e076c42009-06-22 23:27:02 +00001361 StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
1362
Christian Pirkerb5728192014-05-08 14:06:24 +00001363 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, fmrrd.getValue(1-id),
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001364 dl, DAG, NextVA,
1365 Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001366 }
1367}
1368
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001369/// LowerCall - Lowering a call into a callseq_start <-
Evan Cheng4b6c8f72007-02-03 08:53:01 +00001370/// ARMISD:CALL <- callseq_end chain. Also add input and output parameter
1371/// nodes.
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001372SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +00001373ARMTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001374 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +00001375 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001376 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001377 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1378 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1379 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +00001380 SDValue Chain = CLI.Chain;
1381 SDValue Callee = CLI.Callee;
1382 bool &isTailCall = CLI.IsTailCall;
1383 CallingConv::ID CallConv = CLI.CallConv;
1384 bool doesNotRet = CLI.DoesNotReturn;
1385 bool isVarArg = CLI.IsVarArg;
1386
Dale Johannesend679ff72010-06-03 21:09:53 +00001387 MachineFunction &MF = DAG.getMachineFunction();
Stephen Lin4eedb292013-04-23 19:30:12 +00001388 bool isStructRet = (Outs.empty()) ? false : Outs[0].Flags.isSRet();
1389 bool isThisReturn = false;
1390 bool isSibCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001391
Bob Wilson8decdc42011-10-07 17:17:49 +00001392 // Disable tail calls if they're not supported.
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001393 if (!Subtarget->supportsTailCall() || MF.getTarget().Options.DisableTailCalls)
Bob Wilson3c9ed762010-08-13 22:43:33 +00001394 isTailCall = false;
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00001395
Dale Johannesend679ff72010-06-03 21:09:53 +00001396 if (isTailCall) {
1397 // Check if it's really possible to do a tail call.
1398 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
Stephen Lin4eedb292013-04-23 19:30:12 +00001399 isVarArg, isStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001400 Outs, OutVals, Ins, DAG);
Reid Kleckner5772b772014-04-24 20:14:34 +00001401 if (!isTailCall && CLI.CS && CLI.CS->isMustTailCall())
1402 report_fatal_error("failed to perform tail call elimination on a call "
1403 "site marked musttail");
Dale Johannesend679ff72010-06-03 21:09:53 +00001404 // We don't support GuaranteedTailCallOpt for ARM, only automatically
1405 // detected sibcalls.
1406 if (isTailCall) {
1407 ++NumTailCalls;
Stephen Lin4eedb292013-04-23 19:30:12 +00001408 isSibCall = true;
Dale Johannesend679ff72010-06-03 21:09:53 +00001409 }
1410 }
Evan Cheng10043e22007-01-19 07:51:42 +00001411
Bob Wilsona4c22902009-04-17 19:07:39 +00001412 // Analyze operands of the call, assigning locations to each operand.
1413 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001414 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1415 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001416 CCInfo.AnalyzeCallOperands(Outs,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001417 CCAssignFnForNode(CallConv, /* Return*/ false,
1418 isVarArg));
Evan Cheng10043e22007-01-19 07:51:42 +00001419
Bob Wilsona4c22902009-04-17 19:07:39 +00001420 // Get a count of how many bytes are to be pushed on the stack.
1421 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Cheng10043e22007-01-19 07:51:42 +00001422
Dale Johannesend679ff72010-06-03 21:09:53 +00001423 // For tail calls, memory operands are available in our caller's stack.
Stephen Lin4eedb292013-04-23 19:30:12 +00001424 if (isSibCall)
Dale Johannesend679ff72010-06-03 21:09:53 +00001425 NumBytes = 0;
1426
Evan Cheng10043e22007-01-19 07:51:42 +00001427 // Adjust the stack pointer for the new arguments...
1428 // These operations are automatically eliminated by the prolog/epilog pass
Stephen Lin4eedb292013-04-23 19:30:12 +00001429 if (!isSibCall)
Andrew Trickad6d08a2013-05-29 22:03:55 +00001430 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true),
1431 dl);
Evan Cheng10043e22007-01-19 07:51:42 +00001432
Jim Grosbach6ad4bcb2010-02-24 01:43:03 +00001433 SDValue StackPtr = DAG.getCopyFromReg(Chain, dl, ARM::SP, getPointerTy());
Evan Cheng10043e22007-01-19 07:51:42 +00001434
Bob Wilson2e076c42009-06-22 23:27:02 +00001435 RegsToPassVector RegsToPass;
Bob Wilsona4c22902009-04-17 19:07:39 +00001436 SmallVector<SDValue, 8> MemOpChains;
Evan Cheng10043e22007-01-19 07:51:42 +00001437
Bob Wilsona4c22902009-04-17 19:07:39 +00001438 // Walk the register/memloc assignments, inserting copies/loads. In the case
Bob Wilsonea09d4a2009-04-17 20:35:10 +00001439 // of tail call optimization, arguments are handled later.
Bob Wilsona4c22902009-04-17 19:07:39 +00001440 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1441 i != e;
1442 ++i, ++realArgIdx) {
1443 CCValAssign &VA = ArgLocs[i];
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001444 SDValue Arg = OutVals[realArgIdx];
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001445 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001446 bool isByVal = Flags.isByVal();
Evan Cheng10043e22007-01-19 07:51:42 +00001447
Bob Wilsona4c22902009-04-17 19:07:39 +00001448 // Promote the value if needed.
1449 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001450 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00001451 case CCValAssign::Full: break;
1452 case CCValAssign::SExt:
1453 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1454 break;
1455 case CCValAssign::ZExt:
1456 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1457 break;
1458 case CCValAssign::AExt:
1459 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1460 break;
1461 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00001462 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00001463 break;
Evan Cheng10043e22007-01-19 07:51:42 +00001464 }
1465
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00001466 // f64 and v2f64 might be passed in i32 pairs and must be split into pieces
Bob Wilsona4c22902009-04-17 19:07:39 +00001467 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001468 if (VA.getLocVT() == MVT::v2f64) {
1469 SDValue Op0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1470 DAG.getConstant(0, MVT::i32));
1471 SDValue Op1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
1472 DAG.getConstant(1, MVT::i32));
Bob Wilsona4c22902009-04-17 19:07:39 +00001473
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001474 PassF64ArgInRegs(dl, DAG, Chain, Op0, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001475 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1476
1477 VA = ArgLocs[++i]; // skip ahead to next loc
1478 if (VA.isRegLoc()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001479 PassF64ArgInRegs(dl, DAG, Chain, Op1, RegsToPass,
Bob Wilson2e076c42009-06-22 23:27:02 +00001480 VA, ArgLocs[++i], StackPtr, MemOpChains, Flags);
1481 } else {
1482 assert(VA.isMemLoc());
Bob Wilson2e076c42009-06-22 23:27:02 +00001483
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001484 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Op1,
1485 dl, DAG, VA, Flags));
Bob Wilson2e076c42009-06-22 23:27:02 +00001486 }
1487 } else {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001488 PassF64ArgInRegs(dl, DAG, Chain, Arg, RegsToPass, VA, ArgLocs[++i],
Bob Wilson2e076c42009-06-22 23:27:02 +00001489 StackPtr, MemOpChains, Flags);
Bob Wilsona4c22902009-04-17 19:07:39 +00001490 }
1491 } else if (VA.isRegLoc()) {
Stephen Lin8118e0b2013-04-23 19:42:25 +00001492 if (realArgIdx == 0 && Flags.isReturned() && Outs[0].VT == MVT::i32) {
1493 assert(VA.getLocVT() == MVT::i32 &&
1494 "unexpected calling convention register assignment");
1495 assert(!Ins.empty() && Ins[0].VT == MVT::i32 &&
Stephen Linb8bd2322013-04-20 05:14:40 +00001496 "unexpected use of 'returned'");
Stephen Lin4eedb292013-04-23 19:30:12 +00001497 isThisReturn = true;
Stephen Linb8bd2322013-04-20 05:14:40 +00001498 }
Bob Wilsona4c22902009-04-17 19:07:39 +00001499 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001500 } else if (isByVal) {
1501 assert(VA.isMemLoc());
1502 unsigned offset = 0;
1503
1504 // True if this byval aggregate will be split between registers
1505 // and memory.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001506 unsigned ByValArgsCount = CCInfo.getInRegsParamsCount();
1507 unsigned CurByValIdx = CCInfo.getInRegsParamsProceed();
1508
1509 if (CurByValIdx < ByValArgsCount) {
1510
1511 unsigned RegBegin, RegEnd;
1512 CCInfo.getInRegsParamInfo(CurByValIdx, RegBegin, RegEnd);
1513
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001514 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
1515 unsigned int i, j;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001516 for (i = 0, j = RegBegin; j < RegEnd; i++, j++) {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001517 SDValue Const = DAG.getConstant(4*i, MVT::i32);
1518 SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
1519 SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg,
1520 MachinePointerInfo(),
Manman Ren5a787552013-10-07 19:47:53 +00001521 false, false, false,
1522 DAG.InferPtrAlignment(AddArg));
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001523 MemOpChains.push_back(Load.getValue(1));
1524 RegsToPass.push_back(std::make_pair(j, Load));
1525 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001526
1527 // If parameter size outsides register area, "offset" value
1528 // helps us to calculate stack slot for remained part properly.
1529 offset = RegEnd - RegBegin;
1530
1531 CCInfo.nextInRegsParam();
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001532 }
1533
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001534 if (Flags.getByValSize() > 4*offset) {
Manman Ren9f911162012-06-01 02:44:42 +00001535 unsigned LocMemOffset = VA.getLocMemOffset();
1536 SDValue StkPtrOff = DAG.getIntPtrConstant(LocMemOffset);
1537 SDValue Dst = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr,
1538 StkPtrOff);
1539 SDValue SrcOffset = DAG.getIntPtrConstant(4*offset);
1540 SDValue Src = DAG.getNode(ISD::ADD, dl, getPointerTy(), Arg, SrcOffset);
1541 SDValue SizeNode = DAG.getConstant(Flags.getByValSize() - 4*offset,
1542 MVT::i32);
Manman Rene8735522012-06-01 19:33:18 +00001543 SDValue AlignNode = DAG.getConstant(Flags.getByValAlign(), MVT::i32);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001544
Manman Ren9f911162012-06-01 02:44:42 +00001545 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
Manman Rene8735522012-06-01 19:33:18 +00001546 SDValue Ops[] = { Chain, Dst, Src, SizeNode, AlignNode};
Manman Ren9f911162012-06-01 02:44:42 +00001547 MemOpChains.push_back(DAG.getNode(ARMISD::COPY_STRUCT_BYVAL, dl, VTs,
Craig Topper48d114b2014-04-26 18:35:24 +00001548 Ops));
Manman Ren9f911162012-06-01 02:44:42 +00001549 }
Stephen Lin4eedb292013-04-23 19:30:12 +00001550 } else if (!isSibCall) {
Bob Wilsona4c22902009-04-17 19:07:39 +00001551 assert(VA.isMemLoc());
Bob Wilsona4c22902009-04-17 19:07:39 +00001552
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001553 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1554 dl, DAG, VA, Flags));
Bob Wilsona4c22902009-04-17 19:07:39 +00001555 }
Evan Cheng10043e22007-01-19 07:51:42 +00001556 }
1557
1558 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001559 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
Evan Cheng10043e22007-01-19 07:51:42 +00001560
1561 // Build a sequence of copy-to-reg nodes chained together with token chain
1562 // and flag operands which copy the outgoing args into the appropriate regs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001563 SDValue InFlag;
Dale Johannesen44f9dfc2010-06-15 22:08:33 +00001564 // Tail call byval lowering might overwrite argument registers so in case of
1565 // tail call optimization the copies to registers are lowered later.
1566 if (!isTailCall)
1567 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1568 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1569 RegsToPass[i].second, InFlag);
1570 InFlag = Chain.getValue(1);
1571 }
Evan Cheng10043e22007-01-19 07:51:42 +00001572
Dale Johannesend679ff72010-06-03 21:09:53 +00001573 // For tail calls lower the arguments to the 'real' stack slot.
1574 if (isTailCall) {
1575 // Force all the incoming stack arguments to be loaded from the stack
1576 // before any new outgoing arguments are stored to the stack, because the
1577 // outgoing stack slots may alias the incoming argument stack slots, and
1578 // the alias isn't otherwise explicit. This is slightly more conservative
1579 // than necessary, because it means that each store effectively depends
1580 // on every argument instead of just those arguments it would clobber.
1581
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001582 // Do not flag preceding copytoreg stuff together with the following stuff.
Dale Johannesend679ff72010-06-03 21:09:53 +00001583 InFlag = SDValue();
1584 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1585 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1586 RegsToPass[i].second, InFlag);
1587 InFlag = Chain.getValue(1);
1588 }
Stephen Lind36fd2c2013-04-20 00:47:48 +00001589 InFlag = SDValue();
Dale Johannesend679ff72010-06-03 21:09:53 +00001590 }
1591
Bill Wendling24c79f22008-09-16 21:48:12 +00001592 // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1593 // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1594 // node so that legalize doesn't hack it.
Evan Cheng10043e22007-01-19 07:51:42 +00001595 bool isDirect = false;
1596 bool isARMFunc = false;
Evan Chengc3c949b42007-06-19 21:05:09 +00001597 bool isLocalARMFunc = false;
Evan Cheng408aa562009-11-06 22:24:13 +00001598 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001599
1600 if (EnableARMLongCalls) {
1601 assert (getTargetMachine().getRelocationModel() == Reloc::Static
1602 && "long-calls with non-static relocation model!");
1603 // Handle a global address or an external symbol. If it's not one of
1604 // those, the target's already in a register, so we don't need to do
1605 // anything extra.
1606 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anders Carlsson47bccf72010-04-15 03:11:28 +00001607 const GlobalValue *GV = G->getGlobal();
Jim Grosbach32bb3622010-04-14 22:28:31 +00001608 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001609 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00001610 ARMConstantPoolValue *CPV =
1611 ARMConstantPoolConstant::Create(GV, ARMPCLabelIndex, ARMCP::CPValue, 0);
1612
Jim Grosbach32bb3622010-04-14 22:28:31 +00001613 // Get the address of the callee into a register
1614 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1615 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1616 Callee = DAG.getLoad(getPointerTy(), dl,
1617 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001618 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001619 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001620 } else if (ExternalSymbolSDNode *S=dyn_cast<ExternalSymbolSDNode>(Callee)) {
1621 const char *Sym = S->getSymbol();
1622
1623 // Create a constant pool entry for the callee address
Evan Chengdfce83c2011-01-17 08:03:18 +00001624 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001625 ARMConstantPoolValue *CPV =
1626 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1627 ARMPCLabelIndex, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001628 // Get the address of the callee into a register
1629 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
1630 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
1631 Callee = DAG.getLoad(getPointerTy(), dl,
1632 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001633 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001634 false, false, false, 0);
Jim Grosbach32bb3622010-04-14 22:28:31 +00001635 }
1636 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001637 const GlobalValue *GV = G->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00001638 isDirect = true;
Chris Lattner55452c22009-07-15 04:12:33 +00001639 bool isExt = GV->isDeclaration() || GV->isWeakForLinker();
Tim Northoverd6a729b2014-01-06 14:28:05 +00001640 bool isStub = (isExt && Subtarget->isTargetMachO()) &&
Evan Cheng10043e22007-01-19 07:51:42 +00001641 getTargetMachine().getRelocationModel() != Reloc::Static;
1642 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Chengc3c949b42007-06-19 21:05:09 +00001643 // ARM call to a local ARM function is predicable.
Evan Chengf128bdc2010-06-16 07:35:02 +00001644 isLocalARMFunc = !Subtarget->isThumb() && (!isExt || !ARMInterworking);
Evan Cheng83f35172007-01-30 20:37:08 +00001645 // tBX takes a register source operand.
Tim Northover72360d22013-12-02 10:35:41 +00001646 if (isStub && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Tim Northoverd6a729b2014-01-06 14:28:05 +00001647 assert(Subtarget->isTargetMachO() && "WrapperPIC use on non-MachO?");
Tim Northover72360d22013-12-02 10:35:41 +00001648 Callee = DAG.getNode(ARMISD::WrapperPIC, dl, getPointerTy(),
1649 DAG.getTargetGlobalAddress(GV, dl, getPointerTy()));
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001650 } else {
1651 // On ELF targets for PIC code, direct calls should go through the PLT
1652 unsigned OpFlags = 0;
1653 if (Subtarget->isTargetELF() &&
Chad Rosier537ff502013-02-28 19:16:42 +00001654 getTargetMachine().getRelocationModel() == Reloc::PIC_)
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001655 OpFlags = ARMII::MO_PLT;
1656 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
1657 }
Bill Wendling24c79f22008-09-16 21:48:12 +00001658 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng10043e22007-01-19 07:51:42 +00001659 isDirect = true;
Tim Northoverd6a729b2014-01-06 14:28:05 +00001660 bool isStub = Subtarget->isTargetMachO() &&
Evan Cheng10043e22007-01-19 07:51:42 +00001661 getTargetMachine().getRelocationModel() != Reloc::Static;
1662 isARMFunc = !Subtarget->isThumb() || isStub;
Evan Cheng83f35172007-01-30 20:37:08 +00001663 // tBX takes a register source operand.
1664 const char *Sym = S->getSymbol();
David Goodwin22c2fba2009-07-08 23:10:31 +00001665 if (isARMFunc && Subtarget->isThumb1Only() && !Subtarget->hasV5TOps()) {
Evan Chengdfce83c2011-01-17 08:03:18 +00001666 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendlingc214cb02011-10-01 08:58:29 +00001667 ARMConstantPoolValue *CPV =
1668 ARMConstantPoolSymbol::Create(*DAG.getContext(), Sym,
1669 ARMPCLabelIndex, 4);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00001670 SDValue CPAddr = DAG.getTargetConstantPool(CPV, getPointerTy(), 4);
Owen Anderson9f944592009-08-11 20:47:22 +00001671 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Dale Johannesen021052a2009-02-04 20:06:27 +00001672 Callee = DAG.getLoad(getPointerTy(), dl,
Evan Chengcdbb70c2009-10-31 03:39:36 +00001673 DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00001674 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00001675 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00001676 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson7117a912009-03-20 22:42:55 +00001677 Callee = DAG.getNode(ARMISD::PIC_ADD, dl,
Dale Johannesen021052a2009-02-04 20:06:27 +00001678 getPointerTy(), Callee, PICLabel);
Jim Grosbach85dcd3d2010-09-22 23:27:36 +00001679 } else {
1680 unsigned OpFlags = 0;
1681 // On ELF targets for PIC code, direct calls should go through the PLT
1682 if (Subtarget->isTargetELF() &&
1683 getTargetMachine().getRelocationModel() == Reloc::PIC_)
1684 OpFlags = ARMII::MO_PLT;
1685 Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlags);
1686 }
Evan Cheng10043e22007-01-19 07:51:42 +00001687 }
1688
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001689 // FIXME: handle tail calls differently.
1690 unsigned CallOpc;
Tim Northoverdee86042013-12-02 14:46:26 +00001691 bool HasMinSizeAttr = Subtarget->isMinSize();
Evan Cheng6ab54fd2009-08-01 00:16:10 +00001692 if (Subtarget->isThumb()) {
1693 if ((!isDirect || isARMFunc) && !Subtarget->hasV5TOps())
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001694 CallOpc = ARMISD::CALL_NOLINK;
1695 else
1696 CallOpc = isARMFunc ? ARMISD::CALL : ARMISD::tCALL;
1697 } else {
Evan Cheng21b03482012-11-10 02:09:05 +00001698 if (!isDirect && !Subtarget->hasV5TOps())
Evan Cheng65f9d192012-02-28 18:51:51 +00001699 CallOpc = ARMISD::CALL_NOLINK;
Evan Cheng21b03482012-11-10 02:09:05 +00001700 else if (doesNotRet && isDirect && Subtarget->hasRAS() &&
Quentin Colombet8e1fe842012-11-02 21:32:17 +00001701 // Emit regular call when code size is the priority
1702 !HasMinSizeAttr)
Evan Cheng65f9d192012-02-28 18:51:51 +00001703 // "mov lr, pc; b _foo" to avoid confusing the RSP
1704 CallOpc = ARMISD::CALL_NOLINK;
1705 else
1706 CallOpc = isLocalARMFunc ? ARMISD::CALL_PRED : ARMISD::CALL;
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001707 }
Lauro Ramos Venancioa88c4a72007-03-20 17:57:23 +00001708
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001709 std::vector<SDValue> Ops;
Evan Cheng10043e22007-01-19 07:51:42 +00001710 Ops.push_back(Chain);
1711 Ops.push_back(Callee);
1712
1713 // Add argument registers to the end of the list so that they are known live
1714 // into the call.
1715 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1716 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1717 RegsToPass[i].second.getValueType()));
1718
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001719 // Add a register mask operand representing the call-preserved registers.
Matthias Braunc22630e2013-10-04 16:52:54 +00001720 if (!isTailCall) {
1721 const uint32_t *Mask;
1722 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1723 const ARMBaseRegisterInfo *ARI = static_cast<const ARMBaseRegisterInfo*>(TRI);
1724 if (isThisReturn) {
1725 // For 'this' returns, use the R0-preserving mask if applicable
1726 Mask = ARI->getThisReturnPreservedMask(CallConv);
1727 if (!Mask) {
1728 // Set isThisReturn to false if the calling convention is not one that
1729 // allows 'returned' to be modeled in this way, so LowerCallResult does
1730 // not try to pass 'this' straight through
1731 isThisReturn = false;
1732 Mask = ARI->getCallPreservedMask(CallConv);
1733 }
1734 } else
Stephen Linff7fcee2013-06-26 21:42:14 +00001735 Mask = ARI->getCallPreservedMask(CallConv);
Stephen Linb8bd2322013-04-20 05:14:40 +00001736
Matthias Braunc22630e2013-10-04 16:52:54 +00001737 assert(Mask && "Missing call preserved mask for calling convention");
1738 Ops.push_back(DAG.getRegisterMask(Mask));
1739 }
Jakob Stoklund Olesenfa7a5372012-02-24 01:19:29 +00001740
Gabor Greiff304a7a2008-08-28 21:40:38 +00001741 if (InFlag.getNode())
Evan Cheng10043e22007-01-19 07:51:42 +00001742 Ops.push_back(InFlag);
Dale Johannesend679ff72010-06-03 21:09:53 +00001743
Chris Lattner3e5fbd72010-12-21 02:38:05 +00001744 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001745 if (isTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001746 return DAG.getNode(ARMISD::TC_RETURN, dl, NodeTys, Ops);
Dale Johannesend679ff72010-06-03 21:09:53 +00001747
Duncan Sands739a0542008-07-02 17:40:58 +00001748 // Returns a chain and a flag for retval copy to use.
Craig Topper48d114b2014-04-26 18:35:24 +00001749 Chain = DAG.getNode(CallOpc, dl, NodeTys, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00001750 InFlag = Chain.getValue(1);
1751
Chris Lattner27539552008-10-11 22:08:30 +00001752 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001753 DAG.getIntPtrConstant(0, true), InFlag, dl);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00001754 if (!Ins.empty())
Evan Cheng10043e22007-01-19 07:51:42 +00001755 InFlag = Chain.getValue(1);
1756
Bob Wilsona4c22902009-04-17 19:07:39 +00001757 // Handle result values, copying them out of physregs into vregs that we
1758 // return.
Stephen Linb8bd2322013-04-20 05:14:40 +00001759 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG,
Stephen Lin4eedb292013-04-23 19:30:12 +00001760 InVals, isThisReturn,
1761 isThisReturn ? OutVals[0] : SDValue());
Evan Cheng10043e22007-01-19 07:51:42 +00001762}
1763
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001764/// HandleByVal - Every parameter *after* a byval parameter is passed
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001765/// on the stack. Remember the next parameter register to allocate,
1766/// and then confiscate the rest of the parameter registers to insure
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001767/// this.
1768void
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001769ARMTargetLowering::HandleByVal(
1770 CCState *State, unsigned &size, unsigned Align) const {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001771 unsigned reg = State->AllocateReg(GPRArgRegs, 4);
1772 assert((State->getCallOrPrologue() == Prologue ||
1773 State->getCallOrPrologue() == Call) &&
1774 "unhandled ParmContext");
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001775
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001776 if ((ARM::R0 <= reg) && (reg <= ARM::R3)) {
Stepan Dyatkovskiye59a9202012-10-16 07:16:47 +00001777 if (Subtarget->isAAPCS_ABI() && Align > 4) {
1778 unsigned AlignInRegs = Align / 4;
1779 unsigned Waste = (ARM::R4 - reg) % AlignInRegs;
1780 for (unsigned i = 0; i < Waste; ++i)
1781 reg = State->AllocateReg(GPRArgRegs, 4);
1782 }
1783 if (reg != 0) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001784 unsigned excess = 4 * (ARM::R4 - reg);
1785
1786 // Special case when NSAA != SP and parameter size greater than size of
1787 // all remained GPR regs. In that case we can't split parameter, we must
1788 // send it to stack. We also must set NCRN to R4, so waste all
1789 // remained registers.
Oliver Stannardd55e1152014-03-05 15:25:27 +00001790 const unsigned NSAAOffset = State->getNextStackOffset();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001791 if (Subtarget->isAAPCS_ABI() && NSAAOffset != 0 && size > excess) {
1792 while (State->AllocateReg(GPRArgRegs, 4))
1793 ;
1794 return;
1795 }
1796
1797 // First register for byval parameter is the first register that wasn't
1798 // allocated before this method call, so it would be "reg".
1799 // If parameter is small enough to be saved in range [reg, r4), then
1800 // the end (first after last) register would be reg + param-size-in-regs,
1801 // else parameter would be splitted between registers and stack,
1802 // end register would be r4 in this case.
1803 unsigned ByValRegBegin = reg;
Stepan Dyatkovskiy2703bca2013-05-08 14:51:27 +00001804 unsigned ByValRegEnd = (size < excess) ? reg + size/4 : (unsigned)ARM::R4;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00001805 State->addInRegsParamInfo(ByValRegBegin, ByValRegEnd);
1806 // Note, first register is allocated in the beginning of function already,
1807 // allocate remained amount of registers we need.
1808 for (unsigned i = reg+1; i != ByValRegEnd; ++i)
1809 State->AllocateReg(GPRArgRegs, 4);
Oliver Stannardd55e1152014-03-05 15:25:27 +00001810 // A byval parameter that is split between registers and memory needs its
1811 // size truncated here.
1812 // In the case where the entire structure fits in registers, we set the
1813 // size in memory to zero.
1814 if (size < excess)
1815 size = 0;
1816 else
1817 size -= excess;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00001818 }
1819 }
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00001820}
1821
Dale Johannesend679ff72010-06-03 21:09:53 +00001822/// MatchingStackOffset - Return true if the given stack call argument is
1823/// already available in the same position (relatively) of the caller's
1824/// incoming argument stack.
1825static
1826bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
1827 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
Craig Topper07720d82012-03-25 23:49:58 +00001828 const TargetInstrInfo *TII) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001829 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
1830 int FI = INT_MAX;
1831 if (Arg.getOpcode() == ISD::CopyFromReg) {
1832 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesen2fb5b312011-01-10 02:58:51 +00001833 if (!TargetRegisterInfo::isVirtualRegister(VR))
Dale Johannesend679ff72010-06-03 21:09:53 +00001834 return false;
1835 MachineInstr *Def = MRI->getVRegDef(VR);
1836 if (!Def)
1837 return false;
1838 if (!Flags.isByVal()) {
1839 if (!TII->isLoadFromStackSlot(Def, FI))
1840 return false;
1841 } else {
Dale Johannesene2289282010-07-08 01:18:23 +00001842 return false;
Dale Johannesend679ff72010-06-03 21:09:53 +00001843 }
1844 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
1845 if (Flags.isByVal())
1846 // ByVal argument is passed in as a pointer but it's now being
1847 // dereferenced. e.g.
1848 // define @foo(%struct.X* %A) {
1849 // tail call @bar(%struct.X* byval %A)
1850 // }
1851 return false;
1852 SDValue Ptr = Ld->getBasePtr();
1853 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
1854 if (!FINode)
1855 return false;
1856 FI = FINode->getIndex();
1857 } else
1858 return false;
1859
1860 assert(FI != INT_MAX);
1861 if (!MFI->isFixedObjectIndex(FI))
1862 return false;
1863 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
1864}
1865
1866/// IsEligibleForTailCallOptimization - Check whether the call is eligible
1867/// for tail call optimization. Targets which want to do tail call
1868/// optimization should implement this function.
1869bool
1870ARMTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
1871 CallingConv::ID CalleeCC,
1872 bool isVarArg,
1873 bool isCalleeStructRet,
1874 bool isCallerStructRet,
1875 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001876 const SmallVectorImpl<SDValue> &OutVals,
Dale Johannesend679ff72010-06-03 21:09:53 +00001877 const SmallVectorImpl<ISD::InputArg> &Ins,
1878 SelectionDAG& DAG) const {
Dale Johannesend679ff72010-06-03 21:09:53 +00001879 const Function *CallerF = DAG.getMachineFunction().getFunction();
1880 CallingConv::ID CallerCC = CallerF->getCallingConv();
1881 bool CCMatch = CallerCC == CalleeCC;
1882
1883 // Look for obvious safe cases to perform tail call optimization that do not
1884 // require ABI changes. This is what gcc calls sibcall.
1885
Jim Grosbache3864cc2010-06-16 23:45:49 +00001886 // Do not sibcall optimize vararg calls unless the call site is not passing
1887 // any arguments.
Dale Johannesend679ff72010-06-03 21:09:53 +00001888 if (isVarArg && !Outs.empty())
1889 return false;
1890
Tim Northoverd8407452013-10-01 14:33:28 +00001891 // Exception-handling functions need a special set of instructions to indicate
1892 // a return to the hardware. Tail-calling another function would probably
1893 // break this.
1894 if (CallerF->hasFnAttribute("interrupt"))
1895 return false;
1896
Dale Johannesend679ff72010-06-03 21:09:53 +00001897 // Also avoid sibcall optimization if either caller or callee uses struct
1898 // return semantics.
1899 if (isCalleeStructRet || isCallerStructRet)
1900 return false;
1901
Dale Johannesend24c66b2010-06-23 18:52:34 +00001902 // FIXME: Completely disable sibcall for Thumb1 since Thumb1RegisterInfo::
Jim Grosbach3840c902011-07-08 20:18:11 +00001903 // emitEpilogue is not ready for them. Thumb tail calls also use t2B, as
1904 // the Thumb1 16-bit unconditional branch doesn't have sufficient relocation
1905 // support in the assembler and linker to be used. This would need to be
1906 // fixed to fully support tail calls in Thumb1.
1907 //
Dale Johannesene2289282010-07-08 01:18:23 +00001908 // Doing this is tricky, since the LDM/POP instruction on Thumb doesn't take
1909 // LR. This means if we need to reload LR, it takes an extra instructions,
1910 // which outweighs the value of the tail call; but here we don't know yet
1911 // whether LR is going to be used. Probably the right approach is to
Jim Grosbach535d3b42010-09-08 03:54:02 +00001912 // generate the tail call here and turn it back into CALL/RET in
Dale Johannesene2289282010-07-08 01:18:23 +00001913 // emitEpilogue if LR is used.
Dale Johannesene2289282010-07-08 01:18:23 +00001914
1915 // Thumb1 PIC calls to external symbols use BX, so they can be tail calls,
1916 // but we need to make sure there are enough registers; the only valid
1917 // registers are the 4 used for parameters. We don't currently do this
1918 // case.
Evan Chengd4b08732010-11-30 23:55:39 +00001919 if (Subtarget->isThumb1Only())
1920 return false;
Dale Johannesen3ac52b32010-06-18 18:13:11 +00001921
Dale Johannesend679ff72010-06-03 21:09:53 +00001922 // If the calling conventions do not match, then we'd better make sure the
1923 // results are returned in the same way as what the caller expects.
1924 if (!CCMatch) {
1925 SmallVector<CCValAssign, 16> RVLocs1;
Cameron Zwarich89019782011-06-10 20:59:24 +00001926 ARMCCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(),
1927 getTargetMachine(), RVLocs1, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001928 CCInfo1.AnalyzeCallResult(Ins, CCAssignFnForNode(CalleeCC, true, isVarArg));
1929
1930 SmallVector<CCValAssign, 16> RVLocs2;
Cameron Zwarich89019782011-06-10 20:59:24 +00001931 ARMCCState CCInfo2(CallerCC, false, DAG.getMachineFunction(),
1932 getTargetMachine(), RVLocs2, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001933 CCInfo2.AnalyzeCallResult(Ins, CCAssignFnForNode(CallerCC, true, isVarArg));
1934
1935 if (RVLocs1.size() != RVLocs2.size())
1936 return false;
1937 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
1938 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
1939 return false;
1940 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
1941 return false;
1942 if (RVLocs1[i].isRegLoc()) {
1943 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
1944 return false;
1945 } else {
1946 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
1947 return false;
1948 }
1949 }
1950 }
1951
Manman Ren7e48b252012-10-12 23:39:43 +00001952 // If Caller's vararg or byval argument has been split between registers and
1953 // stack, do not perform tail call, since part of the argument is in caller's
1954 // local frame.
1955 const ARMFunctionInfo *AFI_Caller = DAG.getMachineFunction().
1956 getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00001957 if (AFI_Caller->getArgRegsSaveSize())
Manman Ren7e48b252012-10-12 23:39:43 +00001958 return false;
1959
Dale Johannesend679ff72010-06-03 21:09:53 +00001960 // If the callee takes no arguments then go on to check the results of the
1961 // call.
1962 if (!Outs.empty()) {
1963 // Check if stack adjustment is needed. For now, do not do this if any
1964 // argument is passed on the stack.
1965 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00001966 ARMCCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(),
1967 getTargetMachine(), ArgLocs, *DAG.getContext(), Call);
Dale Johannesend679ff72010-06-03 21:09:53 +00001968 CCInfo.AnalyzeCallOperands(Outs,
1969 CCAssignFnForNode(CalleeCC, false, isVarArg));
1970 if (CCInfo.getNextStackOffset()) {
1971 MachineFunction &MF = DAG.getMachineFunction();
1972
1973 // Check if the arguments are already laid out in the right way as
1974 // the caller's fixed stack objects.
1975 MachineFrameInfo *MFI = MF.getFrameInfo();
1976 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Craig Topper07720d82012-03-25 23:49:58 +00001977 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001978 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size();
1979 i != e;
1980 ++i, ++realArgIdx) {
Dale Johannesend679ff72010-06-03 21:09:53 +00001981 CCValAssign &VA = ArgLocs[i];
1982 EVT RegVT = VA.getLocVT();
Dan Gohmanfe7532a2010-07-07 15:54:55 +00001983 SDValue Arg = OutVals[realArgIdx];
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001984 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
Dale Johannesend679ff72010-06-03 21:09:53 +00001985 if (VA.getLocInfo() == CCValAssign::Indirect)
1986 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001987 if (VA.needsCustom()) {
1988 // f64 and vector types are split into multiple registers or
1989 // register/stack-slot combinations. The types will not match
1990 // the registers; give up on memory f64 refs until we figure
1991 // out what to do about this.
1992 if (!VA.isRegLoc())
1993 return false;
1994 if (!ArgLocs[++i].isRegLoc())
Jim Grosbach535d3b42010-09-08 03:54:02 +00001995 return false;
Dale Johannesen81ef35b2010-06-05 00:51:39 +00001996 if (RegVT == MVT::v2f64) {
1997 if (!ArgLocs[++i].isRegLoc())
1998 return false;
1999 if (!ArgLocs[++i].isRegLoc())
2000 return false;
2001 }
2002 } else if (!VA.isRegLoc()) {
Dale Johannesend679ff72010-06-03 21:09:53 +00002003 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2004 MFI, MRI, TII))
2005 return false;
2006 }
2007 }
2008 }
2009 }
2010
2011 return true;
2012}
2013
Benjamin Kramerb1996da2012-11-28 20:55:10 +00002014bool
2015ARMTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2016 MachineFunction &MF, bool isVarArg,
2017 const SmallVectorImpl<ISD::OutputArg> &Outs,
2018 LLVMContext &Context) const {
2019 SmallVector<CCValAssign, 16> RVLocs;
2020 CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), RVLocs, Context);
2021 return CCInfo.CheckReturn(Outs, CCAssignFnForNode(CallConv, /*Return=*/true,
2022 isVarArg));
2023}
2024
Tim Northoverd8407452013-10-01 14:33:28 +00002025static SDValue LowerInterruptReturn(SmallVectorImpl<SDValue> &RetOps,
2026 SDLoc DL, SelectionDAG &DAG) {
2027 const MachineFunction &MF = DAG.getMachineFunction();
2028 const Function *F = MF.getFunction();
2029
2030 StringRef IntKind = F->getFnAttribute("interrupt").getValueAsString();
2031
2032 // See ARM ARM v7 B1.8.3. On exception entry LR is set to a possibly offset
2033 // version of the "preferred return address". These offsets affect the return
2034 // instruction if this is a return from PL1 without hypervisor extensions.
2035 // IRQ/FIQ: +4 "subs pc, lr, #4"
2036 // SWI: 0 "subs pc, lr, #0"
2037 // ABORT: +4 "subs pc, lr, #4"
2038 // UNDEF: +4/+2 "subs pc, lr, #0"
2039 // UNDEF varies depending on where the exception came from ARM or Thumb
2040 // mode. Alongside GCC, we throw our hands up in disgust and pretend it's 0.
2041
2042 int64_t LROffset;
2043 if (IntKind == "" || IntKind == "IRQ" || IntKind == "FIQ" ||
2044 IntKind == "ABORT")
2045 LROffset = 4;
2046 else if (IntKind == "SWI" || IntKind == "UNDEF")
2047 LROffset = 0;
2048 else
2049 report_fatal_error("Unsupported interrupt attribute. If present, value "
2050 "must be one of: IRQ, FIQ, SWI, ABORT or UNDEF");
2051
2052 RetOps.insert(RetOps.begin() + 1, DAG.getConstant(LROffset, MVT::i32, false));
2053
Craig Topper48d114b2014-04-26 18:35:24 +00002054 return DAG.getNode(ARMISD::INTRET_FLAG, DL, MVT::Other, RetOps);
Tim Northoverd8407452013-10-01 14:33:28 +00002055}
2056
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002057SDValue
2058ARMTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002059 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002060 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002061 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002062 SDLoc dl, SelectionDAG &DAG) const {
Bob Wilson7117a912009-03-20 22:42:55 +00002063
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002064 // CCValAssign - represent the assignment of the return value to a location.
Bob Wilsona4c22902009-04-17 19:07:39 +00002065 SmallVector<CCValAssign, 16> RVLocs;
Bob Wilsona4c22902009-04-17 19:07:39 +00002066
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002067 // CCState - Info about the registers and stack slots.
Cameron Zwarich89019782011-06-10 20:59:24 +00002068 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2069 getTargetMachine(), RVLocs, *DAG.getContext(), Call);
Bob Wilsona4c22902009-04-17 19:07:39 +00002070
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002071 // Analyze outgoing return values.
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002072 CCInfo.AnalyzeReturn(Outs, CCAssignFnForNode(CallConv, /* Return */ true,
2073 isVarArg));
Bob Wilsona4c22902009-04-17 19:07:39 +00002074
Bob Wilsona4c22902009-04-17 19:07:39 +00002075 SDValue Flag;
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002076 SmallVector<SDValue, 4> RetOps;
2077 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
Christian Pirkerb5728192014-05-08 14:06:24 +00002078 bool isLittleEndian = Subtarget->isLittle();
Bob Wilsona4c22902009-04-17 19:07:39 +00002079
2080 // Copy the result values into the output registers.
2081 for (unsigned i = 0, realRVLocIdx = 0;
2082 i != RVLocs.size();
2083 ++i, ++realRVLocIdx) {
2084 CCValAssign &VA = RVLocs[i];
2085 assert(VA.isRegLoc() && "Can only return in registers!");
2086
Dan Gohmanfe7532a2010-07-07 15:54:55 +00002087 SDValue Arg = OutVals[realRVLocIdx];
Bob Wilsona4c22902009-04-17 19:07:39 +00002088
2089 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002090 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00002091 case CCValAssign::Full: break;
2092 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00002093 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Bob Wilsona4c22902009-04-17 19:07:39 +00002094 break;
2095 }
2096
Bob Wilsona4c22902009-04-17 19:07:39 +00002097 if (VA.needsCustom()) {
Owen Anderson9f944592009-08-11 20:47:22 +00002098 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002099 // Extract the first half and return it in two registers.
Owen Anderson9f944592009-08-11 20:47:22 +00002100 SDValue Half = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2101 DAG.getConstant(0, MVT::i32));
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002102 SDValue HalfGPRs = DAG.getNode(ARMISD::VMOVRRD, dl,
Owen Anderson9f944592009-08-11 20:47:22 +00002103 DAG.getVTList(MVT::i32, MVT::i32), Half);
Bob Wilson2e076c42009-06-22 23:27:02 +00002104
Christian Pirkerb5728192014-05-08 14:06:24 +00002105 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2106 HalfGPRs.getValue(isLittleEndian ? 0 : 1),
2107 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002108 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002109 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002110 VA = RVLocs[++i]; // skip ahead to next loc
2111 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Christian Pirkerb5728192014-05-08 14:06:24 +00002112 HalfGPRs.getValue(isLittleEndian ? 1 : 0),
2113 Flag);
Bob Wilson2e076c42009-06-22 23:27:02 +00002114 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002115 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilson2e076c42009-06-22 23:27:02 +00002116 VA = RVLocs[++i]; // skip ahead to next loc
2117
2118 // Extract the 2nd half and fall through to handle it as an f64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00002119 Arg = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Arg,
2120 DAG.getConstant(1, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00002121 }
2122 // Legalize ret f64 -> ret 2 x i32. We always have fmrrd if f64 is
2123 // available.
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002124 SDValue fmrrd = DAG.getNode(ARMISD::VMOVRRD, dl,
Craig Topper48d114b2014-04-26 18:35:24 +00002125 DAG.getVTList(MVT::i32, MVT::i32), Arg);
Christian Pirkerb5728192014-05-08 14:06:24 +00002126 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2127 fmrrd.getValue(isLittleEndian ? 0 : 1),
2128 Flag);
Bob Wilsonf134b2d2009-04-24 17:00:36 +00002129 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002130 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002131 VA = RVLocs[++i]; // skip ahead to next loc
Christian Pirkerb5728192014-05-08 14:06:24 +00002132 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2133 fmrrd.getValue(isLittleEndian ? 1 : 0),
Bob Wilsona4c22902009-04-17 19:07:39 +00002134 Flag);
2135 } else
2136 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), Arg, Flag);
2137
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002138 // Guarantee that all emitted copies are
2139 // stuck together, avoiding something bad.
Bob Wilsona4c22902009-04-17 19:07:39 +00002140 Flag = Chain.getValue(1);
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002141 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Bob Wilsona4c22902009-04-17 19:07:39 +00002142 }
2143
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002144 // Update chain and glue.
2145 RetOps[0] = Chain;
Bob Wilsona4c22902009-04-17 19:07:39 +00002146 if (Flag.getNode())
Jakob Stoklund Olesenf90fb6e2013-02-05 18:08:40 +00002147 RetOps.push_back(Flag);
Bob Wilsona4c22902009-04-17 19:07:39 +00002148
Tim Northoverd8407452013-10-01 14:33:28 +00002149 // CPUs which aren't M-class use a special sequence to return from
2150 // exceptions (roughly, any instruction setting pc and cpsr simultaneously,
2151 // though we use "subs pc, lr, #N").
2152 //
2153 // M-class CPUs actually use a normal return sequence with a special
2154 // (hardware-provided) value in LR, so the normal code path works.
2155 if (DAG.getMachineFunction().getFunction()->hasFnAttribute("interrupt") &&
2156 !Subtarget->isMClass()) {
2157 if (Subtarget->isThumb1Only())
2158 report_fatal_error("interrupt attribute is not supported in Thumb1");
2159 return LowerInterruptReturn(RetOps, dl, DAG);
2160 }
2161
Craig Topper48d114b2014-04-26 18:35:24 +00002162 return DAG.getNode(ARMISD::RET_FLAG, dl, MVT::Other, RetOps);
Evan Cheng10043e22007-01-19 07:51:42 +00002163}
2164
Evan Chengf8bad082012-04-10 01:51:00 +00002165bool ARMTargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
Evan Chengd4b08732010-11-30 23:55:39 +00002166 if (N->getNumValues() != 1)
2167 return false;
2168 if (!N->hasNUsesOfValue(1, 0))
2169 return false;
2170
Evan Chengf8bad082012-04-10 01:51:00 +00002171 SDValue TCChain = Chain;
2172 SDNode *Copy = *N->use_begin();
2173 if (Copy->getOpcode() == ISD::CopyToReg) {
2174 // If the copy has a glue operand, we conservatively assume it isn't safe to
2175 // perform a tail call.
2176 if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2177 return false;
2178 TCChain = Copy->getOperand(0);
2179 } else if (Copy->getOpcode() == ARMISD::VMOVRRD) {
2180 SDNode *VMov = Copy;
Evan Chengd4b08732010-11-30 23:55:39 +00002181 // f64 returned in a pair of GPRs.
Evan Chengf8bad082012-04-10 01:51:00 +00002182 SmallPtrSet<SDNode*, 2> Copies;
2183 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
Evan Chengd4b08732010-11-30 23:55:39 +00002184 UI != UE; ++UI) {
2185 if (UI->getOpcode() != ISD::CopyToReg)
2186 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002187 Copies.insert(*UI);
Evan Chengd4b08732010-11-30 23:55:39 +00002188 }
Evan Chengf8bad082012-04-10 01:51:00 +00002189 if (Copies.size() > 2)
2190 return false;
2191
2192 for (SDNode::use_iterator UI = VMov->use_begin(), UE = VMov->use_end();
2193 UI != UE; ++UI) {
2194 SDValue UseChain = UI->getOperand(0);
2195 if (Copies.count(UseChain.getNode()))
2196 // Second CopyToReg
2197 Copy = *UI;
2198 else
2199 // First CopyToReg
2200 TCChain = UseChain;
2201 }
2202 } else if (Copy->getOpcode() == ISD::BITCAST) {
Evan Chengd4b08732010-11-30 23:55:39 +00002203 // f32 returned in a single GPR.
Evan Chengf8bad082012-04-10 01:51:00 +00002204 if (!Copy->hasOneUse())
Evan Chengd4b08732010-11-30 23:55:39 +00002205 return false;
Evan Chengf8bad082012-04-10 01:51:00 +00002206 Copy = *Copy->use_begin();
2207 if (Copy->getOpcode() != ISD::CopyToReg || !Copy->hasNUsesOfValue(1, 0))
Evan Chengd4b08732010-11-30 23:55:39 +00002208 return false;
Lang Hames67c09b32013-05-13 10:21:19 +00002209 TCChain = Copy->getOperand(0);
Evan Chengd4b08732010-11-30 23:55:39 +00002210 } else {
2211 return false;
2212 }
2213
Evan Cheng419ea282010-12-01 22:59:46 +00002214 bool HasRet = false;
Evan Chengf8bad082012-04-10 01:51:00 +00002215 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2216 UI != UE; ++UI) {
Tim Northoverd8407452013-10-01 14:33:28 +00002217 if (UI->getOpcode() != ARMISD::RET_FLAG &&
2218 UI->getOpcode() != ARMISD::INTRET_FLAG)
Evan Chengf8bad082012-04-10 01:51:00 +00002219 return false;
2220 HasRet = true;
Evan Chengd4b08732010-11-30 23:55:39 +00002221 }
2222
Evan Chengf8bad082012-04-10 01:51:00 +00002223 if (!HasRet)
2224 return false;
2225
2226 Chain = TCChain;
2227 return true;
Evan Chengd4b08732010-11-30 23:55:39 +00002228}
2229
Evan Cheng0663f232011-03-21 01:19:09 +00002230bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Saleem Abdulrasoolb720a6b2014-03-11 15:09:49 +00002231 if (!Subtarget->supportsTailCall())
Evan Cheng0663f232011-03-21 01:19:09 +00002232 return false;
2233
Saleem Abdulrasool0d96f3d2014-03-11 15:09:54 +00002234 if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls)
Evan Cheng0663f232011-03-21 01:19:09 +00002235 return false;
2236
2237 return !Subtarget->isThumb1Only();
2238}
2239
Bob Wilsonb389f2a2009-11-03 00:02:05 +00002240// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2241// their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
2242// one of the above mentioned nodes. It has to be wrapped because otherwise
2243// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2244// be used to form addressing mode. These wrapped nodes will be selected
2245// into MOVi.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002246static SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002247 EVT PtrVT = Op.getValueType();
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002248 // FIXME there is no actual debug info here
Andrew Trickef9de2a2013-05-25 02:42:55 +00002249 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00002250 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002251 SDValue Res;
Evan Cheng10043e22007-01-19 07:51:42 +00002252 if (CP->isMachineConstantPoolEntry())
2253 Res = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2254 CP->getAlignment());
2255 else
2256 Res = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2257 CP->getAlignment());
Owen Anderson9f944592009-08-11 20:47:22 +00002258 return DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Res);
Evan Cheng10043e22007-01-19 07:51:42 +00002259}
2260
Jim Grosbach8d3ba732010-07-19 17:20:38 +00002261unsigned ARMTargetLowering::getJumpTableEncoding() const {
2262 return MachineJumpTableInfo::EK_Inline;
2263}
2264
Dan Gohman21cea8a2010-04-17 15:26:15 +00002265SDValue ARMTargetLowering::LowerBlockAddress(SDValue Op,
2266 SelectionDAG &DAG) const {
Evan Cheng408aa562009-11-06 22:24:13 +00002267 MachineFunction &MF = DAG.getMachineFunction();
2268 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2269 unsigned ARMPCLabelIndex = 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002270 SDLoc DL(Op);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002271 EVT PtrVT = getPointerTy();
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002272 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002273 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2274 SDValue CPAddr;
2275 if (RelocM == Reloc::Static) {
2276 CPAddr = DAG.getTargetConstantPool(BA, PtrVT, 4);
2277 } else {
2278 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Chengdfce83c2011-01-17 08:03:18 +00002279 ARMPCLabelIndex = AFI->createPICLabelUId();
Bill Wendling7753d662011-10-01 08:00:54 +00002280 ARMConstantPoolValue *CPV =
2281 ARMConstantPoolConstant::Create(BA, ARMPCLabelIndex,
2282 ARMCP::CPBlockAddress, PCAdj);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002283 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
2284 }
2285 CPAddr = DAG.getNode(ARMISD::Wrapper, DL, PtrVT, CPAddr);
2286 SDValue Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002287 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002288 false, false, false, 0);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002289 if (RelocM == Reloc::Static)
2290 return Result;
Evan Cheng408aa562009-11-06 22:24:13 +00002291 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Bob Wilson1c66e8a2009-11-02 20:59:23 +00002292 return DAG.getNode(ARMISD::PIC_ADD, DL, PtrVT, Result, PICLabel);
Bob Wilson1cf0b032009-10-30 05:45:42 +00002293}
2294
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002295// Lower ISD::GlobalTLSAddress using the "general dynamic" model
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002296SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002297ARMTargetLowering::LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002298 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002299 SDLoc dl(GA);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002300 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002301 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
Evan Cheng408aa562009-11-06 22:24:13 +00002302 MachineFunction &MF = DAG.getMachineFunction();
2303 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002304 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002305 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002306 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2307 ARMCP::CPValue, PCAdj, ARMCP::TLSGD, true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002308 SDValue Argument = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002309 Argument = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Argument);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002310 Argument = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Argument,
Chris Lattner7727d052010-09-21 06:44:06 +00002311 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002312 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002313 SDValue Chain = Argument.getValue(1);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002314
Evan Cheng408aa562009-11-06 22:24:13 +00002315 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002316 Argument = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Argument, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002317
2318 // call __tls_get_addr.
2319 ArgListTy Args;
2320 ArgListEntry Entry;
2321 Entry.Node = Argument;
Chris Lattner229907c2011-07-18 04:54:35 +00002322 Entry.Ty = (Type *) Type::getInt32Ty(*DAG.getContext());
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002323 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002324
Dale Johannesen555a3752009-01-30 23:10:59 +00002325 // FIXME: is there useful debug info available here?
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00002326 TargetLowering::CallLoweringInfo CLI(DAG);
2327 CLI.setDebugLoc(dl).setChain(Chain)
2328 .setCallee(CallingConv::C, Type::getInt32Ty(*DAG.getContext()),
2329 DAG.getExternalSymbol("__tls_get_addr", PtrVT), &Args, 0);
2330
Justin Holewinskiaa583972012-05-25 16:35:28 +00002331 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002332 return CallResult.first;
2333}
2334
2335// Lower ISD::GlobalTLSAddress using the "initial exec" or
2336// "local exec" model.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002337SDValue
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002338ARMTargetLowering::LowerToTLSExecModels(GlobalAddressSDNode *GA,
Hans Wennborgaea41202012-05-04 09:40:39 +00002339 SelectionDAG &DAG,
2340 TLSModel::Model model) const {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002341 const GlobalValue *GV = GA->getGlobal();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002342 SDLoc dl(GA);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002343 SDValue Offset;
2344 SDValue Chain = DAG.getEntryNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002345 EVT PtrVT = getPointerTy();
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002346 // Get the Thread Pointer
Dale Johannesen021052a2009-02-04 20:06:27 +00002347 SDValue ThreadPointer = DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002348
Hans Wennborgaea41202012-05-04 09:40:39 +00002349 if (model == TLSModel::InitialExec) {
Evan Cheng408aa562009-11-06 22:24:13 +00002350 MachineFunction &MF = DAG.getMachineFunction();
2351 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002352 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Evan Cheng408aa562009-11-06 22:24:13 +00002353 // Initial exec model.
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002354 unsigned char PCAdj = Subtarget->isThumb() ? 4 : 8;
2355 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002356 ARMConstantPoolConstant::Create(GA->getGlobal(), ARMPCLabelIndex,
2357 ARMCP::CPValue, PCAdj, ARMCP::GOTTPOFF,
2358 true);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002359 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002360 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002361 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002362 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002363 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002364 Chain = Offset.getValue(1);
2365
Evan Cheng408aa562009-11-06 22:24:13 +00002366 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002367 Offset = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Offset, PICLabel);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002368
Evan Chengcdbb70c2009-10-31 03:39:36 +00002369 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002370 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002371 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002372 } else {
2373 // local exec model
Hans Wennborgaea41202012-05-04 09:40:39 +00002374 assert(model == TLSModel::LocalExec);
Bill Wendling7753d662011-10-01 08:00:54 +00002375 ARMConstantPoolValue *CPV =
2376 ARMConstantPoolConstant::Create(GV, ARMCP::TPOFF);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002377 Offset = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002378 Offset = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, Offset);
Evan Chengcdbb70c2009-10-31 03:39:36 +00002379 Offset = DAG.getLoad(PtrVT, dl, Chain, Offset,
Chris Lattner7727d052010-09-21 06:44:06 +00002380 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002381 false, false, false, 0);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002382 }
2383
2384 // The address of the thread local variable is the add of the thread
2385 // pointer with the offset of the variable.
Dale Johannesen021052a2009-02-04 20:06:27 +00002386 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002387}
2388
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002389SDValue
Dan Gohman21cea8a2010-04-17 15:26:15 +00002390ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002391 // TODO: implement the "local dynamic" model
2392 assert(Subtarget->isTargetELF() &&
2393 "TLS not implemented for non-ELF targets");
2394 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Hans Wennborgaea41202012-05-04 09:40:39 +00002395
2396 TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());
2397
2398 switch (model) {
2399 case TLSModel::GeneralDynamic:
2400 case TLSModel::LocalDynamic:
2401 return LowerToTLSGeneralDynamicModel(GA, DAG);
2402 case TLSModel::InitialExec:
2403 case TLSModel::LocalExec:
2404 return LowerToTLSExecModels(GA, DAG, model);
2405 }
Matt Beaumont-Gaye82ab6b2012-05-04 18:34:27 +00002406 llvm_unreachable("bogus TLS model");
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +00002407}
2408
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002409SDValue ARMTargetLowering::LowerGlobalAddressELF(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002410 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002411 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002412 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002413 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Chad Rosier537ff502013-02-28 19:16:42 +00002414 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Rafael Espindola6de96a12009-01-15 20:18:42 +00002415 bool UseGOTOFF = GV->hasLocalLinkage() || GV->hasHiddenVisibility();
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002416 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002417 ARMConstantPoolConstant::Create(GV,
2418 UseGOTOFF ? ARMCP::GOTOFF : ARMCP::GOT);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002419 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002420 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Bob Wilson7117a912009-03-20 22:42:55 +00002421 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002422 CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002423 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002424 false, false, false, 0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002425 SDValue Chain = Result.getValue(1);
Dale Johannesen62fd95d2009-02-07 00:55:49 +00002426 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
Dale Johannesen021052a2009-02-04 20:06:27 +00002427 Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, GOT);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002428 if (!UseGOTOFF)
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002429 Result = DAG.getLoad(PtrVT, dl, Chain, Result,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002430 MachinePointerInfo::getGOT(),
2431 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002432 return Result;
Evan Chengdfce83c2011-01-17 08:03:18 +00002433 }
2434
2435 // If we have T2 ops, we can materialize the address directly via movt/movw
James Molloydd9137a2011-10-26 08:53:19 +00002436 // pair. This is always cheaper.
2437 if (Subtarget->useMovt()) {
Evan Cheng68aec142011-01-19 02:16:49 +00002438 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002439 // FIXME: Once remat is capable of dealing with instructions with register
2440 // operands, expand this into two nodes.
2441 return DAG.getNode(ARMISD::Wrapper, dl, PtrVT,
2442 DAG.getTargetGlobalAddress(GV, dl, PtrVT));
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002443 } else {
Evan Chengdfce83c2011-01-17 08:03:18 +00002444 SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, 4);
2445 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
2446 return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
2447 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002448 false, false, false, 0);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002449 }
2450}
2451
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002452SDValue ARMTargetLowering::LowerGlobalAddressDarwin(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002453 SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002454 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002455 SDLoc dl(Op);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002456 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng10043e22007-01-19 07:51:42 +00002457 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Evan Chengdfce83c2011-01-17 08:03:18 +00002458
Tim Northover72360d22013-12-02 10:35:41 +00002459 if (Subtarget->useMovt())
Evan Cheng68aec142011-01-19 02:16:49 +00002460 ++NumMovwMovt;
Evan Chengdfce83c2011-01-17 08:03:18 +00002461
Tim Northover72360d22013-12-02 10:35:41 +00002462 // FIXME: Once remat is capable of dealing with instructions with register
2463 // operands, expand this into multiple nodes
2464 unsigned Wrapper =
2465 RelocM == Reloc::PIC_ ? ARMISD::WrapperPIC : ARMISD::Wrapper;
Tim Northoverdb962e2c2013-11-25 16:24:52 +00002466
Tim Northover72360d22013-12-02 10:35:41 +00002467 SDValue G = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, ARMII::MO_NONLAZY);
2468 SDValue Result = DAG.getNode(Wrapper, dl, PtrVT, G);
Evan Cheng43b9ca62009-08-28 23:18:09 +00002469
Evan Cheng1b389522009-09-03 07:04:02 +00002470 if (Subtarget->GVIsIndirectSymbol(GV, RelocM))
Tim Northover72360d22013-12-02 10:35:41 +00002471 Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
2472 MachinePointerInfo::getGOT(), false, false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002473 return Result;
2474}
2475
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00002476SDValue ARMTargetLowering::LowerGlobalAddressWindows(SDValue Op,
2477 SelectionDAG &DAG) const {
2478 assert(Subtarget->isTargetWindows() && "non-Windows COFF is not supported");
2479 assert(Subtarget->useMovt() && "Windows on ARM expects to use movw/movt");
2480
2481 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2482 EVT PtrVT = getPointerTy();
2483 SDLoc DL(Op);
2484
2485 ++NumMovwMovt;
2486
2487 // FIXME: Once remat is capable of dealing with instructions with register
2488 // operands, expand this into two nodes.
2489 return DAG.getNode(ARMISD::Wrapper, DL, PtrVT,
2490 DAG.getTargetGlobalAddress(GV, DL, PtrVT));
2491}
2492
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002493SDValue ARMTargetLowering::LowerGLOBAL_OFFSET_TABLE(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002494 SelectionDAG &DAG) const {
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002495 assert(Subtarget->isTargetELF() &&
2496 "GLOBAL OFFSET TABLE not implemented for non-ELF targets");
Evan Cheng408aa562009-11-06 22:24:13 +00002497 MachineFunction &MF = DAG.getMachineFunction();
2498 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002499 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002500 EVT PtrVT = getPointerTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002501 SDLoc dl(Op);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002502 unsigned PCAdj = Subtarget->isThumb() ? 4 : 8;
Bill Wendlingc214cb02011-10-01 08:58:29 +00002503 ARMConstantPoolValue *CPV =
2504 ARMConstantPoolSymbol::Create(*DAG.getContext(), "_GLOBAL_OFFSET_TABLE_",
2505 ARMPCLabelIndex, PCAdj);
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002506 SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002507 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Anton Korobeynikov75b59fb2009-10-07 00:06:35 +00002508 SDValue Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002509 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002510 false, false, false, 0);
Evan Cheng408aa562009-11-06 22:24:13 +00002511 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Dale Johannesen021052a2009-02-04 20:06:27 +00002512 return DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00002513}
2514
Jim Grosbachaeca45d2009-05-12 23:59:14 +00002515SDValue
Jim Grosbachc98892f2010-05-26 20:22:18 +00002516ARMTargetLowering::LowerEH_SJLJ_SETJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002517 SDLoc dl(Op);
Jim Grosbachfaa3abb2010-05-27 23:49:24 +00002518 SDValue Val = DAG.getConstant(0, MVT::i32);
Bill Wendling7ecfbd92011-10-07 21:25:38 +00002519 return DAG.getNode(ARMISD::EH_SJLJ_SETJMP, dl,
2520 DAG.getVTList(MVT::i32, MVT::Other), Op.getOperand(0),
Jim Grosbachc98892f2010-05-26 20:22:18 +00002521 Op.getOperand(1), Val);
2522}
2523
2524SDValue
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002525ARMTargetLowering::LowerEH_SJLJ_LONGJMP(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002526 SDLoc dl(Op);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00002527 return DAG.getNode(ARMISD::EH_SJLJ_LONGJMP, dl, MVT::Other, Op.getOperand(0),
2528 Op.getOperand(1), DAG.getConstant(0, MVT::i32));
2529}
2530
2531SDValue
Jim Grosbacha570d052010-02-08 23:22:00 +00002532ARMTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG,
Jim Grosbache3864cc2010-06-16 23:45:49 +00002533 const ARMSubtarget *Subtarget) const {
Dan Gohmaneffb8942008-09-12 16:56:44 +00002534 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002535 SDLoc dl(Op);
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002536 switch (IntNo) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002537 default: return SDValue(); // Don't custom lower most intrinsics.
Bob Wilson17f88782009-08-04 00:25:01 +00002538 case Intrinsic::arm_thread_pointer: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002539 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Bob Wilson17f88782009-08-04 00:25:01 +00002540 return DAG.getNode(ARMISD::THREAD_POINTER, dl, PtrVT);
2541 }
Jim Grosbach693e36a2009-08-11 00:09:57 +00002542 case Intrinsic::eh_sjlj_lsda: {
Jim Grosbach693e36a2009-08-11 00:09:57 +00002543 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng408aa562009-11-06 22:24:13 +00002544 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Chengdfce83c2011-01-17 08:03:18 +00002545 unsigned ARMPCLabelIndex = AFI->createPICLabelUId();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002546 EVT PtrVT = getPointerTy();
Jim Grosbach693e36a2009-08-11 00:09:57 +00002547 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
2548 SDValue CPAddr;
2549 unsigned PCAdj = (RelocM != Reloc::PIC_)
2550 ? 0 : (Subtarget->isThumb() ? 4 : 8);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002551 ARMConstantPoolValue *CPV =
Bill Wendling7753d662011-10-01 08:00:54 +00002552 ARMConstantPoolConstant::Create(MF.getFunction(), ARMPCLabelIndex,
2553 ARMCP::CPLSDA, PCAdj);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002554 CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, 4);
Owen Anderson9f944592009-08-11 20:47:22 +00002555 CPAddr = DAG.getNode(ARMISD::Wrapper, dl, MVT::i32, CPAddr);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002556 SDValue Result =
Evan Chengcdbb70c2009-10-31 03:39:36 +00002557 DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), CPAddr,
Chris Lattner7727d052010-09-21 06:44:06 +00002558 MachinePointerInfo::getConstantPool(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002559 false, false, false, 0);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002560
2561 if (RelocM == Reloc::PIC_) {
Evan Cheng408aa562009-11-06 22:24:13 +00002562 SDValue PICLabel = DAG.getConstant(ARMPCLabelIndex, MVT::i32);
Jim Grosbach693e36a2009-08-11 00:09:57 +00002563 Result = DAG.getNode(ARMISD::PIC_ADD, dl, PtrVT, Result, PICLabel);
2564 }
2565 return Result;
2566 }
Evan Cheng18381b42011-03-29 23:06:19 +00002567 case Intrinsic::arm_neon_vmulls:
2568 case Intrinsic::arm_neon_vmullu: {
2569 unsigned NewOpc = (IntNo == Intrinsic::arm_neon_vmulls)
2570 ? ARMISD::VMULLs : ARMISD::VMULLu;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002571 return DAG.getNode(NewOpc, SDLoc(Op), Op.getValueType(),
Evan Cheng18381b42011-03-29 23:06:19 +00002572 Op.getOperand(1), Op.getOperand(2));
2573 }
Lauro Ramos Venanciof6a67bf2007-11-08 17:20:05 +00002574 }
2575}
2576
Eli Friedman30a49e92011-08-03 21:06:02 +00002577static SDValue LowerATOMIC_FENCE(SDValue Op, SelectionDAG &DAG,
2578 const ARMSubtarget *Subtarget) {
2579 // FIXME: handle "fence singlethread" more efficiently.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002580 SDLoc dl(Op);
Eli Friedman26a48482011-07-27 22:21:52 +00002581 if (!Subtarget->hasDataBarrier()) {
2582 // Some ARMv6 cpus can support data barriers with an mcr instruction.
2583 // Thumb1 and pre-v6 ARM mode use a libcall instead and should never get
2584 // here.
2585 assert(Subtarget->hasV6Ops() && !Subtarget->isThumb() &&
Tim Northoverc7ea8042013-10-25 09:30:24 +00002586 "Unexpected ISD::ATOMIC_FENCE encountered. Should be libcall!");
Eli Friedman30a49e92011-08-03 21:06:02 +00002587 return DAG.getNode(ARMISD::MEMBARRIER_MCR, dl, MVT::Other, Op.getOperand(0),
Eli Friedman26a48482011-07-27 22:21:52 +00002588 DAG.getConstant(0, MVT::i32));
2589 }
2590
Tim Northover36b24172013-07-03 09:20:36 +00002591 ConstantSDNode *OrdN = cast<ConstantSDNode>(Op.getOperand(1));
2592 AtomicOrdering Ord = static_cast<AtomicOrdering>(OrdN->getZExtValue());
2593 unsigned Domain = ARM_MB::ISH;
Tim Northoverf5769882013-08-28 14:39:19 +00002594 if (Subtarget->isMClass()) {
2595 // Only a full system barrier exists in the M-class architectures.
2596 Domain = ARM_MB::SY;
2597 } else if (Subtarget->isSwift() && Ord == Release) {
Tim Northover36b24172013-07-03 09:20:36 +00002598 // Swift happens to implement ISHST barriers in a way that's compatible with
2599 // Release semantics but weaker than ISH so we'd be fools not to use
2600 // it. Beware: other processors probably don't!
2601 Domain = ARM_MB::ISHST;
2602 }
2603
Joey Gouly926d3f52013-09-05 15:35:24 +00002604 return DAG.getNode(ISD::INTRINSIC_VOID, dl, MVT::Other, Op.getOperand(0),
2605 DAG.getConstant(Intrinsic::arm_dmb, MVT::i32),
Tim Northover36b24172013-07-03 09:20:36 +00002606 DAG.getConstant(Domain, MVT::i32));
Eli Friedman26a48482011-07-27 22:21:52 +00002607}
2608
Evan Cheng8740ee32010-11-03 06:34:55 +00002609static SDValue LowerPREFETCH(SDValue Op, SelectionDAG &DAG,
2610 const ARMSubtarget *Subtarget) {
2611 // ARM pre v5TE and Thumb1 does not have preload instructions.
2612 if (!(Subtarget->isThumb2() ||
2613 (!Subtarget->isThumb1Only() && Subtarget->hasV5TEOps())))
2614 // Just preserve the chain.
2615 return Op.getOperand(0);
2616
Andrew Trickef9de2a2013-05-25 02:42:55 +00002617 SDLoc dl(Op);
Evan Cheng21acf9f2010-11-04 05:19:35 +00002618 unsigned isRead = ~cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() & 1;
2619 if (!isRead &&
2620 (!Subtarget->hasV7Ops() || !Subtarget->hasMPExtension()))
2621 // ARMv7 with MP extension has PLDW.
2622 return Op.getOperand(0);
Evan Cheng8740ee32010-11-03 06:34:55 +00002623
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002624 unsigned isData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2625 if (Subtarget->isThumb()) {
Evan Cheng8740ee32010-11-03 06:34:55 +00002626 // Invert the bits.
Evan Cheng21acf9f2010-11-04 05:19:35 +00002627 isRead = ~isRead & 1;
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +00002628 isData = ~isData & 1;
2629 }
Evan Cheng8740ee32010-11-03 06:34:55 +00002630
2631 return DAG.getNode(ARMISD::PRELOAD, dl, MVT::Other, Op.getOperand(0),
Evan Cheng21acf9f2010-11-04 05:19:35 +00002632 Op.getOperand(1), DAG.getConstant(isRead, MVT::i32),
2633 DAG.getConstant(isData, MVT::i32));
Evan Cheng8740ee32010-11-03 06:34:55 +00002634}
2635
Dan Gohman31ae5862010-04-17 14:41:14 +00002636static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) {
2637 MachineFunction &MF = DAG.getMachineFunction();
2638 ARMFunctionInfo *FuncInfo = MF.getInfo<ARMFunctionInfo>();
2639
Evan Cheng10043e22007-01-19 07:51:42 +00002640 // vastart just stores the address of the VarArgsFrameIndex slot into the
2641 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002642 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002643 EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
Dan Gohman31ae5862010-04-17 14:41:14 +00002644 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
Dan Gohman2d489b52008-02-06 22:27:42 +00002645 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner886250c2010-09-21 18:51:21 +00002646 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1),
2647 MachinePointerInfo(SV), false, false, 0);
Evan Cheng10043e22007-01-19 07:51:42 +00002648}
2649
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002650SDValue
Bob Wilson2e076c42009-06-22 23:27:02 +00002651ARMTargetLowering::GetF64FormalArgument(CCValAssign &VA, CCValAssign &NextVA,
2652 SDValue &Root, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002653 SDLoc dl) const {
Bob Wilson2e076c42009-06-22 23:27:02 +00002654 MachineFunction &MF = DAG.getMachineFunction();
2655 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2656
Craig Topper760b1342012-02-22 05:59:10 +00002657 const TargetRegisterClass *RC;
David Goodwin22c2fba2009-07-08 23:10:31 +00002658 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002659 RC = &ARM::tGPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002660 else
Craig Topperc7242e02012-04-20 07:30:17 +00002661 RC = &ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002662
2663 // Transform the arguments stored in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002664 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002665 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002666
2667 SDValue ArgValue2;
2668 if (NextVA.isMemLoc()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002669 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng0664a672010-07-03 00:40:23 +00002670 int FI = MFI->CreateFixedObject(4, NextVA.getLocMemOffset(), true);
Bob Wilson2e076c42009-06-22 23:27:02 +00002671
2672 // Create load node to retrieve arguments from the stack.
2673 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Chengcdbb70c2009-10-31 03:39:36 +00002674 ArgValue2 = DAG.getLoad(MVT::i32, dl, Root, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002675 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002676 false, false, false, 0);
Bob Wilson2e076c42009-06-22 23:27:02 +00002677 } else {
Devang Patelf3292b22011-02-21 23:21:26 +00002678 Reg = MF.addLiveIn(NextVA.getLocReg(), RC);
Owen Anderson9f944592009-08-11 20:47:22 +00002679 ArgValue2 = DAG.getCopyFromReg(Root, dl, Reg, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00002680 }
Christian Pirkerb5728192014-05-08 14:06:24 +00002681 if (!Subtarget->isLittle())
2682 std::swap (ArgValue, ArgValue2);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00002683 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, ArgValue, ArgValue2);
Bob Wilson2e076c42009-06-22 23:27:02 +00002684}
2685
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002686void
2687ARMTargetLowering::computeRegArea(CCState &CCInfo, MachineFunction &MF,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002688 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002689 unsigned ArgSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002690 unsigned &ArgRegsSize,
2691 unsigned &ArgRegsSaveSize)
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002692 const {
2693 unsigned NumGPRs;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002694 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2695 unsigned RBegin, REnd;
2696 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2697 NumGPRs = REnd - RBegin;
2698 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002699 unsigned int firstUnalloced;
2700 firstUnalloced = CCInfo.getFirstUnallocated(GPRArgRegs,
2701 sizeof(GPRArgRegs) /
2702 sizeof(GPRArgRegs[0]));
2703 NumGPRs = (firstUnalloced <= 3) ? (4 - firstUnalloced) : 0;
2704 }
2705
2706 unsigned Align = MF.getTarget().getFrameLowering()->getStackAlignment();
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002707 ArgRegsSize = NumGPRs * 4;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002708
2709 // If parameter is split between stack and GPRs...
Mark Seabornbe266aa2014-02-16 18:59:48 +00002710 if (NumGPRs && Align > 4 &&
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002711 (ArgRegsSize < ArgSize ||
2712 InRegsParamRecordIdx >= CCInfo.getInRegsParamsCount())) {
Mark Seabornbe266aa2014-02-16 18:59:48 +00002713 // Add padding for part of param recovered from GPRs. For example,
2714 // if Align == 8, its last byte must be at address K*8 - 1.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002715 // We need to do it, since remained (stack) part of parameter has
2716 // stack alignment, and we need to "attach" "GPRs head" without gaps
2717 // to it:
2718 // Stack:
2719 // |---- 8 bytes block ----| |---- 8 bytes block ----| |---- 8 bytes...
2720 // [ [padding] [GPRs head] ] [ Tail passed via stack ....
2721 //
2722 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2723 unsigned Padding =
Mark Seabornbe266aa2014-02-16 18:59:48 +00002724 OffsetToAlignment(ArgRegsSize + AFI->getArgRegsSaveSize(), Align);
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002725 ArgRegsSaveSize = ArgRegsSize + Padding;
2726 } else
2727 // We don't need to extend regs save size for byval parameters if they
2728 // are passed via GPRs only.
2729 ArgRegsSaveSize = ArgRegsSize;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002730}
2731
2732// The remaining GPRs hold either the beginning of variable-argument
David Peixotto4299cf82013-02-13 00:36:35 +00002733// data, or the beginning of an aggregate passed by value (usually
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002734// byval). Either way, we allocate stack slots adjacent to the data
2735// provided by our caller, and store the unallocated registers there.
2736// If this is a variadic function, the va_list pointer will begin with
2737// these values; otherwise, this reassembles a (byval) structure that
2738// was split between registers and memory.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002739// Return: The frame index registers were stored into.
2740int
2741ARMTargetLowering::StoreByValRegs(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002742 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002743 const Value *OrigArg,
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002744 unsigned InRegsParamRecordIdx,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002745 unsigned OffsetFromOrigArg,
2746 unsigned ArgOffset,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002747 unsigned ArgSize,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002748 bool ForceMutable,
2749 unsigned ByValStoreOffset,
2750 unsigned TotalArgRegsSaveSize) const {
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002751
2752 // Currently, two use-cases possible:
Alp Tokerf907b892013-12-05 05:44:44 +00002753 // Case #1. Non-var-args function, and we meet first byval parameter.
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002754 // Setup first unallocated register as first byval register;
2755 // eat all remained registers
2756 // (these two actions are performed by HandleByVal method).
2757 // Then, here, we initialize stack frame with
2758 // "store-reg" instructions.
2759 // Case #2. Var-args function, that doesn't contain byval parameters.
2760 // The same: eat all remained unallocated registers,
2761 // initialize stack frame.
2762
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002763 MachineFunction &MF = DAG.getMachineFunction();
2764 MachineFrameInfo *MFI = MF.getFrameInfo();
2765 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002766 unsigned firstRegToSaveIndex, lastRegToSaveIndex;
2767 unsigned RBegin, REnd;
2768 if (InRegsParamRecordIdx < CCInfo.getInRegsParamsCount()) {
2769 CCInfo.getInRegsParamInfo(InRegsParamRecordIdx, RBegin, REnd);
2770 firstRegToSaveIndex = RBegin - ARM::R0;
2771 lastRegToSaveIndex = REnd - ARM::R0;
2772 } else {
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002773 firstRegToSaveIndex = CCInfo.getFirstUnallocated
Craig Topper58713212013-07-15 04:27:47 +00002774 (GPRArgRegs, array_lengthof(GPRArgRegs));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002775 lastRegToSaveIndex = 4;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002776 }
2777
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002778 unsigned ArgRegsSize, ArgRegsSaveSize;
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002779 computeRegArea(CCInfo, MF, InRegsParamRecordIdx, ArgSize,
2780 ArgRegsSize, ArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002781
2782 // Store any by-val regs to their spots on the stack so that they may be
2783 // loaded by deferencing the result of formal parameter pointer or va_next.
2784 // Note: once stack area for byval/varargs registers
2785 // was initialized, it can't be initialized again.
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002786 if (ArgRegsSaveSize) {
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002787 unsigned Padding = ArgRegsSaveSize - ArgRegsSize;
2788
2789 if (Padding) {
2790 assert(AFI->getStoredByValParamsPadding() == 0 &&
2791 "The only parameter may be padded.");
2792 AFI->setStoredByValParamsPadding(Padding);
2793 }
2794
Oliver Stannardd55e1152014-03-05 15:25:27 +00002795 int FrameIndex = MFI->CreateFixedObject(ArgRegsSaveSize,
2796 Padding +
2797 ByValStoreOffset -
2798 (int64_t)TotalArgRegsSaveSize,
2799 false);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002800 SDValue FIN = DAG.getFrameIndex(FrameIndex, getPointerTy());
Oliver Stannardd55e1152014-03-05 15:25:27 +00002801 if (Padding) {
2802 MFI->CreateFixedObject(Padding,
2803 ArgOffset + ByValStoreOffset -
2804 (int64_t)ArgRegsSaveSize,
2805 false);
2806 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002807
2808 SmallVector<SDValue, 4> MemOps;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002809 for (unsigned i = 0; firstRegToSaveIndex < lastRegToSaveIndex;
2810 ++firstRegToSaveIndex, ++i) {
Craig Topper760b1342012-02-22 05:59:10 +00002811 const TargetRegisterClass *RC;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002812 if (AFI->isThumb1OnlyFunction())
Craig Topperc7242e02012-04-20 07:30:17 +00002813 RC = &ARM::tGPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002814 else
Craig Topperc7242e02012-04-20 07:30:17 +00002815 RC = &ARM::GPRRegClass;
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002816
2817 unsigned VReg = MF.addLiveIn(GPRArgRegs[firstRegToSaveIndex], RC);
2818 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
2819 SDValue Store =
2820 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002821 MachinePointerInfo(OrigArg, OffsetFromOrigArg + 4*i),
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002822 false, false, 0);
2823 MemOps.push_back(Store);
2824 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
2825 DAG.getConstant(4, getPointerTy()));
2826 }
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002827
2828 AFI->setArgRegsSaveSize(ArgRegsSaveSize + AFI->getArgRegsSaveSize());
2829
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002830 if (!MemOps.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00002831 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002832 return FrameIndex;
Oliver Stannardd55e1152014-03-05 15:25:27 +00002833 } else {
2834 if (ArgSize == 0) {
2835 // We cannot allocate a zero-byte object for the first variadic argument,
2836 // so just make up a size.
2837 ArgSize = 4;
2838 }
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002839 // This will point to the next argument passed via stack.
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00002840 return MFI->CreateFixedObject(
Oliver Stannardd55e1152014-03-05 15:25:27 +00002841 ArgSize, ArgOffset, !ForceMutable);
2842 }
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002843}
2844
2845// Setup stack frame, the va_list pointer will start from.
2846void
2847ARMTargetLowering::VarArgStyleRegisters(CCState &CCInfo, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002848 SDLoc dl, SDValue &Chain,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002849 unsigned ArgOffset,
Oliver Stannardd55e1152014-03-05 15:25:27 +00002850 unsigned TotalArgRegsSaveSize,
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002851 bool ForceMutable) const {
2852 MachineFunction &MF = DAG.getMachineFunction();
2853 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2854
2855 // Try to store any remaining integer argument regs
2856 // to their spots on the stack so that they may be loaded by deferencing
2857 // the result of va_next.
2858 // If there is no regs to be stored, just point address after last
2859 // argument passed via stack.
2860 int FrameIndex =
Craig Topper062a2ba2014-04-25 05:30:21 +00002861 StoreByValRegs(CCInfo, DAG, dl, Chain, nullptr,
2862 CCInfo.getInRegsParamsCount(), 0, ArgOffset, 0, ForceMutable,
2863 0, TotalArgRegsSaveSize);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00002864
2865 AFI->setVarArgsFrameIndex(FrameIndex);
Stuart Hastings45fe3c32011-04-20 16:47:52 +00002866}
2867
Bob Wilson2e076c42009-06-22 23:27:02 +00002868SDValue
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002869ARMTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel68c5f472009-09-02 08:44:58 +00002870 CallingConv::ID CallConv, bool isVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002871 const SmallVectorImpl<ISD::InputArg>
2872 &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002873 SDLoc dl, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002874 SmallVectorImpl<SDValue> &InVals)
2875 const {
Bob Wilsona4c22902009-04-17 19:07:39 +00002876 MachineFunction &MF = DAG.getMachineFunction();
2877 MachineFrameInfo *MFI = MF.getFrameInfo();
2878
Bob Wilsona4c22902009-04-17 19:07:39 +00002879 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
2880
2881 // Assign locations to all of the incoming arguments.
2882 SmallVector<CCValAssign, 16> ArgLocs;
Cameron Zwarich89019782011-06-10 20:59:24 +00002883 ARMCCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
2884 getTargetMachine(), ArgLocs, *DAG.getContext(), Prologue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002885 CCInfo.AnalyzeFormalArguments(Ins,
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002886 CCAssignFnForNode(CallConv, /* Return*/ false,
2887 isVarArg));
Jim Grosbach54efea02013-03-02 20:16:15 +00002888
Bob Wilsona4c22902009-04-17 19:07:39 +00002889 SmallVector<SDValue, 16> ArgValues;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002890 int lastInsIndex = -1;
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00002891 SDValue ArgValue;
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002892 Function::const_arg_iterator CurOrigArg = MF.getFunction()->arg_begin();
2893 unsigned CurArgIdx = 0;
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00002894
2895 // Initially ArgRegsSaveSize is zero.
2896 // Then we increase this value each time we meet byval parameter.
2897 // We also increase this value in case of varargs function.
2898 AFI->setArgRegsSaveSize(0);
2899
Oliver Stannardd55e1152014-03-05 15:25:27 +00002900 unsigned ByValStoreOffset = 0;
2901 unsigned TotalArgRegsSaveSize = 0;
2902 unsigned ArgRegsSaveSizeMaxAlign = 4;
2903
2904 // Calculate the amount of stack space that we need to allocate to store
2905 // byval and variadic arguments that are passed in registers.
2906 // We need to know this before we allocate the first byval or variadic
2907 // argument, as they will be allocated a stack slot below the CFA (Canonical
2908 // Frame Address, the stack pointer at entry to the function).
2909 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2910 CCValAssign &VA = ArgLocs[i];
2911 if (VA.isMemLoc()) {
2912 int index = VA.getValNo();
2913 if (index != lastInsIndex) {
2914 ISD::ArgFlagsTy Flags = Ins[index].Flags;
2915 if (Flags.isByVal()) {
2916 unsigned ExtraArgRegsSize;
2917 unsigned ExtraArgRegsSaveSize;
2918 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsProceed(),
2919 Flags.getByValSize(),
2920 ExtraArgRegsSize, ExtraArgRegsSaveSize);
2921
2922 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
2923 if (Flags.getByValAlign() > ArgRegsSaveSizeMaxAlign)
2924 ArgRegsSaveSizeMaxAlign = Flags.getByValAlign();
2925 CCInfo.nextInRegsParam();
2926 }
2927 lastInsIndex = index;
2928 }
2929 }
2930 }
2931 CCInfo.rewindByValRegsInfo();
2932 lastInsIndex = -1;
2933 if (isVarArg) {
2934 unsigned ExtraArgRegsSize;
2935 unsigned ExtraArgRegsSaveSize;
2936 computeRegArea(CCInfo, MF, CCInfo.getInRegsParamsCount(), 0,
2937 ExtraArgRegsSize, ExtraArgRegsSaveSize);
2938 TotalArgRegsSaveSize += ExtraArgRegsSaveSize;
2939 }
2940 // If the arg regs save area contains N-byte aligned values, the
2941 // bottom of it must be at least N-byte aligned.
2942 TotalArgRegsSaveSize = RoundUpToAlignment(TotalArgRegsSaveSize, ArgRegsSaveSizeMaxAlign);
2943 TotalArgRegsSaveSize = std::min(TotalArgRegsSaveSize, 16U);
2944
Bob Wilsona4c22902009-04-17 19:07:39 +00002945 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2946 CCValAssign &VA = ArgLocs[i];
Stepan Dyatkovskiyf13dbb82012-10-10 11:37:36 +00002947 std::advance(CurOrigArg, Ins[VA.getValNo()].OrigArgIndex - CurArgIdx);
2948 CurArgIdx = Ins[VA.getValNo()].OrigArgIndex;
Bob Wilsonea09d4a2009-04-17 20:35:10 +00002949 // Arguments stored in registers.
Bob Wilsona4c22902009-04-17 19:07:39 +00002950 if (VA.isRegLoc()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002951 EVT RegVT = VA.getLocVT();
Bob Wilsona4c22902009-04-17 19:07:39 +00002952
Bob Wilsona4c22902009-04-17 19:07:39 +00002953 if (VA.needsCustom()) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002954 // f64 and vector types are split up into multiple registers or
2955 // combinations of registers and stack slots.
Owen Anderson9f944592009-08-11 20:47:22 +00002956 if (VA.getLocVT() == MVT::v2f64) {
Bob Wilson2e076c42009-06-22 23:27:02 +00002957 SDValue ArgValue1 = GetF64FormalArgument(VA, ArgLocs[++i],
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002958 Chain, DAG, dl);
Bob Wilson2e076c42009-06-22 23:27:02 +00002959 VA = ArgLocs[++i]; // skip ahead to next loc
Bob Wilson699bdf72010-04-13 22:03:22 +00002960 SDValue ArgValue2;
2961 if (VA.isMemLoc()) {
Evan Cheng0664a672010-07-03 00:40:23 +00002962 int FI = MFI->CreateFixedObject(8, VA.getLocMemOffset(), true);
Bob Wilson699bdf72010-04-13 22:03:22 +00002963 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2964 ArgValue2 = DAG.getLoad(MVT::f64, dl, Chain, FIN,
Chris Lattner7727d052010-09-21 06:44:06 +00002965 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00002966 false, false, false, 0);
Bob Wilson699bdf72010-04-13 22:03:22 +00002967 } else {
2968 ArgValue2 = GetF64FormalArgument(VA, ArgLocs[++i],
2969 Chain, DAG, dl);
2970 }
Owen Anderson9f944592009-08-11 20:47:22 +00002971 ArgValue = DAG.getNode(ISD::UNDEF, dl, MVT::v2f64);
2972 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00002973 ArgValue, ArgValue1, DAG.getIntPtrConstant(0));
Owen Anderson9f944592009-08-11 20:47:22 +00002974 ArgValue = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64,
Bob Wilson2e076c42009-06-22 23:27:02 +00002975 ArgValue, ArgValue2, DAG.getIntPtrConstant(1));
2976 } else
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002977 ArgValue = GetF64FormalArgument(VA, ArgLocs[++i], Chain, DAG, dl);
Bob Wilsona4c22902009-04-17 19:07:39 +00002978
Bob Wilson2e076c42009-06-22 23:27:02 +00002979 } else {
Craig Topper760b1342012-02-22 05:59:10 +00002980 const TargetRegisterClass *RC;
Anton Korobeynikov22ef7512009-08-05 19:04:42 +00002981
Owen Anderson9f944592009-08-11 20:47:22 +00002982 if (RegVT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +00002983 RC = &ARM::SPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00002984 else if (RegVT == MVT::f64)
Craig Topperc7242e02012-04-20 07:30:17 +00002985 RC = &ARM::DPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00002986 else if (RegVT == MVT::v2f64)
Craig Topperc7242e02012-04-20 07:30:17 +00002987 RC = &ARM::QPRRegClass;
Owen Anderson9f944592009-08-11 20:47:22 +00002988 else if (RegVT == MVT::i32)
Craig Topperc7242e02012-04-20 07:30:17 +00002989 RC = AFI->isThumb1OnlyFunction() ?
2990 (const TargetRegisterClass*)&ARM::tGPRRegClass :
2991 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bob Wilson2e076c42009-06-22 23:27:02 +00002992 else
Anton Korobeynikovef98dbe2009-08-05 20:15:19 +00002993 llvm_unreachable("RegVT not supported by FORMAL_ARGUMENTS Lowering");
Bob Wilson2e076c42009-06-22 23:27:02 +00002994
2995 // Transform the arguments in physical registers into virtual ones.
Devang Patelf3292b22011-02-21 23:21:26 +00002996 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00002997 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Bob Wilsona4c22902009-04-17 19:07:39 +00002998 }
2999
3000 // If this is an 8 or 16-bit value, it is really passed promoted
3001 // to 32 bits. Insert an assert[sz]ext to capture this, then
3002 // truncate to the right size.
3003 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003004 default: llvm_unreachable("Unknown loc info!");
Bob Wilsona4c22902009-04-17 19:07:39 +00003005 case CCValAssign::Full: break;
3006 case CCValAssign::BCvt:
Wesley Peck527da1b2010-11-23 03:31:01 +00003007 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003008 break;
3009 case CCValAssign::SExt:
3010 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
3011 DAG.getValueType(VA.getValVT()));
3012 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3013 break;
3014 case CCValAssign::ZExt:
3015 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
3016 DAG.getValueType(VA.getValVT()));
3017 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
3018 break;
3019 }
3020
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003021 InVals.push_back(ArgValue);
Bob Wilsona4c22902009-04-17 19:07:39 +00003022
3023 } else { // VA.isRegLoc()
3024
3025 // sanity check
3026 assert(VA.isMemLoc());
Owen Anderson9f944592009-08-11 20:47:22 +00003027 assert(VA.getValVT() != MVT::i64 && "i64 should already be lowered");
Bob Wilsona4c22902009-04-17 19:07:39 +00003028
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003029 int index = ArgLocs[i].getValNo();
Owen Anderson77aa2662011-04-05 21:48:57 +00003030
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003031 // Some Ins[] entries become multiple ArgLoc[] entries.
3032 // Process them only once.
3033 if (index != lastInsIndex)
3034 {
3035 ISD::ArgFlagsTy Flags = Ins[index].Flags;
Eric Christopher0713a9d2011-06-08 23:55:35 +00003036 // FIXME: For now, all byval parameter objects are marked mutable.
Eric Christophere02e07c2011-04-29 23:12:01 +00003037 // This can be changed with more analysis.
3038 // In case of tail call optimization mark all arguments mutable.
3039 // Since they could be overwritten by lowering of arguments in case of
3040 // a tail call.
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003041 if (Flags.isByVal()) {
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003042 unsigned CurByValIndex = CCInfo.getInRegsParamsProceed();
Oliver Stannardd55e1152014-03-05 15:25:27 +00003043
3044 ByValStoreOffset = RoundUpToAlignment(ByValStoreOffset, Flags.getByValAlign());
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003045 int FrameIndex = StoreByValRegs(
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003046 CCInfo, DAG, dl, Chain, CurOrigArg,
3047 CurByValIndex,
3048 Ins[VA.getValNo()].PartOffset,
3049 VA.getLocMemOffset(),
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003050 Flags.getByValSize(),
Oliver Stannardd55e1152014-03-05 15:25:27 +00003051 true /*force mutable frames*/,
3052 ByValStoreOffset,
3053 TotalArgRegsSaveSize);
3054 ByValStoreOffset += Flags.getByValSize();
3055 ByValStoreOffset = std::min(ByValStoreOffset, 16U);
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003056 InVals.push_back(DAG.getFrameIndex(FrameIndex, getPointerTy()));
Stepan Dyatkovskiy8c02c982013-05-05 07:48:36 +00003057 CCInfo.nextInRegsParam();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003058 } else {
Oliver Stannardd55e1152014-03-05 15:25:27 +00003059 unsigned FIOffset = VA.getLocMemOffset();
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003060 int FI = MFI->CreateFixedObject(VA.getLocVT().getSizeInBits()/8,
Stepan Dyatkovskiyd0e34a22013-05-20 08:01:34 +00003061 FIOffset, true);
Bob Wilsona4c22902009-04-17 19:07:39 +00003062
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003063 // Create load nodes to retrieve arguments from the stack.
3064 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
3065 InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
3066 MachinePointerInfo::getFixedStack(FI),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003067 false, false, false, 0));
Stuart Hastings67c5c3e2011-02-28 17:17:53 +00003068 }
3069 lastInsIndex = index;
3070 }
Bob Wilsona4c22902009-04-17 19:07:39 +00003071 }
3072 }
3073
3074 // varargs
Stuart Hastings45fe3c32011-04-20 16:47:52 +00003075 if (isVarArg)
Stepan Dyatkovskiyf5aa83d2013-04-30 07:19:58 +00003076 VarArgStyleRegisters(CCInfo, DAG, dl, Chain,
Oliver Stannardd55e1152014-03-05 15:25:27 +00003077 CCInfo.getNextStackOffset(),
3078 TotalArgRegsSaveSize);
Evan Cheng10043e22007-01-19 07:51:42 +00003079
Oliver Stannardb14c6252014-04-02 16:10:33 +00003080 AFI->setArgumentStackSize(CCInfo.getNextStackOffset());
3081
Dan Gohmanf9bbcd12009-08-05 01:29:28 +00003082 return Chain;
Evan Cheng10043e22007-01-19 07:51:42 +00003083}
3084
3085/// isFloatingPointZero - Return true if this is +0.0.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003086static bool isFloatingPointZero(SDValue Op) {
Evan Cheng10043e22007-01-19 07:51:42 +00003087 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003088 return CFP->getValueAPF().isPosZero();
Gabor Greiff304a7a2008-08-28 21:40:38 +00003089 else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
Evan Cheng10043e22007-01-19 07:51:42 +00003090 // Maybe this has already been legalized into the constant pool?
3091 if (Op.getOperand(1).getOpcode() == ARMISD::Wrapper) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003092 SDValue WrapperOp = Op.getOperand(1).getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003093 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(WrapperOp))
Dan Gohmanbcaf6812010-04-15 01:51:59 +00003094 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
Dale Johannesen3cf889f2007-08-31 04:03:46 +00003095 return CFP->getValueAPF().isPosZero();
Evan Cheng10043e22007-01-19 07:51:42 +00003096 }
3097 }
3098 return false;
3099}
3100
Evan Cheng10043e22007-01-19 07:51:42 +00003101/// Returns appropriate ARM CMP (cmp) and corresponding condition code for
3102/// the given operands.
Evan Cheng15b80e42009-11-12 07:13:11 +00003103SDValue
3104ARMTargetLowering::getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003105 SDValue &ARMcc, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003106 SDLoc dl) const {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003107 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS.getNode())) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003108 unsigned C = RHSC->getZExtValue();
Evan Cheng15b80e42009-11-12 07:13:11 +00003109 if (!isLegalICmpImmediate(C)) {
Evan Cheng10043e22007-01-19 07:51:42 +00003110 // Constant does not fit, try adjusting it by one?
3111 switch (CC) {
3112 default: break;
3113 case ISD::SETLT:
Evan Cheng10043e22007-01-19 07:51:42 +00003114 case ISD::SETGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003115 if (C != 0x80000000 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003116 CC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003117 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003118 }
3119 break;
3120 case ISD::SETULT:
3121 case ISD::SETUGE:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003122 if (C != 0 && isLegalICmpImmediate(C-1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003123 CC = (CC == ISD::SETULT) ? ISD::SETULE : ISD::SETUGT;
Owen Anderson9f944592009-08-11 20:47:22 +00003124 RHS = DAG.getConstant(C-1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003125 }
3126 break;
3127 case ISD::SETLE:
Evan Cheng10043e22007-01-19 07:51:42 +00003128 case ISD::SETGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003129 if (C != 0x7fffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003130 CC = (CC == ISD::SETLE) ? ISD::SETLT : ISD::SETGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003131 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng48b094d2007-02-02 01:53:26 +00003132 }
3133 break;
3134 case ISD::SETULE:
3135 case ISD::SETUGT:
Daniel Dunbara54a1b02010-08-25 16:58:05 +00003136 if (C != 0xffffffff && isLegalICmpImmediate(C+1)) {
Evan Cheng48b094d2007-02-02 01:53:26 +00003137 CC = (CC == ISD::SETULE) ? ISD::SETULT : ISD::SETUGE;
Owen Anderson9f944592009-08-11 20:47:22 +00003138 RHS = DAG.getConstant(C+1, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003139 }
3140 break;
3141 }
3142 }
3143 }
3144
3145 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003146 ARMISD::NodeType CompareType;
3147 switch (CondCode) {
3148 default:
3149 CompareType = ARMISD::CMP;
3150 break;
3151 case ARMCC::EQ:
3152 case ARMCC::NE:
David Goodwindbf11ba2009-06-29 15:33:01 +00003153 // Uses only Z Flag
3154 CompareType = ARMISD::CMPZ;
Lauro Ramos Venancio6be85332007-04-02 01:30:03 +00003155 break;
3156 }
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003157 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003158 return DAG.getNode(CompareType, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003159}
3160
3161/// Returns a appropriate VFP CMP (fcmp{s|d}+fmstat) for the given operands.
Evan Cheng25f93642010-07-08 02:08:50 +00003162SDValue
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003163ARMTargetLowering::getVFPCmp(SDValue LHS, SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003164 SDLoc dl) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003165 SDValue Cmp;
Evan Cheng10043e22007-01-19 07:51:42 +00003166 if (!isFloatingPointZero(RHS))
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003167 Cmp = DAG.getNode(ARMISD::CMPFP, dl, MVT::Glue, LHS, RHS);
Evan Cheng10043e22007-01-19 07:51:42 +00003168 else
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003169 Cmp = DAG.getNode(ARMISD::CMPFPw0, dl, MVT::Glue, LHS);
3170 return DAG.getNode(ARMISD::FMSTAT, dl, MVT::Glue, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003171}
3172
Bob Wilson45acbd02011-03-08 01:17:20 +00003173/// duplicateCmp - Glue values can have only one use, so this function
3174/// duplicates a comparison node.
3175SDValue
3176ARMTargetLowering::duplicateCmp(SDValue Cmp, SelectionDAG &DAG) const {
3177 unsigned Opc = Cmp.getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003178 SDLoc DL(Cmp);
Bob Wilson45acbd02011-03-08 01:17:20 +00003179 if (Opc == ARMISD::CMP || Opc == ARMISD::CMPZ)
3180 return DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3181
3182 assert(Opc == ARMISD::FMSTAT && "unexpected comparison operation");
3183 Cmp = Cmp.getOperand(0);
3184 Opc = Cmp.getOpcode();
3185 if (Opc == ARMISD::CMPFP)
3186 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0),Cmp.getOperand(1));
3187 else {
3188 assert(Opc == ARMISD::CMPFPw0 && "unexpected operand of FMSTAT");
3189 Cmp = DAG.getNode(Opc, DL, MVT::Glue, Cmp.getOperand(0));
3190 }
3191 return DAG.getNode(ARMISD::FMSTAT, DL, MVT::Glue, Cmp);
3192}
3193
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003194std::pair<SDValue, SDValue>
3195ARMTargetLowering::getARMXALUOOp(SDValue Op, SelectionDAG &DAG,
3196 SDValue &ARMcc) const {
3197 assert(Op.getValueType() == MVT::i32 && "Unsupported value type");
3198
3199 SDValue Value, OverflowCmp;
3200 SDValue LHS = Op.getOperand(0);
3201 SDValue RHS = Op.getOperand(1);
3202
3203
3204 // FIXME: We are currently always generating CMPs because we don't support
3205 // generating CMN through the backend. This is not as good as the natural
3206 // CMP case because it causes a register dependency and cannot be folded
3207 // later.
3208
3209 switch (Op.getOpcode()) {
3210 default:
3211 llvm_unreachable("Unknown overflow instruction!");
3212 case ISD::SADDO:
3213 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3214 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3215 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3216 break;
3217 case ISD::UADDO:
3218 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3219 Value = DAG.getNode(ISD::ADD, SDLoc(Op), Op.getValueType(), LHS, RHS);
3220 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, Value, LHS);
3221 break;
3222 case ISD::SSUBO:
3223 ARMcc = DAG.getConstant(ARMCC::VC, MVT::i32);
3224 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3225 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3226 break;
3227 case ISD::USUBO:
3228 ARMcc = DAG.getConstant(ARMCC::HS, MVT::i32);
3229 Value = DAG.getNode(ISD::SUB, SDLoc(Op), Op.getValueType(), LHS, RHS);
3230 OverflowCmp = DAG.getNode(ARMISD::CMP, SDLoc(Op), MVT::Glue, LHS, RHS);
3231 break;
3232 } // switch (...)
3233
3234 return std::make_pair(Value, OverflowCmp);
3235}
3236
3237
3238SDValue
3239ARMTargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
3240 // Let legalize expand this if it isn't a legal type yet.
3241 if (!DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()))
3242 return SDValue();
3243
3244 SDValue Value, OverflowCmp;
3245 SDValue ARMcc;
3246 std::tie(Value, OverflowCmp) = getARMXALUOOp(Op, DAG, ARMcc);
3247 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3248 // We use 0 and 1 as false and true values.
3249 SDValue TVal = DAG.getConstant(1, MVT::i32);
3250 SDValue FVal = DAG.getConstant(0, MVT::i32);
3251 EVT VT = Op.getValueType();
3252
3253 SDValue Overflow = DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, TVal, FVal,
3254 ARMcc, CCR, OverflowCmp);
3255
3256 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
3257 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), VTs, Value, Overflow);
3258}
3259
3260
Bill Wendling6a981312010-08-11 08:43:16 +00003261SDValue ARMTargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
3262 SDValue Cond = Op.getOperand(0);
3263 SDValue SelectTrue = Op.getOperand(1);
3264 SDValue SelectFalse = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003265 SDLoc dl(Op);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00003266 unsigned Opc = Cond.getOpcode();
3267
3268 if (Cond.getResNo() == 1 &&
3269 (Opc == ISD::SADDO || Opc == ISD::UADDO || Opc == ISD::SSUBO ||
3270 Opc == ISD::USUBO)) {
3271 if (!DAG.getTargetLoweringInfo().isTypeLegal(Cond->getValueType(0)))
3272 return SDValue();
3273
3274 SDValue Value, OverflowCmp;
3275 SDValue ARMcc;
3276 std::tie(Value, OverflowCmp) = getARMXALUOOp(Cond, DAG, ARMcc);
3277 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3278 EVT VT = Op.getValueType();
3279
3280 return DAG.getNode(ARMISD::CMOV, SDLoc(Op), VT, SelectTrue, SelectFalse,
3281 ARMcc, CCR, OverflowCmp);
3282
3283 }
Bill Wendling6a981312010-08-11 08:43:16 +00003284
3285 // Convert:
3286 //
3287 // (select (cmov 1, 0, cond), t, f) -> (cmov t, f, cond)
3288 // (select (cmov 0, 1, cond), t, f) -> (cmov f, t, cond)
3289 //
3290 if (Cond.getOpcode() == ARMISD::CMOV && Cond.hasOneUse()) {
3291 const ConstantSDNode *CMOVTrue =
3292 dyn_cast<ConstantSDNode>(Cond.getOperand(0));
3293 const ConstantSDNode *CMOVFalse =
3294 dyn_cast<ConstantSDNode>(Cond.getOperand(1));
3295
3296 if (CMOVTrue && CMOVFalse) {
3297 unsigned CMOVTrueVal = CMOVTrue->getZExtValue();
3298 unsigned CMOVFalseVal = CMOVFalse->getZExtValue();
3299
3300 SDValue True;
3301 SDValue False;
3302 if (CMOVTrueVal == 1 && CMOVFalseVal == 0) {
3303 True = SelectTrue;
3304 False = SelectFalse;
3305 } else if (CMOVTrueVal == 0 && CMOVFalseVal == 1) {
3306 True = SelectFalse;
3307 False = SelectTrue;
3308 }
3309
3310 if (True.getNode() && False.getNode()) {
Evan Cheng522fbfe2011-05-18 18:59:17 +00003311 EVT VT = Op.getValueType();
Bill Wendling6a981312010-08-11 08:43:16 +00003312 SDValue ARMcc = Cond.getOperand(2);
3313 SDValue CCR = Cond.getOperand(3);
Bob Wilson45acbd02011-03-08 01:17:20 +00003314 SDValue Cmp = duplicateCmp(Cond.getOperand(4), DAG);
Evan Cheng522fbfe2011-05-18 18:59:17 +00003315 assert(True.getValueType() == VT);
3316 return DAG.getNode(ARMISD::CMOV, dl, VT, True, False, ARMcc, CCR, Cmp);
Bill Wendling6a981312010-08-11 08:43:16 +00003317 }
3318 }
3319 }
3320
Dan Gohmand4a77c42012-02-24 00:09:36 +00003321 // ARM's BooleanContents value is UndefinedBooleanContent. Mask out the
3322 // undefined bits before doing a full-word comparison with zero.
3323 Cond = DAG.getNode(ISD::AND, dl, Cond.getValueType(), Cond,
3324 DAG.getConstant(1, Cond.getValueType()));
3325
Bill Wendling6a981312010-08-11 08:43:16 +00003326 return DAG.getSelectCC(dl, Cond,
3327 DAG.getConstant(0, Cond.getValueType()),
3328 SelectTrue, SelectFalse, ISD::SETNE);
3329}
3330
Joey Gouly881eab52013-08-22 15:29:11 +00003331static ISD::CondCode getInverseCCForVSEL(ISD::CondCode CC) {
3332 if (CC == ISD::SETNE)
3333 return ISD::SETEQ;
Weiming Zhao63871d22013-12-18 22:25:17 +00003334 return ISD::getSetCCInverse(CC, true);
Joey Gouly881eab52013-08-22 15:29:11 +00003335}
3336
3337static void checkVSELConstraints(ISD::CondCode CC, ARMCC::CondCodes &CondCode,
3338 bool &swpCmpOps, bool &swpVselOps) {
3339 // Start by selecting the GE condition code for opcodes that return true for
3340 // 'equality'
3341 if (CC == ISD::SETUGE || CC == ISD::SETOGE || CC == ISD::SETOLE ||
3342 CC == ISD::SETULE)
3343 CondCode = ARMCC::GE;
3344
3345 // and GT for opcodes that return false for 'equality'.
3346 else if (CC == ISD::SETUGT || CC == ISD::SETOGT || CC == ISD::SETOLT ||
3347 CC == ISD::SETULT)
3348 CondCode = ARMCC::GT;
3349
3350 // Since we are constrained to GE/GT, if the opcode contains 'less', we need
3351 // to swap the compare operands.
3352 if (CC == ISD::SETOLE || CC == ISD::SETULE || CC == ISD::SETOLT ||
3353 CC == ISD::SETULT)
3354 swpCmpOps = true;
3355
3356 // Both GT and GE are ordered comparisons, and return false for 'unordered'.
3357 // If we have an unordered opcode, we need to swap the operands to the VSEL
3358 // instruction (effectively negating the condition).
3359 //
3360 // This also has the effect of swapping which one of 'less' or 'greater'
3361 // returns true, so we also swap the compare operands. It also switches
3362 // whether we return true for 'equality', so we compensate by picking the
3363 // opposite condition code to our original choice.
3364 if (CC == ISD::SETULE || CC == ISD::SETULT || CC == ISD::SETUGE ||
3365 CC == ISD::SETUGT) {
3366 swpCmpOps = !swpCmpOps;
3367 swpVselOps = !swpVselOps;
3368 CondCode = CondCode == ARMCC::GT ? ARMCC::GE : ARMCC::GT;
3369 }
3370
3371 // 'ordered' is 'anything but unordered', so use the VS condition code and
3372 // swap the VSEL operands.
3373 if (CC == ISD::SETO) {
3374 CondCode = ARMCC::VS;
3375 swpVselOps = true;
3376 }
3377
3378 // 'unordered or not equal' is 'anything but equal', so use the EQ condition
3379 // code and swap the VSEL operands.
3380 if (CC == ISD::SETUNE) {
3381 CondCode = ARMCC::EQ;
3382 swpVselOps = true;
3383 }
3384}
3385
Dan Gohman21cea8a2010-04-17 15:26:15 +00003386SDValue ARMTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003387 EVT VT = Op.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003388 SDValue LHS = Op.getOperand(0);
3389 SDValue RHS = Op.getOperand(1);
Evan Cheng10043e22007-01-19 07:51:42 +00003390 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003391 SDValue TrueVal = Op.getOperand(2);
3392 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003393 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003394
Owen Anderson9f944592009-08-11 20:47:22 +00003395 if (LHS.getValueType() == MVT::i32) {
Joey Gouly881eab52013-08-22 15:29:11 +00003396 // Try to generate VSEL on ARMv8.
3397 // The VSEL instruction can't use all the usual ARM condition
3398 // codes: it only has two bits to select the condition code, so it's
3399 // constrained to use only GE, GT, VS and EQ.
3400 //
3401 // To implement all the various ISD::SETXXX opcodes, we sometimes need to
3402 // swap the operands of the previous compare instruction (effectively
3403 // inverting the compare condition, swapping 'less' and 'greater') and
3404 // sometimes need to swap the operands to the VSEL (which inverts the
3405 // condition in the sense of firing whenever the previous condition didn't)
Joey Goulyccd04892013-09-13 13:46:57 +00003406 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003407 TrueVal.getValueType() == MVT::f64)) {
3408 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3409 if (CondCode == ARMCC::LT || CondCode == ARMCC::LE ||
3410 CondCode == ARMCC::VC || CondCode == ARMCC::NE) {
3411 CC = getInverseCCForVSEL(CC);
3412 std::swap(TrueVal, FalseVal);
3413 }
3414 }
3415
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003416 SDValue ARMcc;
Owen Anderson9f944592009-08-11 20:47:22 +00003417 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003418 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Joey Gouly881eab52013-08-22 15:29:11 +00003419 return DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc, CCR,
3420 Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003421 }
3422
3423 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003424 FPCCToARMCC(CC, CondCode, CondCode2);
Evan Cheng10043e22007-01-19 07:51:42 +00003425
Joey Gouly881eab52013-08-22 15:29:11 +00003426 // Try to generate VSEL on ARMv8.
Joey Goulyccd04892013-09-13 13:46:57 +00003427 if (getSubtarget()->hasFPARMv8() && (TrueVal.getValueType() == MVT::f32 ||
Joey Gouly881eab52013-08-22 15:29:11 +00003428 TrueVal.getValueType() == MVT::f64)) {
Joey Goulye3dd6842013-08-23 12:01:13 +00003429 // We can select VMAXNM/VMINNM from a compare followed by a select with the
3430 // same operands, as follows:
3431 // c = fcmp [ogt, olt, ugt, ult] a, b
3432 // select c, a, b
3433 // We only do this in unsafe-fp-math, because signed zeros and NaNs are
3434 // handled differently than the original code sequence.
3435 if (getTargetMachine().Options.UnsafeFPMath && LHS == TrueVal &&
3436 RHS == FalseVal) {
3437 if (CC == ISD::SETOGT || CC == ISD::SETUGT)
3438 return DAG.getNode(ARMISD::VMAXNM, dl, VT, TrueVal, FalseVal);
3439 if (CC == ISD::SETOLT || CC == ISD::SETULT)
3440 return DAG.getNode(ARMISD::VMINNM, dl, VT, TrueVal, FalseVal);
3441 }
3442
Joey Gouly881eab52013-08-22 15:29:11 +00003443 bool swpCmpOps = false;
3444 bool swpVselOps = false;
3445 checkVSELConstraints(CC, CondCode, swpCmpOps, swpVselOps);
3446
3447 if (CondCode == ARMCC::GT || CondCode == ARMCC::GE ||
3448 CondCode == ARMCC::VS || CondCode == ARMCC::EQ) {
3449 if (swpCmpOps)
3450 std::swap(LHS, RHS);
3451 if (swpVselOps)
3452 std::swap(TrueVal, FalseVal);
3453 }
3454 }
3455
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003456 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3457 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003458 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Dale Johannesen400dc2e2009-02-06 21:50:26 +00003459 SDValue Result = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003460 ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003461 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003462 SDValue ARMcc2 = DAG.getConstant(CondCode2, MVT::i32);
Evan Cheng10043e22007-01-19 07:51:42 +00003463 // FIXME: Needs another CMP because flag can have but one use.
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003464 SDValue Cmp2 = getVFPCmp(LHS, RHS, DAG, dl);
Bob Wilson7117a912009-03-20 22:42:55 +00003465 Result = DAG.getNode(ARMISD::CMOV, dl, VT,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003466 Result, TrueVal, ARMcc2, CCR, Cmp2);
Evan Cheng10043e22007-01-19 07:51:42 +00003467 }
3468 return Result;
3469}
3470
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003471/// canChangeToInt - Given the fp compare operand, return true if it is suitable
3472/// to morph to an integer compare sequence.
3473static bool canChangeToInt(SDValue Op, bool &SeenZero,
3474 const ARMSubtarget *Subtarget) {
3475 SDNode *N = Op.getNode();
3476 if (!N->hasOneUse())
3477 // Otherwise it requires moving the value from fp to integer registers.
3478 return false;
3479 if (!N->getNumValues())
3480 return false;
3481 EVT VT = Op.getValueType();
3482 if (VT != MVT::f32 && !Subtarget->isFPBrccSlow())
3483 // f32 case is generally profitable. f64 case only makes sense when vcmpe +
3484 // vmrs are very slow, e.g. cortex-a8.
3485 return false;
3486
3487 if (isFloatingPointZero(Op)) {
3488 SeenZero = true;
3489 return true;
3490 }
3491 return ISD::isNormalLoad(N);
3492}
3493
3494static SDValue bitcastf32Toi32(SDValue Op, SelectionDAG &DAG) {
3495 if (isFloatingPointZero(Op))
3496 return DAG.getConstant(0, MVT::i32);
3497
3498 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003499 return DAG.getLoad(MVT::i32, SDLoc(Op),
Chris Lattner7727d052010-09-21 06:44:06 +00003500 Ld->getChain(), Ld->getBasePtr(), Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003501 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003502 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003503
3504 llvm_unreachable("Unknown VFP cmp argument!");
3505}
3506
3507static void expandf64Toi32(SDValue Op, SelectionDAG &DAG,
3508 SDValue &RetVal1, SDValue &RetVal2) {
3509 if (isFloatingPointZero(Op)) {
3510 RetVal1 = DAG.getConstant(0, MVT::i32);
3511 RetVal2 = DAG.getConstant(0, MVT::i32);
3512 return;
3513 }
3514
3515 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op)) {
3516 SDValue Ptr = Ld->getBasePtr();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003517 RetVal1 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003518 Ld->getChain(), Ptr,
Chris Lattner7727d052010-09-21 06:44:06 +00003519 Ld->getPointerInfo(),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003520 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003521 Ld->isInvariant(), Ld->getAlignment());
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003522
3523 EVT PtrType = Ptr.getValueType();
3524 unsigned NewAlign = MinAlign(Ld->getAlignment(), 4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003525 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003526 PtrType, Ptr, DAG.getConstant(4, PtrType));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003527 RetVal2 = DAG.getLoad(MVT::i32, SDLoc(Op),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003528 Ld->getChain(), NewPtr,
Chris Lattner7727d052010-09-21 06:44:06 +00003529 Ld->getPointerInfo().getWithOffset(4),
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003530 Ld->isVolatile(), Ld->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003531 Ld->isInvariant(), NewAlign);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003532 return;
3533 }
3534
3535 llvm_unreachable("Unknown VFP cmp argument!");
3536}
3537
3538/// OptimizeVFPBrcond - With -enable-unsafe-fp-math, it's legal to optimize some
3539/// f32 and even f64 comparisons to integer ones.
3540SDValue
3541ARMTargetLowering::OptimizeVFPBrcond(SDValue Op, SelectionDAG &DAG) const {
3542 SDValue Chain = Op.getOperand(0);
Evan Cheng10043e22007-01-19 07:51:42 +00003543 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003544 SDValue LHS = Op.getOperand(2);
3545 SDValue RHS = Op.getOperand(3);
3546 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003547 SDLoc dl(Op);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003548
Evan Chengd12af5d2012-03-01 23:27:13 +00003549 bool LHSSeenZero = false;
3550 bool LHSOk = canChangeToInt(LHS, LHSSeenZero, Subtarget);
3551 bool RHSSeenZero = false;
3552 bool RHSOk = canChangeToInt(RHS, RHSSeenZero, Subtarget);
3553 if (LHSOk && RHSOk && (LHSSeenZero || RHSSeenZero)) {
Bob Wilson70bd3632011-03-08 01:17:16 +00003554 // If unsafe fp math optimization is enabled and there are no other uses of
3555 // the CMP operands, and the condition code is EQ or NE, we can optimize it
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003556 // to an integer comparison.
3557 if (CC == ISD::SETOEQ)
3558 CC = ISD::SETEQ;
3559 else if (CC == ISD::SETUNE)
3560 CC = ISD::SETNE;
3561
Evan Chengd12af5d2012-03-01 23:27:13 +00003562 SDValue Mask = DAG.getConstant(0x7fffffff, MVT::i32);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003563 SDValue ARMcc;
3564 if (LHS.getValueType() == MVT::f32) {
Evan Chengd12af5d2012-03-01 23:27:13 +00003565 LHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3566 bitcastf32Toi32(LHS, DAG), Mask);
3567 RHS = DAG.getNode(ISD::AND, dl, MVT::i32,
3568 bitcastf32Toi32(RHS, DAG), Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003569 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
3570 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3571 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
3572 Chain, Dest, ARMcc, CCR, Cmp);
3573 }
3574
3575 SDValue LHS1, LHS2;
3576 SDValue RHS1, RHS2;
3577 expandf64Toi32(LHS, DAG, LHS1, LHS2);
3578 expandf64Toi32(RHS, DAG, RHS1, RHS2);
Evan Chengd12af5d2012-03-01 23:27:13 +00003579 LHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, LHS2, Mask);
3580 RHS2 = DAG.getNode(ISD::AND, dl, MVT::i32, RHS2, Mask);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003581 ARMCC::CondCodes CondCode = IntCCToARMCC(CC);
3582 ARMcc = DAG.getConstant(CondCode, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003583 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003584 SDValue Ops[] = { Chain, ARMcc, LHS1, LHS2, RHS1, RHS2, Dest };
Craig Topper48d114b2014-04-26 18:35:24 +00003585 return DAG.getNode(ARMISD::BCC_i64, dl, VTList, Ops);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003586 }
3587
3588 return SDValue();
3589}
3590
3591SDValue ARMTargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
3592 SDValue Chain = Op.getOperand(0);
3593 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
3594 SDValue LHS = Op.getOperand(2);
3595 SDValue RHS = Op.getOperand(3);
3596 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003597 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003598
Owen Anderson9f944592009-08-11 20:47:22 +00003599 if (LHS.getValueType() == MVT::i32) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003600 SDValue ARMcc;
3601 SDValue Cmp = getARMCmp(LHS, RHS, CC, ARMcc, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003602 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Owen Anderson9f944592009-08-11 20:47:22 +00003603 return DAG.getNode(ARMISD::BRCOND, dl, MVT::Other,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003604 Chain, Dest, ARMcc, CCR, Cmp);
Evan Cheng10043e22007-01-19 07:51:42 +00003605 }
3606
Owen Anderson9f944592009-08-11 20:47:22 +00003607 assert(LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003608
Nick Lewycky50f02cb2011-12-02 22:16:29 +00003609 if (getTargetMachine().Options.UnsafeFPMath &&
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003610 (CC == ISD::SETEQ || CC == ISD::SETOEQ ||
3611 CC == ISD::SETNE || CC == ISD::SETUNE)) {
3612 SDValue Result = OptimizeVFPBrcond(Op, DAG);
3613 if (Result.getNode())
3614 return Result;
3615 }
3616
Evan Cheng10043e22007-01-19 07:51:42 +00003617 ARMCC::CondCodes CondCode, CondCode2;
Bob Wilsona2e83332009-09-09 23:14:54 +00003618 FPCCToARMCC(CC, CondCode, CondCode2);
Bob Wilson7117a912009-03-20 22:42:55 +00003619
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003620 SDValue ARMcc = DAG.getConstant(CondCode, MVT::i32);
3621 SDValue Cmp = getVFPCmp(LHS, RHS, DAG, dl);
Owen Anderson9f944592009-08-11 20:47:22 +00003622 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
Chris Lattner3e5fbd72010-12-21 02:38:05 +00003623 SDVTList VTList = DAG.getVTList(MVT::Other, MVT::Glue);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003624 SDValue Ops[] = { Chain, Dest, ARMcc, CCR, Cmp };
Craig Topper48d114b2014-04-26 18:35:24 +00003625 SDValue Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003626 if (CondCode2 != ARMCC::AL) {
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003627 ARMcc = DAG.getConstant(CondCode2, MVT::i32);
3628 SDValue Ops[] = { Res, Dest, ARMcc, CCR, Res.getValue(1) };
Craig Topper48d114b2014-04-26 18:35:24 +00003629 Res = DAG.getNode(ARMISD::BRCOND, dl, VTList, Ops);
Evan Cheng10043e22007-01-19 07:51:42 +00003630 }
3631 return Res;
3632}
3633
Dan Gohman21cea8a2010-04-17 15:26:15 +00003634SDValue ARMTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003635 SDValue Chain = Op.getOperand(0);
3636 SDValue Table = Op.getOperand(1);
3637 SDValue Index = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003638 SDLoc dl(Op);
Evan Cheng10043e22007-01-19 07:51:42 +00003639
Owen Anderson53aa7a92009-08-10 22:56:29 +00003640 EVT PTy = getPointerTy();
Evan Cheng10043e22007-01-19 07:51:42 +00003641 JumpTableSDNode *JT = cast<JumpTableSDNode>(Table);
3642 ARMFunctionInfo *AFI = DAG.getMachineFunction().getInfo<ARMFunctionInfo>();
Bob Wilson3f17aee2009-07-14 18:44:34 +00003643 SDValue UId = DAG.getConstant(AFI->createJumpTableUId(), PTy);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003644 SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PTy);
Owen Anderson9f944592009-08-11 20:47:22 +00003645 Table = DAG.getNode(ARMISD::WrapperJT, dl, MVT::i32, JTI, UId);
Evan Chengc8bed032009-07-28 20:53:24 +00003646 Index = DAG.getNode(ISD::MUL, dl, PTy, Index, DAG.getConstant(4, PTy));
3647 SDValue Addr = DAG.getNode(ISD::ADD, dl, PTy, Index, Table);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003648 if (Subtarget->isThumb2()) {
3649 // Thumb2 uses a two-level jump. That is, it jumps into the jump table
3650 // which does another jump to the destination. This also makes it easier
3651 // to translate it to TBB / TBH later.
3652 // FIXME: This might not work if the function is extremely large.
Owen Anderson9f944592009-08-11 20:47:22 +00003653 return DAG.getNode(ARMISD::BR2_JT, dl, MVT::Other, Chain,
Evan Chengc6d70ae2009-07-29 02:18:14 +00003654 Addr, Op.getOperand(2), JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003655 }
Evan Chengf3a1fce2009-07-25 00:33:29 +00003656 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003657 Addr = DAG.getLoad((EVT)MVT::i32, dl, Chain, Addr,
Chris Lattner7727d052010-09-21 06:44:06 +00003658 MachinePointerInfo::getJumpTable(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003659 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003660 Chain = Addr.getValue(1);
Dale Johannesen021052a2009-02-04 20:06:27 +00003661 Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, Table);
Owen Anderson9f944592009-08-11 20:47:22 +00003662 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003663 } else {
Evan Chengcdbb70c2009-10-31 03:39:36 +00003664 Addr = DAG.getLoad(PTy, dl, Chain, Addr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00003665 MachinePointerInfo::getJumpTable(),
3666 false, false, false, 0);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003667 Chain = Addr.getValue(1);
Owen Anderson9f944592009-08-11 20:47:22 +00003668 return DAG.getNode(ARMISD::BR_JT, dl, MVT::Other, Chain, Addr, JTI, UId);
Evan Chengf3a1fce2009-07-25 00:33:29 +00003669 }
Evan Cheng10043e22007-01-19 07:51:42 +00003670}
3671
Eli Friedman2d4055b2011-11-09 23:36:02 +00003672static SDValue LowerVectorFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
James Molloy547d4c02012-02-20 09:24:05 +00003673 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003674 SDLoc dl(Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003675
James Molloy547d4c02012-02-20 09:24:05 +00003676 if (Op.getValueType().getVectorElementType() == MVT::i32) {
3677 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::f32)
3678 return Op;
3679 return DAG.UnrollVectorOp(Op.getNode());
3680 }
3681
3682 assert(Op.getOperand(0).getValueType() == MVT::v4f32 &&
3683 "Invalid type for custom lowering!");
3684 if (VT != MVT::v4i16)
3685 return DAG.UnrollVectorOp(Op.getNode());
3686
3687 Op = DAG.getNode(Op.getOpcode(), dl, MVT::v4i32, Op.getOperand(0));
3688 return DAG.getNode(ISD::TRUNCATE, dl, VT, Op);
Eli Friedman2d4055b2011-11-09 23:36:02 +00003689}
3690
Bob Wilsone4191e72010-03-19 22:51:32 +00003691static SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman2d4055b2011-11-09 23:36:02 +00003692 EVT VT = Op.getValueType();
3693 if (VT.isVector())
3694 return LowerVectorFP_TO_INT(Op, DAG);
3695
Andrew Trickef9de2a2013-05-25 02:42:55 +00003696 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003697 unsigned Opc;
3698
3699 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003700 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003701 case ISD::FP_TO_SINT:
3702 Opc = ARMISD::FTOSI;
3703 break;
3704 case ISD::FP_TO_UINT:
3705 Opc = ARMISD::FTOUI;
3706 break;
3707 }
3708 Op = DAG.getNode(Opc, dl, MVT::f32, Op.getOperand(0));
Wesley Peck527da1b2010-11-23 03:31:01 +00003709 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003710}
3711
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003712static SDValue LowerVectorINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3713 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003714 SDLoc dl(Op);
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003715
Eli Friedman2d4055b2011-11-09 23:36:02 +00003716 if (Op.getOperand(0).getValueType().getVectorElementType() == MVT::i32) {
3717 if (VT.getVectorElementType() == MVT::f32)
3718 return Op;
3719 return DAG.UnrollVectorOp(Op.getNode());
3720 }
3721
Duncan Sandsa41634e2011-08-12 14:54:45 +00003722 assert(Op.getOperand(0).getValueType() == MVT::v4i16 &&
3723 "Invalid type for custom lowering!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003724 if (VT != MVT::v4f32)
3725 return DAG.UnrollVectorOp(Op.getNode());
3726
3727 unsigned CastOpc;
3728 unsigned Opc;
3729 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003730 default: llvm_unreachable("Invalid opcode!");
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003731 case ISD::SINT_TO_FP:
3732 CastOpc = ISD::SIGN_EXTEND;
3733 Opc = ISD::SINT_TO_FP;
3734 break;
3735 case ISD::UINT_TO_FP:
3736 CastOpc = ISD::ZERO_EXTEND;
3737 Opc = ISD::UINT_TO_FP;
3738 break;
3739 }
3740
3741 Op = DAG.getNode(CastOpc, dl, MVT::v4i32, Op.getOperand(0));
3742 return DAG.getNode(Opc, dl, VT, Op);
3743}
3744
Bob Wilsone4191e72010-03-19 22:51:32 +00003745static SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
3746 EVT VT = Op.getValueType();
Cameron Zwarich143f9ae2011-03-29 21:41:55 +00003747 if (VT.isVector())
3748 return LowerVectorINT_TO_FP(Op, DAG);
3749
Andrew Trickef9de2a2013-05-25 02:42:55 +00003750 SDLoc dl(Op);
Bob Wilsone4191e72010-03-19 22:51:32 +00003751 unsigned Opc;
3752
3753 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00003754 default: llvm_unreachable("Invalid opcode!");
Bob Wilsone4191e72010-03-19 22:51:32 +00003755 case ISD::SINT_TO_FP:
3756 Opc = ARMISD::SITOF;
3757 break;
3758 case ISD::UINT_TO_FP:
3759 Opc = ARMISD::UITOF;
3760 break;
3761 }
3762
Wesley Peck527da1b2010-11-23 03:31:01 +00003763 Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Bob Wilsone4191e72010-03-19 22:51:32 +00003764 return DAG.getNode(Opc, dl, VT, Op);
3765}
3766
Evan Cheng25f93642010-07-08 02:08:50 +00003767SDValue ARMTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00003768 // Implement fcopysign with a fabs and a conditional fneg.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003769 SDValue Tmp0 = Op.getOperand(0);
3770 SDValue Tmp1 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003771 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003772 EVT VT = Op.getValueType();
3773 EVT SrcVT = Tmp1.getValueType();
Evan Chengd6b641e2011-02-23 02:24:55 +00003774 bool InGPR = Tmp0.getOpcode() == ISD::BITCAST ||
3775 Tmp0.getOpcode() == ARMISD::VMOVDRR;
3776 bool UseNEON = !InGPR && Subtarget->hasNEON();
3777
3778 if (UseNEON) {
3779 // Use VBSL to copy the sign bit.
3780 unsigned EncodedVal = ARM_AM::createNEONModImm(0x6, 0x80);
3781 SDValue Mask = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v2i32,
3782 DAG.getTargetConstant(EncodedVal, MVT::i32));
3783 EVT OpVT = (VT == MVT::f32) ? MVT::v2i32 : MVT::v1i64;
3784 if (VT == MVT::f64)
3785 Mask = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3786 DAG.getNode(ISD::BITCAST, dl, OpVT, Mask),
3787 DAG.getConstant(32, MVT::i32));
3788 else /*if (VT == MVT::f32)*/
3789 Tmp0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp0);
3790 if (SrcVT == MVT::f32) {
3791 Tmp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f32, Tmp1);
3792 if (VT == MVT::f64)
3793 Tmp1 = DAG.getNode(ARMISD::VSHL, dl, OpVT,
3794 DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1),
3795 DAG.getConstant(32, MVT::i32));
Evan Cheng12bb05b2011-04-15 01:31:00 +00003796 } else if (VT == MVT::f32)
3797 Tmp1 = DAG.getNode(ARMISD::VSHRu, dl, MVT::v1i64,
3798 DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, Tmp1),
3799 DAG.getConstant(32, MVT::i32));
Evan Chengd6b641e2011-02-23 02:24:55 +00003800 Tmp0 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp0);
3801 Tmp1 = DAG.getNode(ISD::BITCAST, dl, OpVT, Tmp1);
3802
3803 SDValue AllOnes = DAG.getTargetConstant(ARM_AM::createNEONModImm(0xe, 0xff),
3804 MVT::i32);
3805 AllOnes = DAG.getNode(ARMISD::VMOVIMM, dl, MVT::v8i8, AllOnes);
3806 SDValue MaskNot = DAG.getNode(ISD::XOR, dl, OpVT, Mask,
3807 DAG.getNode(ISD::BITCAST, dl, OpVT, AllOnes));
Owen Anderson77aa2662011-04-05 21:48:57 +00003808
Evan Chengd6b641e2011-02-23 02:24:55 +00003809 SDValue Res = DAG.getNode(ISD::OR, dl, OpVT,
3810 DAG.getNode(ISD::AND, dl, OpVT, Tmp1, Mask),
3811 DAG.getNode(ISD::AND, dl, OpVT, Tmp0, MaskNot));
Evan Cheng6e3d4432011-02-28 18:45:27 +00003812 if (VT == MVT::f32) {
Evan Chengd6b641e2011-02-23 02:24:55 +00003813 Res = DAG.getNode(ISD::BITCAST, dl, MVT::v2f32, Res);
3814 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res,
3815 DAG.getConstant(0, MVT::i32));
3816 } else {
3817 Res = DAG.getNode(ISD::BITCAST, dl, MVT::f64, Res);
3818 }
3819
3820 return Res;
3821 }
Evan Cheng2da1c952011-02-11 02:28:55 +00003822
3823 // Bitcast operand 1 to i32.
3824 if (SrcVT == MVT::f64)
3825 Tmp1 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00003826 Tmp1).getValue(1);
Evan Cheng2da1c952011-02-11 02:28:55 +00003827 Tmp1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp1);
3828
Evan Chengd6b641e2011-02-23 02:24:55 +00003829 // Or in the signbit with integer operations.
3830 SDValue Mask1 = DAG.getConstant(0x80000000, MVT::i32);
3831 SDValue Mask2 = DAG.getConstant(0x7fffffff, MVT::i32);
3832 Tmp1 = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp1, Mask1);
3833 if (VT == MVT::f32) {
3834 Tmp0 = DAG.getNode(ISD::AND, dl, MVT::i32,
3835 DAG.getNode(ISD::BITCAST, dl, MVT::i32, Tmp0), Mask2);
3836 return DAG.getNode(ISD::BITCAST, dl, MVT::f32,
3837 DAG.getNode(ISD::OR, dl, MVT::i32, Tmp0, Tmp1));
Evan Cheng2da1c952011-02-11 02:28:55 +00003838 }
3839
Evan Chengd6b641e2011-02-23 02:24:55 +00003840 // f64: Or the high part with signbit and then combine two parts.
3841 Tmp0 = DAG.getNode(ARMISD::VMOVRRD, dl, DAG.getVTList(MVT::i32, MVT::i32),
Craig Topper48d114b2014-04-26 18:35:24 +00003842 Tmp0);
Evan Chengd6b641e2011-02-23 02:24:55 +00003843 SDValue Lo = Tmp0.getValue(0);
3844 SDValue Hi = DAG.getNode(ISD::AND, dl, MVT::i32, Tmp0.getValue(1), Mask2);
3845 Hi = DAG.getNode(ISD::OR, dl, MVT::i32, Hi, Tmp1);
3846 return DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi);
Evan Cheng10043e22007-01-19 07:51:42 +00003847}
3848
Evan Cheng168ced92010-05-22 01:47:14 +00003849SDValue ARMTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const{
3850 MachineFunction &MF = DAG.getMachineFunction();
3851 MachineFrameInfo *MFI = MF.getFrameInfo();
3852 MFI->setReturnAddressIsTaken(true);
3853
Bill Wendling908bf812014-01-06 00:43:20 +00003854 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003855 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00003856
Evan Cheng168ced92010-05-22 01:47:14 +00003857 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003858 SDLoc dl(Op);
Evan Cheng168ced92010-05-22 01:47:14 +00003859 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3860 if (Depth) {
3861 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
3862 SDValue Offset = DAG.getConstant(4, MVT::i32);
3863 return DAG.getLoad(VT, dl, DAG.getEntryNode(),
3864 DAG.getNode(ISD::ADD, dl, VT, FrameAddr, Offset),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003865 MachinePointerInfo(), false, false, false, 0);
Evan Cheng168ced92010-05-22 01:47:14 +00003866 }
3867
3868 // Return LR, which contains the return address. Mark it an implicit live-in.
Devang Patelf3292b22011-02-21 23:21:26 +00003869 unsigned Reg = MF.addLiveIn(ARM::LR, getRegClassFor(MVT::i32));
Evan Cheng168ced92010-05-22 01:47:14 +00003870 return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
3871}
3872
Dan Gohman21cea8a2010-04-17 15:26:15 +00003873SDValue ARMTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00003874 const ARMBaseRegisterInfo &ARI =
3875 *static_cast<const ARMBaseRegisterInfo*>(RegInfo);
3876 MachineFunction &MF = DAG.getMachineFunction();
3877 MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003878 MFI->setFrameAddressIsTaken(true);
Evan Cheng168ced92010-05-22 01:47:14 +00003879
Owen Anderson53aa7a92009-08-10 22:56:29 +00003880 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003881 SDLoc dl(Op); // FIXME probably not meaningful
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003882 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Saleem Abdulrasoolf11f4b42014-05-18 03:18:09 +00003883 unsigned FrameReg = ARI.getFrameRegister(MF);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003884 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
3885 while (Depth--)
Chris Lattner7727d052010-09-21 06:44:06 +00003886 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
3887 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00003888 false, false, false, 0);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00003889 return FrameAddr;
3890}
3891
Renato Golinc7aea402014-05-06 16:51:25 +00003892// FIXME? Maybe this could be a TableGen attribute on some registers and
3893// this table could be generated automatically from RegInfo.
Hal Finkelf0e086a2014-05-11 19:29:07 +00003894unsigned ARMTargetLowering::getRegisterByName(const char* RegName,
3895 EVT VT) const {
Renato Golinc7aea402014-05-06 16:51:25 +00003896 unsigned Reg = StringSwitch<unsigned>(RegName)
3897 .Case("sp", ARM::SP)
3898 .Default(0);
3899 if (Reg)
3900 return Reg;
3901 report_fatal_error("Invalid register name global variable");
3902}
3903
Wesley Peck527da1b2010-11-23 03:31:01 +00003904/// ExpandBITCAST - If the target supports VFP, this function is called to
Bob Wilson59b70ea2010-04-17 05:30:19 +00003905/// expand a bit convert where either the source or destination type is i64 to
3906/// use a VMOVDRR or VMOVRRD node. This should not be done when the non-i64
3907/// operand type is illegal (e.g., v2f32 for a target that doesn't support
3908/// vectors), since the legalizer won't know what to do with that.
Wesley Peck527da1b2010-11-23 03:31:01 +00003909static SDValue ExpandBITCAST(SDNode *N, SelectionDAG &DAG) {
Bob Wilson59b70ea2010-04-17 05:30:19 +00003910 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003911 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003912 SDValue Op = N->getOperand(0);
Bob Wilsonc05b8872010-04-14 20:45:23 +00003913
Bob Wilson59b70ea2010-04-17 05:30:19 +00003914 // This function is only supposed to be called for i64 types, either as the
3915 // source or destination of the bit convert.
3916 EVT SrcVT = Op.getValueType();
3917 EVT DstVT = N->getValueType(0);
3918 assert((SrcVT == MVT::i64 || DstVT == MVT::i64) &&
Wesley Peck527da1b2010-11-23 03:31:01 +00003919 "ExpandBITCAST called for non-i64 type");
Bob Wilsonc05b8872010-04-14 20:45:23 +00003920
Bob Wilson59b70ea2010-04-17 05:30:19 +00003921 // Turn i64->f64 into VMOVDRR.
3922 if (SrcVT == MVT::i64 && TLI.isTypeLegal(DstVT)) {
Owen Anderson9f944592009-08-11 20:47:22 +00003923 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3924 DAG.getConstant(0, MVT::i32));
3925 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Op,
3926 DAG.getConstant(1, MVT::i32));
Wesley Peck527da1b2010-11-23 03:31:01 +00003927 return DAG.getNode(ISD::BITCAST, dl, DstVT,
Bob Wilsonf07d33d2010-06-11 22:45:25 +00003928 DAG.getNode(ARMISD::VMOVDRR, dl, MVT::f64, Lo, Hi));
Evan Cheng297b32a2008-11-04 19:57:48 +00003929 }
Bob Wilson7117a912009-03-20 22:42:55 +00003930
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00003931 // Turn f64->i64 into VMOVRRD.
Bob Wilson59b70ea2010-04-17 05:30:19 +00003932 if (DstVT == MVT::i64 && TLI.isTypeLegal(SrcVT)) {
Christian Pirker238c7c12014-05-12 11:19:20 +00003933 SDValue Cvt;
Christian Pirker6692e7c2014-05-14 16:59:44 +00003934 if (TLI.isBigEndian() && SrcVT.isVector() &&
3935 SrcVT.getVectorNumElements() > 1)
Christian Pirker238c7c12014-05-12 11:19:20 +00003936 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3937 DAG.getVTList(MVT::i32, MVT::i32),
3938 DAG.getNode(ARMISD::VREV64, dl, SrcVT, Op));
3939 else
3940 Cvt = DAG.getNode(ARMISD::VMOVRRD, dl,
3941 DAG.getVTList(MVT::i32, MVT::i32), Op);
Bob Wilson59b70ea2010-04-17 05:30:19 +00003942 // Merge the pieces into a single i64 value.
3943 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Cvt, Cvt.getValue(1));
3944 }
Bob Wilson7117a912009-03-20 22:42:55 +00003945
Bob Wilson59b70ea2010-04-17 05:30:19 +00003946 return SDValue();
Chris Lattnerf81d5882007-11-24 07:07:01 +00003947}
3948
Bob Wilson2e076c42009-06-22 23:27:02 +00003949/// getZeroVector - Returns a vector of specified type with all zero elements.
Bob Wilsona3f19012010-07-13 21:16:48 +00003950/// Zero vectors are used to represent vector negation and in those cases
3951/// will be implemented with the NEON VNEG instruction. However, VNEG does
3952/// not support i64 elements, so sometimes the zero vectors will need to be
3953/// explicitly constructed. Regardless, use a canonical VMOV to create the
3954/// zero vector.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003955static SDValue getZeroVector(EVT VT, SelectionDAG &DAG, SDLoc dl) {
Bob Wilson2e076c42009-06-22 23:27:02 +00003956 assert(VT.isVector() && "Expected a vector type");
Bob Wilsona3f19012010-07-13 21:16:48 +00003957 // The canonical modified immediate encoding of a zero vector is....0!
3958 SDValue EncodedVal = DAG.getTargetConstant(0, MVT::i32);
3959 EVT VmovVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
3960 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, EncodedVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00003961 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilson2e076c42009-06-22 23:27:02 +00003962}
3963
Jim Grosbach624fcb22009-10-31 21:00:56 +00003964/// LowerShiftRightParts - Lower SRA_PARTS, which returns two
3965/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00003966SDValue ARMTargetLowering::LowerShiftRightParts(SDValue Op,
3967 SelectionDAG &DAG) const {
Jim Grosbach624fcb22009-10-31 21:00:56 +00003968 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
3969 EVT VT = Op.getValueType();
3970 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003971 SDLoc dl(Op);
Jim Grosbach624fcb22009-10-31 21:00:56 +00003972 SDValue ShOpLo = Op.getOperand(0);
3973 SDValue ShOpHi = Op.getOperand(1);
3974 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003975 SDValue ARMcc;
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003976 unsigned Opc = (Op.getOpcode() == ISD::SRA_PARTS) ? ISD::SRA : ISD::SRL;
Jim Grosbach624fcb22009-10-31 21:00:56 +00003977
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003978 assert(Op.getOpcode() == ISD::SRA_PARTS || Op.getOpcode() == ISD::SRL_PARTS);
3979
Jim Grosbach624fcb22009-10-31 21:00:56 +00003980 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
3981 DAG.getConstant(VTBits, MVT::i32), ShAmt);
3982 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, ShAmt);
3983 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
3984 DAG.getConstant(VTBits, MVT::i32));
3985 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, RevShAmt);
3986 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003987 SDValue TrueVal = DAG.getNode(Opc, dl, VT, ShOpHi, ExtraShAmt);
Jim Grosbach624fcb22009-10-31 21:00:56 +00003988
3989 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
3990 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003991 ARMcc, DAG, dl);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00003992 SDValue Hi = DAG.getNode(Opc, dl, VT, ShOpHi, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00003993 SDValue Lo = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, TrueVal, ARMcc,
Jim Grosbach624fcb22009-10-31 21:00:56 +00003994 CCR, Cmp);
3995
3996 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00003997 return DAG.getMergeValues(Ops, dl);
Jim Grosbach624fcb22009-10-31 21:00:56 +00003998}
3999
Jim Grosbach5d994042009-10-31 19:38:01 +00004000/// LowerShiftLeftParts - Lower SHL_PARTS, which returns two
4001/// i32 values and take a 2 x i32 value to shift plus a shift amount.
Dan Gohman21cea8a2010-04-17 15:26:15 +00004002SDValue ARMTargetLowering::LowerShiftLeftParts(SDValue Op,
4003 SelectionDAG &DAG) const {
Jim Grosbach5d994042009-10-31 19:38:01 +00004004 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4005 EVT VT = Op.getValueType();
4006 unsigned VTBits = VT.getSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004007 SDLoc dl(Op);
Jim Grosbach5d994042009-10-31 19:38:01 +00004008 SDValue ShOpLo = Op.getOperand(0);
4009 SDValue ShOpHi = Op.getOperand(1);
4010 SDValue ShAmt = Op.getOperand(2);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004011 SDValue ARMcc;
Jim Grosbach5d994042009-10-31 19:38:01 +00004012
4013 assert(Op.getOpcode() == ISD::SHL_PARTS);
4014 SDValue RevShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32,
4015 DAG.getConstant(VTBits, MVT::i32), ShAmt);
4016 SDValue Tmp1 = DAG.getNode(ISD::SRL, dl, VT, ShOpLo, RevShAmt);
4017 SDValue ExtraShAmt = DAG.getNode(ISD::SUB, dl, MVT::i32, ShAmt,
4018 DAG.getConstant(VTBits, MVT::i32));
4019 SDValue Tmp2 = DAG.getNode(ISD::SHL, dl, VT, ShOpHi, ShAmt);
4020 SDValue Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ExtraShAmt);
4021
4022 SDValue FalseVal = DAG.getNode(ISD::OR, dl, VT, Tmp1, Tmp2);
4023 SDValue CCR = DAG.getRegister(ARM::CPSR, MVT::i32);
4024 SDValue Cmp = getARMCmp(ExtraShAmt, DAG.getConstant(0, MVT::i32), ISD::SETGE,
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004025 ARMcc, DAG, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004026 SDValue Lo = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00004027 SDValue Hi = DAG.getNode(ARMISD::CMOV, dl, VT, FalseVal, Tmp3, ARMcc,
Jim Grosbach5d994042009-10-31 19:38:01 +00004028 CCR, Cmp);
4029
4030 SDValue Ops[2] = { Lo, Hi };
Craig Topper64941d92014-04-27 19:20:57 +00004031 return DAG.getMergeValues(Ops, dl);
Jim Grosbach5d994042009-10-31 19:38:01 +00004032}
4033
Jim Grosbach535d3b42010-09-08 03:54:02 +00004034SDValue ARMTargetLowering::LowerFLT_ROUNDS_(SDValue Op,
Nate Begemanb69b1822010-08-03 21:31:55 +00004035 SelectionDAG &DAG) const {
4036 // The rounding mode is in bits 23:22 of the FPSCR.
4037 // The ARM rounding mode value to FLT_ROUNDS mapping is 0->1, 1->2, 2->3, 3->0
4038 // The formula we use to implement this is (((FPSCR + 1 << 22) >> 22) & 3)
4039 // so that the shift + and get folded into a bitfield extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004040 SDLoc dl(Op);
Nate Begemanb69b1822010-08-03 21:31:55 +00004041 SDValue FPSCR = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::i32,
4042 DAG.getConstant(Intrinsic::arm_get_fpscr,
4043 MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004044 SDValue FltRounds = DAG.getNode(ISD::ADD, dl, MVT::i32, FPSCR,
Nate Begemanb69b1822010-08-03 21:31:55 +00004045 DAG.getConstant(1U << 22, MVT::i32));
4046 SDValue RMODE = DAG.getNode(ISD::SRL, dl, MVT::i32, FltRounds,
4047 DAG.getConstant(22, MVT::i32));
Jim Grosbach535d3b42010-09-08 03:54:02 +00004048 return DAG.getNode(ISD::AND, dl, MVT::i32, RMODE,
Nate Begemanb69b1822010-08-03 21:31:55 +00004049 DAG.getConstant(3, MVT::i32));
4050}
4051
Jim Grosbach8546ec92010-01-18 19:58:49 +00004052static SDValue LowerCTTZ(SDNode *N, SelectionDAG &DAG,
4053 const ARMSubtarget *ST) {
4054 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004055 SDLoc dl(N);
Jim Grosbach8546ec92010-01-18 19:58:49 +00004056
4057 if (!ST->hasV6T2Ops())
4058 return SDValue();
4059
4060 SDValue rbit = DAG.getNode(ARMISD::RBIT, dl, VT, N->getOperand(0));
4061 return DAG.getNode(ISD::CTLZ, dl, VT, rbit);
4062}
4063
Evan Chengb4eae132012-12-04 22:41:50 +00004064/// getCTPOP16BitCounts - Returns a v8i8/v16i8 vector containing the bit-count
4065/// for each 16-bit element from operand, repeated. The basic idea is to
4066/// leverage vcnt to get the 8-bit counts, gather and add the results.
4067///
4068/// Trace for v4i16:
4069/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4070/// cast: N0 = [w0 w1 w2 w3 w4 w5 w6 w7] (v0 = [w0 w1], wi 8-bit element)
4071/// 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 +00004072/// vrev: N2 = [b1 b0 b3 b2 b5 b4 b7 b6]
Evan Chengb4eae132012-12-04 22:41:50 +00004073/// [b0 b1 b2 b3 b4 b5 b6 b7]
4074/// +[b1 b0 b3 b2 b5 b4 b7 b6]
4075/// N3=N1+N2 = [k0 k0 k1 k1 k2 k2 k3 k3] (k0 = b0+b1 = bit-count of 16-bit v0,
4076/// vuzp: = [k0 k1 k2 k3 k0 k1 k2 k3] each ki is 8-bits)
4077static SDValue getCTPOP16BitCounts(SDNode *N, SelectionDAG &DAG) {
4078 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004079 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004080
4081 EVT VT8Bit = VT.is64BitVector() ? MVT::v8i8 : MVT::v16i8;
4082 SDValue N0 = DAG.getNode(ISD::BITCAST, DL, VT8Bit, N->getOperand(0));
4083 SDValue N1 = DAG.getNode(ISD::CTPOP, DL, VT8Bit, N0);
4084 SDValue N2 = DAG.getNode(ARMISD::VREV16, DL, VT8Bit, N1);
4085 SDValue N3 = DAG.getNode(ISD::ADD, DL, VT8Bit, N1, N2);
4086 return DAG.getNode(ARMISD::VUZP, DL, VT8Bit, N3, N3);
4087}
4088
4089/// lowerCTPOP16BitElements - Returns a v4i16/v8i16 vector containing the
4090/// bit-count for each 16-bit element from the operand. We need slightly
4091/// different sequencing for v4i16 and v8i16 to stay within NEON's available
4092/// 64/128-bit registers.
Jim Grosbach54efea02013-03-02 20:16:15 +00004093///
Evan Chengb4eae132012-12-04 22:41:50 +00004094/// Trace for v4i16:
4095/// input = [v0 v1 v2 v3 ] (vi 16-bit element)
4096/// v8i8: BitCounts = [k0 k1 k2 k3 k0 k1 k2 k3 ] (ki is the bit-count of vi)
4097/// v8i16:Extended = [k0 k1 k2 k3 k0 k1 k2 k3 ]
4098/// v4i16:Extracted = [k0 k1 k2 k3 ]
4099static SDValue lowerCTPOP16BitElements(SDNode *N, SelectionDAG &DAG) {
4100 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004101 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004102
4103 SDValue BitCounts = getCTPOP16BitCounts(N, DAG);
4104 if (VT.is64BitVector()) {
4105 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, BitCounts);
4106 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, Extended,
4107 DAG.getIntPtrConstant(0));
4108 } else {
4109 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i8,
4110 BitCounts, DAG.getIntPtrConstant(0));
4111 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v8i16, Extracted);
4112 }
4113}
4114
4115/// lowerCTPOP32BitElements - Returns a v2i32/v4i32 vector containing the
4116/// bit-count for each 32-bit element from the operand. The idea here is
4117/// to split the vector into 16-bit elements, leverage the 16-bit count
4118/// routine, and then combine the results.
4119///
4120/// Trace for v2i32 (v4i32 similar with Extracted/Extended exchanged):
4121/// input = [v0 v1 ] (vi: 32-bit elements)
4122/// Bitcast = [w0 w1 w2 w3 ] (wi: 16-bit elements, v0 = [w0 w1])
4123/// Counts16 = [k0 k1 k2 k3 ] (ki: 16-bit elements, bit-count of wi)
Jim Grosbach54efea02013-03-02 20:16:15 +00004124/// vrev: N0 = [k1 k0 k3 k2 ]
Evan Chengb4eae132012-12-04 22:41:50 +00004125/// [k0 k1 k2 k3 ]
4126/// N1 =+[k1 k0 k3 k2 ]
4127/// [k0 k2 k1 k3 ]
4128/// N2 =+[k1 k3 k0 k2 ]
4129/// [k0 k2 k1 k3 ]
4130/// Extended =+[k1 k3 k0 k2 ]
4131/// [k0 k2 ]
4132/// Extracted=+[k1 k3 ]
4133///
4134static SDValue lowerCTPOP32BitElements(SDNode *N, SelectionDAG &DAG) {
4135 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004136 SDLoc DL(N);
Evan Chengb4eae132012-12-04 22:41:50 +00004137
4138 EVT VT16Bit = VT.is64BitVector() ? MVT::v4i16 : MVT::v8i16;
4139
4140 SDValue Bitcast = DAG.getNode(ISD::BITCAST, DL, VT16Bit, N->getOperand(0));
4141 SDValue Counts16 = lowerCTPOP16BitElements(Bitcast.getNode(), DAG);
4142 SDValue N0 = DAG.getNode(ARMISD::VREV32, DL, VT16Bit, Counts16);
4143 SDValue N1 = DAG.getNode(ISD::ADD, DL, VT16Bit, Counts16, N0);
4144 SDValue N2 = DAG.getNode(ARMISD::VUZP, DL, VT16Bit, N1, N1);
4145
4146 if (VT.is64BitVector()) {
4147 SDValue Extended = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, N2);
4148 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i32, Extended,
4149 DAG.getIntPtrConstant(0));
4150 } else {
4151 SDValue Extracted = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i16, N2,
4152 DAG.getIntPtrConstant(0));
4153 return DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i32, Extracted);
4154 }
4155}
4156
4157static SDValue LowerCTPOP(SDNode *N, SelectionDAG &DAG,
4158 const ARMSubtarget *ST) {
4159 EVT VT = N->getValueType(0);
4160
4161 assert(ST->hasNEON() && "Custom ctpop lowering requires NEON.");
Matt Beaumont-Gay50f61b62012-12-04 23:54:02 +00004162 assert((VT == MVT::v2i32 || VT == MVT::v4i32 ||
4163 VT == MVT::v4i16 || VT == MVT::v8i16) &&
Evan Chengb4eae132012-12-04 22:41:50 +00004164 "Unexpected type for custom ctpop lowering");
4165
4166 if (VT.getVectorElementType() == MVT::i32)
4167 return lowerCTPOP32BitElements(N, DAG);
4168 else
4169 return lowerCTPOP16BitElements(N, DAG);
4170}
4171
Bob Wilson2e076c42009-06-22 23:27:02 +00004172static SDValue LowerShift(SDNode *N, SelectionDAG &DAG,
4173 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004174 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004175 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004176
Bob Wilson7d471332010-11-18 21:16:28 +00004177 if (!VT.isVector())
4178 return SDValue();
4179
Bob Wilson2e076c42009-06-22 23:27:02 +00004180 // Lower vector shifts on NEON to use VSHL.
Bob Wilson7d471332010-11-18 21:16:28 +00004181 assert(ST->hasNEON() && "unexpected vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00004182
Bob Wilson7d471332010-11-18 21:16:28 +00004183 // Left shifts translate directly to the vshiftu intrinsic.
4184 if (N->getOpcode() == ISD::SHL)
Bob Wilson2e076c42009-06-22 23:27:02 +00004185 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Bob Wilson7d471332010-11-18 21:16:28 +00004186 DAG.getConstant(Intrinsic::arm_neon_vshiftu, MVT::i32),
4187 N->getOperand(0), N->getOperand(1));
4188
4189 assert((N->getOpcode() == ISD::SRA ||
4190 N->getOpcode() == ISD::SRL) && "unexpected vector shift opcode");
4191
4192 // NEON uses the same intrinsics for both left and right shifts. For
4193 // right shifts, the shift amounts are negative, so negate the vector of
4194 // shift amounts.
4195 EVT ShiftVT = N->getOperand(1).getValueType();
4196 SDValue NegatedCount = DAG.getNode(ISD::SUB, dl, ShiftVT,
4197 getZeroVector(ShiftVT, DAG, dl),
4198 N->getOperand(1));
4199 Intrinsic::ID vshiftInt = (N->getOpcode() == ISD::SRA ?
4200 Intrinsic::arm_neon_vshifts :
4201 Intrinsic::arm_neon_vshiftu);
4202 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
4203 DAG.getConstant(vshiftInt, MVT::i32),
4204 N->getOperand(0), NegatedCount);
4205}
4206
4207static SDValue Expand64BitShift(SDNode *N, SelectionDAG &DAG,
4208 const ARMSubtarget *ST) {
4209 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004210 SDLoc dl(N);
Bob Wilson2e076c42009-06-22 23:27:02 +00004211
Eli Friedman682d8c12009-08-22 03:13:10 +00004212 // We can get here for a node like i32 = ISD::SHL i32, i64
4213 if (VT != MVT::i64)
4214 return SDValue();
4215
4216 assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&
Chris Lattnerf81d5882007-11-24 07:07:01 +00004217 "Unknown shift to lower!");
Duncan Sands6ed40142008-12-01 11:39:25 +00004218
Chris Lattnerf81d5882007-11-24 07:07:01 +00004219 // We only lower SRA, SRL of 1 here, all others use generic lowering.
4220 if (!isa<ConstantSDNode>(N->getOperand(1)) ||
Dan Gohmaneffb8942008-09-12 16:56:44 +00004221 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue() != 1)
Duncan Sands6ed40142008-12-01 11:39:25 +00004222 return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004223
Chris Lattnerf81d5882007-11-24 07:07:01 +00004224 // If we are in thumb mode, we don't have RRX.
David Goodwin22c2fba2009-07-08 23:10:31 +00004225 if (ST->isThumb1Only()) return SDValue();
Bob Wilson7117a912009-03-20 22:42:55 +00004226
Chris Lattnerf81d5882007-11-24 07:07:01 +00004227 // Okay, we have a 64-bit SRA or SRL of 1. Lower this to an RRX expr.
Owen Anderson9f944592009-08-11 20:47:22 +00004228 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004229 DAG.getConstant(0, MVT::i32));
Owen Anderson9f944592009-08-11 20:47:22 +00004230 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(0),
Bob Wilson26fdebc2010-05-25 03:36:52 +00004231 DAG.getConstant(1, MVT::i32));
Bob Wilson7117a912009-03-20 22:42:55 +00004232
Chris Lattnerf81d5882007-11-24 07:07:01 +00004233 // First, build a SRA_FLAG/SRL_FLAG op, which shifts the top part by one and
4234 // captures the result into a carry flag.
4235 unsigned Opc = N->getOpcode() == ISD::SRL ? ARMISD::SRL_FLAG:ARMISD::SRA_FLAG;
Craig Topper48d114b2014-04-26 18:35:24 +00004236 Hi = DAG.getNode(Opc, dl, DAG.getVTList(MVT::i32, MVT::Glue), Hi);
Bob Wilson7117a912009-03-20 22:42:55 +00004237
Chris Lattnerf81d5882007-11-24 07:07:01 +00004238 // The low part is an ARMISD::RRX operand, which shifts the carry in.
Owen Anderson9f944592009-08-11 20:47:22 +00004239 Lo = DAG.getNode(ARMISD::RRX, dl, MVT::i32, Lo, Hi.getValue(1));
Bob Wilson7117a912009-03-20 22:42:55 +00004240
Chris Lattnerf81d5882007-11-24 07:07:01 +00004241 // Merge the pieces into a single i64 value.
Owen Anderson9f944592009-08-11 20:47:22 +00004242 return DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi);
Chris Lattnerf81d5882007-11-24 07:07:01 +00004243}
4244
Bob Wilson2e076c42009-06-22 23:27:02 +00004245static SDValue LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4246 SDValue TmpOp0, TmpOp1;
4247 bool Invert = false;
4248 bool Swap = false;
4249 unsigned Opc = 0;
4250
4251 SDValue Op0 = Op.getOperand(0);
4252 SDValue Op1 = Op.getOperand(1);
4253 SDValue CC = Op.getOperand(2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004254 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004255 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004256 SDLoc dl(Op);
Bob Wilson2e076c42009-06-22 23:27:02 +00004257
4258 if (Op.getOperand(1).getValueType().isFloatingPoint()) {
4259 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004260 default: llvm_unreachable("Illegal FP comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004261 case ISD::SETUNE:
4262 case ISD::SETNE: Invert = true; // Fallthrough
4263 case ISD::SETOEQ:
4264 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4265 case ISD::SETOLT:
4266 case ISD::SETLT: Swap = true; // Fallthrough
4267 case ISD::SETOGT:
4268 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4269 case ISD::SETOLE:
4270 case ISD::SETLE: Swap = true; // Fallthrough
4271 case ISD::SETOGE:
4272 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4273 case ISD::SETUGE: Swap = true; // Fallthrough
4274 case ISD::SETULE: Invert = true; Opc = ARMISD::VCGT; break;
4275 case ISD::SETUGT: Swap = true; // Fallthrough
4276 case ISD::SETULT: Invert = true; Opc = ARMISD::VCGE; break;
4277 case ISD::SETUEQ: Invert = true; // Fallthrough
4278 case ISD::SETONE:
4279 // Expand this to (OLT | OGT).
4280 TmpOp0 = Op0;
4281 TmpOp1 = Op1;
4282 Opc = ISD::OR;
4283 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4284 Op1 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp0, TmpOp1);
4285 break;
4286 case ISD::SETUO: Invert = true; // Fallthrough
4287 case ISD::SETO:
4288 // Expand this to (OLT | OGE).
4289 TmpOp0 = Op0;
4290 TmpOp1 = Op1;
4291 Opc = ISD::OR;
4292 Op0 = DAG.getNode(ARMISD::VCGT, dl, VT, TmpOp1, TmpOp0);
4293 Op1 = DAG.getNode(ARMISD::VCGE, dl, VT, TmpOp0, TmpOp1);
4294 break;
4295 }
4296 } else {
4297 // Integer comparisons.
4298 switch (SetCCOpcode) {
David Blaikie46a9f012012-01-20 21:51:11 +00004299 default: llvm_unreachable("Illegal integer comparison");
Bob Wilson2e076c42009-06-22 23:27:02 +00004300 case ISD::SETNE: Invert = true;
4301 case ISD::SETEQ: Opc = ARMISD::VCEQ; break;
4302 case ISD::SETLT: Swap = true;
4303 case ISD::SETGT: Opc = ARMISD::VCGT; break;
4304 case ISD::SETLE: Swap = true;
4305 case ISD::SETGE: Opc = ARMISD::VCGE; break;
4306 case ISD::SETULT: Swap = true;
4307 case ISD::SETUGT: Opc = ARMISD::VCGTU; break;
4308 case ISD::SETULE: Swap = true;
4309 case ISD::SETUGE: Opc = ARMISD::VCGEU; break;
4310 }
4311
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004312 // Detect VTST (Vector Test Bits) = icmp ne (and (op0, op1), zero).
Bob Wilson2e076c42009-06-22 23:27:02 +00004313 if (Opc == ARMISD::VCEQ) {
4314
4315 SDValue AndOp;
4316 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4317 AndOp = Op0;
4318 else if (ISD::isBuildVectorAllZeros(Op0.getNode()))
4319 AndOp = Op1;
4320
4321 // Ignore bitconvert.
Wesley Peck527da1b2010-11-23 03:31:01 +00004322 if (AndOp.getNode() && AndOp.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00004323 AndOp = AndOp.getOperand(0);
4324
4325 if (AndOp.getNode() && AndOp.getOpcode() == ISD::AND) {
4326 Opc = ARMISD::VTST;
Wesley Peck527da1b2010-11-23 03:31:01 +00004327 Op0 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(0));
4328 Op1 = DAG.getNode(ISD::BITCAST, dl, VT, AndOp.getOperand(1));
Bob Wilson2e076c42009-06-22 23:27:02 +00004329 Invert = !Invert;
4330 }
4331 }
4332 }
4333
4334 if (Swap)
4335 std::swap(Op0, Op1);
4336
Owen Andersonc7baee32010-11-08 23:21:22 +00004337 // If one of the operands is a constant vector zero, attempt to fold the
4338 // comparison to a specialized compare-against-zero form.
4339 SDValue SingleOp;
4340 if (ISD::isBuildVectorAllZeros(Op1.getNode()))
4341 SingleOp = Op0;
4342 else if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
4343 if (Opc == ARMISD::VCGE)
4344 Opc = ARMISD::VCLEZ;
4345 else if (Opc == ARMISD::VCGT)
4346 Opc = ARMISD::VCLTZ;
4347 SingleOp = Op1;
4348 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004349
Owen Andersonc7baee32010-11-08 23:21:22 +00004350 SDValue Result;
4351 if (SingleOp.getNode()) {
4352 switch (Opc) {
4353 case ARMISD::VCEQ:
4354 Result = DAG.getNode(ARMISD::VCEQZ, dl, VT, SingleOp); break;
4355 case ARMISD::VCGE:
4356 Result = DAG.getNode(ARMISD::VCGEZ, dl, VT, SingleOp); break;
4357 case ARMISD::VCLEZ:
4358 Result = DAG.getNode(ARMISD::VCLEZ, dl, VT, SingleOp); break;
4359 case ARMISD::VCGT:
4360 Result = DAG.getNode(ARMISD::VCGTZ, dl, VT, SingleOp); break;
4361 case ARMISD::VCLTZ:
4362 Result = DAG.getNode(ARMISD::VCLTZ, dl, VT, SingleOp); break;
4363 default:
4364 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4365 }
4366 } else {
4367 Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
4368 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004369
4370 if (Invert)
4371 Result = DAG.getNOT(dl, Result, VT);
4372
4373 return Result;
4374}
4375
Bob Wilson5b2b5042010-06-14 22:19:57 +00004376/// isNEONModifiedImm - Check if the specified splat value corresponds to a
4377/// valid vector constant for a NEON instruction with a "modified immediate"
Bob Wilsona3f19012010-07-13 21:16:48 +00004378/// operand (e.g., VMOV). If so, return the encoded value.
Bob Wilson5b2b5042010-06-14 22:19:57 +00004379static SDValue isNEONModifiedImm(uint64_t SplatBits, uint64_t SplatUndef,
4380 unsigned SplatBitSize, SelectionDAG &DAG,
Owen Andersona4076922010-11-05 21:57:54 +00004381 EVT &VT, bool is128Bits, NEONModImmType type) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004382 unsigned OpCmode, Imm;
Bob Wilson6eae5202010-06-11 21:34:50 +00004383
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004384 // SplatBitSize is set to the smallest size that splats the vector, so a
4385 // zero vector will always have SplatBitSize == 8. However, NEON modified
4386 // immediate instructions others than VMOV do not support the 8-bit encoding
4387 // of a zero vector, and the default encoding of zero is supposed to be the
4388 // 32-bit version.
4389 if (SplatBits == 0)
4390 SplatBitSize = 32;
4391
Bob Wilson2e076c42009-06-22 23:27:02 +00004392 switch (SplatBitSize) {
4393 case 8:
Owen Andersona4076922010-11-05 21:57:54 +00004394 if (type != VMOVModImm)
Bob Wilsonbad47f62010-07-14 06:31:50 +00004395 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004396 // Any 1-byte value is OK. Op=0, Cmode=1110.
Bob Wilson2e076c42009-06-22 23:27:02 +00004397 assert((SplatBits & ~0xff) == 0 && "one byte splat value is too big");
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004398 OpCmode = 0xe;
Bob Wilson6eae5202010-06-11 21:34:50 +00004399 Imm = SplatBits;
Bob Wilsona3f19012010-07-13 21:16:48 +00004400 VT = is128Bits ? MVT::v16i8 : MVT::v8i8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004401 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00004402
4403 case 16:
4404 // NEON's 16-bit VMOV supports splat values where only one byte is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004405 VT = is128Bits ? MVT::v8i16 : MVT::v4i16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004406 if ((SplatBits & ~0xff) == 0) {
4407 // Value = 0x00nn: Op=x, Cmode=100x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004408 OpCmode = 0x8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004409 Imm = SplatBits;
4410 break;
4411 }
4412 if ((SplatBits & ~0xff00) == 0) {
4413 // Value = 0xnn00: Op=x, Cmode=101x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004414 OpCmode = 0xa;
Bob Wilson6eae5202010-06-11 21:34:50 +00004415 Imm = SplatBits >> 8;
4416 break;
4417 }
4418 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004419
4420 case 32:
4421 // NEON's 32-bit VMOV supports splat values where:
4422 // * only one byte is nonzero, or
4423 // * the least significant byte is 0xff and the second byte is nonzero, or
4424 // * the least significant 2 bytes are 0xff and the third is nonzero.
Bob Wilsona3f19012010-07-13 21:16:48 +00004425 VT = is128Bits ? MVT::v4i32 : MVT::v2i32;
Bob Wilson6eae5202010-06-11 21:34:50 +00004426 if ((SplatBits & ~0xff) == 0) {
4427 // Value = 0x000000nn: Op=x, Cmode=000x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004428 OpCmode = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004429 Imm = SplatBits;
4430 break;
4431 }
4432 if ((SplatBits & ~0xff00) == 0) {
4433 // Value = 0x0000nn00: Op=x, Cmode=001x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004434 OpCmode = 0x2;
Bob Wilson6eae5202010-06-11 21:34:50 +00004435 Imm = SplatBits >> 8;
4436 break;
4437 }
4438 if ((SplatBits & ~0xff0000) == 0) {
4439 // Value = 0x00nn0000: Op=x, Cmode=010x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004440 OpCmode = 0x4;
Bob Wilson6eae5202010-06-11 21:34:50 +00004441 Imm = SplatBits >> 16;
4442 break;
4443 }
4444 if ((SplatBits & ~0xff000000) == 0) {
4445 // Value = 0xnn000000: Op=x, Cmode=011x.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004446 OpCmode = 0x6;
Bob Wilson6eae5202010-06-11 21:34:50 +00004447 Imm = SplatBits >> 24;
4448 break;
4449 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004450
Owen Andersona4076922010-11-05 21:57:54 +00004451 // cmode == 0b1100 and cmode == 0b1101 are not supported for VORR or VBIC
4452 if (type == OtherModImm) return SDValue();
4453
Bob Wilson2e076c42009-06-22 23:27:02 +00004454 if ((SplatBits & ~0xffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004455 ((SplatBits | SplatUndef) & 0xff) == 0xff) {
4456 // Value = 0x0000nnff: Op=x, Cmode=1100.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004457 OpCmode = 0xc;
Bob Wilson6eae5202010-06-11 21:34:50 +00004458 Imm = SplatBits >> 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004459 break;
4460 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004461
4462 if ((SplatBits & ~0xffffff) == 0 &&
Bob Wilson6eae5202010-06-11 21:34:50 +00004463 ((SplatBits | SplatUndef) & 0xffff) == 0xffff) {
4464 // Value = 0x00nnffff: Op=x, Cmode=1101.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004465 OpCmode = 0xd;
Bob Wilson6eae5202010-06-11 21:34:50 +00004466 Imm = SplatBits >> 16;
Bob Wilson6eae5202010-06-11 21:34:50 +00004467 break;
4468 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004469
4470 // Note: there are a few 32-bit splat values (specifically: 00ffff00,
4471 // ff000000, ff0000ff, and ffff00ff) that are valid for VMOV.I64 but not
4472 // VMOV.I32. A (very) minor optimization would be to replicate the value
4473 // and fall through here to test for a valid 64-bit splat. But, then the
4474 // caller would also need to check and handle the change in size.
Bob Wilson6eae5202010-06-11 21:34:50 +00004475 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00004476
4477 case 64: {
Owen Andersona4076922010-11-05 21:57:54 +00004478 if (type != VMOVModImm)
Bob Wilsonf3f7a772010-06-15 19:05:35 +00004479 return SDValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004480 // NEON has a 64-bit VMOV splat where each byte is either 0 or 0xff.
Bob Wilson2e076c42009-06-22 23:27:02 +00004481 uint64_t BitMask = 0xff;
4482 uint64_t Val = 0;
Bob Wilson6eae5202010-06-11 21:34:50 +00004483 unsigned ImmMask = 1;
4484 Imm = 0;
Bob Wilson2e076c42009-06-22 23:27:02 +00004485 for (int ByteNum = 0; ByteNum < 8; ++ByteNum) {
Bob Wilson6eae5202010-06-11 21:34:50 +00004486 if (((SplatBits | SplatUndef) & BitMask) == BitMask) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004487 Val |= BitMask;
Bob Wilson6eae5202010-06-11 21:34:50 +00004488 Imm |= ImmMask;
4489 } else if ((SplatBits & BitMask) != 0) {
Bob Wilson2e076c42009-06-22 23:27:02 +00004490 return SDValue();
Bob Wilson6eae5202010-06-11 21:34:50 +00004491 }
Bob Wilson2e076c42009-06-22 23:27:02 +00004492 BitMask <<= 8;
Bob Wilson6eae5202010-06-11 21:34:50 +00004493 ImmMask <<= 1;
Bob Wilson2e076c42009-06-22 23:27:02 +00004494 }
Bob Wilson6eae5202010-06-11 21:34:50 +00004495 // Op=1, Cmode=1110.
Bob Wilsonc1c6f472010-07-13 04:44:34 +00004496 OpCmode = 0x1e;
Bob Wilsona3f19012010-07-13 21:16:48 +00004497 VT = is128Bits ? MVT::v2i64 : MVT::v1i64;
Bob Wilson2e076c42009-06-22 23:27:02 +00004498 break;
4499 }
4500
Bob Wilson6eae5202010-06-11 21:34:50 +00004501 default:
Bob Wilson0ae08932010-06-19 05:32:09 +00004502 llvm_unreachable("unexpected size for isNEONModifiedImm");
Bob Wilson6eae5202010-06-11 21:34:50 +00004503 }
4504
Bob Wilsona3f19012010-07-13 21:16:48 +00004505 unsigned EncodedVal = ARM_AM::createNEONModImm(OpCmode, Imm);
4506 return DAG.getTargetConstant(EncodedVal, MVT::i32);
Bob Wilson2e076c42009-06-22 23:27:02 +00004507}
4508
Lang Hames591cdaf2012-03-29 21:56:11 +00004509SDValue ARMTargetLowering::LowerConstantFP(SDValue Op, SelectionDAG &DAG,
4510 const ARMSubtarget *ST) const {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004511 if (!ST->hasVFP3())
Lang Hames591cdaf2012-03-29 21:56:11 +00004512 return SDValue();
4513
Tim Northoverf79c3a52013-08-20 08:57:11 +00004514 bool IsDouble = Op.getValueType() == MVT::f64;
Lang Hames591cdaf2012-03-29 21:56:11 +00004515 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004516
4517 // Try splatting with a VMOV.f32...
4518 APFloat FPVal = CFP->getValueAPF();
Tim Northoverf79c3a52013-08-20 08:57:11 +00004519 int ImmVal = IsDouble ? ARM_AM::getFP64Imm(FPVal) : ARM_AM::getFP32Imm(FPVal);
4520
Lang Hames591cdaf2012-03-29 21:56:11 +00004521 if (ImmVal != -1) {
Tim Northoverf79c3a52013-08-20 08:57:11 +00004522 if (IsDouble || !ST->useNEONForSinglePrecisionFP()) {
4523 // We have code in place to select a valid ConstantFP already, no need to
4524 // do any mangling.
4525 return Op;
4526 }
4527
4528 // It's a float and we are trying to use NEON operations where
4529 // possible. Lower it to a splat followed by an extract.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004530 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004531 SDValue NewVal = DAG.getTargetConstant(ImmVal, MVT::i32);
4532 SDValue VecConstant = DAG.getNode(ARMISD::VMOVFPIMM, DL, MVT::v2f32,
4533 NewVal);
4534 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecConstant,
4535 DAG.getConstant(0, MVT::i32));
4536 }
4537
Tim Northoverf79c3a52013-08-20 08:57:11 +00004538 // The rest of our options are NEON only, make sure that's allowed before
4539 // proceeding..
4540 if (!ST->hasNEON() || (!IsDouble && !ST->useNEONForSinglePrecisionFP()))
4541 return SDValue();
4542
Lang Hames591cdaf2012-03-29 21:56:11 +00004543 EVT VMovVT;
Tim Northoverf79c3a52013-08-20 08:57:11 +00004544 uint64_t iVal = FPVal.bitcastToAPInt().getZExtValue();
4545
4546 // It wouldn't really be worth bothering for doubles except for one very
4547 // important value, which does happen to match: 0.0. So make sure we don't do
4548 // anything stupid.
4549 if (IsDouble && (iVal & 0xffffffff) != (iVal >> 32))
4550 return SDValue();
4551
4552 // Try a VMOV.i32 (FIXME: i8, i16, or i64 could work too).
4553 SDValue NewVal = isNEONModifiedImm(iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4554 false, VMOVModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004555 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004556 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004557 SDValue VecConstant = DAG.getNode(ARMISD::VMOVIMM, DL, VMovVT,
4558 NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004559 if (IsDouble)
4560 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4561
4562 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004563 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4564 VecConstant);
4565 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4566 DAG.getConstant(0, MVT::i32));
4567 }
4568
4569 // Finally, try a VMVN.i32
Tim Northoverf79c3a52013-08-20 08:57:11 +00004570 NewVal = isNEONModifiedImm(~iVal & 0xffffffffU, 0, 32, DAG, VMovVT,
4571 false, VMVNModImm);
Lang Hames591cdaf2012-03-29 21:56:11 +00004572 if (NewVal != SDValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004573 SDLoc DL(Op);
Lang Hames591cdaf2012-03-29 21:56:11 +00004574 SDValue VecConstant = DAG.getNode(ARMISD::VMVNIMM, DL, VMovVT, NewVal);
Tim Northoverf79c3a52013-08-20 08:57:11 +00004575
4576 if (IsDouble)
4577 return DAG.getNode(ISD::BITCAST, DL, MVT::f64, VecConstant);
4578
4579 // It's a float: cast and extract a vector element.
Lang Hames591cdaf2012-03-29 21:56:11 +00004580 SDValue VecFConstant = DAG.getNode(ISD::BITCAST, DL, MVT::v2f32,
4581 VecConstant);
4582 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, VecFConstant,
4583 DAG.getConstant(0, MVT::i32));
4584 }
4585
4586 return SDValue();
4587}
4588
Quentin Colombet8e1fe842012-11-02 21:32:17 +00004589// check if an VEXT instruction can handle the shuffle mask when the
4590// vector sources of the shuffle are the same.
4591static bool isSingletonVEXTMask(ArrayRef<int> M, EVT VT, unsigned &Imm) {
4592 unsigned NumElts = VT.getVectorNumElements();
4593
4594 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4595 if (M[0] < 0)
4596 return false;
4597
4598 Imm = M[0];
4599
4600 // If this is a VEXT shuffle, the immediate value is the index of the first
4601 // element. The other shuffle indices must be the successive elements after
4602 // the first one.
4603 unsigned ExpectedElt = Imm;
4604 for (unsigned i = 1; i < NumElts; ++i) {
4605 // Increment the expected index. If it wraps around, just follow it
4606 // back to index zero and keep going.
4607 ++ExpectedElt;
4608 if (ExpectedElt == NumElts)
4609 ExpectedElt = 0;
4610
4611 if (M[i] < 0) continue; // ignore UNDEF indices
4612 if (ExpectedElt != static_cast<unsigned>(M[i]))
4613 return false;
4614 }
4615
4616 return true;
4617}
4618
Lang Hames591cdaf2012-03-29 21:56:11 +00004619
Benjamin Kramer339ced42012-01-15 13:16:05 +00004620static bool isVEXTMask(ArrayRef<int> M, EVT VT,
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004621 bool &ReverseVEXT, unsigned &Imm) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004622 unsigned NumElts = VT.getVectorNumElements();
4623 ReverseVEXT = false;
Bob Wilson411dfad2010-08-17 05:54:34 +00004624
4625 // Assume that the first shuffle index is not UNDEF. Fail if it is.
4626 if (M[0] < 0)
4627 return false;
4628
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004629 Imm = M[0];
Bob Wilson32cd8552009-08-19 17:03:43 +00004630
4631 // If this is a VEXT shuffle, the immediate value is the index of the first
4632 // element. The other shuffle indices must be the successive elements after
4633 // the first one.
4634 unsigned ExpectedElt = Imm;
4635 for (unsigned i = 1; i < NumElts; ++i) {
Bob Wilson32cd8552009-08-19 17:03:43 +00004636 // Increment the expected index. If it wraps around, it may still be
4637 // a VEXT but the source vectors must be swapped.
4638 ExpectedElt += 1;
4639 if (ExpectedElt == NumElts * 2) {
4640 ExpectedElt = 0;
4641 ReverseVEXT = true;
4642 }
4643
Bob Wilson411dfad2010-08-17 05:54:34 +00004644 if (M[i] < 0) continue; // ignore UNDEF indices
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004645 if (ExpectedElt != static_cast<unsigned>(M[i]))
Bob Wilson32cd8552009-08-19 17:03:43 +00004646 return false;
4647 }
4648
4649 // Adjust the index value if the source operands will be swapped.
4650 if (ReverseVEXT)
4651 Imm -= NumElts;
4652
Bob Wilson32cd8552009-08-19 17:03:43 +00004653 return true;
4654}
4655
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004656/// isVREVMask - Check if a vector shuffle corresponds to a VREV
4657/// instruction with the specified blocksize. (The order of the elements
4658/// within each block of the vector is reversed.)
Benjamin Kramer339ced42012-01-15 13:16:05 +00004659static bool isVREVMask(ArrayRef<int> M, EVT VT, unsigned BlockSize) {
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004660 assert((BlockSize==16 || BlockSize==32 || BlockSize==64) &&
4661 "Only possible block sizes for VREV are: 16, 32, 64");
4662
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004663 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
Bob Wilson854530a2009-10-21 21:36:27 +00004664 if (EltSz == 64)
4665 return false;
4666
4667 unsigned NumElts = VT.getVectorNumElements();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00004668 unsigned BlockElts = M[0] + 1;
Bob Wilson411dfad2010-08-17 05:54:34 +00004669 // If the first shuffle index is UNDEF, be optimistic.
4670 if (M[0] < 0)
4671 BlockElts = BlockSize / EltSz;
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004672
4673 if (BlockSize <= EltSz || BlockSize != BlockElts * EltSz)
4674 return false;
4675
4676 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004677 if (M[i] < 0) continue; // ignore UNDEF indices
4678 if ((unsigned) M[i] != (i - i%BlockElts) + (BlockElts - 1 - i%BlockElts))
Bob Wilson8a37bbe2009-07-26 00:39:34 +00004679 return false;
4680 }
4681
4682 return true;
4683}
4684
Benjamin Kramer339ced42012-01-15 13:16:05 +00004685static bool isVTBLMask(ArrayRef<int> M, EVT VT) {
Bill Wendling865f8b52011-03-15 21:15:20 +00004686 // We can handle <8 x i8> vector shuffles. If the index in the mask is out of
4687 // range, then 0 is placed into the resulting vector. So pretty much any mask
4688 // of 8 elements can work here.
4689 return VT == MVT::v8i8 && M.size() == 8;
4690}
4691
Benjamin Kramer339ced42012-01-15 13:16:05 +00004692static bool isVTRNMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004693 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4694 if (EltSz == 64)
4695 return false;
4696
Bob Wilsona7062312009-08-21 20:54:19 +00004697 unsigned NumElts = VT.getVectorNumElements();
4698 WhichResult = (M[0] == 0 ? 0 : 1);
4699 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004700 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4701 (M[i+1] >= 0 && (unsigned) M[i+1] != i + NumElts + WhichResult))
Bob Wilsona7062312009-08-21 20:54:19 +00004702 return false;
4703 }
4704 return true;
4705}
4706
Bob Wilson0bbd3072009-12-03 06:40:55 +00004707/// isVTRN_v_undef_Mask - Special case of isVTRNMask for canonical form of
4708/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4709/// Mask is e.g., <0, 0, 2, 2> instead of <0, 4, 2, 6>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004710static bool isVTRN_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004711 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4712 if (EltSz == 64)
4713 return false;
4714
4715 unsigned NumElts = VT.getVectorNumElements();
4716 WhichResult = (M[0] == 0 ? 0 : 1);
4717 for (unsigned i = 0; i < NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004718 if ((M[i] >= 0 && (unsigned) M[i] != i + WhichResult) ||
4719 (M[i+1] >= 0 && (unsigned) M[i+1] != i + WhichResult))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004720 return false;
4721 }
4722 return true;
4723}
4724
Benjamin Kramer339ced42012-01-15 13:16:05 +00004725static bool isVUZPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004726 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4727 if (EltSz == 64)
4728 return false;
4729
Bob Wilsona7062312009-08-21 20:54:19 +00004730 unsigned NumElts = VT.getVectorNumElements();
4731 WhichResult = (M[0] == 0 ? 0 : 1);
4732 for (unsigned i = 0; i != NumElts; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004733 if (M[i] < 0) continue; // ignore UNDEF indices
Bob Wilsona7062312009-08-21 20:54:19 +00004734 if ((unsigned) M[i] != 2 * i + WhichResult)
4735 return false;
4736 }
4737
4738 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004739 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004740 return false;
4741
4742 return true;
4743}
4744
Bob Wilson0bbd3072009-12-03 06:40:55 +00004745/// isVUZP_v_undef_Mask - Special case of isVUZPMask for canonical form of
4746/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4747/// Mask is e.g., <0, 2, 0, 2> instead of <0, 2, 4, 6>,
Benjamin Kramer339ced42012-01-15 13:16:05 +00004748static bool isVUZP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004749 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4750 if (EltSz == 64)
4751 return false;
4752
4753 unsigned Half = VT.getVectorNumElements() / 2;
4754 WhichResult = (M[0] == 0 ? 0 : 1);
4755 for (unsigned j = 0; j != 2; ++j) {
4756 unsigned Idx = WhichResult;
4757 for (unsigned i = 0; i != Half; ++i) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004758 int MIdx = M[i + j * Half];
4759 if (MIdx >= 0 && (unsigned) MIdx != Idx)
Bob Wilson0bbd3072009-12-03 06:40:55 +00004760 return false;
4761 Idx += 2;
4762 }
4763 }
4764
4765 // VUZP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4766 if (VT.is64BitVector() && EltSz == 32)
4767 return false;
4768
4769 return true;
4770}
4771
Benjamin Kramer339ced42012-01-15 13:16:05 +00004772static bool isVZIPMask(ArrayRef<int> M, EVT VT, unsigned &WhichResult) {
Bob Wilson854530a2009-10-21 21:36:27 +00004773 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4774 if (EltSz == 64)
4775 return false;
4776
Bob Wilsona7062312009-08-21 20:54:19 +00004777 unsigned NumElts = VT.getVectorNumElements();
4778 WhichResult = (M[0] == 0 ? 0 : 1);
4779 unsigned Idx = WhichResult * NumElts / 2;
4780 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004781 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4782 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx + NumElts))
Bob Wilsona7062312009-08-21 20:54:19 +00004783 return false;
4784 Idx += 1;
4785 }
4786
4787 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
Bob Wilson854530a2009-10-21 21:36:27 +00004788 if (VT.is64BitVector() && EltSz == 32)
Bob Wilsona7062312009-08-21 20:54:19 +00004789 return false;
4790
4791 return true;
4792}
4793
Bob Wilson0bbd3072009-12-03 06:40:55 +00004794/// isVZIP_v_undef_Mask - Special case of isVZIPMask for canonical form of
4795/// "vector_shuffle v, v", i.e., "vector_shuffle v, undef".
4796/// Mask is e.g., <0, 0, 1, 1> instead of <0, 4, 1, 5>.
Benjamin Kramer339ced42012-01-15 13:16:05 +00004797static bool isVZIP_v_undef_Mask(ArrayRef<int> M, EVT VT, unsigned &WhichResult){
Bob Wilson0bbd3072009-12-03 06:40:55 +00004798 unsigned EltSz = VT.getVectorElementType().getSizeInBits();
4799 if (EltSz == 64)
4800 return false;
4801
4802 unsigned NumElts = VT.getVectorNumElements();
4803 WhichResult = (M[0] == 0 ? 0 : 1);
4804 unsigned Idx = WhichResult * NumElts / 2;
4805 for (unsigned i = 0; i != NumElts; i += 2) {
Bob Wilson411dfad2010-08-17 05:54:34 +00004806 if ((M[i] >= 0 && (unsigned) M[i] != Idx) ||
4807 (M[i+1] >= 0 && (unsigned) M[i+1] != Idx))
Bob Wilson0bbd3072009-12-03 06:40:55 +00004808 return false;
4809 Idx += 1;
4810 }
4811
4812 // VZIP.32 for 64-bit vectors is a pseudo-instruction alias for VTRN.32.
4813 if (VT.is64BitVector() && EltSz == 32)
4814 return false;
4815
4816 return true;
4817}
4818
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00004819/// \return true if this is a reverse operation on an vector.
4820static bool isReverseMask(ArrayRef<int> M, EVT VT) {
4821 unsigned NumElts = VT.getVectorNumElements();
4822 // Make sure the mask has the right size.
4823 if (NumElts != M.size())
4824 return false;
4825
4826 // Look for <15, ..., 3, -1, 1, 0>.
4827 for (unsigned i = 0; i != NumElts; ++i)
4828 if (M[i] >= 0 && M[i] != (int) (NumElts - 1 - i))
4829 return false;
4830
4831 return true;
4832}
4833
Dale Johannesen2bff5052010-07-29 20:10:08 +00004834// If N is an integer constant that can be moved into a register in one
4835// instruction, return an SDValue of such a constant (will become a MOV
4836// instruction). Otherwise return null.
4837static SDValue IsSingleInstrConstant(SDValue N, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004838 const ARMSubtarget *ST, SDLoc dl) {
Dale Johannesen2bff5052010-07-29 20:10:08 +00004839 uint64_t Val;
4840 if (!isa<ConstantSDNode>(N))
4841 return SDValue();
4842 Val = cast<ConstantSDNode>(N)->getZExtValue();
4843
4844 if (ST->isThumb1Only()) {
4845 if (Val <= 255 || ~Val <= 255)
4846 return DAG.getConstant(Val, MVT::i32);
4847 } else {
4848 if (ARM_AM::getSOImmVal(Val) != -1 || ARM_AM::getSOImmVal(~Val) != -1)
4849 return DAG.getConstant(Val, MVT::i32);
4850 }
4851 return SDValue();
4852}
4853
Bob Wilson2e076c42009-06-22 23:27:02 +00004854// If this is a case we can't handle, return null and let the default
4855// expansion code take care of it.
Bob Wilson6f2b8962011-01-07 21:37:30 +00004856SDValue ARMTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
4857 const ARMSubtarget *ST) const {
Bob Wilsonfcd63612009-08-13 01:57:47 +00004858 BuildVectorSDNode *BVN = cast<BuildVectorSDNode>(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004859 SDLoc dl(Op);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004860 EVT VT = Op.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00004861
4862 APInt SplatBits, SplatUndef;
4863 unsigned SplatBitSize;
4864 bool HasAnyUndefs;
4865 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
Anton Korobeynikovece642a2009-08-29 00:08:18 +00004866 if (SplatBitSize <= 64) {
Bob Wilson5b2b5042010-06-14 22:19:57 +00004867 // Check if an immediate VMOV works.
Bob Wilsona3f19012010-07-13 21:16:48 +00004868 EVT VmovVT;
Bob Wilson5b2b5042010-06-14 22:19:57 +00004869 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
Bob Wilsona3f19012010-07-13 21:16:48 +00004870 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00004871 DAG, VmovVT, VT.is128BitVector(),
4872 VMOVModImm);
Bob Wilsona3f19012010-07-13 21:16:48 +00004873 if (Val.getNode()) {
4874 SDValue Vmov = DAG.getNode(ARMISD::VMOVIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00004875 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsona3f19012010-07-13 21:16:48 +00004876 }
Bob Wilsonbad47f62010-07-14 06:31:50 +00004877
4878 // Try an immediate VMVN.
Eli Friedmanaa6ec392011-10-13 22:40:23 +00004879 uint64_t NegatedImm = (~SplatBits).getZExtValue();
Bob Wilsonbad47f62010-07-14 06:31:50 +00004880 Val = isNEONModifiedImm(NegatedImm,
4881 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00004882 DAG, VmovVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00004883 VMVNModImm);
Bob Wilsonbad47f62010-07-14 06:31:50 +00004884 if (Val.getNode()) {
4885 SDValue Vmov = DAG.getNode(ARMISD::VMVNIMM, dl, VmovVT, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00004886 return DAG.getNode(ISD::BITCAST, dl, VT, Vmov);
Bob Wilsonbad47f62010-07-14 06:31:50 +00004887 }
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00004888
4889 // Use vmov.f32 to materialize other v2f32 and v4f32 splats.
Eli Friedmanc9bf1b12011-12-15 22:56:53 +00004890 if ((VT == MVT::v2f32 || VT == MVT::v4f32) && SplatBitSize == 32) {
Eli Friedman4e36a932011-12-09 23:54:42 +00004891 int ImmVal = ARM_AM::getFP32Imm(SplatBits);
Evan Cheng7ca4b6e2011-11-15 02:12:34 +00004892 if (ImmVal != -1) {
4893 SDValue Val = DAG.getTargetConstant(ImmVal, MVT::i32);
4894 return DAG.getNode(ARMISD::VMOVFPIMM, dl, VT, Val);
4895 }
4896 }
Anton Korobeynikovece642a2009-08-29 00:08:18 +00004897 }
Bob Wilson0dbdec82009-07-30 00:31:25 +00004898 }
4899
Bob Wilson91fdf682010-05-22 00:23:12 +00004900 // Scan through the operands to see if only one value is used.
James Molloy49bdbce2012-09-06 09:55:02 +00004901 //
4902 // As an optimisation, even if more than one value is used it may be more
4903 // profitable to splat with one value then change some lanes.
4904 //
4905 // Heuristically we decide to do this if the vector has a "dominant" value,
4906 // defined as splatted to more than half of the lanes.
Bob Wilson91fdf682010-05-22 00:23:12 +00004907 unsigned NumElts = VT.getVectorNumElements();
4908 bool isOnlyLowElement = true;
4909 bool usesOnlyOneValue = true;
James Molloy49bdbce2012-09-06 09:55:02 +00004910 bool hasDominantValue = false;
Bob Wilson91fdf682010-05-22 00:23:12 +00004911 bool isConstant = true;
James Molloy49bdbce2012-09-06 09:55:02 +00004912
4913 // Map of the number of times a particular SDValue appears in the
4914 // element list.
James Molloy9d30dc22012-09-06 10:32:08 +00004915 DenseMap<SDValue, unsigned> ValueCounts;
Bob Wilson91fdf682010-05-22 00:23:12 +00004916 SDValue Value;
4917 for (unsigned i = 0; i < NumElts; ++i) {
4918 SDValue V = Op.getOperand(i);
4919 if (V.getOpcode() == ISD::UNDEF)
4920 continue;
4921 if (i > 0)
4922 isOnlyLowElement = false;
4923 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
4924 isConstant = false;
4925
James Molloy49bdbce2012-09-06 09:55:02 +00004926 ValueCounts.insert(std::make_pair(V, 0));
James Molloy9d30dc22012-09-06 10:32:08 +00004927 unsigned &Count = ValueCounts[V];
Jim Grosbach54efea02013-03-02 20:16:15 +00004928
James Molloy49bdbce2012-09-06 09:55:02 +00004929 // Is this value dominant? (takes up more than half of the lanes)
4930 if (++Count > (NumElts / 2)) {
4931 hasDominantValue = true;
Bob Wilson91fdf682010-05-22 00:23:12 +00004932 Value = V;
James Molloy49bdbce2012-09-06 09:55:02 +00004933 }
Bob Wilson91fdf682010-05-22 00:23:12 +00004934 }
James Molloy49bdbce2012-09-06 09:55:02 +00004935 if (ValueCounts.size() != 1)
4936 usesOnlyOneValue = false;
4937 if (!Value.getNode() && ValueCounts.size() > 0)
4938 Value = ValueCounts.begin()->first;
Bob Wilson91fdf682010-05-22 00:23:12 +00004939
James Molloy49bdbce2012-09-06 09:55:02 +00004940 if (ValueCounts.size() == 0)
Bob Wilson91fdf682010-05-22 00:23:12 +00004941 return DAG.getUNDEF(VT);
4942
Quentin Colombet0f2fe742013-07-23 22:34:47 +00004943 // Loads are better lowered with insert_vector_elt/ARMISD::BUILD_VECTOR.
4944 // Keep going if we are hitting this case.
4945 if (isOnlyLowElement && !ISD::isNormalLoad(Value.getNode()))
Bob Wilson91fdf682010-05-22 00:23:12 +00004946 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value);
4947
Dale Johannesen2bff5052010-07-29 20:10:08 +00004948 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
4949
Dale Johannesen710a2d92010-10-19 20:00:17 +00004950 // Use VDUP for non-constant splats. For f32 constant splats, reduce to
4951 // i32 and try again.
James Molloy49bdbce2012-09-06 09:55:02 +00004952 if (hasDominantValue && EltSize <= 32) {
4953 if (!isConstant) {
4954 SDValue N;
4955
4956 // If we are VDUPing a value that comes directly from a vector, that will
4957 // cause an unnecessary move to and from a GPR, where instead we could
Jim Grosbacha3c5c762013-03-02 20:16:24 +00004958 // just use VDUPLANE. We can only do this if the lane being extracted
4959 // is at a constant index, as the VDUP from lane instructions only have
4960 // constant-index forms.
4961 if (Value->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4962 isa<ConstantSDNode>(Value->getOperand(1))) {
Silviu Barangab1409702012-10-15 09:41:32 +00004963 // We need to create a new undef vector to use for the VDUPLANE if the
4964 // size of the vector from which we get the value is different than the
4965 // size of the vector that we need to create. We will insert the element
4966 // such that the register coalescer will remove unnecessary copies.
4967 if (VT != Value->getOperand(0).getValueType()) {
4968 ConstantSDNode *constIndex;
4969 constIndex = dyn_cast<ConstantSDNode>(Value->getOperand(1));
4970 assert(constIndex && "The index is not a constant!");
4971 unsigned index = constIndex->getAPIntValue().getLimitedValue() %
4972 VT.getVectorNumElements();
4973 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
4974 DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DAG.getUNDEF(VT),
4975 Value, DAG.getConstant(index, MVT::i32)),
4976 DAG.getConstant(index, MVT::i32));
Jim Grosbachc6f19142013-03-02 20:16:19 +00004977 } else
Silviu Barangab1409702012-10-15 09:41:32 +00004978 N = DAG.getNode(ARMISD::VDUPLANE, dl, VT,
James Molloy49bdbce2012-09-06 09:55:02 +00004979 Value->getOperand(0), Value->getOperand(1));
Jim Grosbachc6f19142013-03-02 20:16:19 +00004980 } else
James Molloy49bdbce2012-09-06 09:55:02 +00004981 N = DAG.getNode(ARMISD::VDUP, dl, VT, Value);
4982
4983 if (!usesOnlyOneValue) {
4984 // The dominant value was splatted as 'N', but we now have to insert
4985 // all differing elements.
4986 for (unsigned I = 0; I < NumElts; ++I) {
4987 if (Op.getOperand(I) == Value)
4988 continue;
4989 SmallVector<SDValue, 3> Ops;
4990 Ops.push_back(N);
4991 Ops.push_back(Op.getOperand(I));
4992 Ops.push_back(DAG.getConstant(I, MVT::i32));
Craig Topper48d114b2014-04-26 18:35:24 +00004993 N = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ops);
James Molloy49bdbce2012-09-06 09:55:02 +00004994 }
4995 }
4996 return N;
4997 }
Dale Johannesen710a2d92010-10-19 20:00:17 +00004998 if (VT.getVectorElementType().isFloatingPoint()) {
4999 SmallVector<SDValue, 8> Ops;
5000 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005001 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, MVT::i32,
Dale Johannesen710a2d92010-10-19 20:00:17 +00005002 Op.getOperand(i)));
Nate Begemanca524112010-11-10 21:35:41 +00005003 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00005004 SDValue Val = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, Ops);
Dale Johannesenff376752010-10-20 22:03:37 +00005005 Val = LowerBUILD_VECTOR(Val, DAG, ST);
5006 if (Val.getNode())
Wesley Peck527da1b2010-11-23 03:31:01 +00005007 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Dale Johannesen2bff5052010-07-29 20:10:08 +00005008 }
James Molloy49bdbce2012-09-06 09:55:02 +00005009 if (usesOnlyOneValue) {
5010 SDValue Val = IsSingleInstrConstant(Value, DAG, ST, dl);
5011 if (isConstant && Val.getNode())
Jim Grosbach54efea02013-03-02 20:16:15 +00005012 return DAG.getNode(ARMISD::VDUP, dl, VT, Val);
James Molloy49bdbce2012-09-06 09:55:02 +00005013 }
Dale Johannesen2bff5052010-07-29 20:10:08 +00005014 }
5015
5016 // If all elements are constants and the case above didn't get hit, fall back
5017 // to the default expansion, which will generate a load from the constant
5018 // pool.
Bob Wilson91fdf682010-05-22 00:23:12 +00005019 if (isConstant)
5020 return SDValue();
5021
Bob Wilson6f2b8962011-01-07 21:37:30 +00005022 // Empirical tests suggest this is rarely worth it for vectors of length <= 2.
5023 if (NumElts >= 4) {
5024 SDValue shuffle = ReconstructShuffle(Op, DAG);
5025 if (shuffle != SDValue())
5026 return shuffle;
5027 }
5028
Bob Wilson91fdf682010-05-22 00:23:12 +00005029 // Vectors with 32- or 64-bit elements can be built by directly assigning
Bob Wilsond8a9a042010-06-04 00:04:02 +00005030 // the subregisters. Lower it to an ARMISD::BUILD_VECTOR so the operands
5031 // will be legalized.
Bob Wilson91fdf682010-05-22 00:23:12 +00005032 if (EltSize >= 32) {
5033 // Do the expansion with floating-point types, since that is what the VFP
5034 // registers are defined to use, and since i64 is not legal.
5035 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5036 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005037 SmallVector<SDValue, 8> Ops;
5038 for (unsigned i = 0; i < NumElts; ++i)
Wesley Peck527da1b2010-11-23 03:31:01 +00005039 Ops.push_back(DAG.getNode(ISD::BITCAST, dl, EltVT, Op.getOperand(i)));
Craig Topper48d114b2014-04-26 18:35:24 +00005040 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005041 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005042 }
5043
Jim Grosbach24e102a2013-07-08 18:18:52 +00005044 // If all else fails, just use a sequence of INSERT_VECTOR_ELT when we
5045 // know the default expansion would otherwise fall back on something even
5046 // worse. For a vector with one or two non-undef values, that's
5047 // scalar_to_vector for the elements followed by a shuffle (provided the
5048 // shuffle is valid for the target) and materialization element by element
5049 // on the stack followed by a load for everything else.
5050 if (!isConstant && !usesOnlyOneValue) {
5051 SDValue Vec = DAG.getUNDEF(VT);
5052 for (unsigned i = 0 ; i < NumElts; ++i) {
5053 SDValue V = Op.getOperand(i);
5054 if (V.getOpcode() == ISD::UNDEF)
5055 continue;
5056 SDValue LaneIdx = DAG.getConstant(i, MVT::i32);
5057 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Vec, V, LaneIdx);
5058 }
5059 return Vec;
5060 }
5061
Bob Wilson2e076c42009-06-22 23:27:02 +00005062 return SDValue();
5063}
5064
Bob Wilson6f2b8962011-01-07 21:37:30 +00005065// Gather data to see if the operation can be modelled as a
Andrew Trick5eb0a302011-01-19 02:26:13 +00005066// shuffle in combination with VEXTs.
Eric Christopher2af95512011-01-14 23:50:53 +00005067SDValue ARMTargetLowering::ReconstructShuffle(SDValue Op,
5068 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005069 SDLoc dl(Op);
Bob Wilson6f2b8962011-01-07 21:37:30 +00005070 EVT VT = Op.getValueType();
5071 unsigned NumElts = VT.getVectorNumElements();
5072
5073 SmallVector<SDValue, 2> SourceVecs;
5074 SmallVector<unsigned, 2> MinElts;
5075 SmallVector<unsigned, 2> MaxElts;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005076
Bob Wilson6f2b8962011-01-07 21:37:30 +00005077 for (unsigned i = 0; i < NumElts; ++i) {
5078 SDValue V = Op.getOperand(i);
5079 if (V.getOpcode() == ISD::UNDEF)
5080 continue;
5081 else if (V.getOpcode() != ISD::EXTRACT_VECTOR_ELT) {
5082 // A shuffle can only come from building a vector from various
5083 // elements of other vectors.
5084 return SDValue();
Eli Friedman74d1da52011-10-14 23:58:49 +00005085 } else if (V.getOperand(0).getValueType().getVectorElementType() !=
5086 VT.getVectorElementType()) {
5087 // This code doesn't know how to handle shuffles where the vector
5088 // element types do not match (this happens because type legalization
5089 // promotes the return type of EXTRACT_VECTOR_ELT).
5090 // FIXME: It might be appropriate to extend this code to handle
5091 // mismatched types.
5092 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005093 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005094
Bob Wilson6f2b8962011-01-07 21:37:30 +00005095 // Record this extraction against the appropriate vector if possible...
5096 SDValue SourceVec = V.getOperand(0);
Jim Grosbach6df755c2012-07-25 17:02:47 +00005097 // If the element number isn't a constant, we can't effectively
5098 // analyze what's going on.
5099 if (!isa<ConstantSDNode>(V.getOperand(1)))
5100 return SDValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005101 unsigned EltNo = cast<ConstantSDNode>(V.getOperand(1))->getZExtValue();
5102 bool FoundSource = false;
5103 for (unsigned j = 0; j < SourceVecs.size(); ++j) {
5104 if (SourceVecs[j] == SourceVec) {
5105 if (MinElts[j] > EltNo)
5106 MinElts[j] = EltNo;
5107 if (MaxElts[j] < EltNo)
5108 MaxElts[j] = EltNo;
5109 FoundSource = true;
5110 break;
5111 }
5112 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005113
Bob Wilson6f2b8962011-01-07 21:37:30 +00005114 // Or record a new source if not...
5115 if (!FoundSource) {
5116 SourceVecs.push_back(SourceVec);
5117 MinElts.push_back(EltNo);
5118 MaxElts.push_back(EltNo);
5119 }
5120 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005121
Bob Wilson6f2b8962011-01-07 21:37:30 +00005122 // Currently only do something sane when at most two source vectors
5123 // involved.
5124 if (SourceVecs.size() > 2)
5125 return SDValue();
5126
5127 SDValue ShuffleSrcs[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT) };
5128 int VEXTOffsets[2] = {0, 0};
Andrew Trick5eb0a302011-01-19 02:26:13 +00005129
Bob Wilson6f2b8962011-01-07 21:37:30 +00005130 // This loop extracts the usage patterns of the source vectors
5131 // and prepares appropriate SDValues for a shuffle if possible.
5132 for (unsigned i = 0; i < SourceVecs.size(); ++i) {
5133 if (SourceVecs[i].getValueType() == VT) {
5134 // No VEXT necessary
5135 ShuffleSrcs[i] = SourceVecs[i];
5136 VEXTOffsets[i] = 0;
5137 continue;
5138 } else if (SourceVecs[i].getValueType().getVectorNumElements() < NumElts) {
5139 // It probably isn't worth padding out a smaller vector just to
5140 // break it down again in a shuffle.
5141 return SDValue();
5142 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005143
Bob Wilson6f2b8962011-01-07 21:37:30 +00005144 // Since only 64-bit and 128-bit vectors are legal on ARM and
5145 // we've eliminated the other cases...
Bob Wilson3fa9c062011-01-07 23:40:46 +00005146 assert(SourceVecs[i].getValueType().getVectorNumElements() == 2*NumElts &&
5147 "unexpected vector sizes in ReconstructShuffle");
Andrew Trick5eb0a302011-01-19 02:26:13 +00005148
Bob Wilson6f2b8962011-01-07 21:37:30 +00005149 if (MaxElts[i] - MinElts[i] >= NumElts) {
5150 // Span too large for a VEXT to cope
5151 return SDValue();
Andrew Trick5eb0a302011-01-19 02:26:13 +00005152 }
5153
Bob Wilson6f2b8962011-01-07 21:37:30 +00005154 if (MinElts[i] >= NumElts) {
5155 // The extraction can just take the second half
5156 VEXTOffsets[i] = NumElts;
Eric Christopher2af95512011-01-14 23:50:53 +00005157 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5158 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005159 DAG.getIntPtrConstant(NumElts));
5160 } else if (MaxElts[i] < NumElts) {
5161 // The extraction can just take the first half
5162 VEXTOffsets[i] = 0;
Eric Christopher2af95512011-01-14 23:50:53 +00005163 ShuffleSrcs[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5164 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005165 DAG.getIntPtrConstant(0));
5166 } else {
5167 // An actual VEXT is needed
5168 VEXTOffsets[i] = MinElts[i];
Eric Christopher2af95512011-01-14 23:50:53 +00005169 SDValue VEXTSrc1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5170 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005171 DAG.getIntPtrConstant(0));
Eric Christopher2af95512011-01-14 23:50:53 +00005172 SDValue VEXTSrc2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
5173 SourceVecs[i],
Bob Wilson6f2b8962011-01-07 21:37:30 +00005174 DAG.getIntPtrConstant(NumElts));
5175 ShuffleSrcs[i] = DAG.getNode(ARMISD::VEXT, dl, VT, VEXTSrc1, VEXTSrc2,
5176 DAG.getConstant(VEXTOffsets[i], MVT::i32));
5177 }
5178 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005179
Bob Wilson6f2b8962011-01-07 21:37:30 +00005180 SmallVector<int, 8> Mask;
Andrew Trick5eb0a302011-01-19 02:26:13 +00005181
Bob Wilson6f2b8962011-01-07 21:37:30 +00005182 for (unsigned i = 0; i < NumElts; ++i) {
5183 SDValue Entry = Op.getOperand(i);
5184 if (Entry.getOpcode() == ISD::UNDEF) {
5185 Mask.push_back(-1);
5186 continue;
5187 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005188
Bob Wilson6f2b8962011-01-07 21:37:30 +00005189 SDValue ExtractVec = Entry.getOperand(0);
Eric Christopher2af95512011-01-14 23:50:53 +00005190 int ExtractElt = cast<ConstantSDNode>(Op.getOperand(i)
5191 .getOperand(1))->getSExtValue();
Bob Wilson6f2b8962011-01-07 21:37:30 +00005192 if (ExtractVec == SourceVecs[0]) {
5193 Mask.push_back(ExtractElt - VEXTOffsets[0]);
5194 } else {
5195 Mask.push_back(ExtractElt + NumElts - VEXTOffsets[1]);
5196 }
5197 }
Andrew Trick5eb0a302011-01-19 02:26:13 +00005198
Bob Wilson6f2b8962011-01-07 21:37:30 +00005199 // Final check before we try to produce nonsense...
5200 if (isShuffleMaskLegal(Mask, VT))
Eric Christopher2af95512011-01-14 23:50:53 +00005201 return DAG.getVectorShuffle(VT, dl, ShuffleSrcs[0], ShuffleSrcs[1],
5202 &Mask[0]);
Andrew Trick5eb0a302011-01-19 02:26:13 +00005203
Bob Wilson6f2b8962011-01-07 21:37:30 +00005204 return SDValue();
5205}
5206
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005207/// isShuffleMaskLegal - Targets can use this to indicate that they only
5208/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5209/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5210/// are assumed to be legal.
5211bool
5212ARMTargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
5213 EVT VT) const {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005214 if (VT.getVectorNumElements() == 4 &&
5215 (VT.is128BitVector() || VT.is64BitVector())) {
5216 unsigned PFIndexes[4];
5217 for (unsigned i = 0; i != 4; ++i) {
5218 if (M[i] < 0)
5219 PFIndexes[i] = 8;
5220 else
5221 PFIndexes[i] = M[i];
5222 }
5223
5224 // Compute the index in the perfect shuffle table.
5225 unsigned PFTableIndex =
5226 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
5227 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5228 unsigned Cost = (PFEntry >> 30);
5229
5230 if (Cost <= 4)
5231 return true;
5232 }
5233
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005234 bool ReverseVEXT;
Bob Wilsona7062312009-08-21 20:54:19 +00005235 unsigned Imm, WhichResult;
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005236
Bob Wilson846bd792010-06-07 23:53:38 +00005237 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5238 return (EltSize >= 32 ||
5239 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005240 isVREVMask(M, VT, 64) ||
5241 isVREVMask(M, VT, 32) ||
5242 isVREVMask(M, VT, 16) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005243 isVEXTMask(M, VT, ReverseVEXT, Imm) ||
Bill Wendling865f8b52011-03-15 21:15:20 +00005244 isVTBLMask(M, VT) ||
Bob Wilsona7062312009-08-21 20:54:19 +00005245 isVTRNMask(M, VT, WhichResult) ||
5246 isVUZPMask(M, VT, WhichResult) ||
Bob Wilson0bbd3072009-12-03 06:40:55 +00005247 isVZIPMask(M, VT, WhichResult) ||
5248 isVTRN_v_undef_Mask(M, VT, WhichResult) ||
5249 isVUZP_v_undef_Mask(M, VT, WhichResult) ||
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005250 isVZIP_v_undef_Mask(M, VT, WhichResult) ||
5251 ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(M, VT)));
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005252}
5253
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005254/// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
5255/// the specified operations to build the shuffle.
5256static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
5257 SDValue RHS, SelectionDAG &DAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005258 SDLoc dl) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005259 unsigned OpNum = (PFEntry >> 26) & 0x0F;
5260 unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
5261 unsigned RHSID = (PFEntry >> 0) & ((1 << 13)-1);
5262
5263 enum {
5264 OP_COPY = 0, // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
5265 OP_VREV,
5266 OP_VDUP0,
5267 OP_VDUP1,
5268 OP_VDUP2,
5269 OP_VDUP3,
5270 OP_VEXT1,
5271 OP_VEXT2,
5272 OP_VEXT3,
5273 OP_VUZPL, // VUZP, left result
5274 OP_VUZPR, // VUZP, right result
5275 OP_VZIPL, // VZIP, left result
5276 OP_VZIPR, // VZIP, right result
5277 OP_VTRNL, // VTRN, left result
5278 OP_VTRNR // VTRN, right result
5279 };
5280
5281 if (OpNum == OP_COPY) {
5282 if (LHSID == (1*9+2)*9+3) return LHS;
5283 assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
5284 return RHS;
5285 }
5286
5287 SDValue OpLHS, OpRHS;
5288 OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
5289 OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
5290 EVT VT = OpLHS.getValueType();
5291
5292 switch (OpNum) {
5293 default: llvm_unreachable("Unknown shuffle opcode!");
5294 case OP_VREV:
Tanya Lattner48b182c2011-05-18 06:42:21 +00005295 // VREV divides the vector in half and swaps within the half.
Tanya Lattner1d117202011-05-18 21:44:54 +00005296 if (VT.getVectorElementType() == MVT::i32 ||
5297 VT.getVectorElementType() == MVT::f32)
Tanya Lattner48b182c2011-05-18 06:42:21 +00005298 return DAG.getNode(ARMISD::VREV64, dl, VT, OpLHS);
5299 // vrev <4 x i16> -> VREV32
5300 if (VT.getVectorElementType() == MVT::i16)
5301 return DAG.getNode(ARMISD::VREV32, dl, VT, OpLHS);
5302 // vrev <4 x i8> -> VREV16
5303 assert(VT.getVectorElementType() == MVT::i8);
5304 return DAG.getNode(ARMISD::VREV16, dl, VT, OpLHS);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005305 case OP_VDUP0:
5306 case OP_VDUP1:
5307 case OP_VDUP2:
5308 case OP_VDUP3:
5309 return DAG.getNode(ARMISD::VDUPLANE, dl, VT,
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005310 OpLHS, DAG.getConstant(OpNum-OP_VDUP0, MVT::i32));
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005311 case OP_VEXT1:
5312 case OP_VEXT2:
5313 case OP_VEXT3:
5314 return DAG.getNode(ARMISD::VEXT, dl, VT,
5315 OpLHS, OpRHS,
5316 DAG.getConstant(OpNum-OP_VEXT1+1, MVT::i32));
5317 case OP_VUZPL:
5318 case OP_VUZPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005319 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005320 OpLHS, OpRHS).getValue(OpNum-OP_VUZPL);
5321 case OP_VZIPL:
5322 case OP_VZIPR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005323 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005324 OpLHS, OpRHS).getValue(OpNum-OP_VZIPL);
5325 case OP_VTRNL:
5326 case OP_VTRNR:
Anton Korobeynikov232b19c2009-08-21 12:41:42 +00005327 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5328 OpLHS, OpRHS).getValue(OpNum-OP_VTRNL);
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005329 }
5330}
5331
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005332static SDValue LowerVECTOR_SHUFFLEv8i8(SDValue Op,
Benjamin Kramer339ced42012-01-15 13:16:05 +00005333 ArrayRef<int> ShuffleMask,
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005334 SelectionDAG &DAG) {
5335 // Check to see if we can use the VTBL instruction.
5336 SDValue V1 = Op.getOperand(0);
5337 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005338 SDLoc DL(Op);
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005339
5340 SmallVector<SDValue, 8> VTBLMask;
Benjamin Kramer339ced42012-01-15 13:16:05 +00005341 for (ArrayRef<int>::iterator
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005342 I = ShuffleMask.begin(), E = ShuffleMask.end(); I != E; ++I)
5343 VTBLMask.push_back(DAG.getConstant(*I, MVT::i32));
5344
5345 if (V2.getNode()->getOpcode() == ISD::UNDEF)
5346 return DAG.getNode(ARMISD::VTBL1, DL, MVT::v8i8, V1,
Craig Topper48d114b2014-04-26 18:35:24 +00005347 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlingebecb332011-03-15 20:47:26 +00005348
Owen Anderson77aa2662011-04-05 21:48:57 +00005349 return DAG.getNode(ARMISD::VTBL2, DL, MVT::v8i8, V1, V2,
Craig Topper48d114b2014-04-26 18:35:24 +00005350 DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i8, VTBLMask));
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005351}
5352
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005353static SDValue LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(SDValue Op,
5354 SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005355 SDLoc DL(Op);
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005356 SDValue OpLHS = Op.getOperand(0);
5357 EVT VT = OpLHS.getValueType();
5358
5359 assert((VT == MVT::v8i16 || VT == MVT::v16i8) &&
5360 "Expect an v8i16/v16i8 type");
5361 OpLHS = DAG.getNode(ARMISD::VREV64, DL, VT, OpLHS);
5362 // For a v16i8 type: After the VREV, we have got <8, ...15, 8, ..., 0>. Now,
5363 // extract the first 8 bytes into the top double word and the last 8 bytes
5364 // into the bottom double word. The v8i16 case is similar.
5365 unsigned ExtractNum = (VT == MVT::v16i8) ? 8 : 4;
5366 return DAG.getNode(ARMISD::VEXT, DL, VT, OpLHS, OpLHS,
5367 DAG.getConstant(ExtractNum, MVT::i32));
5368}
5369
Bob Wilson2e076c42009-06-22 23:27:02 +00005370static SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005371 SDValue V1 = Op.getOperand(0);
5372 SDValue V2 = Op.getOperand(1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005373 SDLoc dl(Op);
Bob Wilsonea3a4022009-08-12 22:31:50 +00005374 EVT VT = Op.getValueType();
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005375 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op.getNode());
Bob Wilsonea3a4022009-08-12 22:31:50 +00005376
Bob Wilsonc6800b52009-08-13 02:13:04 +00005377 // Convert shuffles that are directly supported on NEON to target-specific
5378 // DAG nodes, instead of keeping them as shuffles and matching them again
5379 // during code selection. This is more efficient and avoids the possibility
5380 // of inconsistencies between legalization and selection.
Bob Wilson3e4c0122009-08-13 06:01:30 +00005381 // FIXME: floating-point vectors should be canonicalized to integer vectors
5382 // of the same time so that they get CSEd properly.
Benjamin Kramer339ced42012-01-15 13:16:05 +00005383 ArrayRef<int> ShuffleMask = SVN->getMask();
Anton Korobeynikovc32e99e2009-08-21 12:40:07 +00005384
Bob Wilson846bd792010-06-07 23:53:38 +00005385 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5386 if (EltSize <= 32) {
5387 if (ShuffleVectorSDNode::isSplatMask(&ShuffleMask[0], VT)) {
5388 int Lane = SVN->getSplatIndex();
5389 // If this is undef splat, generate it via "just" vdup, if possible.
5390 if (Lane == -1) Lane = 0;
Anton Korobeynikov4d237542009-11-02 00:12:06 +00005391
Dan Gohman198b7ff2011-11-03 21:49:52 +00005392 // Test if V1 is a SCALAR_TO_VECTOR.
Bob Wilson846bd792010-06-07 23:53:38 +00005393 if (Lane == 0 && V1.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5394 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5395 }
Dan Gohman198b7ff2011-11-03 21:49:52 +00005396 // Test if V1 is a BUILD_VECTOR which is equivalent to a SCALAR_TO_VECTOR
5397 // (and probably will turn into a SCALAR_TO_VECTOR once legalization
5398 // reaches it).
5399 if (Lane == 0 && V1.getOpcode() == ISD::BUILD_VECTOR &&
5400 !isa<ConstantSDNode>(V1.getOperand(0))) {
5401 bool IsScalarToVector = true;
5402 for (unsigned i = 1, e = V1.getNumOperands(); i != e; ++i)
5403 if (V1.getOperand(i).getOpcode() != ISD::UNDEF) {
5404 IsScalarToVector = false;
5405 break;
5406 }
5407 if (IsScalarToVector)
5408 return DAG.getNode(ARMISD::VDUP, dl, VT, V1.getOperand(0));
5409 }
Bob Wilson846bd792010-06-07 23:53:38 +00005410 return DAG.getNode(ARMISD::VDUPLANE, dl, VT, V1,
5411 DAG.getConstant(Lane, MVT::i32));
Bob Wilsoneb54d512009-08-14 05:13:08 +00005412 }
Bob Wilson846bd792010-06-07 23:53:38 +00005413
5414 bool ReverseVEXT;
5415 unsigned Imm;
5416 if (isVEXTMask(ShuffleMask, VT, ReverseVEXT, Imm)) {
5417 if (ReverseVEXT)
5418 std::swap(V1, V2);
5419 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V2,
5420 DAG.getConstant(Imm, MVT::i32));
5421 }
5422
5423 if (isVREVMask(ShuffleMask, VT, 64))
5424 return DAG.getNode(ARMISD::VREV64, dl, VT, V1);
5425 if (isVREVMask(ShuffleMask, VT, 32))
5426 return DAG.getNode(ARMISD::VREV32, dl, VT, V1);
5427 if (isVREVMask(ShuffleMask, VT, 16))
5428 return DAG.getNode(ARMISD::VREV16, dl, VT, V1);
5429
Quentin Colombet8e1fe842012-11-02 21:32:17 +00005430 if (V2->getOpcode() == ISD::UNDEF &&
5431 isSingletonVEXTMask(ShuffleMask, VT, Imm)) {
5432 return DAG.getNode(ARMISD::VEXT, dl, VT, V1, V1,
5433 DAG.getConstant(Imm, MVT::i32));
5434 }
5435
Bob Wilson846bd792010-06-07 23:53:38 +00005436 // Check for Neon shuffles that modify both input vectors in place.
5437 // If both results are used, i.e., if there are two shuffles with the same
5438 // source operands and with masks corresponding to both results of one of
5439 // these operations, DAG memoization will ensure that a single node is
5440 // used for both shuffles.
5441 unsigned WhichResult;
5442 if (isVTRNMask(ShuffleMask, VT, WhichResult))
5443 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5444 V1, V2).getValue(WhichResult);
5445 if (isVUZPMask(ShuffleMask, VT, WhichResult))
5446 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5447 V1, V2).getValue(WhichResult);
5448 if (isVZIPMask(ShuffleMask, VT, WhichResult))
5449 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5450 V1, V2).getValue(WhichResult);
5451
5452 if (isVTRN_v_undef_Mask(ShuffleMask, VT, WhichResult))
5453 return DAG.getNode(ARMISD::VTRN, dl, DAG.getVTList(VT, VT),
5454 V1, V1).getValue(WhichResult);
5455 if (isVUZP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5456 return DAG.getNode(ARMISD::VUZP, dl, DAG.getVTList(VT, VT),
5457 V1, V1).getValue(WhichResult);
5458 if (isVZIP_v_undef_Mask(ShuffleMask, VT, WhichResult))
5459 return DAG.getNode(ARMISD::VZIP, dl, DAG.getVTList(VT, VT),
5460 V1, V1).getValue(WhichResult);
Bob Wilsoncce31f62009-08-14 05:08:32 +00005461 }
Bob Wilson32cd8552009-08-19 17:03:43 +00005462
Bob Wilsona7062312009-08-21 20:54:19 +00005463 // If the shuffle is not directly supported and it has 4 elements, use
5464 // the PerfectShuffle-generated table to synthesize it from other shuffles.
Bob Wilson91fdf682010-05-22 00:23:12 +00005465 unsigned NumElts = VT.getVectorNumElements();
5466 if (NumElts == 4) {
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005467 unsigned PFIndexes[4];
5468 for (unsigned i = 0; i != 4; ++i) {
5469 if (ShuffleMask[i] < 0)
5470 PFIndexes[i] = 8;
5471 else
5472 PFIndexes[i] = ShuffleMask[i];
5473 }
5474
5475 // Compute the index in the perfect shuffle table.
5476 unsigned PFTableIndex =
5477 PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
Anton Korobeynikov9a232f42009-08-21 12:41:24 +00005478 unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
5479 unsigned Cost = (PFEntry >> 30);
5480
5481 if (Cost <= 4)
5482 return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
5483 }
Bob Wilsonea3a4022009-08-12 22:31:50 +00005484
Bob Wilsond8a9a042010-06-04 00:04:02 +00005485 // Implement shuffles with 32- or 64-bit elements as ARMISD::BUILD_VECTORs.
Bob Wilson91fdf682010-05-22 00:23:12 +00005486 if (EltSize >= 32) {
5487 // Do the expansion with floating-point types, since that is what the VFP
5488 // registers are defined to use, and since i64 is not legal.
5489 EVT EltVT = EVT::getFloatingPointVT(EltSize);
5490 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), EltVT, NumElts);
Wesley Peck527da1b2010-11-23 03:31:01 +00005491 V1 = DAG.getNode(ISD::BITCAST, dl, VecVT, V1);
5492 V2 = DAG.getNode(ISD::BITCAST, dl, VecVT, V2);
Bob Wilsond8a9a042010-06-04 00:04:02 +00005493 SmallVector<SDValue, 8> Ops;
Bob Wilson91fdf682010-05-22 00:23:12 +00005494 for (unsigned i = 0; i < NumElts; ++i) {
Bob Wilson59549942010-05-20 18:39:53 +00005495 if (ShuffleMask[i] < 0)
Bob Wilsond8a9a042010-06-04 00:04:02 +00005496 Ops.push_back(DAG.getUNDEF(EltVT));
5497 else
5498 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT,
5499 ShuffleMask[i] < (int)NumElts ? V1 : V2,
5500 DAG.getConstant(ShuffleMask[i] & (NumElts-1),
5501 MVT::i32)));
Bob Wilson59549942010-05-20 18:39:53 +00005502 }
Craig Topper48d114b2014-04-26 18:35:24 +00005503 SDValue Val = DAG.getNode(ARMISD::BUILD_VECTOR, dl, VecVT, Ops);
Wesley Peck527da1b2010-11-23 03:31:01 +00005504 return DAG.getNode(ISD::BITCAST, dl, VT, Val);
Bob Wilson59549942010-05-20 18:39:53 +00005505 }
5506
Arnold Schwaighofer1f3d3ca2013-02-12 01:58:32 +00005507 if ((VT == MVT::v8i16 || VT == MVT::v16i8) && isReverseMask(ShuffleMask, VT))
5508 return LowerReverse_VECTOR_SHUFFLEv16i8_v8i16(Op, DAG);
5509
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00005510 if (VT == MVT::v8i8) {
5511 SDValue NewOp = LowerVECTOR_SHUFFLEv8i8(Op, ShuffleMask, DAG);
5512 if (NewOp.getNode())
5513 return NewOp;
5514 }
5515
Bob Wilson6f34e272009-08-14 05:16:33 +00005516 return SDValue();
Bob Wilson2e076c42009-06-22 23:27:02 +00005517}
5518
Eli Friedmana5e244c2011-10-24 23:08:52 +00005519static SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
5520 // INSERT_VECTOR_ELT is legal only for immediate indexes.
5521 SDValue Lane = Op.getOperand(2);
5522 if (!isa<ConstantSDNode>(Lane))
5523 return SDValue();
5524
5525 return Op;
5526}
5527
Bob Wilson2e076c42009-06-22 23:27:02 +00005528static SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Bob Wilsonceb49292010-11-03 16:24:50 +00005529 // EXTRACT_VECTOR_ELT is legal only for immediate indexes.
Bob Wilson2e076c42009-06-22 23:27:02 +00005530 SDValue Lane = Op.getOperand(1);
Bob Wilsonceb49292010-11-03 16:24:50 +00005531 if (!isa<ConstantSDNode>(Lane))
5532 return SDValue();
5533
5534 SDValue Vec = Op.getOperand(0);
5535 if (Op.getValueType() == MVT::i32 &&
5536 Vec.getValueType().getVectorElementType().getSizeInBits() < 32) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005537 SDLoc dl(Op);
Bob Wilsonceb49292010-11-03 16:24:50 +00005538 return DAG.getNode(ARMISD::VGETLANEu, dl, MVT::i32, Vec, Lane);
5539 }
5540
5541 return Op;
Bob Wilson2e076c42009-06-22 23:27:02 +00005542}
5543
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005544static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
5545 // The only time a CONCAT_VECTORS operation can have legal types is when
5546 // two 64-bit vectors are concatenated to a 128-bit vector.
5547 assert(Op.getValueType().is128BitVector() && Op.getNumOperands() == 2 &&
5548 "unexpected CONCAT_VECTORS");
Andrew Trickef9de2a2013-05-25 02:42:55 +00005549 SDLoc dl(Op);
Owen Anderson9f944592009-08-11 20:47:22 +00005550 SDValue Val = DAG.getUNDEF(MVT::v2f64);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005551 SDValue Op0 = Op.getOperand(0);
5552 SDValue Op1 = Op.getOperand(1);
5553 if (Op0.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005554 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005555 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op0),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005556 DAG.getIntPtrConstant(0));
5557 if (Op1.getOpcode() != ISD::UNDEF)
Owen Anderson9f944592009-08-11 20:47:22 +00005558 Val = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v2f64, Val,
Wesley Peck527da1b2010-11-23 03:31:01 +00005559 DAG.getNode(ISD::BITCAST, dl, MVT::f64, Op1),
Bob Wilsonf307e0b2009-08-03 20:36:38 +00005560 DAG.getIntPtrConstant(1));
Wesley Peck527da1b2010-11-23 03:31:01 +00005561 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Val);
Bob Wilson2e076c42009-06-22 23:27:02 +00005562}
5563
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005564/// isExtendedBUILD_VECTOR - Check if N is a constant BUILD_VECTOR where each
5565/// element has been zero/sign-extended, depending on the isSigned parameter,
5566/// from an integer type half its size.
5567static bool isExtendedBUILD_VECTOR(SDNode *N, SelectionDAG &DAG,
5568 bool isSigned) {
5569 // A v2i64 BUILD_VECTOR will have been legalized to a BITCAST from v4i32.
5570 EVT VT = N->getValueType(0);
5571 if (VT == MVT::v2i64 && N->getOpcode() == ISD::BITCAST) {
5572 SDNode *BVN = N->getOperand(0).getNode();
5573 if (BVN->getValueType(0) != MVT::v4i32 ||
5574 BVN->getOpcode() != ISD::BUILD_VECTOR)
5575 return false;
5576 unsigned LoElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
5577 unsigned HiElt = 1 - LoElt;
5578 ConstantSDNode *Lo0 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt));
5579 ConstantSDNode *Hi0 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt));
5580 ConstantSDNode *Lo1 = dyn_cast<ConstantSDNode>(BVN->getOperand(LoElt+2));
5581 ConstantSDNode *Hi1 = dyn_cast<ConstantSDNode>(BVN->getOperand(HiElt+2));
5582 if (!Lo0 || !Hi0 || !Lo1 || !Hi1)
5583 return false;
5584 if (isSigned) {
5585 if (Hi0->getSExtValue() == Lo0->getSExtValue() >> 32 &&
5586 Hi1->getSExtValue() == Lo1->getSExtValue() >> 32)
5587 return true;
5588 } else {
5589 if (Hi0->isNullValue() && Hi1->isNullValue())
5590 return true;
5591 }
5592 return false;
5593 }
5594
5595 if (N->getOpcode() != ISD::BUILD_VECTOR)
5596 return false;
5597
5598 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
5599 SDNode *Elt = N->getOperand(i).getNode();
5600 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
5601 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
5602 unsigned HalfSize = EltSize / 2;
5603 if (isSigned) {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005604 if (!isIntN(HalfSize, C->getSExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005605 return false;
5606 } else {
Bob Wilson93b0f7b2011-10-18 18:46:49 +00005607 if (!isUIntN(HalfSize, C->getZExtValue()))
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005608 return false;
5609 }
5610 continue;
5611 }
5612 return false;
5613 }
5614
5615 return true;
5616}
5617
5618/// isSignExtended - Check if a node is a vector value that is sign-extended
5619/// or a constant BUILD_VECTOR with sign-extended elements.
5620static bool isSignExtended(SDNode *N, SelectionDAG &DAG) {
5621 if (N->getOpcode() == ISD::SIGN_EXTEND || ISD::isSEXTLoad(N))
5622 return true;
5623 if (isExtendedBUILD_VECTOR(N, DAG, true))
5624 return true;
5625 return false;
5626}
5627
5628/// isZeroExtended - Check if a node is a vector value that is zero-extended
5629/// or a constant BUILD_VECTOR with zero-extended elements.
5630static bool isZeroExtended(SDNode *N, SelectionDAG &DAG) {
5631 if (N->getOpcode() == ISD::ZERO_EXTEND || ISD::isZEXTLoad(N))
5632 return true;
5633 if (isExtendedBUILD_VECTOR(N, DAG, false))
5634 return true;
5635 return false;
5636}
5637
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005638static EVT getExtensionTo64Bits(const EVT &OrigVT) {
5639 if (OrigVT.getSizeInBits() >= 64)
5640 return OrigVT;
5641
5642 assert(OrigVT.isSimple() && "Expecting a simple value type");
5643
5644 MVT::SimpleValueType OrigSimpleTy = OrigVT.getSimpleVT().SimpleTy;
5645 switch (OrigSimpleTy) {
5646 default: llvm_unreachable("Unexpected Vector Type");
5647 case MVT::v2i8:
5648 case MVT::v2i16:
5649 return MVT::v2i32;
5650 case MVT::v4i8:
5651 return MVT::v4i16;
5652 }
5653}
5654
Sebastian Popa204f722012-11-30 19:08:04 +00005655/// AddRequiredExtensionForVMULL - Add a sign/zero extension to extend the total
5656/// value size to 64 bits. We need a 64-bit D register as an operand to VMULL.
5657/// We insert the required extension here to get the vector to fill a D register.
5658static SDValue AddRequiredExtensionForVMULL(SDValue N, SelectionDAG &DAG,
5659 const EVT &OrigTy,
5660 const EVT &ExtTy,
5661 unsigned ExtOpcode) {
5662 // The vector originally had a size of OrigTy. It was then extended to ExtTy.
5663 // We expect the ExtTy to be 128-bits total. If the OrigTy is less than
5664 // 64-bits we need to insert a new extension so that it will be 64-bits.
5665 assert(ExtTy.is128BitVector() && "Unexpected extension size");
5666 if (OrigTy.getSizeInBits() >= 64)
5667 return N;
5668
5669 // Must extend size to at least 64 bits to be used as an operand for VMULL.
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005670 EVT NewVT = getExtensionTo64Bits(OrigTy);
5671
Andrew Trickef9de2a2013-05-25 02:42:55 +00005672 return DAG.getNode(ExtOpcode, SDLoc(N), NewVT, N);
Sebastian Popa204f722012-11-30 19:08:04 +00005673}
5674
5675/// SkipLoadExtensionForVMULL - return a load of the original vector size that
5676/// does not do any sign/zero extension. If the original vector is less
5677/// than 64 bits, an appropriate extension will be added after the load to
5678/// reach a total size of 64 bits. We have to add the extension separately
5679/// because ARM does not have a sign/zero extending load for vectors.
5680static SDValue SkipLoadExtensionForVMULL(LoadSDNode *LD, SelectionDAG& DAG) {
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005681 EVT ExtendedTy = getExtensionTo64Bits(LD->getMemoryVT());
5682
5683 // The load already has the right type.
5684 if (ExtendedTy == LD->getMemoryVT())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005685 return DAG.getLoad(LD->getMemoryVT(), SDLoc(LD), LD->getChain(),
Sebastian Popa204f722012-11-30 19:08:04 +00005686 LD->getBasePtr(), LD->getPointerInfo(), LD->isVolatile(),
5687 LD->isNonTemporal(), LD->isInvariant(),
5688 LD->getAlignment());
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005689
5690 // We need to create a zextload/sextload. We cannot just create a load
5691 // followed by a zext/zext node because LowerMUL is also run during normal
5692 // operation legalization where we can't create illegal types.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005693 return DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD), ExtendedTy,
Arnold Schwaighoferaf85f602013-05-14 22:33:24 +00005694 LD->getChain(), LD->getBasePtr(), LD->getPointerInfo(),
5695 LD->getMemoryVT(), LD->isVolatile(),
5696 LD->isNonTemporal(), LD->getAlignment());
Sebastian Popa204f722012-11-30 19:08:04 +00005697}
5698
5699/// SkipExtensionForVMULL - For a node that is a SIGN_EXTEND, ZERO_EXTEND,
5700/// extending load, or BUILD_VECTOR with extended elements, return the
5701/// unextended value. The unextended vector should be 64 bits so that it can
5702/// be used as an operand to a VMULL instruction. If the original vector size
5703/// before extension is less than 64 bits we add a an extension to resize
5704/// the vector to 64 bits.
5705static SDValue SkipExtensionForVMULL(SDNode *N, SelectionDAG &DAG) {
Bob Wilson38ab35a2010-09-01 23:50:19 +00005706 if (N->getOpcode() == ISD::SIGN_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND)
Sebastian Popa204f722012-11-30 19:08:04 +00005707 return AddRequiredExtensionForVMULL(N->getOperand(0), DAG,
5708 N->getOperand(0)->getValueType(0),
5709 N->getValueType(0),
5710 N->getOpcode());
5711
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005712 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N))
Sebastian Popa204f722012-11-30 19:08:04 +00005713 return SkipLoadExtensionForVMULL(LD, DAG);
5714
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005715 // Otherwise, the value must be a BUILD_VECTOR. For v2i64, it will
5716 // have been legalized as a BITCAST from v4i32.
5717 if (N->getOpcode() == ISD::BITCAST) {
5718 SDNode *BVN = N->getOperand(0).getNode();
5719 assert(BVN->getOpcode() == ISD::BUILD_VECTOR &&
5720 BVN->getValueType(0) == MVT::v4i32 && "expected v4i32 BUILD_VECTOR");
5721 unsigned LowElt = DAG.getTargetLoweringInfo().isBigEndian() ? 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00005722 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), MVT::v2i32,
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005723 BVN->getOperand(LowElt), BVN->getOperand(LowElt+2));
5724 }
5725 // Construct a new BUILD_VECTOR with elements truncated to half the size.
5726 assert(N->getOpcode() == ISD::BUILD_VECTOR && "expected BUILD_VECTOR");
5727 EVT VT = N->getValueType(0);
5728 unsigned EltSize = VT.getVectorElementType().getSizeInBits() / 2;
5729 unsigned NumElts = VT.getVectorNumElements();
5730 MVT TruncVT = MVT::getIntegerVT(EltSize);
5731 SmallVector<SDValue, 8> Ops;
5732 for (unsigned i = 0; i != NumElts; ++i) {
5733 ConstantSDNode *C = cast<ConstantSDNode>(N->getOperand(i));
5734 const APInt &CInt = C->getAPIntValue();
Bob Wilson9245c932012-04-30 16:53:34 +00005735 // Element types smaller than 32 bits are not legal, so use i32 elements.
5736 // The values are implicitly truncated so sext vs. zext doesn't matter.
5737 Ops.push_back(DAG.getConstant(CInt.zextOrTrunc(32), MVT::i32));
Bob Wilsond7d2cf72010-11-23 19:38:38 +00005738 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005739 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topper48d114b2014-04-26 18:35:24 +00005740 MVT::getVectorVT(TruncVT, NumElts), Ops);
Bob Wilson38ab35a2010-09-01 23:50:19 +00005741}
5742
Evan Chenge2086e72011-03-29 01:56:09 +00005743static bool isAddSubSExt(SDNode *N, SelectionDAG &DAG) {
5744 unsigned Opcode = N->getOpcode();
5745 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5746 SDNode *N0 = N->getOperand(0).getNode();
5747 SDNode *N1 = N->getOperand(1).getNode();
5748 return N0->hasOneUse() && N1->hasOneUse() &&
5749 isSignExtended(N0, DAG) && isSignExtended(N1, DAG);
5750 }
5751 return false;
5752}
5753
5754static bool isAddSubZExt(SDNode *N, SelectionDAG &DAG) {
5755 unsigned Opcode = N->getOpcode();
5756 if (Opcode == ISD::ADD || Opcode == ISD::SUB) {
5757 SDNode *N0 = N->getOperand(0).getNode();
5758 SDNode *N1 = N->getOperand(1).getNode();
5759 return N0->hasOneUse() && N1->hasOneUse() &&
5760 isZeroExtended(N0, DAG) && isZeroExtended(N1, DAG);
5761 }
5762 return false;
5763}
5764
Bob Wilson38ab35a2010-09-01 23:50:19 +00005765static SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) {
5766 // Multiplications are only custom-lowered for 128-bit vectors so that
5767 // VMULL can be detected. Otherwise v2i64 multiplications are not legal.
5768 EVT VT = Op.getValueType();
Sebastian Popa204f722012-11-30 19:08:04 +00005769 assert(VT.is128BitVector() && VT.isInteger() &&
5770 "unexpected type for custom-lowering ISD::MUL");
Bob Wilson38ab35a2010-09-01 23:50:19 +00005771 SDNode *N0 = Op.getOperand(0).getNode();
5772 SDNode *N1 = Op.getOperand(1).getNode();
5773 unsigned NewOpc = 0;
Evan Chenge2086e72011-03-29 01:56:09 +00005774 bool isMLA = false;
5775 bool isN0SExt = isSignExtended(N0, DAG);
5776 bool isN1SExt = isSignExtended(N1, DAG);
5777 if (isN0SExt && isN1SExt)
Bob Wilson38ab35a2010-09-01 23:50:19 +00005778 NewOpc = ARMISD::VMULLs;
Evan Chenge2086e72011-03-29 01:56:09 +00005779 else {
5780 bool isN0ZExt = isZeroExtended(N0, DAG);
5781 bool isN1ZExt = isZeroExtended(N1, DAG);
5782 if (isN0ZExt && isN1ZExt)
5783 NewOpc = ARMISD::VMULLu;
5784 else if (isN1SExt || isN1ZExt) {
5785 // Look for (s/zext A + s/zext B) * (s/zext C). We want to turn these
5786 // into (s/zext A * s/zext C) + (s/zext B * s/zext C)
5787 if (isN1SExt && isAddSubSExt(N0, DAG)) {
5788 NewOpc = ARMISD::VMULLs;
5789 isMLA = true;
5790 } else if (isN1ZExt && isAddSubZExt(N0, DAG)) {
5791 NewOpc = ARMISD::VMULLu;
5792 isMLA = true;
5793 } else if (isN0ZExt && isAddSubZExt(N1, DAG)) {
5794 std::swap(N0, N1);
5795 NewOpc = ARMISD::VMULLu;
5796 isMLA = true;
5797 }
5798 }
5799
5800 if (!NewOpc) {
5801 if (VT == MVT::v2i64)
5802 // Fall through to expand this. It is not legal.
5803 return SDValue();
5804 else
5805 // Other vector multiplications are legal.
5806 return Op;
5807 }
5808 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005809
5810 // Legalize to a VMULL instruction.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005811 SDLoc DL(Op);
Evan Chenge2086e72011-03-29 01:56:09 +00005812 SDValue Op0;
Sebastian Popa204f722012-11-30 19:08:04 +00005813 SDValue Op1 = SkipExtensionForVMULL(N1, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005814 if (!isMLA) {
Sebastian Popa204f722012-11-30 19:08:04 +00005815 Op0 = SkipExtensionForVMULL(N0, DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005816 assert(Op0.getValueType().is64BitVector() &&
5817 Op1.getValueType().is64BitVector() &&
5818 "unexpected types for extended operands to VMULL");
5819 return DAG.getNode(NewOpc, DL, VT, Op0, Op1);
5820 }
Bob Wilson38ab35a2010-09-01 23:50:19 +00005821
Evan Chenge2086e72011-03-29 01:56:09 +00005822 // Optimizing (zext A + zext B) * C, to (VMULL A, C) + (VMULL B, C) during
5823 // isel lowering to take advantage of no-stall back to back vmul + vmla.
5824 // vmull q0, d4, d6
5825 // vmlal q0, d5, d6
5826 // is faster than
5827 // vaddl q0, d4, d5
5828 // vmovl q1, d6
5829 // vmul q0, q0, q1
Sebastian Popa204f722012-11-30 19:08:04 +00005830 SDValue N00 = SkipExtensionForVMULL(N0->getOperand(0).getNode(), DAG);
5831 SDValue N01 = SkipExtensionForVMULL(N0->getOperand(1).getNode(), DAG);
Evan Chenge2086e72011-03-29 01:56:09 +00005832 EVT Op1VT = Op1.getValueType();
5833 return DAG.getNode(N0->getOpcode(), DL, VT,
5834 DAG.getNode(NewOpc, DL, VT,
5835 DAG.getNode(ISD::BITCAST, DL, Op1VT, N00), Op1),
5836 DAG.getNode(NewOpc, DL, VT,
5837 DAG.getNode(ISD::BITCAST, DL, Op1VT, N01), Op1));
Bob Wilson38ab35a2010-09-01 23:50:19 +00005838}
5839
Owen Anderson77aa2662011-04-05 21:48:57 +00005840static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005841LowerSDIV_v4i8(SDValue X, SDValue Y, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005842 // Convert to float
5843 // float4 xf = vcvt_f32_s32(vmovl_s16(a.lo));
5844 // float4 yf = vcvt_f32_s32(vmovl_s16(b.lo));
5845 X = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, X);
5846 Y = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Y);
5847 X = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, X);
5848 Y = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, Y);
5849 // Get reciprocal estimate.
5850 // float4 recip = vrecpeq_f32(yf);
Owen Anderson77aa2662011-04-05 21:48:57 +00005851 Y = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005852 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), Y);
5853 // Because char has a smaller range than uchar, we can actually get away
5854 // without any newton steps. This requires that we use a weird bias
5855 // of 0xb000, however (again, this has been exhaustively tested).
5856 // float4 result = as_float4(as_int4(xf*recip) + 0xb000);
5857 X = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, X, Y);
5858 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, X);
5859 Y = DAG.getConstant(0xb000, MVT::i32);
5860 Y = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Y, Y, Y, Y);
5861 X = DAG.getNode(ISD::ADD, dl, MVT::v4i32, X, Y);
5862 X = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, X);
5863 // Convert back to short.
5864 X = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, X);
5865 X = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, X);
5866 return X;
5867}
5868
Owen Anderson77aa2662011-04-05 21:48:57 +00005869static SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +00005870LowerSDIV_v4i16(SDValue N0, SDValue N1, SDLoc dl, SelectionDAG &DAG) {
Nate Begemanfa62d502011-02-11 20:53:29 +00005871 SDValue N2;
5872 // Convert to float.
5873 // float4 yf = vcvt_f32_s32(vmovl_s16(y));
5874 // float4 xf = vcvt_f32_s32(vmovl_s16(x));
5875 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N0);
5876 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, N1);
5877 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
5878 N1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005879
Nate Begemanfa62d502011-02-11 20:53:29 +00005880 // Use reciprocal estimate and one refinement step.
5881 // float4 recip = vrecpeq_f32(yf);
5882 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00005883 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005884 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005885 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005886 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
5887 N1, N2);
5888 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5889 // Because short has a smaller range than ushort, we can actually get away
5890 // with only a single newton step. This requires that we use a weird bias
5891 // of 89, however (again, this has been exhaustively tested).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005892 // float4 result = as_float4(as_int4(xf*recip) + 0x89);
Nate Begemanfa62d502011-02-11 20:53:29 +00005893 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
5894 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005895 N1 = DAG.getConstant(0x89, MVT::i32);
Nate Begemanfa62d502011-02-11 20:53:29 +00005896 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
5897 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
5898 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
5899 // Convert back to integer and return.
5900 // return vmovn_s32(vcvt_s32_f32(result));
5901 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
5902 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
5903 return N0;
5904}
5905
5906static SDValue LowerSDIV(SDValue Op, SelectionDAG &DAG) {
5907 EVT VT = Op.getValueType();
5908 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5909 "unexpected type for custom-lowering ISD::SDIV");
5910
Andrew Trickef9de2a2013-05-25 02:42:55 +00005911 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00005912 SDValue N0 = Op.getOperand(0);
5913 SDValue N1 = Op.getOperand(1);
5914 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00005915
Nate Begemanfa62d502011-02-11 20:53:29 +00005916 if (VT == MVT::v8i8) {
5917 N0 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N0);
5918 N1 = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005919
Nate Begemanfa62d502011-02-11 20:53:29 +00005920 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5921 DAG.getIntPtrConstant(4));
5922 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00005923 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00005924 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5925 DAG.getIntPtrConstant(0));
5926 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5927 DAG.getIntPtrConstant(0));
5928
5929 N0 = LowerSDIV_v4i8(N0, N1, dl, DAG); // v4i16
5930 N2 = LowerSDIV_v4i8(N2, N3, dl, DAG); // v4i16
5931
5932 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5933 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00005934
Nate Begemanfa62d502011-02-11 20:53:29 +00005935 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i8, N0);
5936 return N0;
5937 }
5938 return LowerSDIV_v4i16(N0, N1, dl, DAG);
5939}
5940
5941static SDValue LowerUDIV(SDValue Op, SelectionDAG &DAG) {
5942 EVT VT = Op.getValueType();
5943 assert((VT == MVT::v4i16 || VT == MVT::v8i8) &&
5944 "unexpected type for custom-lowering ISD::UDIV");
5945
Andrew Trickef9de2a2013-05-25 02:42:55 +00005946 SDLoc dl(Op);
Nate Begemanfa62d502011-02-11 20:53:29 +00005947 SDValue N0 = Op.getOperand(0);
5948 SDValue N1 = Op.getOperand(1);
5949 SDValue N2, N3;
Owen Anderson77aa2662011-04-05 21:48:57 +00005950
Nate Begemanfa62d502011-02-11 20:53:29 +00005951 if (VT == MVT::v8i8) {
5952 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N0);
5953 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v8i16, N1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005954
Nate Begemanfa62d502011-02-11 20:53:29 +00005955 N2 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5956 DAG.getIntPtrConstant(4));
5957 N3 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
Owen Anderson77aa2662011-04-05 21:48:57 +00005958 DAG.getIntPtrConstant(4));
Nate Begemanfa62d502011-02-11 20:53:29 +00005959 N0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N0,
5960 DAG.getIntPtrConstant(0));
5961 N1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v4i16, N1,
5962 DAG.getIntPtrConstant(0));
Owen Anderson77aa2662011-04-05 21:48:57 +00005963
Nate Begemanfa62d502011-02-11 20:53:29 +00005964 N0 = LowerSDIV_v4i16(N0, N1, dl, DAG); // v4i16
5965 N2 = LowerSDIV_v4i16(N2, N3, dl, DAG); // v4i16
Owen Anderson77aa2662011-04-05 21:48:57 +00005966
Nate Begemanfa62d502011-02-11 20:53:29 +00005967 N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, N0, N2);
5968 N0 = LowerCONCAT_VECTORS(N0, DAG);
Owen Anderson77aa2662011-04-05 21:48:57 +00005969
5970 N0 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v8i8,
Nate Begemanfa62d502011-02-11 20:53:29 +00005971 DAG.getConstant(Intrinsic::arm_neon_vqmovnsu, MVT::i32),
5972 N0);
5973 return N0;
5974 }
Owen Anderson77aa2662011-04-05 21:48:57 +00005975
Nate Begemanfa62d502011-02-11 20:53:29 +00005976 // v4i16 sdiv ... Convert to float.
5977 // float4 yf = vcvt_f32_s32(vmovl_u16(y));
5978 // float4 xf = vcvt_f32_s32(vmovl_u16(x));
5979 N0 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N0);
5980 N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v4i32, N1);
5981 N0 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N0);
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005982 SDValue BN1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::v4f32, N1);
Nate Begemanfa62d502011-02-11 20:53:29 +00005983
5984 // Use reciprocal estimate and two refinement steps.
5985 // float4 recip = vrecpeq_f32(yf);
5986 // recip *= vrecpsq_f32(yf, recip);
5987 // recip *= vrecpsq_f32(yf, recip);
Owen Anderson77aa2662011-04-05 21:48:57 +00005988 N2 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005989 DAG.getConstant(Intrinsic::arm_neon_vrecpe, MVT::i32), BN1);
Owen Anderson77aa2662011-04-05 21:48:57 +00005990 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005991 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005992 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00005993 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
Owen Anderson77aa2662011-04-05 21:48:57 +00005994 N1 = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, MVT::v4f32,
Nate Begemanfa62d502011-02-11 20:53:29 +00005995 DAG.getConstant(Intrinsic::arm_neon_vrecps, MVT::i32),
Mon P Wang6d9e1c72011-05-19 04:15:07 +00005996 BN1, N2);
Nate Begemanfa62d502011-02-11 20:53:29 +00005997 N2 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N1, N2);
5998 // Simply multiplying by the reciprocal estimate can leave us a few ulps
5999 // too low, so we add 2 ulps (exhaustive testing shows that this is enough,
6000 // and that it will never cause us to return an answer too large).
Mon P Wang6d9e1c72011-05-19 04:15:07 +00006001 // float4 result = as_float4(as_int4(xf*recip) + 2);
Nate Begemanfa62d502011-02-11 20:53:29 +00006002 N0 = DAG.getNode(ISD::FMUL, dl, MVT::v4f32, N0, N2);
6003 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, N0);
6004 N1 = DAG.getConstant(2, MVT::i32);
6005 N1 = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, N1, N1, N1, N1);
6006 N0 = DAG.getNode(ISD::ADD, dl, MVT::v4i32, N0, N1);
6007 N0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, N0);
6008 // Convert back to integer and return.
6009 // return vmovn_u32(vcvt_s32_f32(result));
6010 N0 = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::v4i32, N0);
6011 N0 = DAG.getNode(ISD::TRUNCATE, dl, MVT::v4i16, N0);
6012 return N0;
6013}
6014
Evan Chenge8916542011-08-30 01:34:54 +00006015static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
6016 EVT VT = Op.getNode()->getValueType(0);
6017 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
6018
6019 unsigned Opc;
6020 bool ExtraOp = false;
6021 switch (Op.getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00006022 default: llvm_unreachable("Invalid code");
Evan Chenge8916542011-08-30 01:34:54 +00006023 case ISD::ADDC: Opc = ARMISD::ADDC; break;
6024 case ISD::ADDE: Opc = ARMISD::ADDE; ExtraOp = true; break;
6025 case ISD::SUBC: Opc = ARMISD::SUBC; break;
6026 case ISD::SUBE: Opc = ARMISD::SUBE; ExtraOp = true; break;
6027 }
6028
6029 if (!ExtraOp)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006030 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006031 Op.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006032 return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
Evan Chenge8916542011-08-30 01:34:54 +00006033 Op.getOperand(1), Op.getOperand(2));
6034}
6035
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006036SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
6037 assert(Subtarget->isTargetDarwin());
6038
6039 // For iOS, we want to call an alternative entry point: __sincos_stret,
6040 // return values are passed via sret.
6041 SDLoc dl(Op);
6042 SDValue Arg = Op.getOperand(0);
6043 EVT ArgVT = Arg.getValueType();
6044 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
6045
6046 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
6047 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
6048
6049 // Pair of floats / doubles used to pass the result.
6050 StructType *RetTy = StructType::get(ArgTy, ArgTy, NULL);
6051
6052 // Create stack object for sret.
6053 const uint64_t ByteSize = TLI.getDataLayout()->getTypeAllocSize(RetTy);
6054 const unsigned StackAlign = TLI.getDataLayout()->getPrefTypeAlignment(RetTy);
6055 int FrameIdx = FrameInfo->CreateStackObject(ByteSize, StackAlign, false);
6056 SDValue SRet = DAG.getFrameIndex(FrameIdx, TLI.getPointerTy());
6057
6058 ArgListTy Args;
6059 ArgListEntry Entry;
6060
6061 Entry.Node = SRet;
6062 Entry.Ty = RetTy->getPointerTo();
6063 Entry.isSExt = false;
6064 Entry.isZExt = false;
6065 Entry.isSRet = true;
6066 Args.push_back(Entry);
6067
6068 Entry.Node = Arg;
6069 Entry.Ty = ArgTy;
6070 Entry.isSExt = false;
6071 Entry.isZExt = false;
6072 Args.push_back(Entry);
6073
6074 const char *LibcallName = (ArgVT == MVT::f64)
6075 ? "__sincos_stret" : "__sincosf_stret";
6076 SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy());
6077
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +00006078 TargetLowering::CallLoweringInfo CLI(DAG);
6079 CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
6080 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), Callee,
6081 &Args, 0)
6082 .setDiscardResult();
6083
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006084 std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
6085
6086 SDValue LoadSin = DAG.getLoad(ArgVT, dl, CallResult.second, SRet,
6087 MachinePointerInfo(), false, false, false, 0);
6088
6089 // Address of cos field.
6090 SDValue Add = DAG.getNode(ISD::ADD, dl, getPointerTy(), SRet,
6091 DAG.getIntPtrConstant(ArgVT.getStoreSize()));
6092 SDValue LoadCos = DAG.getLoad(ArgVT, dl, LoadSin.getValue(1), Add,
6093 MachinePointerInfo(), false, false, false, 0);
6094
6095 SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
6096 return DAG.getNode(ISD::MERGE_VALUES, dl, Tys,
6097 LoadSin.getValue(0), LoadCos.getValue(0));
6098}
6099
Eli Friedman10f9ce22011-09-15 22:26:18 +00006100static SDValue LowerAtomicLoadStore(SDValue Op, SelectionDAG &DAG) {
Eli Friedmanba912e02011-09-15 22:18:49 +00006101 // Monotonic load/store is legal for all targets
6102 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
6103 return Op;
6104
Alp Tokercb402912014-01-24 17:20:08 +00006105 // Acquire/Release load/store is not legal for targets without a
Eli Friedmanba912e02011-09-15 22:18:49 +00006106 // dmb or equivalent available.
6107 return SDValue();
6108}
6109
Tim Northoverbc933082013-05-23 19:11:20 +00006110static void ReplaceREADCYCLECOUNTER(SDNode *N,
6111 SmallVectorImpl<SDValue> &Results,
6112 SelectionDAG &DAG,
6113 const ARMSubtarget *Subtarget) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006114 SDLoc DL(N);
Tim Northoverbc933082013-05-23 19:11:20 +00006115 SDValue Cycles32, OutChain;
6116
6117 if (Subtarget->hasPerfMon()) {
6118 // Under Power Management extensions, the cycle-count is:
6119 // mrc p15, #0, <Rt>, c9, c13, #0
6120 SDValue Ops[] = { N->getOperand(0), // Chain
6121 DAG.getConstant(Intrinsic::arm_mrc, MVT::i32),
6122 DAG.getConstant(15, MVT::i32),
6123 DAG.getConstant(0, MVT::i32),
6124 DAG.getConstant(9, MVT::i32),
6125 DAG.getConstant(13, MVT::i32),
6126 DAG.getConstant(0, MVT::i32)
6127 };
6128
6129 Cycles32 = DAG.getNode(ISD::INTRINSIC_W_CHAIN, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00006130 DAG.getVTList(MVT::i32, MVT::Other), Ops);
Tim Northoverbc933082013-05-23 19:11:20 +00006131 OutChain = Cycles32.getValue(1);
6132 } else {
6133 // Intrinsic is defined to return 0 on unsupported platforms. Technically
6134 // there are older ARM CPUs that have implementation-specific ways of
6135 // obtaining this information (FIXME!).
6136 Cycles32 = DAG.getConstant(0, MVT::i32);
6137 OutChain = DAG.getEntryNode();
6138 }
6139
6140
6141 SDValue Cycles64 = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64,
6142 Cycles32, DAG.getConstant(0, MVT::i32));
6143 Results.push_back(Cycles64);
6144 Results.push_back(OutChain);
6145}
6146
Dan Gohman21cea8a2010-04-17 15:26:15 +00006147SDValue ARMTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng10043e22007-01-19 07:51:42 +00006148 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00006149 default: llvm_unreachable("Don't know how to custom lower this!");
Evan Cheng10043e22007-01-19 07:51:42 +00006150 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Bob Wilson1cf0b032009-10-30 05:45:42 +00006151 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006152 case ISD::GlobalAddress:
Saleem Abdulrasool40bca0a2014-05-09 00:58:32 +00006153 switch (Subtarget->getTargetTriple().getObjectFormat()) {
6154 default: llvm_unreachable("unknown object format");
6155 case Triple::COFF:
6156 return LowerGlobalAddressWindows(Op, DAG);
6157 case Triple::ELF:
6158 return LowerGlobalAddressELF(Op, DAG);
6159 case Triple::MachO:
6160 return LowerGlobalAddressDarwin(Op, DAG);
6161 }
Bill Wendlinge1fd78f2011-03-14 23:02:38 +00006162 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling6a981312010-08-11 08:43:16 +00006163 case ISD::SELECT: return LowerSELECT(Op, DAG);
Evan Cheng15b80e42009-11-12 07:13:11 +00006164 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
6165 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006166 case ISD::BR_JT: return LowerBR_JT(Op, DAG);
Dan Gohman31ae5862010-04-17 14:41:14 +00006167 case ISD::VASTART: return LowerVASTART(Op, DAG);
Eli Friedman26a48482011-07-27 22:21:52 +00006168 case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, DAG, Subtarget);
Evan Cheng8740ee32010-11-03 06:34:55 +00006169 case ISD::PREFETCH: return LowerPREFETCH(Op, DAG, Subtarget);
Bob Wilsone4191e72010-03-19 22:51:32 +00006170 case ISD::SINT_TO_FP:
6171 case ISD::UINT_TO_FP: return LowerINT_TO_FP(Op, DAG);
6172 case ISD::FP_TO_SINT:
6173 case ISD::FP_TO_UINT: return LowerFP_TO_INT(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006174 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng168ced92010-05-22 01:47:14 +00006175 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
Jim Grosbachaeca45d2009-05-12 23:59:14 +00006176 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +00006177 case ISD::GLOBAL_OFFSET_TABLE: return LowerGLOBAL_OFFSET_TABLE(Op, DAG);
Jim Grosbachc98892f2010-05-26 20:22:18 +00006178 case ISD::EH_SJLJ_SETJMP: return LowerEH_SJLJ_SETJMP(Op, DAG);
Jim Grosbachbd9485d2010-05-22 01:06:18 +00006179 case ISD::EH_SJLJ_LONGJMP: return LowerEH_SJLJ_LONGJMP(Op, DAG);
Jim Grosbacha570d052010-02-08 23:22:00 +00006180 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG,
6181 Subtarget);
Evan Cheng383ecd82011-03-14 18:02:30 +00006182 case ISD::BITCAST: return ExpandBITCAST(Op.getNode(), DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006183 case ISD::SHL:
Chris Lattnerf81d5882007-11-24 07:07:01 +00006184 case ISD::SRL:
Bob Wilson2e076c42009-06-22 23:27:02 +00006185 case ISD::SRA: return LowerShift(Op.getNode(), DAG, Subtarget);
Evan Cheng15b80e42009-11-12 07:13:11 +00006186 case ISD::SHL_PARTS: return LowerShiftLeftParts(Op, DAG);
Jim Grosbach8fe6fd72009-10-31 21:42:19 +00006187 case ISD::SRL_PARTS:
Evan Cheng15b80e42009-11-12 07:13:11 +00006188 case ISD::SRA_PARTS: return LowerShiftRightParts(Op, DAG);
Jim Grosbach8546ec92010-01-18 19:58:49 +00006189 case ISD::CTTZ: return LowerCTTZ(Op.getNode(), DAG, Subtarget);
Evan Chengb4eae132012-12-04 22:41:50 +00006190 case ISD::CTPOP: return LowerCTPOP(Op.getNode(), DAG, Subtarget);
Duncan Sandsf2641e12011-09-06 19:07:46 +00006191 case ISD::SETCC: return LowerVSETCC(Op, DAG);
Lang Hamesc35ee8b2012-03-15 18:49:02 +00006192 case ISD::ConstantFP: return LowerConstantFP(Op, DAG, Subtarget);
Dale Johannesen2bff5052010-07-29 20:10:08 +00006193 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00006194 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
Eli Friedmana5e244c2011-10-24 23:08:52 +00006195 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00006196 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
Bob Wilsonf307e0b2009-08-03 20:36:38 +00006197 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Bob Wilson9a511c02010-08-20 04:54:02 +00006198 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Bob Wilson38ab35a2010-09-01 23:50:19 +00006199 case ISD::MUL: return LowerMUL(Op, DAG);
Nate Begemanfa62d502011-02-11 20:53:29 +00006200 case ISD::SDIV: return LowerSDIV(Op, DAG);
6201 case ISD::UDIV: return LowerUDIV(Op, DAG);
Evan Chenge8916542011-08-30 01:34:54 +00006202 case ISD::ADDC:
6203 case ISD::ADDE:
6204 case ISD::SUBC:
6205 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Louis Gerbarg3342bf12014-05-09 17:02:49 +00006206 case ISD::SADDO:
6207 case ISD::UADDO:
6208 case ISD::SSUBO:
6209 case ISD::USUBO:
6210 return LowerXALUO(Op, DAG);
Eli Friedmanba912e02011-09-15 22:18:49 +00006211 case ISD::ATOMIC_LOAD:
Eli Friedman10f9ce22011-09-15 22:26:18 +00006212 case ISD::ATOMIC_STORE: return LowerAtomicLoadStore(Op, DAG);
Bob Wilsone7dde0c2013-11-03 06:14:38 +00006213 case ISD::FSINCOS: return LowerFSINCOS(Op, DAG);
Renato Golin87610692013-07-16 09:32:17 +00006214 case ISD::SDIVREM:
6215 case ISD::UDIVREM: return LowerDivRem(Op, DAG);
Evan Cheng10043e22007-01-19 07:51:42 +00006216 }
Evan Cheng10043e22007-01-19 07:51:42 +00006217}
6218
Duncan Sands6ed40142008-12-01 11:39:25 +00006219/// ReplaceNodeResults - Replace the results of node with an illegal result
6220/// type with new values built out of custom code.
Duncan Sands6ed40142008-12-01 11:39:25 +00006221void ARMTargetLowering::ReplaceNodeResults(SDNode *N,
6222 SmallVectorImpl<SDValue>&Results,
Dan Gohman21cea8a2010-04-17 15:26:15 +00006223 SelectionDAG &DAG) const {
Bob Wilsonc05b8872010-04-14 20:45:23 +00006224 SDValue Res;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006225 switch (N->getOpcode()) {
Duncan Sands6ed40142008-12-01 11:39:25 +00006226 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00006227 llvm_unreachable("Don't know how to custom expand this!");
Wesley Peck527da1b2010-11-23 03:31:01 +00006228 case ISD::BITCAST:
6229 Res = ExpandBITCAST(N, DAG);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006230 break;
Chris Lattnerf81d5882007-11-24 07:07:01 +00006231 case ISD::SRL:
Bob Wilsonc05b8872010-04-14 20:45:23 +00006232 case ISD::SRA:
Bob Wilson7d471332010-11-18 21:16:28 +00006233 Res = Expand64BitShift(N, DAG, Subtarget);
Bob Wilsonc05b8872010-04-14 20:45:23 +00006234 break;
Tim Northoverbc933082013-05-23 19:11:20 +00006235 case ISD::READCYCLECOUNTER:
6236 ReplaceREADCYCLECOUNTER(N, Results, DAG, Subtarget);
6237 return;
Duncan Sands6ed40142008-12-01 11:39:25 +00006238 }
Bob Wilsonc05b8872010-04-14 20:45:23 +00006239 if (Res.getNode())
6240 Results.push_back(Res);
Chris Lattnerf81d5882007-11-24 07:07:01 +00006241}
Chris Lattnerf81d5882007-11-24 07:07:01 +00006242
Evan Cheng10043e22007-01-19 07:51:42 +00006243//===----------------------------------------------------------------------===//
6244// ARM Scheduler Hooks
6245//===----------------------------------------------------------------------===//
6246
Bill Wendling030b58e2011-10-06 22:18:16 +00006247/// SetupEntryBlockForSjLj - Insert code into the entry block that creates and
6248/// registers the function context.
6249void ARMTargetLowering::
6250SetupEntryBlockForSjLj(MachineInstr *MI, MachineBasicBlock *MBB,
6251 MachineBasicBlock *DispatchBB, int FI) const {
Bill Wendling374ee192011-10-03 21:25:38 +00006252 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6253 DebugLoc dl = MI->getDebugLoc();
6254 MachineFunction *MF = MBB->getParent();
6255 MachineRegisterInfo *MRI = &MF->getRegInfo();
6256 MachineConstantPool *MCP = MF->getConstantPool();
6257 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6258 const Function *F = MF->getFunction();
Bill Wendling374ee192011-10-03 21:25:38 +00006259
Bill Wendling374ee192011-10-03 21:25:38 +00006260 bool isThumb = Subtarget->isThumb();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006261 bool isThumb2 = Subtarget->isThumb2();
Bill Wendling030b58e2011-10-06 22:18:16 +00006262
Bill Wendling374ee192011-10-03 21:25:38 +00006263 unsigned PCLabelId = AFI->createPICLabelUId();
Bill Wendling1eab54f2011-10-03 22:44:15 +00006264 unsigned PCAdj = (isThumb || isThumb2) ? 4 : 8;
Bill Wendling374ee192011-10-03 21:25:38 +00006265 ARMConstantPoolValue *CPV =
6266 ARMConstantPoolMBB::Create(F->getContext(), DispatchBB, PCLabelId, PCAdj);
6267 unsigned CPI = MCP->getConstantPoolIndex(CPV, 4);
6268
Craig Topperc7242e02012-04-20 07:30:17 +00006269 const TargetRegisterClass *TRC = isThumb ?
6270 (const TargetRegisterClass*)&ARM::tGPRRegClass :
6271 (const TargetRegisterClass*)&ARM::GPRRegClass;
Bill Wendling374ee192011-10-03 21:25:38 +00006272
Bill Wendling030b58e2011-10-06 22:18:16 +00006273 // Grab constant pool and fixed stack memory operands.
6274 MachineMemOperand *CPMMO =
6275 MF->getMachineMemOperand(MachinePointerInfo::getConstantPool(),
6276 MachineMemOperand::MOLoad, 4, 4);
6277
6278 MachineMemOperand *FIMMOSt =
6279 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
6280 MachineMemOperand::MOStore, 4, 4);
6281
6282 // Load the address of the dispatch MBB into the jump buffer.
6283 if (isThumb2) {
6284 // Incoming value: jbuf
6285 // ldr.n r5, LCPI1_1
6286 // orr r5, r5, #1
6287 // add r5, pc
6288 // str r5, [$jbuf, #+4] ; &jbuf[1]
6289 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6290 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2LDRpci), NewVReg1)
6291 .addConstantPoolIndex(CPI)
6292 .addMemOperand(CPMMO));
6293 // Set the low bit because of thumb mode.
6294 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6295 AddDefaultCC(
6296 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2ORRri), NewVReg2)
6297 .addReg(NewVReg1, RegState::Kill)
6298 .addImm(0x01)));
6299 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6300 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg3)
6301 .addReg(NewVReg2, RegState::Kill)
6302 .addImm(PCLabelId);
6303 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::t2STRi12))
6304 .addReg(NewVReg3, RegState::Kill)
6305 .addFrameIndex(FI)
6306 .addImm(36) // &jbuf[1] :: pc
6307 .addMemOperand(FIMMOSt));
6308 } else if (isThumb) {
6309 // Incoming value: jbuf
6310 // ldr.n r1, LCPI1_4
6311 // add r1, pc
6312 // mov r2, #1
6313 // orrs r1, r2
6314 // add r2, $jbuf, #+4 ; &jbuf[1]
6315 // str r1, [r2]
6316 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6317 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tLDRpci), NewVReg1)
6318 .addConstantPoolIndex(CPI)
6319 .addMemOperand(CPMMO));
6320 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6321 BuildMI(*MBB, MI, dl, TII->get(ARM::tPICADD), NewVReg2)
6322 .addReg(NewVReg1, RegState::Kill)
6323 .addImm(PCLabelId);
6324 // Set the low bit because of thumb mode.
6325 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6326 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tMOVi8), NewVReg3)
6327 .addReg(ARM::CPSR, RegState::Define)
6328 .addImm(1));
6329 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6330 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tORR), NewVReg4)
6331 .addReg(ARM::CPSR, RegState::Define)
6332 .addReg(NewVReg2, RegState::Kill)
6333 .addReg(NewVReg3, RegState::Kill));
6334 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6335 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tADDrSPi), NewVReg5)
6336 .addFrameIndex(FI)
6337 .addImm(36)); // &jbuf[1] :: pc
6338 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::tSTRi))
6339 .addReg(NewVReg4, RegState::Kill)
6340 .addReg(NewVReg5, RegState::Kill)
6341 .addImm(0)
6342 .addMemOperand(FIMMOSt));
6343 } else {
6344 // Incoming value: jbuf
6345 // ldr r1, LCPI1_1
6346 // add r1, pc, r1
6347 // str r1, [$jbuf, #+4] ; &jbuf[1]
6348 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6349 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::LDRi12), NewVReg1)
6350 .addConstantPoolIndex(CPI)
6351 .addImm(0)
6352 .addMemOperand(CPMMO));
6353 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6354 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::PICADD), NewVReg2)
6355 .addReg(NewVReg1, RegState::Kill)
6356 .addImm(PCLabelId));
6357 AddDefaultPred(BuildMI(*MBB, MI, dl, TII->get(ARM::STRi12))
6358 .addReg(NewVReg2, RegState::Kill)
6359 .addFrameIndex(FI)
6360 .addImm(36) // &jbuf[1] :: pc
6361 .addMemOperand(FIMMOSt));
6362 }
6363}
6364
6365MachineBasicBlock *ARMTargetLowering::
6366EmitSjLjDispatchBlock(MachineInstr *MI, MachineBasicBlock *MBB) const {
6367 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6368 DebugLoc dl = MI->getDebugLoc();
6369 MachineFunction *MF = MBB->getParent();
6370 MachineRegisterInfo *MRI = &MF->getRegInfo();
6371 ARMFunctionInfo *AFI = MF->getInfo<ARMFunctionInfo>();
6372 MachineFrameInfo *MFI = MF->getFrameInfo();
6373 int FI = MFI->getFunctionContextIndex();
6374
Craig Topperc7242e02012-04-20 07:30:17 +00006375 const TargetRegisterClass *TRC = Subtarget->isThumb() ?
6376 (const TargetRegisterClass*)&ARM::tGPRRegClass :
Jakob Stoklund Olesen691ae332012-05-20 06:38:47 +00006377 (const TargetRegisterClass*)&ARM::GPRnopcRegClass;
Bill Wendling030b58e2011-10-06 22:18:16 +00006378
Bill Wendling362c1b02011-10-06 21:29:56 +00006379 // Get a mapping of the call site numbers to all of the landing pads they're
6380 // associated with.
Bill Wendling202803e2011-10-05 00:02:33 +00006381 DenseMap<unsigned, SmallVector<MachineBasicBlock*, 2> > CallSiteNumToLPad;
6382 unsigned MaxCSNum = 0;
6383 MachineModuleInfo &MMI = MF->getMMI();
Jim Grosbach0c509fa2012-04-06 23:43:50 +00006384 for (MachineFunction::iterator BB = MF->begin(), E = MF->end(); BB != E;
6385 ++BB) {
Bill Wendling202803e2011-10-05 00:02:33 +00006386 if (!BB->isLandingPad()) continue;
6387
6388 // FIXME: We should assert that the EH_LABEL is the first MI in the landing
6389 // pad.
6390 for (MachineBasicBlock::iterator
6391 II = BB->begin(), IE = BB->end(); II != IE; ++II) {
6392 if (!II->isEHLabel()) continue;
6393
6394 MCSymbol *Sym = II->getOperand(0).getMCSymbol();
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006395 if (!MMI.hasCallSiteLandingPad(Sym)) continue;
Bill Wendling202803e2011-10-05 00:02:33 +00006396
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006397 SmallVectorImpl<unsigned> &CallSiteIdxs = MMI.getCallSiteLandingPad(Sym);
6398 for (SmallVectorImpl<unsigned>::iterator
6399 CSI = CallSiteIdxs.begin(), CSE = CallSiteIdxs.end();
6400 CSI != CSE; ++CSI) {
6401 CallSiteNumToLPad[*CSI].push_back(BB);
6402 MaxCSNum = std::max(MaxCSNum, *CSI);
6403 }
Bill Wendling202803e2011-10-05 00:02:33 +00006404 break;
6405 }
6406 }
6407
6408 // Get an ordered list of the machine basic blocks for the jump table.
6409 std::vector<MachineBasicBlock*> LPadList;
Bill Wendling883ec972011-10-07 23:18:02 +00006410 SmallPtrSet<MachineBasicBlock*, 64> InvokeBBs;
Bill Wendling202803e2011-10-05 00:02:33 +00006411 LPadList.reserve(CallSiteNumToLPad.size());
6412 for (unsigned I = 1; I <= MaxCSNum; ++I) {
6413 SmallVectorImpl<MachineBasicBlock*> &MBBList = CallSiteNumToLPad[I];
6414 for (SmallVectorImpl<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006415 II = MBBList.begin(), IE = MBBList.end(); II != IE; ++II) {
Bill Wendling202803e2011-10-05 00:02:33 +00006416 LPadList.push_back(*II);
Bill Wendling883ec972011-10-07 23:18:02 +00006417 InvokeBBs.insert((*II)->pred_begin(), (*II)->pred_end());
6418 }
Bill Wendling202803e2011-10-05 00:02:33 +00006419 }
6420
Bill Wendlingf793e7e2011-10-05 23:28:57 +00006421 assert(!LPadList.empty() &&
6422 "No landing pad destinations for the dispatch jump table!");
6423
Bill Wendling362c1b02011-10-06 21:29:56 +00006424 // Create the jump table and associated information.
Bill Wendling202803e2011-10-05 00:02:33 +00006425 MachineJumpTableInfo *JTI =
6426 MF->getOrCreateJumpTableInfo(MachineJumpTableInfo::EK_Inline);
6427 unsigned MJTI = JTI->createJumpTableIndex(LPadList);
6428 unsigned UId = AFI->createJumpTableUId();
Chad Rosier96603432013-03-01 18:30:38 +00006429 Reloc::Model RelocM = getTargetMachine().getRelocationModel();
Bill Wendling202803e2011-10-05 00:02:33 +00006430
Bill Wendling362c1b02011-10-06 21:29:56 +00006431 // Create the MBBs for the dispatch code.
Bill Wendling030b58e2011-10-06 22:18:16 +00006432
6433 // Shove the dispatch's address into the return slot in the function context.
6434 MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock();
6435 DispatchBB->setIsLandingPad();
Bill Wendling030b58e2011-10-06 22:18:16 +00006436
Bill Wendling324be982011-10-05 00:39:32 +00006437 MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock();
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006438 unsigned trap_opcode;
Chad Rosier11a98282013-02-28 18:54:27 +00006439 if (Subtarget->isThumb())
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006440 trap_opcode = ARM::tTRAP;
Chad Rosier11a98282013-02-28 18:54:27 +00006441 else
6442 trap_opcode = Subtarget->useNaClTrap() ? ARM::TRAPNaCl : ARM::TRAP;
6443
Eli Bendersky2e2ce492013-01-30 16:30:19 +00006444 BuildMI(TrapBB, dl, TII->get(trap_opcode));
Bill Wendling324be982011-10-05 00:39:32 +00006445 DispatchBB->addSuccessor(TrapBB);
6446
6447 MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock();
6448 DispatchBB->addSuccessor(DispContBB);
Bill Wendling202803e2011-10-05 00:02:33 +00006449
Bill Wendling510fbcd2011-10-17 21:32:56 +00006450 // Insert and MBBs.
Bill Wendling61346552011-10-06 00:53:33 +00006451 MF->insert(MF->end(), DispatchBB);
6452 MF->insert(MF->end(), DispContBB);
6453 MF->insert(MF->end(), TrapBB);
Bill Wendling61346552011-10-06 00:53:33 +00006454
Bill Wendling030b58e2011-10-06 22:18:16 +00006455 // Insert code into the entry block that creates and registers the function
6456 // context.
6457 SetupEntryBlockForSjLj(MI, MBB, DispatchBB, FI);
6458
Bill Wendling030b58e2011-10-06 22:18:16 +00006459 MachineMemOperand *FIMMOLd =
Bill Wendling362c1b02011-10-06 21:29:56 +00006460 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
Bill Wendlingb3d46782011-10-06 23:37:36 +00006461 MachineMemOperand::MOLoad |
6462 MachineMemOperand::MOVolatile, 4, 4);
Bill Wendling61346552011-10-06 00:53:33 +00006463
Chad Rosier1ec8e402012-11-06 23:05:24 +00006464 MachineInstrBuilder MIB;
6465 MIB = BuildMI(DispatchBB, dl, TII->get(ARM::Int_eh_sjlj_dispatchsetup));
6466
6467 const ARMBaseInstrInfo *AII = static_cast<const ARMBaseInstrInfo*>(TII);
6468 const ARMBaseRegisterInfo &RI = AII->getRegisterInfo();
6469
6470 // Add a register mask with no preserved registers. This results in all
6471 // registers being marked as clobbered.
6472 MIB.addRegMask(RI.getNoPreservedMask());
Bob Wilsonf6d17282011-11-16 07:11:57 +00006473
Bill Wendling85833f72011-10-18 22:49:07 +00006474 unsigned NumLPads = LPadList.size();
Bill Wendling5626c662011-10-06 22:53:00 +00006475 if (Subtarget->isThumb2()) {
6476 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6477 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2LDRi12), NewVReg1)
6478 .addFrameIndex(FI)
6479 .addImm(4)
6480 .addMemOperand(FIMMOLd));
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006481
Bill Wendling85833f72011-10-18 22:49:07 +00006482 if (NumLPads < 256) {
6483 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPri))
6484 .addReg(NewVReg1)
6485 .addImm(LPadList.size()));
6486 } else {
6487 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6488 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006489 .addImm(NumLPads & 0xFFFF));
6490
6491 unsigned VReg2 = VReg1;
6492 if ((NumLPads & 0xFFFF0000) != 0) {
6493 VReg2 = MRI->createVirtualRegister(TRC);
6494 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2MOVTi16), VReg2)
6495 .addReg(VReg1)
6496 .addImm(NumLPads >> 16));
6497 }
6498
Bill Wendling85833f72011-10-18 22:49:07 +00006499 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::t2CMPrr))
6500 .addReg(NewVReg1)
6501 .addReg(VReg2));
6502 }
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006503
Bill Wendling5626c662011-10-06 22:53:00 +00006504 BuildMI(DispatchBB, dl, TII->get(ARM::t2Bcc))
6505 .addMBB(TrapBB)
6506 .addImm(ARMCC::HI)
6507 .addReg(ARM::CPSR);
Bill Wendling324be982011-10-05 00:39:32 +00006508
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006509 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
6510 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::t2LEApcrelJT),NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006511 .addJumpTableIndex(MJTI)
6512 .addImm(UId));
Bill Wendling202803e2011-10-05 00:02:33 +00006513
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006514 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006515 AddDefaultCC(
6516 AddDefaultPred(
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006517 BuildMI(DispContBB, dl, TII->get(ARM::t2ADDrs), NewVReg4)
6518 .addReg(NewVReg3, RegState::Kill)
Bill Wendling5626c662011-10-06 22:53:00 +00006519 .addReg(NewVReg1)
6520 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
6521
6522 BuildMI(DispContBB, dl, TII->get(ARM::t2BR_JT))
Bill Wendlingb2a703d2011-10-18 21:55:58 +00006523 .addReg(NewVReg4, RegState::Kill)
Bill Wendling202803e2011-10-05 00:02:33 +00006524 .addReg(NewVReg1)
Bill Wendling5626c662011-10-06 22:53:00 +00006525 .addJumpTableIndex(MJTI)
6526 .addImm(UId);
6527 } else if (Subtarget->isThumb()) {
Bill Wendlingb3d46782011-10-06 23:37:36 +00006528 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6529 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRspi), NewVReg1)
6530 .addFrameIndex(FI)
6531 .addImm(1)
6532 .addMemOperand(FIMMOLd));
Bill Wendlingf9f5e452011-10-07 22:08:37 +00006533
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006534 if (NumLPads < 256) {
6535 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPi8))
6536 .addReg(NewVReg1)
6537 .addImm(NumLPads));
6538 } else {
6539 MachineConstantPool *ConstantPool = MF->getConstantPool();
Bill Wendling2977a152011-10-19 09:24:02 +00006540 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6541 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6542
6543 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006544 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006545 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006546 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006547 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
Bill Wendling64e6bfc2011-10-18 23:11:05 +00006548
6549 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6550 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tLDRpci))
6551 .addReg(VReg1, RegState::Define)
6552 .addConstantPoolIndex(Idx));
6553 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::tCMPr))
6554 .addReg(NewVReg1)
6555 .addReg(VReg1));
6556 }
6557
Bill Wendlingb3d46782011-10-06 23:37:36 +00006558 BuildMI(DispatchBB, dl, TII->get(ARM::tBcc))
6559 .addMBB(TrapBB)
6560 .addImm(ARMCC::HI)
6561 .addReg(ARM::CPSR);
6562
6563 unsigned NewVReg2 = MRI->createVirtualRegister(TRC);
6564 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLSLri), NewVReg2)
6565 .addReg(ARM::CPSR, RegState::Define)
6566 .addReg(NewVReg1)
6567 .addImm(2));
6568
6569 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling8d50ea02011-10-06 23:41:14 +00006570 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLEApcrelJT), NewVReg3)
Bill Wendlingb3d46782011-10-06 23:37:36 +00006571 .addJumpTableIndex(MJTI)
6572 .addImm(UId));
6573
6574 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6575 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg4)
6576 .addReg(ARM::CPSR, RegState::Define)
6577 .addReg(NewVReg2, RegState::Kill)
6578 .addReg(NewVReg3));
6579
6580 MachineMemOperand *JTMMOLd =
6581 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6582 MachineMemOperand::MOLoad, 4, 4);
6583
6584 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
6585 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tLDRi), NewVReg5)
6586 .addReg(NewVReg4, RegState::Kill)
6587 .addImm(0)
6588 .addMemOperand(JTMMOLd));
6589
Chad Rosier96603432013-03-01 18:30:38 +00006590 unsigned NewVReg6 = NewVReg5;
6591 if (RelocM == Reloc::PIC_) {
6592 NewVReg6 = MRI->createVirtualRegister(TRC);
6593 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::tADDrr), NewVReg6)
6594 .addReg(ARM::CPSR, RegState::Define)
6595 .addReg(NewVReg5, RegState::Kill)
6596 .addReg(NewVReg3));
6597 }
Bill Wendlingb3d46782011-10-06 23:37:36 +00006598
6599 BuildMI(DispContBB, dl, TII->get(ARM::tBR_JTr))
6600 .addReg(NewVReg6, RegState::Kill)
6601 .addJumpTableIndex(MJTI)
6602 .addImm(UId);
Bill Wendling5626c662011-10-06 22:53:00 +00006603 } else {
6604 unsigned NewVReg1 = MRI->createVirtualRegister(TRC);
6605 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRi12), NewVReg1)
6606 .addFrameIndex(FI)
6607 .addImm(4)
6608 .addMemOperand(FIMMOLd));
Bill Wendling973c8172011-10-18 22:11:18 +00006609
Bill Wendling4969dcd2011-10-18 22:52:20 +00006610 if (NumLPads < 256) {
6611 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPri))
6612 .addReg(NewVReg1)
6613 .addImm(NumLPads));
Bill Wendling2977a152011-10-19 09:24:02 +00006614 } else if (Subtarget->hasV6T2Ops() && isUInt<16>(NumLPads)) {
Bill Wendling4969dcd2011-10-18 22:52:20 +00006615 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6616 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVi16), VReg1)
Bill Wendling94f60012011-10-18 23:19:55 +00006617 .addImm(NumLPads & 0xFFFF));
6618
6619 unsigned VReg2 = VReg1;
6620 if ((NumLPads & 0xFFFF0000) != 0) {
6621 VReg2 = MRI->createVirtualRegister(TRC);
6622 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::MOVTi16), VReg2)
6623 .addReg(VReg1)
6624 .addImm(NumLPads >> 16));
6625 }
6626
Bill Wendling4969dcd2011-10-18 22:52:20 +00006627 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6628 .addReg(NewVReg1)
6629 .addReg(VReg2));
Bill Wendling2977a152011-10-19 09:24:02 +00006630 } else {
6631 MachineConstantPool *ConstantPool = MF->getConstantPool();
6632 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
6633 const Constant *C = ConstantInt::get(Int32Ty, NumLPads);
6634
6635 // MachineConstantPool wants an explicit alignment.
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006636 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
Bill Wendling2977a152011-10-19 09:24:02 +00006637 if (Align == 0)
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006638 Align = getDataLayout()->getTypeAllocSize(C->getType());
Bill Wendling2977a152011-10-19 09:24:02 +00006639 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
6640
6641 unsigned VReg1 = MRI->createVirtualRegister(TRC);
6642 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::LDRcp))
6643 .addReg(VReg1, RegState::Define)
Bill Wendlingcf7bdf42011-10-20 20:37:11 +00006644 .addConstantPoolIndex(Idx)
6645 .addImm(0));
Bill Wendling2977a152011-10-19 09:24:02 +00006646 AddDefaultPred(BuildMI(DispatchBB, dl, TII->get(ARM::CMPrr))
6647 .addReg(NewVReg1)
6648 .addReg(VReg1, RegState::Kill));
Bill Wendling4969dcd2011-10-18 22:52:20 +00006649 }
6650
Bill Wendling5626c662011-10-06 22:53:00 +00006651 BuildMI(DispatchBB, dl, TII->get(ARM::Bcc))
6652 .addMBB(TrapBB)
6653 .addImm(ARMCC::HI)
6654 .addReg(ARM::CPSR);
Bill Wendling202803e2011-10-05 00:02:33 +00006655
Bill Wendling973c8172011-10-18 22:11:18 +00006656 unsigned NewVReg3 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006657 AddDefaultCC(
Bill Wendling973c8172011-10-18 22:11:18 +00006658 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::MOVsi), NewVReg3)
Bill Wendling5626c662011-10-06 22:53:00 +00006659 .addReg(NewVReg1)
6660 .addImm(ARM_AM::getSORegOpc(ARM_AM::lsl, 2))));
Bill Wendling973c8172011-10-18 22:11:18 +00006661 unsigned NewVReg4 = MRI->createVirtualRegister(TRC);
6662 AddDefaultPred(BuildMI(DispContBB, dl, TII->get(ARM::LEApcrelJT), NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006663 .addJumpTableIndex(MJTI)
6664 .addImm(UId));
6665
6666 MachineMemOperand *JTMMOLd =
6667 MF->getMachineMemOperand(MachinePointerInfo::getJumpTable(),
6668 MachineMemOperand::MOLoad, 4, 4);
Bill Wendling973c8172011-10-18 22:11:18 +00006669 unsigned NewVReg5 = MRI->createVirtualRegister(TRC);
Bill Wendling5626c662011-10-06 22:53:00 +00006670 AddDefaultPred(
Bill Wendling973c8172011-10-18 22:11:18 +00006671 BuildMI(DispContBB, dl, TII->get(ARM::LDRrs), NewVReg5)
6672 .addReg(NewVReg3, RegState::Kill)
6673 .addReg(NewVReg4)
Bill Wendling5626c662011-10-06 22:53:00 +00006674 .addImm(0)
6675 .addMemOperand(JTMMOLd));
6676
Chad Rosier96603432013-03-01 18:30:38 +00006677 if (RelocM == Reloc::PIC_) {
6678 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTadd))
6679 .addReg(NewVReg5, RegState::Kill)
6680 .addReg(NewVReg4)
6681 .addJumpTableIndex(MJTI)
6682 .addImm(UId);
6683 } else {
6684 BuildMI(DispContBB, dl, TII->get(ARM::BR_JTr))
6685 .addReg(NewVReg5, RegState::Kill)
6686 .addJumpTableIndex(MJTI)
6687 .addImm(UId);
6688 }
Bill Wendling5626c662011-10-06 22:53:00 +00006689 }
Bill Wendling202803e2011-10-05 00:02:33 +00006690
Bill Wendling324be982011-10-05 00:39:32 +00006691 // Add the jump table entries as successors to the MBB.
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006692 SmallPtrSet<MachineBasicBlock*, 8> SeenMBBs;
Bill Wendling324be982011-10-05 00:39:32 +00006693 for (std::vector<MachineBasicBlock*>::iterator
Bill Wendling883ec972011-10-07 23:18:02 +00006694 I = LPadList.begin(), E = LPadList.end(); I != E; ++I) {
6695 MachineBasicBlock *CurMBB = *I;
Jakob Stoklund Olesen710093e2012-08-20 20:52:03 +00006696 if (SeenMBBs.insert(CurMBB))
Bill Wendling883ec972011-10-07 23:18:02 +00006697 DispContBB->addSuccessor(CurMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006698 }
6699
Bill Wendling26d27802011-10-17 05:25:09 +00006700 // N.B. the order the invoke BBs are processed in doesn't matter here.
Craig Topper840beec2014-04-04 05:16:06 +00006701 const MCPhysReg *SavedRegs = RI.getCalleeSavedRegs(MF);
Bill Wendling617075f2011-10-18 18:30:49 +00006702 SmallVector<MachineBasicBlock*, 64> MBBLPads;
Bill Wendling883ec972011-10-07 23:18:02 +00006703 for (SmallPtrSet<MachineBasicBlock*, 64>::iterator
6704 I = InvokeBBs.begin(), E = InvokeBBs.end(); I != E; ++I) {
6705 MachineBasicBlock *BB = *I;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006706
6707 // Remove the landing pad successor from the invoke block and replace it
6708 // with the new dispatch block.
Bill Wendling1414bc52011-10-26 07:16:18 +00006709 SmallVector<MachineBasicBlock*, 4> Successors(BB->succ_begin(),
6710 BB->succ_end());
6711 while (!Successors.empty()) {
6712 MachineBasicBlock *SMBB = Successors.pop_back_val();
Bill Wendling883ec972011-10-07 23:18:02 +00006713 if (SMBB->isLandingPad()) {
6714 BB->removeSuccessor(SMBB);
Bill Wendling617075f2011-10-18 18:30:49 +00006715 MBBLPads.push_back(SMBB);
Bill Wendling883ec972011-10-07 23:18:02 +00006716 }
6717 }
6718
6719 BB->addSuccessor(DispatchBB);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006720
6721 // Find the invoke call and mark all of the callee-saved registers as
6722 // 'implicit defined' so that they're spilled. This prevents code from
6723 // moving instructions to before the EH block, where they will never be
6724 // executed.
6725 for (MachineBasicBlock::reverse_iterator
6726 II = BB->rbegin(), IE = BB->rend(); II != IE; ++II) {
Evan Cheng7f8e5632011-12-07 07:15:52 +00006727 if (!II->isCall()) continue;
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006728
6729 DenseMap<unsigned, bool> DefRegs;
6730 for (MachineInstr::mop_iterator
6731 OI = II->operands_begin(), OE = II->operands_end();
6732 OI != OE; ++OI) {
6733 if (!OI->isReg()) continue;
6734 DefRegs[OI->getReg()] = true;
6735 }
6736
Jakob Stoklund Olesenb159b5f2012-12-19 21:31:56 +00006737 MachineInstrBuilder MIB(*MF, &*II);
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006738
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006739 for (unsigned i = 0; SavedRegs[i] != 0; ++i) {
Bill Wendling94e66432011-10-22 00:29:28 +00006740 unsigned Reg = SavedRegs[i];
6741 if (Subtarget->isThumb2() &&
Craig Topperc7242e02012-04-20 07:30:17 +00006742 !ARM::tGPRRegClass.contains(Reg) &&
6743 !ARM::hGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006744 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006745 if (Subtarget->isThumb1Only() && !ARM::tGPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006746 continue;
Craig Topperc7242e02012-04-20 07:30:17 +00006747 if (!Subtarget->isThumb() && !ARM::GPRRegClass.contains(Reg))
Bill Wendling94e66432011-10-22 00:29:28 +00006748 continue;
6749 if (!DefRegs[Reg])
6750 MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead);
Bill Wendling9e0cd1e2011-10-14 23:55:44 +00006751 }
Bill Wendling6f3f9a32011-10-14 23:34:37 +00006752
6753 break;
6754 }
Bill Wendling883ec972011-10-07 23:18:02 +00006755 }
Bill Wendling324be982011-10-05 00:39:32 +00006756
Bill Wendling617075f2011-10-18 18:30:49 +00006757 // Mark all former landing pads as non-landing pads. The dispatch is the only
6758 // landing pad now.
6759 for (SmallVectorImpl<MachineBasicBlock*>::iterator
6760 I = MBBLPads.begin(), E = MBBLPads.end(); I != E; ++I)
6761 (*I)->setIsLandingPad(false);
6762
Bill Wendling324be982011-10-05 00:39:32 +00006763 // The instruction is gone now.
6764 MI->eraseFromParent();
6765
Bill Wendling374ee192011-10-03 21:25:38 +00006766 return MBB;
6767}
6768
Evan Cheng0cc4ad92010-07-13 19:27:42 +00006769static
6770MachineBasicBlock *OtherSucc(MachineBasicBlock *MBB, MachineBasicBlock *Succ) {
6771 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
6772 E = MBB->succ_end(); I != E; ++I)
6773 if (*I != Succ)
6774 return *I;
6775 llvm_unreachable("Expecting a BB with two successors!");
6776}
6777
Manman Renb504f492013-10-29 22:27:32 +00006778/// Return the load opcode for a given load size. If load size >= 8,
6779/// neon opcode will be returned.
6780static unsigned getLdOpcode(unsigned LdSize, bool IsThumb1, bool IsThumb2) {
6781 if (LdSize >= 8)
6782 return LdSize == 16 ? ARM::VLD1q32wb_fixed
6783 : LdSize == 8 ? ARM::VLD1d32wb_fixed : 0;
6784 if (IsThumb1)
6785 return LdSize == 4 ? ARM::tLDRi
6786 : LdSize == 2 ? ARM::tLDRHi
6787 : LdSize == 1 ? ARM::tLDRBi : 0;
6788 if (IsThumb2)
6789 return LdSize == 4 ? ARM::t2LDR_POST
6790 : LdSize == 2 ? ARM::t2LDRH_POST
6791 : LdSize == 1 ? ARM::t2LDRB_POST : 0;
6792 return LdSize == 4 ? ARM::LDR_POST_IMM
6793 : LdSize == 2 ? ARM::LDRH_POST
6794 : LdSize == 1 ? ARM::LDRB_POST_IMM : 0;
6795}
6796
6797/// Return the store opcode for a given store size. If store size >= 8,
6798/// neon opcode will be returned.
6799static unsigned getStOpcode(unsigned StSize, bool IsThumb1, bool IsThumb2) {
6800 if (StSize >= 8)
6801 return StSize == 16 ? ARM::VST1q32wb_fixed
6802 : StSize == 8 ? ARM::VST1d32wb_fixed : 0;
6803 if (IsThumb1)
6804 return StSize == 4 ? ARM::tSTRi
6805 : StSize == 2 ? ARM::tSTRHi
6806 : StSize == 1 ? ARM::tSTRBi : 0;
6807 if (IsThumb2)
6808 return StSize == 4 ? ARM::t2STR_POST
6809 : StSize == 2 ? ARM::t2STRH_POST
6810 : StSize == 1 ? ARM::t2STRB_POST : 0;
6811 return StSize == 4 ? ARM::STR_POST_IMM
6812 : StSize == 2 ? ARM::STRH_POST
6813 : StSize == 1 ? ARM::STRB_POST_IMM : 0;
6814}
6815
6816/// Emit a post-increment load operation with given size. The instructions
6817/// will be added to BB at Pos.
6818static void emitPostLd(MachineBasicBlock *BB, MachineInstr *Pos,
6819 const TargetInstrInfo *TII, DebugLoc dl,
6820 unsigned LdSize, unsigned Data, unsigned AddrIn,
6821 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6822 unsigned LdOpc = getLdOpcode(LdSize, IsThumb1, IsThumb2);
6823 assert(LdOpc != 0 && "Should have a load opcode");
6824 if (LdSize >= 8) {
6825 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6826 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6827 .addImm(0));
6828 } else if (IsThumb1) {
6829 // load + update AddrIn
6830 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6831 .addReg(AddrIn).addImm(0));
6832 MachineInstrBuilder MIB =
6833 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6834 MIB = AddDefaultT1CC(MIB);
6835 MIB.addReg(AddrIn).addImm(LdSize);
6836 AddDefaultPred(MIB);
6837 } else if (IsThumb2) {
6838 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6839 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6840 .addImm(LdSize));
6841 } else { // arm
6842 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(LdOpc), Data)
6843 .addReg(AddrOut, RegState::Define).addReg(AddrIn)
6844 .addReg(0).addImm(LdSize));
6845 }
6846}
6847
6848/// Emit a post-increment store operation with given size. The instructions
6849/// will be added to BB at Pos.
6850static void emitPostSt(MachineBasicBlock *BB, MachineInstr *Pos,
6851 const TargetInstrInfo *TII, DebugLoc dl,
6852 unsigned StSize, unsigned Data, unsigned AddrIn,
6853 unsigned AddrOut, bool IsThumb1, bool IsThumb2) {
6854 unsigned StOpc = getStOpcode(StSize, IsThumb1, IsThumb2);
6855 assert(StOpc != 0 && "Should have a store opcode");
6856 if (StSize >= 8) {
6857 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6858 .addReg(AddrIn).addImm(0).addReg(Data));
6859 } else if (IsThumb1) {
6860 // store + update AddrIn
6861 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc)).addReg(Data)
6862 .addReg(AddrIn).addImm(0));
6863 MachineInstrBuilder MIB =
6864 BuildMI(*BB, Pos, dl, TII->get(ARM::tADDi8), AddrOut);
6865 MIB = AddDefaultT1CC(MIB);
6866 MIB.addReg(AddrIn).addImm(StSize);
6867 AddDefaultPred(MIB);
6868 } else if (IsThumb2) {
6869 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6870 .addReg(Data).addReg(AddrIn).addImm(StSize));
6871 } else { // arm
6872 AddDefaultPred(BuildMI(*BB, Pos, dl, TII->get(StOpc), AddrOut)
6873 .addReg(Data).addReg(AddrIn).addReg(0)
6874 .addImm(StSize));
6875 }
6876}
6877
David Peixottoc32e24a2013-10-17 19:49:22 +00006878MachineBasicBlock *
6879ARMTargetLowering::EmitStructByval(MachineInstr *MI,
6880 MachineBasicBlock *BB) const {
Manman Rene8735522012-06-01 19:33:18 +00006881 // This pseudo instruction has 3 operands: dst, src, size
6882 // We expand it to a loop if size > Subtarget->getMaxInlineSizeThreshold().
6883 // Otherwise, we will generate unrolled scalar copies.
6884 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6885 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6886 MachineFunction::iterator It = BB;
6887 ++It;
6888
6889 unsigned dest = MI->getOperand(0).getReg();
6890 unsigned src = MI->getOperand(1).getReg();
6891 unsigned SizeVal = MI->getOperand(2).getImm();
6892 unsigned Align = MI->getOperand(3).getImm();
6893 DebugLoc dl = MI->getDebugLoc();
6894
Manman Rene8735522012-06-01 19:33:18 +00006895 MachineFunction *MF = BB->getParent();
6896 MachineRegisterInfo &MRI = MF->getRegInfo();
David Peixottoc32e24a2013-10-17 19:49:22 +00006897 unsigned UnitSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00006898 const TargetRegisterClass *TRC = nullptr;
6899 const TargetRegisterClass *VecTRC = nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00006900
6901 bool IsThumb1 = Subtarget->isThumb1Only();
6902 bool IsThumb2 = Subtarget->isThumb2();
Manman Rene8735522012-06-01 19:33:18 +00006903
6904 if (Align & 1) {
Manman Rene8735522012-06-01 19:33:18 +00006905 UnitSize = 1;
6906 } else if (Align & 2) {
Manman Rene8735522012-06-01 19:33:18 +00006907 UnitSize = 2;
6908 } else {
Manman Ren6e1fd462012-06-18 22:23:48 +00006909 // Check whether we can use NEON instructions.
Bill Wendling698e84f2012-12-30 10:32:01 +00006910 if (!MF->getFunction()->getAttributes().
6911 hasAttribute(AttributeSet::FunctionIndex,
6912 Attribute::NoImplicitFloat) &&
Manman Ren6e1fd462012-06-18 22:23:48 +00006913 Subtarget->hasNEON()) {
David Peixottoc32e24a2013-10-17 19:49:22 +00006914 if ((Align % 16 == 0) && SizeVal >= 16)
Manman Ren6e1fd462012-06-18 22:23:48 +00006915 UnitSize = 16;
David Peixottoc32e24a2013-10-17 19:49:22 +00006916 else if ((Align % 8 == 0) && SizeVal >= 8)
Manman Ren6e1fd462012-06-18 22:23:48 +00006917 UnitSize = 8;
Manman Ren6e1fd462012-06-18 22:23:48 +00006918 }
6919 // Can't use NEON instructions.
David Peixottoc32e24a2013-10-17 19:49:22 +00006920 if (UnitSize == 0)
Manman Ren6e1fd462012-06-18 22:23:48 +00006921 UnitSize = 4;
Manman Rene8735522012-06-01 19:33:18 +00006922 }
Manman Ren6e1fd462012-06-18 22:23:48 +00006923
David Peixottob0653e532013-10-24 16:39:36 +00006924 // Select the correct opcode and register class for unit size load/store
6925 bool IsNeon = UnitSize >= 8;
6926 TRC = (IsThumb1 || IsThumb2) ? (const TargetRegisterClass *)&ARM::tGPRRegClass
6927 : (const TargetRegisterClass *)&ARM::GPRRegClass;
Manman Renb504f492013-10-29 22:27:32 +00006928 if (IsNeon)
David Peixottob0653e532013-10-24 16:39:36 +00006929 VecTRC = UnitSize == 16
6930 ? (const TargetRegisterClass *)&ARM::DPairRegClass
6931 : UnitSize == 8
6932 ? (const TargetRegisterClass *)&ARM::DPRRegClass
Craig Topper062a2ba2014-04-25 05:30:21 +00006933 : nullptr;
David Peixottob0653e532013-10-24 16:39:36 +00006934
Manman Rene8735522012-06-01 19:33:18 +00006935 unsigned BytesLeft = SizeVal % UnitSize;
6936 unsigned LoopSize = SizeVal - BytesLeft;
6937
6938 if (SizeVal <= Subtarget->getMaxInlineSizeThreshold()) {
6939 // Use LDR and STR to copy.
6940 // [scratch, srcOut] = LDR_POST(srcIn, UnitSize)
6941 // [destOut] = STR_POST(scratch, destIn, UnitSize)
6942 unsigned srcIn = src;
6943 unsigned destIn = dest;
6944 for (unsigned i = 0; i < LoopSize; i+=UnitSize) {
David Peixottob0653e532013-10-24 16:39:36 +00006945 unsigned srcOut = MRI.createVirtualRegister(TRC);
6946 unsigned destOut = MRI.createVirtualRegister(TRC);
6947 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00006948 emitPostLd(BB, MI, TII, dl, UnitSize, scratch, srcIn, srcOut,
6949 IsThumb1, IsThumb2);
6950 emitPostSt(BB, MI, TII, dl, UnitSize, scratch, destIn, destOut,
6951 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00006952 srcIn = srcOut;
6953 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00006954 }
6955
6956 // Handle the leftover bytes with LDRB and STRB.
6957 // [scratch, srcOut] = LDRB_POST(srcIn, 1)
6958 // [destOut] = STRB_POST(scratch, destIn, 1)
Manman Rene8735522012-06-01 19:33:18 +00006959 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00006960 unsigned srcOut = MRI.createVirtualRegister(TRC);
6961 unsigned destOut = MRI.createVirtualRegister(TRC);
6962 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00006963 emitPostLd(BB, MI, TII, dl, 1, scratch, srcIn, srcOut,
6964 IsThumb1, IsThumb2);
6965 emitPostSt(BB, MI, TII, dl, 1, scratch, destIn, destOut,
6966 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00006967 srcIn = srcOut;
6968 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00006969 }
6970 MI->eraseFromParent(); // The instruction is gone now.
6971 return BB;
6972 }
6973
6974 // Expand the pseudo op to a loop.
6975 // thisMBB:
6976 // ...
6977 // movw varEnd, # --> with thumb2
6978 // movt varEnd, #
6979 // ldrcp varEnd, idx --> without thumb2
6980 // fallthrough --> loopMBB
6981 // loopMBB:
6982 // PHI varPhi, varEnd, varLoop
6983 // PHI srcPhi, src, srcLoop
6984 // PHI destPhi, dst, destLoop
6985 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
6986 // [destLoop] = STR_POST(scratch, destPhi, UnitSize)
6987 // subs varLoop, varPhi, #UnitSize
6988 // bne loopMBB
6989 // fallthrough --> exitMBB
6990 // exitMBB:
6991 // epilogue to handle left-over bytes
6992 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
6993 // [destOut] = STRB_POST(scratch, destLoop, 1)
6994 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6995 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
6996 MF->insert(It, loopMBB);
6997 MF->insert(It, exitMBB);
6998
6999 // Transfer the remainder of BB and its successor edges to exitMBB.
7000 exitMBB->splice(exitMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007001 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Manman Rene8735522012-06-01 19:33:18 +00007002 exitMBB->transferSuccessorsAndUpdatePHIs(BB);
7003
7004 // Load an immediate to varEnd.
David Peixottob0653e532013-10-24 16:39:36 +00007005 unsigned varEnd = MRI.createVirtualRegister(TRC);
7006 if (IsThumb2) {
7007 unsigned Vtmp = varEnd;
7008 if ((LoopSize & 0xFFFF0000) != 0)
7009 Vtmp = MRI.createVirtualRegister(TRC);
7010 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVi16), Vtmp)
7011 .addImm(LoopSize & 0xFFFF));
7012
7013 if ((LoopSize & 0xFFFF0000) != 0)
7014 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2MOVTi16), varEnd)
7015 .addReg(Vtmp).addImm(LoopSize >> 16));
7016 } else {
7017 MachineConstantPool *ConstantPool = MF->getConstantPool();
7018 Type *Int32Ty = Type::getInt32Ty(MF->getFunction()->getContext());
7019 const Constant *C = ConstantInt::get(Int32Ty, LoopSize);
7020
7021 // MachineConstantPool wants an explicit alignment.
7022 unsigned Align = getDataLayout()->getPrefTypeAlignment(Int32Ty);
7023 if (Align == 0)
7024 Align = getDataLayout()->getTypeAllocSize(C->getType());
7025 unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align);
7026
7027 if (IsThumb1)
7028 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::tLDRpci)).addReg(
7029 varEnd, RegState::Define).addConstantPoolIndex(Idx));
7030 else
7031 AddDefaultPred(BuildMI(*BB, MI, dl, TII->get(ARM::LDRcp)).addReg(
7032 varEnd, RegState::Define).addConstantPoolIndex(Idx).addImm(0));
7033 }
Manman Rene8735522012-06-01 19:33:18 +00007034 BB->addSuccessor(loopMBB);
7035
7036 // Generate the loop body:
7037 // varPhi = PHI(varLoop, varEnd)
7038 // srcPhi = PHI(srcLoop, src)
7039 // destPhi = PHI(destLoop, dst)
7040 MachineBasicBlock *entryBB = BB;
7041 BB = loopMBB;
David Peixottob0653e532013-10-24 16:39:36 +00007042 unsigned varLoop = MRI.createVirtualRegister(TRC);
7043 unsigned varPhi = MRI.createVirtualRegister(TRC);
7044 unsigned srcLoop = MRI.createVirtualRegister(TRC);
7045 unsigned srcPhi = MRI.createVirtualRegister(TRC);
7046 unsigned destLoop = MRI.createVirtualRegister(TRC);
7047 unsigned destPhi = MRI.createVirtualRegister(TRC);
Manman Rene8735522012-06-01 19:33:18 +00007048
7049 BuildMI(*BB, BB->begin(), dl, TII->get(ARM::PHI), varPhi)
7050 .addReg(varLoop).addMBB(loopMBB)
7051 .addReg(varEnd).addMBB(entryBB);
7052 BuildMI(BB, dl, TII->get(ARM::PHI), srcPhi)
7053 .addReg(srcLoop).addMBB(loopMBB)
7054 .addReg(src).addMBB(entryBB);
7055 BuildMI(BB, dl, TII->get(ARM::PHI), destPhi)
7056 .addReg(destLoop).addMBB(loopMBB)
7057 .addReg(dest).addMBB(entryBB);
7058
7059 // [scratch, srcLoop] = LDR_POST(srcPhi, UnitSize)
7060 // [destLoop] = STR_POST(scratch, destPhi, UnitSiz)
David Peixottob0653e532013-10-24 16:39:36 +00007061 unsigned scratch = MRI.createVirtualRegister(IsNeon ? VecTRC : TRC);
Manman Renb504f492013-10-29 22:27:32 +00007062 emitPostLd(BB, BB->end(), TII, dl, UnitSize, scratch, srcPhi, srcLoop,
7063 IsThumb1, IsThumb2);
7064 emitPostSt(BB, BB->end(), TII, dl, UnitSize, scratch, destPhi, destLoop,
7065 IsThumb1, IsThumb2);
Manman Rene8735522012-06-01 19:33:18 +00007066
7067 // Decrement loop variable by UnitSize.
David Peixottob0653e532013-10-24 16:39:36 +00007068 if (IsThumb1) {
7069 MachineInstrBuilder MIB =
7070 BuildMI(*BB, BB->end(), dl, TII->get(ARM::tSUBi8), varLoop);
7071 MIB = AddDefaultT1CC(MIB);
7072 MIB.addReg(varPhi).addImm(UnitSize);
7073 AddDefaultPred(MIB);
7074 } else {
7075 MachineInstrBuilder MIB =
7076 BuildMI(*BB, BB->end(), dl,
7077 TII->get(IsThumb2 ? ARM::t2SUBri : ARM::SUBri), varLoop);
7078 AddDefaultCC(AddDefaultPred(MIB.addReg(varPhi).addImm(UnitSize)));
7079 MIB->getOperand(5).setReg(ARM::CPSR);
7080 MIB->getOperand(5).setIsDef(true);
7081 }
7082 BuildMI(*BB, BB->end(), dl,
7083 TII->get(IsThumb1 ? ARM::tBcc : IsThumb2 ? ARM::t2Bcc : ARM::Bcc))
7084 .addMBB(loopMBB).addImm(ARMCC::NE).addReg(ARM::CPSR);
Manman Rene8735522012-06-01 19:33:18 +00007085
7086 // loopMBB can loop back to loopMBB or fall through to exitMBB.
7087 BB->addSuccessor(loopMBB);
7088 BB->addSuccessor(exitMBB);
7089
7090 // Add epilogue to handle BytesLeft.
7091 BB = exitMBB;
7092 MachineInstr *StartOfExit = exitMBB->begin();
Manman Rene8735522012-06-01 19:33:18 +00007093
7094 // [scratch, srcOut] = LDRB_POST(srcLoop, 1)
7095 // [destOut] = STRB_POST(scratch, destLoop, 1)
7096 unsigned srcIn = srcLoop;
7097 unsigned destIn = destLoop;
7098 for (unsigned i = 0; i < BytesLeft; i++) {
David Peixottob0653e532013-10-24 16:39:36 +00007099 unsigned srcOut = MRI.createVirtualRegister(TRC);
7100 unsigned destOut = MRI.createVirtualRegister(TRC);
7101 unsigned scratch = MRI.createVirtualRegister(TRC);
Manman Renb504f492013-10-29 22:27:32 +00007102 emitPostLd(BB, StartOfExit, TII, dl, 1, scratch, srcIn, srcOut,
7103 IsThumb1, IsThumb2);
7104 emitPostSt(BB, StartOfExit, TII, dl, 1, scratch, destIn, destOut,
7105 IsThumb1, IsThumb2);
David Peixottob0653e532013-10-24 16:39:36 +00007106 srcIn = srcOut;
7107 destIn = destOut;
Manman Rene8735522012-06-01 19:33:18 +00007108 }
7109
7110 MI->eraseFromParent(); // The instruction is gone now.
7111 return BB;
7112}
7113
Jim Grosbach8f9a3ac2009-12-12 01:40:06 +00007114MachineBasicBlock *
Evan Cheng29cfb672008-01-30 18:18:23 +00007115ARMTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00007116 MachineBasicBlock *BB) const {
Evan Cheng10043e22007-01-19 07:51:42 +00007117 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Dale Johannesen7647da62009-02-13 02:25:56 +00007118 DebugLoc dl = MI->getDebugLoc();
Jim Grosbach57ccc192009-12-14 20:14:59 +00007119 bool isThumb2 = Subtarget->isThumb2();
Evan Cheng10043e22007-01-19 07:51:42 +00007120 switch (MI->getOpcode()) {
Andrew Trick0ed57782011-04-23 03:55:32 +00007121 default: {
Jim Grosbach5c4e99f2009-12-11 01:42:04 +00007122 MI->dump();
Evan Chengb972e562009-08-07 00:34:42 +00007123 llvm_unreachable("Unexpected instr type to insert");
Andrew Trick0ed57782011-04-23 03:55:32 +00007124 }
Jim Grosbach9c0b86a2011-09-16 21:55:56 +00007125 // The Thumb2 pre-indexed stores have the same MI operands, they just
7126 // define them differently in the .td files from the isel patterns, so
7127 // they need pseudos.
7128 case ARM::t2STR_preidx:
7129 MI->setDesc(TII->get(ARM::t2STR_PRE));
7130 return BB;
7131 case ARM::t2STRB_preidx:
7132 MI->setDesc(TII->get(ARM::t2STRB_PRE));
7133 return BB;
7134 case ARM::t2STRH_preidx:
7135 MI->setDesc(TII->get(ARM::t2STRH_PRE));
7136 return BB;
7137
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007138 case ARM::STRi_preidx:
7139 case ARM::STRBi_preidx: {
Jim Grosbach5e80abb2011-08-09 21:22:41 +00007140 unsigned NewOpc = MI->getOpcode() == ARM::STRi_preidx ?
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007141 ARM::STR_PRE_IMM : ARM::STRB_PRE_IMM;
7142 // Decode the offset.
7143 unsigned Offset = MI->getOperand(4).getImm();
7144 bool isSub = ARM_AM::getAM2Op(Offset) == ARM_AM::sub;
7145 Offset = ARM_AM::getAM2Offset(Offset);
7146 if (isSub)
7147 Offset = -Offset;
7148
Jim Grosbachf402f692011-08-12 21:02:34 +00007149 MachineMemOperand *MMO = *MI->memoperands_begin();
Benjamin Kramer61a1ff52011-08-27 17:36:14 +00007150 BuildMI(*BB, MI, dl, TII->get(NewOpc))
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007151 .addOperand(MI->getOperand(0)) // Rn_wb
7152 .addOperand(MI->getOperand(1)) // Rt
7153 .addOperand(MI->getOperand(2)) // Rn
7154 .addImm(Offset) // offset (skip GPR==zero_reg)
7155 .addOperand(MI->getOperand(5)) // pred
Jim Grosbachf402f692011-08-12 21:02:34 +00007156 .addOperand(MI->getOperand(6))
7157 .addMemOperand(MMO);
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007158 MI->eraseFromParent();
7159 return BB;
7160 }
7161 case ARM::STRr_preidx:
Jim Grosbachd886f8c2011-08-11 21:17:22 +00007162 case ARM::STRBr_preidx:
7163 case ARM::STRH_preidx: {
7164 unsigned NewOpc;
7165 switch (MI->getOpcode()) {
7166 default: llvm_unreachable("unexpected opcode!");
7167 case ARM::STRr_preidx: NewOpc = ARM::STR_PRE_REG; break;
7168 case ARM::STRBr_preidx: NewOpc = ARM::STRB_PRE_REG; break;
7169 case ARM::STRH_preidx: NewOpc = ARM::STRH_PRE; break;
7170 }
Jim Grosbachf0c95ca2011-08-05 20:35:44 +00007171 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(NewOpc));
7172 for (unsigned i = 0; i < MI->getNumOperands(); ++i)
7173 MIB.addOperand(MI->getOperand(i));
7174 MI->eraseFromParent();
7175 return BB;
7176 }
Eli Friedmanc3f9c4a2011-08-31 00:31:29 +00007177
Evan Chengbb2af352009-08-12 05:17:19 +00007178 case ARM::tMOVCCr_pseudo: {
Evan Cheng10043e22007-01-19 07:51:42 +00007179 // To "insert" a SELECT_CC instruction, we actually have to insert the
7180 // diamond control-flow pattern. The incoming instruction knows the
7181 // destination vreg to set, the condition code register to branch on, the
7182 // true/false values to select between, and a branch opcode to use.
7183 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00007184 MachineFunction::iterator It = BB;
Evan Cheng10043e22007-01-19 07:51:42 +00007185 ++It;
7186
7187 // thisMBB:
7188 // ...
7189 // TrueVal = ...
7190 // cmpTY ccX, r1, r2
7191 // bCC copy1MBB
7192 // fallthrough --> copy0MBB
7193 MachineBasicBlock *thisMBB = BB;
Dan Gohman3b460302008-07-07 23:14:23 +00007194 MachineFunction *F = BB->getParent();
7195 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7196 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dan Gohmanf4f04102010-07-06 15:49:48 +00007197 F->insert(It, copy0MBB);
7198 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00007199
7200 // Transfer the remainder of BB and its successor edges to sinkMBB.
7201 sinkMBB->splice(sinkMBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007202 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Dan Gohman34396292010-07-06 20:24:04 +00007203 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
7204
Dan Gohmanf4f04102010-07-06 15:49:48 +00007205 BB->addSuccessor(copy0MBB);
7206 BB->addSuccessor(sinkMBB);
Dan Gohman12205642010-07-06 15:18:19 +00007207
Dan Gohman34396292010-07-06 20:24:04 +00007208 BuildMI(BB, dl, TII->get(ARM::tBcc)).addMBB(sinkMBB)
7209 .addImm(MI->getOperand(3).getImm()).addReg(MI->getOperand(4).getReg());
7210
Evan Cheng10043e22007-01-19 07:51:42 +00007211 // copy0MBB:
7212 // %FalseValue = ...
7213 // # fallthrough to sinkMBB
7214 BB = copy0MBB;
7215
7216 // Update machine-CFG edges
7217 BB->addSuccessor(sinkMBB);
7218
7219 // sinkMBB:
7220 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7221 // ...
7222 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00007223 BuildMI(*BB, BB->begin(), dl,
7224 TII->get(ARM::PHI), MI->getOperand(0).getReg())
Evan Cheng10043e22007-01-19 07:51:42 +00007225 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7226 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7227
Dan Gohman34396292010-07-06 20:24:04 +00007228 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng10043e22007-01-19 07:51:42 +00007229 return BB;
7230 }
Evan Chengb972e562009-08-07 00:34:42 +00007231
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007232 case ARM::BCCi64:
7233 case ARM::BCCZi64: {
Bob Wilson36be00c2010-12-23 22:45:49 +00007234 // If there is an unconditional branch to the other successor, remove it.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007235 BB->erase(std::next(MachineBasicBlock::iterator(MI)), BB->end());
Andrew Trick5eb0a302011-01-19 02:26:13 +00007236
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007237 // Compare both parts that make up the double comparison separately for
7238 // equality.
7239 bool RHSisZero = MI->getOpcode() == ARM::BCCZi64;
7240
7241 unsigned LHS1 = MI->getOperand(1).getReg();
7242 unsigned LHS2 = MI->getOperand(2).getReg();
7243 if (RHSisZero) {
7244 AddDefaultPred(BuildMI(BB, dl,
7245 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7246 .addReg(LHS1).addImm(0));
7247 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7248 .addReg(LHS2).addImm(0)
7249 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7250 } else {
7251 unsigned RHS1 = MI->getOperand(3).getReg();
7252 unsigned RHS2 = MI->getOperand(4).getReg();
7253 AddDefaultPred(BuildMI(BB, dl,
7254 TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7255 .addReg(LHS1).addReg(RHS1));
7256 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2CMPrr : ARM::CMPrr))
7257 .addReg(LHS2).addReg(RHS2)
7258 .addImm(ARMCC::EQ).addReg(ARM::CPSR);
7259 }
7260
7261 MachineBasicBlock *destMBB = MI->getOperand(RHSisZero ? 3 : 5).getMBB();
7262 MachineBasicBlock *exitMBB = OtherSucc(BB, destMBB);
7263 if (MI->getOperand(0).getImm() == ARMCC::NE)
7264 std::swap(destMBB, exitMBB);
7265
7266 BuildMI(BB, dl, TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc))
7267 .addMBB(destMBB).addImm(ARMCC::EQ).addReg(ARM::CPSR);
Owen Anderson29cfe6c2011-09-09 21:48:23 +00007268 if (isThumb2)
7269 AddDefaultPred(BuildMI(BB, dl, TII->get(ARM::t2B)).addMBB(exitMBB));
7270 else
7271 BuildMI(BB, dl, TII->get(ARM::B)) .addMBB(exitMBB);
Evan Cheng0cc4ad92010-07-13 19:27:42 +00007272
7273 MI->eraseFromParent(); // The pseudo instruction is gone now.
7274 return BB;
7275 }
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007276
Bill Wendlingf7f223f2011-10-17 20:37:20 +00007277 case ARM::Int_eh_sjlj_setjmp:
7278 case ARM::Int_eh_sjlj_setjmp_nofp:
7279 case ARM::tInt_eh_sjlj_setjmp:
7280 case ARM::t2Int_eh_sjlj_setjmp:
7281 case ARM::t2Int_eh_sjlj_setjmp_nofp:
7282 EmitSjLjDispatchBlock(MI, BB);
7283 return BB;
7284
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007285 case ARM::ABS:
7286 case ARM::t2ABS: {
7287 // To insert an ABS instruction, we have to insert the
7288 // diamond control-flow pattern. The incoming instruction knows the
7289 // source vreg to test against 0, the destination vreg to set,
7290 // the condition code register to branch on, the
Andrew Trick3f07c422011-10-18 18:40:53 +00007291 // true/false values to select between, and a branch opcode to use.
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007292 // It transforms
7293 // V1 = ABS V0
7294 // into
7295 // V2 = MOVS V0
7296 // BCC (branch to SinkBB if V0 >= 0)
7297 // RSBBB: V3 = RSBri V2, 0 (compute ABS if V2 < 0)
Andrew Trick3f07c422011-10-18 18:40:53 +00007298 // SinkBB: V1 = PHI(V2, V3)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007299 const BasicBlock *LLVM_BB = BB->getBasicBlock();
7300 MachineFunction::iterator BBI = BB;
7301 ++BBI;
7302 MachineFunction *Fn = BB->getParent();
7303 MachineBasicBlock *RSBBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7304 MachineBasicBlock *SinkBB = Fn->CreateMachineBasicBlock(LLVM_BB);
7305 Fn->insert(BBI, RSBBB);
7306 Fn->insert(BBI, SinkBB);
7307
7308 unsigned int ABSSrcReg = MI->getOperand(1).getReg();
7309 unsigned int ABSDstReg = MI->getOperand(0).getReg();
7310 bool isThumb2 = Subtarget->isThumb2();
7311 MachineRegisterInfo &MRI = Fn->getRegInfo();
7312 // In Thumb mode S must not be specified if source register is the SP or
7313 // PC and if destination register is the SP, so restrict register class
Craig Topperc7242e02012-04-20 07:30:17 +00007314 unsigned NewRsbDstReg = MRI.createVirtualRegister(isThumb2 ?
7315 (const TargetRegisterClass*)&ARM::rGPRRegClass :
7316 (const TargetRegisterClass*)&ARM::GPRRegClass);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007317
7318 // Transfer the remainder of BB and its successor edges to sinkMBB.
7319 SinkBB->splice(SinkBB->begin(), BB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00007320 std::next(MachineBasicBlock::iterator(MI)), BB->end());
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007321 SinkBB->transferSuccessorsAndUpdatePHIs(BB);
7322
7323 BB->addSuccessor(RSBBB);
7324 BB->addSuccessor(SinkBB);
7325
7326 // fall through to SinkMBB
7327 RSBBB->addSuccessor(SinkBB);
7328
Manman Rene0763c72012-06-15 21:32:12 +00007329 // insert a cmp at the end of BB
Andrew Trickbc325162012-07-18 18:34:24 +00007330 AddDefaultPred(BuildMI(BB, dl,
Manman Rene0763c72012-06-15 21:32:12 +00007331 TII->get(isThumb2 ? ARM::t2CMPri : ARM::CMPri))
7332 .addReg(ABSSrcReg).addImm(0));
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007333
7334 // insert a bcc with opposite CC to ARMCC::MI at the end of BB
Andrew Trick3f07c422011-10-18 18:40:53 +00007335 BuildMI(BB, dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007336 TII->get(isThumb2 ? ARM::t2Bcc : ARM::Bcc)).addMBB(SinkBB)
7337 .addImm(ARMCC::getOppositeCondition(ARMCC::MI)).addReg(ARM::CPSR);
7338
7339 // insert rsbri in RSBBB
7340 // Note: BCC and rsbri will be converted into predicated rsbmi
7341 // by if-conversion pass
Andrew Trick3f07c422011-10-18 18:40:53 +00007342 BuildMI(*RSBBB, RSBBB->begin(), dl,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007343 TII->get(isThumb2 ? ARM::t2RSBri : ARM::RSBri), NewRsbDstReg)
Manman Rene0763c72012-06-15 21:32:12 +00007344 .addReg(ABSSrcReg, RegState::Kill)
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007345 .addImm(0).addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
7346
Andrew Trick3f07c422011-10-18 18:40:53 +00007347 // insert PHI in SinkBB,
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007348 // reuse ABSDstReg to not change uses of ABS instruction
7349 BuildMI(*SinkBB, SinkBB->begin(), dl,
7350 TII->get(ARM::PHI), ABSDstReg)
7351 .addReg(NewRsbDstReg).addMBB(RSBBB)
Manman Rene0763c72012-06-15 21:32:12 +00007352 .addReg(ABSSrcReg).addMBB(BB);
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007353
7354 // remove ABS instruction
Andrew Trick3f07c422011-10-18 18:40:53 +00007355 MI->eraseFromParent();
Bill Wendlinga7d697e2011-10-10 22:59:55 +00007356
7357 // return last added BB
7358 return SinkBB;
7359 }
Manman Rene8735522012-06-01 19:33:18 +00007360 case ARM::COPY_STRUCT_BYVAL_I32:
Manman Ren9f911162012-06-01 02:44:42 +00007361 ++NumLoopByVals;
Manman Rene8735522012-06-01 19:33:18 +00007362 return EmitStructByval(MI, BB);
Evan Cheng10043e22007-01-19 07:51:42 +00007363 }
7364}
7365
Evan Chenge6fba772011-08-30 19:09:48 +00007366void ARMTargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
7367 SDNode *Node) const {
Evan Cheng7f8e5632011-12-07 07:15:52 +00007368 if (!MI->hasPostISelHook()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007369 assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
7370 "Pseudo flag-setting opcodes must be marked with 'hasPostISelHook'");
7371 return;
7372 }
7373
Evan Cheng7f8e5632011-12-07 07:15:52 +00007374 const MCInstrDesc *MCID = &MI->getDesc();
Andrew Trick8586e622011-09-20 03:17:40 +00007375 // Adjust potentially 's' setting instructions after isel, i.e. ADC, SBC, RSB,
7376 // RSC. Coming out of isel, they have an implicit CPSR def, but the optional
7377 // operand is still set to noreg. If needed, set the optional operand's
7378 // register to CPSR, and remove the redundant implicit def.
Andrew Trick924123a2011-09-21 02:20:46 +00007379 //
Andrew Trick88b24502011-10-18 19:18:52 +00007380 // e.g. ADCS (..., CPSR<imp-def>) -> ADC (... opt:CPSR<def>).
Andrew Trick8586e622011-09-20 03:17:40 +00007381
Andrew Trick924123a2011-09-21 02:20:46 +00007382 // Rename pseudo opcodes.
7383 unsigned NewOpc = convertAddSubFlagsOpcode(MI->getOpcode());
7384 if (NewOpc) {
7385 const ARMBaseInstrInfo *TII =
7386 static_cast<const ARMBaseInstrInfo*>(getTargetMachine().getInstrInfo());
Andrew Trick88b24502011-10-18 19:18:52 +00007387 MCID = &TII->get(NewOpc);
7388
7389 assert(MCID->getNumOperands() == MI->getDesc().getNumOperands() + 1 &&
7390 "converted opcode should be the same except for cc_out");
7391
7392 MI->setDesc(*MCID);
7393
7394 // Add the optional cc_out operand
7395 MI->addOperand(MachineOperand::CreateReg(0, /*isDef=*/true));
Andrew Trick924123a2011-09-21 02:20:46 +00007396 }
Andrew Trick88b24502011-10-18 19:18:52 +00007397 unsigned ccOutIdx = MCID->getNumOperands() - 1;
Andrew Trick8586e622011-09-20 03:17:40 +00007398
7399 // Any ARM instruction that sets the 's' bit should specify an optional
7400 // "cc_out" operand in the last operand position.
Evan Cheng7f8e5632011-12-07 07:15:52 +00007401 if (!MI->hasOptionalDef() || !MCID->OpInfo[ccOutIdx].isOptionalDef()) {
Andrew Trick924123a2011-09-21 02:20:46 +00007402 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007403 return;
7404 }
Andrew Trick924123a2011-09-21 02:20:46 +00007405 // Look for an implicit def of CPSR added by MachineInstr ctor. Remove it
7406 // since we already have an optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007407 bool definesCPSR = false;
7408 bool deadCPSR = false;
Andrew Trick88b24502011-10-18 19:18:52 +00007409 for (unsigned i = MCID->getNumOperands(), e = MI->getNumOperands();
Andrew Trick8586e622011-09-20 03:17:40 +00007410 i != e; ++i) {
7411 const MachineOperand &MO = MI->getOperand(i);
7412 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) {
7413 definesCPSR = true;
7414 if (MO.isDead())
7415 deadCPSR = true;
7416 MI->RemoveOperand(i);
7417 break;
Evan Chenge6fba772011-08-30 19:09:48 +00007418 }
7419 }
Andrew Trick8586e622011-09-20 03:17:40 +00007420 if (!definesCPSR) {
Andrew Trick924123a2011-09-21 02:20:46 +00007421 assert(!NewOpc && "Optional cc_out operand required");
Andrew Trick8586e622011-09-20 03:17:40 +00007422 return;
7423 }
7424 assert(deadCPSR == !Node->hasAnyUseOfValue(1) && "inconsistent dead flag");
Andrew Trick924123a2011-09-21 02:20:46 +00007425 if (deadCPSR) {
7426 assert(!MI->getOperand(ccOutIdx).getReg() &&
7427 "expect uninitialized optional cc_out operand");
Andrew Trick8586e622011-09-20 03:17:40 +00007428 return;
Andrew Trick924123a2011-09-21 02:20:46 +00007429 }
Andrew Trick8586e622011-09-20 03:17:40 +00007430
Andrew Trick924123a2011-09-21 02:20:46 +00007431 // If this instruction was defined with an optional CPSR def and its dag node
7432 // had a live implicit CPSR def, then activate the optional CPSR def.
Andrew Trick8586e622011-09-20 03:17:40 +00007433 MachineOperand &MO = MI->getOperand(ccOutIdx);
7434 MO.setReg(ARM::CPSR);
7435 MO.setIsDef(true);
Evan Chenge6fba772011-08-30 19:09:48 +00007436}
7437
Evan Cheng10043e22007-01-19 07:51:42 +00007438//===----------------------------------------------------------------------===//
7439// ARM Optimization Hooks
7440//===----------------------------------------------------------------------===//
7441
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007442// Helper function that checks if N is a null or all ones constant.
7443static inline bool isZeroOrAllOnes(SDValue N, bool AllOnes) {
7444 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
7445 if (!C)
7446 return false;
7447 return AllOnes ? C->isAllOnesValue() : C->isNullValue();
7448}
7449
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007450// Return true if N is conditionally 0 or all ones.
7451// Detects these expressions where cc is an i1 value:
7452//
7453// (select cc 0, y) [AllOnes=0]
7454// (select cc y, 0) [AllOnes=0]
7455// (zext cc) [AllOnes=0]
7456// (sext cc) [AllOnes=0/1]
7457// (select cc -1, y) [AllOnes=1]
7458// (select cc y, -1) [AllOnes=1]
7459//
7460// Invert is set when N is the null/all ones constant when CC is false.
7461// OtherOp is set to the alternative value of N.
7462static bool isConditionalZeroOrAllOnes(SDNode *N, bool AllOnes,
7463 SDValue &CC, bool &Invert,
7464 SDValue &OtherOp,
7465 SelectionDAG &DAG) {
7466 switch (N->getOpcode()) {
7467 default: return false;
7468 case ISD::SELECT: {
7469 CC = N->getOperand(0);
7470 SDValue N1 = N->getOperand(1);
7471 SDValue N2 = N->getOperand(2);
7472 if (isZeroOrAllOnes(N1, AllOnes)) {
7473 Invert = false;
7474 OtherOp = N2;
7475 return true;
7476 }
7477 if (isZeroOrAllOnes(N2, AllOnes)) {
7478 Invert = true;
7479 OtherOp = N1;
7480 return true;
7481 }
7482 return false;
7483 }
7484 case ISD::ZERO_EXTEND:
7485 // (zext cc) can never be the all ones value.
7486 if (AllOnes)
7487 return false;
7488 // Fall through.
7489 case ISD::SIGN_EXTEND: {
7490 EVT VT = N->getValueType(0);
7491 CC = N->getOperand(0);
7492 if (CC.getValueType() != MVT::i1)
7493 return false;
7494 Invert = !AllOnes;
7495 if (AllOnes)
7496 // When looking for an AllOnes constant, N is an sext, and the 'other'
7497 // value is 0.
7498 OtherOp = DAG.getConstant(0, VT);
7499 else if (N->getOpcode() == ISD::ZERO_EXTEND)
7500 // When looking for a 0 constant, N can be zext or sext.
7501 OtherOp = DAG.getConstant(1, VT);
7502 else
7503 OtherOp = DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
7504 return true;
7505 }
7506 }
7507}
7508
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007509// Combine a constant select operand into its use:
7510//
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007511// (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
7512// (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
7513// (and (select cc, -1, c), x) -> (select cc, x, (and, x, c)) [AllOnes=1]
7514// (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
7515// (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007516//
7517// The transform is rejected if the select doesn't have a constant operand that
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007518// is null, or all ones when AllOnes is set.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007519//
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007520// Also recognize sext/zext from i1:
7521//
7522// (add (zext cc), x) -> (select cc (add x, 1), x)
7523// (add (sext cc), x) -> (select cc (add x, -1), x)
7524//
7525// These transformations eventually create predicated instructions.
7526//
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007527// @param N The node to transform.
7528// @param Slct The N operand that is a select.
7529// @param OtherOp The other N operand (x above).
7530// @param DCI Context.
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007531// @param AllOnes Require the select constant to be all ones instead of null.
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007532// @returns The new node, or SDValue() on failure.
Chris Lattner4147f082009-03-12 06:52:53 +00007533static
7534SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007535 TargetLowering::DAGCombinerInfo &DCI,
7536 bool AllOnes = false) {
Chris Lattner4147f082009-03-12 06:52:53 +00007537 SelectionDAG &DAG = DCI.DAG;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007538 EVT VT = N->getValueType(0);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007539 SDValue NonConstantVal;
7540 SDValue CCOp;
7541 bool SwapSelectOps;
7542 if (!isConditionalZeroOrAllOnes(Slct.getNode(), AllOnes, CCOp, SwapSelectOps,
7543 NonConstantVal, DAG))
Jakob Stoklund Olesenc1dee482012-08-17 16:59:09 +00007544 return SDValue();
7545
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007546 // Slct is now know to be the desired identity constant when CC is true.
7547 SDValue TrueVal = OtherOp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007548 SDValue FalseVal = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007549 OtherOp, NonConstantVal);
7550 // Unless SwapSelectOps says CC should be false.
7551 if (SwapSelectOps)
7552 std::swap(TrueVal, FalseVal);
7553
Andrew Trickef9de2a2013-05-25 02:42:55 +00007554 return DAG.getNode(ISD::SELECT, SDLoc(N), VT,
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007555 CCOp, TrueVal, FalseVal);
Chris Lattner4147f082009-03-12 06:52:53 +00007556}
7557
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007558// Attempt combineSelectAndUse on each operand of a commutative operator N.
7559static
7560SDValue combineSelectAndUseCommutative(SDNode *N, bool AllOnes,
7561 TargetLowering::DAGCombinerInfo &DCI) {
7562 SDValue N0 = N->getOperand(0);
7563 SDValue N1 = N->getOperand(1);
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007564 if (N0.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007565 SDValue Result = combineSelectAndUse(N, N0, N1, DCI, AllOnes);
7566 if (Result.getNode())
7567 return Result;
7568 }
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007569 if (N1.getNode()->hasOneUse()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00007570 SDValue Result = combineSelectAndUse(N, N1, N0, DCI, AllOnes);
7571 if (Result.getNode())
7572 return Result;
7573 }
7574 return SDValue();
7575}
7576
Eric Christopher1b8b94192011-06-29 21:10:36 +00007577// AddCombineToVPADDL- For pair-wise add on neon, use the vpaddl instruction
Tanya Lattnere9e67052011-06-14 23:48:48 +00007578// (only after legalization).
7579static SDValue AddCombineToVPADDL(SDNode *N, SDValue N0, SDValue N1,
7580 TargetLowering::DAGCombinerInfo &DCI,
7581 const ARMSubtarget *Subtarget) {
7582
7583 // Only perform optimization if after legalize, and if NEON is available. We
7584 // also expected both operands to be BUILD_VECTORs.
7585 if (DCI.isBeforeLegalize() || !Subtarget->hasNEON()
7586 || N0.getOpcode() != ISD::BUILD_VECTOR
7587 || N1.getOpcode() != ISD::BUILD_VECTOR)
7588 return SDValue();
7589
7590 // Check output type since VPADDL operand elements can only be 8, 16, or 32.
7591 EVT VT = N->getValueType(0);
7592 if (!VT.isInteger() || VT.getVectorElementType() == MVT::i64)
7593 return SDValue();
7594
7595 // Check that the vector operands are of the right form.
7596 // N0 and N1 are BUILD_VECTOR nodes with N number of EXTRACT_VECTOR
7597 // operands, where N is the size of the formed vector.
7598 // Each EXTRACT_VECTOR should have the same input vector and odd or even
7599 // index such that we have a pair wise add pattern.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007600
7601 // Grab the vector that all EXTRACT_VECTOR nodes should be referencing.
Bob Wilson4b12a112011-06-15 06:04:34 +00007602 if (N0->getOperand(0)->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Tanya Lattnere9e67052011-06-14 23:48:48 +00007603 return SDValue();
Bob Wilson4b12a112011-06-15 06:04:34 +00007604 SDValue Vec = N0->getOperand(0)->getOperand(0);
7605 SDNode *V = Vec.getNode();
7606 unsigned nextIndex = 0;
Tanya Lattnere9e67052011-06-14 23:48:48 +00007607
Eric Christopher1b8b94192011-06-29 21:10:36 +00007608 // For each operands to the ADD which are BUILD_VECTORs,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007609 // check to see if each of their operands are an EXTRACT_VECTOR with
7610 // the same vector and appropriate index.
7611 for (unsigned i = 0, e = N0->getNumOperands(); i != e; ++i) {
7612 if (N0->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT
7613 && N1->getOperand(i)->getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
Eric Christopher1b8b94192011-06-29 21:10:36 +00007614
Tanya Lattnere9e67052011-06-14 23:48:48 +00007615 SDValue ExtVec0 = N0->getOperand(i);
7616 SDValue ExtVec1 = N1->getOperand(i);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007617
Tanya Lattnere9e67052011-06-14 23:48:48 +00007618 // First operand is the vector, verify its the same.
7619 if (V != ExtVec0->getOperand(0).getNode() ||
7620 V != ExtVec1->getOperand(0).getNode())
7621 return SDValue();
Eric Christopher1b8b94192011-06-29 21:10:36 +00007622
Tanya Lattnere9e67052011-06-14 23:48:48 +00007623 // Second is the constant, verify its correct.
7624 ConstantSDNode *C0 = dyn_cast<ConstantSDNode>(ExtVec0->getOperand(1));
7625 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(ExtVec1->getOperand(1));
Eric Christopher1b8b94192011-06-29 21:10:36 +00007626
Tanya Lattnere9e67052011-06-14 23:48:48 +00007627 // For the constant, we want to see all the even or all the odd.
7628 if (!C0 || !C1 || C0->getZExtValue() != nextIndex
7629 || C1->getZExtValue() != nextIndex+1)
7630 return SDValue();
7631
7632 // Increment index.
7633 nextIndex+=2;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007634 } else
Tanya Lattnere9e67052011-06-14 23:48:48 +00007635 return SDValue();
7636 }
7637
7638 // Create VPADDL node.
7639 SelectionDAG &DAG = DCI.DAG;
7640 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Tanya Lattnere9e67052011-06-14 23:48:48 +00007641
7642 // Build operand list.
7643 SmallVector<SDValue, 8> Ops;
7644 Ops.push_back(DAG.getConstant(Intrinsic::arm_neon_vpaddls,
7645 TLI.getPointerTy()));
7646
7647 // Input is the vector.
7648 Ops.push_back(Vec);
Eric Christopher1b8b94192011-06-29 21:10:36 +00007649
Tanya Lattnere9e67052011-06-14 23:48:48 +00007650 // Get widened type and narrowed type.
7651 MVT widenType;
7652 unsigned numElem = VT.getVectorNumElements();
Silviu Barangaa3106e62014-04-03 10:44:27 +00007653
7654 EVT inputLaneType = Vec.getValueType().getVectorElementType();
7655 switch (inputLaneType.getSimpleVT().SimpleTy) {
Tanya Lattnere9e67052011-06-14 23:48:48 +00007656 case MVT::i8: widenType = MVT::getVectorVT(MVT::i16, numElem); break;
7657 case MVT::i16: widenType = MVT::getVectorVT(MVT::i32, numElem); break;
7658 case MVT::i32: widenType = MVT::getVectorVT(MVT::i64, numElem); break;
7659 default:
Craig Toppere55c5562012-02-07 02:50:20 +00007660 llvm_unreachable("Invalid vector element type for padd optimization.");
Tanya Lattnere9e67052011-06-14 23:48:48 +00007661 }
7662
Craig Topper48d114b2014-04-26 18:35:24 +00007663 SDValue tmp = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N), widenType, Ops);
Silviu Barangaa3106e62014-04-03 10:44:27 +00007664 unsigned ExtOp = VT.bitsGT(tmp.getValueType()) ? ISD::ANY_EXTEND : ISD::TRUNCATE;
7665 return DAG.getNode(ExtOp, SDLoc(N), VT, tmp);
Tanya Lattnere9e67052011-06-14 23:48:48 +00007666}
7667
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007668static SDValue findMUL_LOHI(SDValue V) {
7669 if (V->getOpcode() == ISD::UMUL_LOHI ||
7670 V->getOpcode() == ISD::SMUL_LOHI)
7671 return V;
7672 return SDValue();
7673}
7674
7675static SDValue AddCombineTo64bitMLAL(SDNode *AddcNode,
7676 TargetLowering::DAGCombinerInfo &DCI,
7677 const ARMSubtarget *Subtarget) {
7678
7679 if (Subtarget->isThumb1Only()) return SDValue();
7680
7681 // Only perform the checks after legalize when the pattern is available.
7682 if (DCI.isBeforeLegalize()) return SDValue();
7683
7684 // Look for multiply add opportunities.
7685 // The pattern is a ISD::UMUL_LOHI followed by two add nodes, where
7686 // each add nodes consumes a value from ISD::UMUL_LOHI and there is
7687 // a glue link from the first add to the second add.
7688 // If we find this pattern, we can replace the U/SMUL_LOHI, ADDC, and ADDE by
7689 // a S/UMLAL instruction.
7690 // loAdd UMUL_LOHI
7691 // \ / :lo \ :hi
7692 // \ / \ [no multiline comment]
7693 // ADDC | hiAdd
7694 // \ :glue / /
7695 // \ / /
7696 // ADDE
7697 //
7698 assert(AddcNode->getOpcode() == ISD::ADDC && "Expect an ADDC");
7699 SDValue AddcOp0 = AddcNode->getOperand(0);
7700 SDValue AddcOp1 = AddcNode->getOperand(1);
7701
7702 // Check if the two operands are from the same mul_lohi node.
7703 if (AddcOp0.getNode() == AddcOp1.getNode())
7704 return SDValue();
7705
7706 assert(AddcNode->getNumValues() == 2 &&
7707 AddcNode->getValueType(0) == MVT::i32 &&
Michael Gottesmanb2a70562013-06-18 20:49:40 +00007708 "Expect ADDC with two result values. First: i32");
7709
7710 // Check that we have a glued ADDC node.
7711 if (AddcNode->getValueType(1) != MVT::Glue)
7712 return SDValue();
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007713
7714 // Check that the ADDC adds the low result of the S/UMUL_LOHI.
7715 if (AddcOp0->getOpcode() != ISD::UMUL_LOHI &&
7716 AddcOp0->getOpcode() != ISD::SMUL_LOHI &&
7717 AddcOp1->getOpcode() != ISD::UMUL_LOHI &&
7718 AddcOp1->getOpcode() != ISD::SMUL_LOHI)
7719 return SDValue();
7720
7721 // Look for the glued ADDE.
7722 SDNode* AddeNode = AddcNode->getGluedUser();
Craig Topper062a2ba2014-04-25 05:30:21 +00007723 if (!AddeNode)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007724 return SDValue();
7725
7726 // Make sure it is really an ADDE.
7727 if (AddeNode->getOpcode() != ISD::ADDE)
7728 return SDValue();
7729
7730 assert(AddeNode->getNumOperands() == 3 &&
7731 AddeNode->getOperand(2).getValueType() == MVT::Glue &&
7732 "ADDE node has the wrong inputs");
7733
7734 // Check for the triangle shape.
7735 SDValue AddeOp0 = AddeNode->getOperand(0);
7736 SDValue AddeOp1 = AddeNode->getOperand(1);
7737
7738 // Make sure that the ADDE operands are not coming from the same node.
7739 if (AddeOp0.getNode() == AddeOp1.getNode())
7740 return SDValue();
7741
7742 // Find the MUL_LOHI node walking up ADDE's operands.
7743 bool IsLeftOperandMUL = false;
7744 SDValue MULOp = findMUL_LOHI(AddeOp0);
7745 if (MULOp == SDValue())
7746 MULOp = findMUL_LOHI(AddeOp1);
7747 else
7748 IsLeftOperandMUL = true;
7749 if (MULOp == SDValue())
7750 return SDValue();
7751
7752 // Figure out the right opcode.
7753 unsigned Opc = MULOp->getOpcode();
7754 unsigned FinalOpc = (Opc == ISD::SMUL_LOHI) ? ARMISD::SMLAL : ARMISD::UMLAL;
7755
7756 // Figure out the high and low input values to the MLAL node.
7757 SDValue* HiMul = &MULOp;
Craig Topper062a2ba2014-04-25 05:30:21 +00007758 SDValue* HiAdd = nullptr;
7759 SDValue* LoMul = nullptr;
7760 SDValue* LowAdd = nullptr;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007761
7762 if (IsLeftOperandMUL)
7763 HiAdd = &AddeOp1;
7764 else
7765 HiAdd = &AddeOp0;
7766
7767
7768 if (AddcOp0->getOpcode() == Opc) {
7769 LoMul = &AddcOp0;
7770 LowAdd = &AddcOp1;
7771 }
7772 if (AddcOp1->getOpcode() == Opc) {
7773 LoMul = &AddcOp1;
7774 LowAdd = &AddcOp0;
7775 }
7776
Craig Topper062a2ba2014-04-25 05:30:21 +00007777 if (!LoMul)
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007778 return SDValue();
7779
7780 if (LoMul->getNode() != HiMul->getNode())
7781 return SDValue();
7782
7783 // Create the merged node.
7784 SelectionDAG &DAG = DCI.DAG;
7785
7786 // Build operand list.
7787 SmallVector<SDValue, 8> Ops;
7788 Ops.push_back(LoMul->getOperand(0));
7789 Ops.push_back(LoMul->getOperand(1));
7790 Ops.push_back(*LowAdd);
7791 Ops.push_back(*HiAdd);
7792
Andrew Trickef9de2a2013-05-25 02:42:55 +00007793 SDValue MLALNode = DAG.getNode(FinalOpc, SDLoc(AddcNode),
Craig Topper48d114b2014-04-26 18:35:24 +00007794 DAG.getVTList(MVT::i32, MVT::i32), Ops);
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00007795
7796 // Replace the ADDs' nodes uses by the MLA node's values.
7797 SDValue HiMLALResult(MLALNode.getNode(), 1);
7798 DAG.ReplaceAllUsesOfValueWith(SDValue(AddeNode, 0), HiMLALResult);
7799
7800 SDValue LoMLALResult(MLALNode.getNode(), 0);
7801 DAG.ReplaceAllUsesOfValueWith(SDValue(AddcNode, 0), LoMLALResult);
7802
7803 // Return original node to notify the driver to stop replacing.
7804 SDValue resNode(AddcNode, 0);
7805 return resNode;
7806}
7807
7808/// PerformADDCCombine - Target-specific dag combine transform from
7809/// ISD::ADDC, ISD::ADDE, and ISD::MUL_LOHI to MLAL.
7810static SDValue PerformADDCCombine(SDNode *N,
7811 TargetLowering::DAGCombinerInfo &DCI,
7812 const ARMSubtarget *Subtarget) {
7813
7814 return AddCombineTo64bitMLAL(N, DCI, Subtarget);
7815
7816}
7817
Bob Wilson728eb292010-07-29 20:34:14 +00007818/// PerformADDCombineWithOperands - Try DAG combinations for an ADD with
7819/// operands N0 and N1. This is a helper for PerformADDCombine that is
7820/// called with the default operands, and if that fails, with commuted
7821/// operands.
7822static SDValue PerformADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007823 TargetLowering::DAGCombinerInfo &DCI,
7824 const ARMSubtarget *Subtarget){
7825
7826 // Attempt to create vpaddl for this add.
7827 SDValue Result = AddCombineToVPADDL(N, N0, N1, DCI, Subtarget);
7828 if (Result.getNode())
7829 return Result;
Eric Christopher1b8b94192011-06-29 21:10:36 +00007830
Chris Lattner4147f082009-03-12 06:52:53 +00007831 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007832 if (N0.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00007833 SDValue Result = combineSelectAndUse(N, N0, N1, DCI);
7834 if (Result.getNode()) return Result;
7835 }
Chris Lattner4147f082009-03-12 06:52:53 +00007836 return SDValue();
7837}
7838
Bob Wilson728eb292010-07-29 20:34:14 +00007839/// PerformADDCombine - Target-specific dag combine xforms for ISD::ADD.
7840///
7841static SDValue PerformADDCombine(SDNode *N,
Tanya Lattnere9e67052011-06-14 23:48:48 +00007842 TargetLowering::DAGCombinerInfo &DCI,
7843 const ARMSubtarget *Subtarget) {
Bob Wilson728eb292010-07-29 20:34:14 +00007844 SDValue N0 = N->getOperand(0);
7845 SDValue N1 = N->getOperand(1);
7846
7847 // First try with the default operand order.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007848 SDValue Result = PerformADDCombineWithOperands(N, N0, N1, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00007849 if (Result.getNode())
7850 return Result;
7851
7852 // If that didn't work, try again with the operands commuted.
Tanya Lattnere9e67052011-06-14 23:48:48 +00007853 return PerformADDCombineWithOperands(N, N1, N0, DCI, Subtarget);
Bob Wilson728eb292010-07-29 20:34:14 +00007854}
7855
Chris Lattner4147f082009-03-12 06:52:53 +00007856/// PerformSUBCombine - Target-specific dag combine xforms for ISD::SUB.
Bob Wilson728eb292010-07-29 20:34:14 +00007857///
Chris Lattner4147f082009-03-12 06:52:53 +00007858static SDValue PerformSUBCombine(SDNode *N,
7859 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson728eb292010-07-29 20:34:14 +00007860 SDValue N0 = N->getOperand(0);
7861 SDValue N1 = N->getOperand(1);
Bob Wilson7117a912009-03-20 22:42:55 +00007862
Chris Lattner4147f082009-03-12 06:52:53 +00007863 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
Jakob Stoklund Olesendded0612012-08-18 21:25:22 +00007864 if (N1.getNode()->hasOneUse()) {
Chris Lattner4147f082009-03-12 06:52:53 +00007865 SDValue Result = combineSelectAndUse(N, N1, N0, DCI);
7866 if (Result.getNode()) return Result;
7867 }
Bob Wilson7117a912009-03-20 22:42:55 +00007868
Chris Lattner4147f082009-03-12 06:52:53 +00007869 return SDValue();
7870}
7871
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007872/// PerformVMULCombine
7873/// Distribute (A + B) * C to (A * C) + (B * C) to take advantage of the
7874/// special multiplier accumulator forwarding.
7875/// vmul d3, d0, d2
7876/// vmla d3, d1, d2
7877/// is faster than
7878/// vadd d3, d0, d1
7879/// vmul d3, d3, d2
Weiming Zhao2052f482013-09-25 23:12:06 +00007880// However, for (A + B) * (A + B),
7881// vadd d2, d0, d1
7882// vmul d3, d0, d2
7883// vmla d3, d1, d2
7884// is slower than
7885// vadd d2, d0, d1
7886// vmul d3, d2, d2
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007887static SDValue PerformVMULCombine(SDNode *N,
7888 TargetLowering::DAGCombinerInfo &DCI,
7889 const ARMSubtarget *Subtarget) {
7890 if (!Subtarget->hasVMLxForwarding())
7891 return SDValue();
7892
7893 SelectionDAG &DAG = DCI.DAG;
7894 SDValue N0 = N->getOperand(0);
7895 SDValue N1 = N->getOperand(1);
7896 unsigned Opcode = N0.getOpcode();
7897 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7898 Opcode != ISD::FADD && Opcode != ISD::FSUB) {
Chad Rosier27301622011-06-16 01:21:54 +00007899 Opcode = N1.getOpcode();
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007900 if (Opcode != ISD::ADD && Opcode != ISD::SUB &&
7901 Opcode != ISD::FADD && Opcode != ISD::FSUB)
7902 return SDValue();
7903 std::swap(N0, N1);
7904 }
7905
Weiming Zhao2052f482013-09-25 23:12:06 +00007906 if (N0 == N1)
7907 return SDValue();
7908
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007909 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007910 SDLoc DL(N);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007911 SDValue N00 = N0->getOperand(0);
7912 SDValue N01 = N0->getOperand(1);
7913 return DAG.getNode(Opcode, DL, VT,
7914 DAG.getNode(ISD::MUL, DL, VT, N00, N1),
7915 DAG.getNode(ISD::MUL, DL, VT, N01, N1));
7916}
7917
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007918static SDValue PerformMULCombine(SDNode *N,
7919 TargetLowering::DAGCombinerInfo &DCI,
7920 const ARMSubtarget *Subtarget) {
7921 SelectionDAG &DAG = DCI.DAG;
7922
7923 if (Subtarget->isThumb1Only())
7924 return SDValue();
7925
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007926 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
7927 return SDValue();
7928
7929 EVT VT = N->getValueType(0);
Evan Cheng38bf5ad2011-03-31 19:38:48 +00007930 if (VT.is64BitVector() || VT.is128BitVector())
7931 return PerformVMULCombine(N, DCI, Subtarget);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007932 if (VT != MVT::i32)
7933 return SDValue();
7934
7935 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
7936 if (!C)
7937 return SDValue();
7938
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007939 int64_t MulAmt = C->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00007940 unsigned ShiftAmt = countTrailingZeros<uint64_t>(MulAmt);
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007941
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007942 ShiftAmt = ShiftAmt & (32 - 1);
7943 SDValue V = N->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007944 SDLoc DL(N);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007945
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00007946 SDValue Res;
7947 MulAmt >>= ShiftAmt;
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007948
7949 if (MulAmt >= 0) {
7950 if (isPowerOf2_32(MulAmt - 1)) {
7951 // (mul x, 2^N + 1) => (add (shl x, N), x)
7952 Res = DAG.getNode(ISD::ADD, DL, VT,
7953 V,
7954 DAG.getNode(ISD::SHL, DL, VT,
7955 V,
7956 DAG.getConstant(Log2_32(MulAmt - 1),
7957 MVT::i32)));
7958 } else if (isPowerOf2_32(MulAmt + 1)) {
7959 // (mul x, 2^N - 1) => (sub (shl x, N), x)
7960 Res = DAG.getNode(ISD::SUB, DL, VT,
7961 DAG.getNode(ISD::SHL, DL, VT,
7962 V,
7963 DAG.getConstant(Log2_32(MulAmt + 1),
7964 MVT::i32)),
7965 V);
7966 } else
7967 return SDValue();
7968 } else {
7969 uint64_t MulAmtAbs = -MulAmt;
7970 if (isPowerOf2_32(MulAmtAbs + 1)) {
7971 // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
7972 Res = DAG.getNode(ISD::SUB, DL, VT,
7973 V,
7974 DAG.getNode(ISD::SHL, DL, VT,
7975 V,
7976 DAG.getConstant(Log2_32(MulAmtAbs + 1),
7977 MVT::i32)));
7978 } else if (isPowerOf2_32(MulAmtAbs - 1)) {
7979 // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
7980 Res = DAG.getNode(ISD::ADD, DL, VT,
7981 V,
7982 DAG.getNode(ISD::SHL, DL, VT,
7983 V,
7984 DAG.getConstant(Log2_32(MulAmtAbs-1),
7985 MVT::i32)));
7986 Res = DAG.getNode(ISD::SUB, DL, VT,
7987 DAG.getConstant(0, MVT::i32),Res);
7988
7989 } else
7990 return SDValue();
7991 }
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00007992
7993 if (ShiftAmt != 0)
Anton Korobeynikov3edd854d2012-03-19 19:19:50 +00007994 Res = DAG.getNode(ISD::SHL, DL, VT,
7995 Res, DAG.getConstant(ShiftAmt, MVT::i32));
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007996
7997 // Do not add new nodes to DAG combiner worklist.
Anton Korobeynikov4c719c42010-05-16 08:54:20 +00007998 DCI.CombineTo(N, Res, false);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00007999 return SDValue();
8000}
8001
Owen Anderson30c48922010-11-05 19:27:46 +00008002static SDValue PerformANDCombine(SDNode *N,
Evan Chenge87681c2012-02-23 01:19:06 +00008003 TargetLowering::DAGCombinerInfo &DCI,
8004 const ARMSubtarget *Subtarget) {
Owen Anderson77aa2662011-04-05 21:48:57 +00008005
Owen Anderson30c48922010-11-05 19:27:46 +00008006 // Attempt to use immediate-form VBIC
8007 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008008 SDLoc dl(N);
Owen Anderson30c48922010-11-05 19:27:46 +00008009 EVT VT = N->getValueType(0);
8010 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008011
Tanya Lattner266792a2011-04-07 15:24:20 +00008012 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8013 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008014
Owen Anderson30c48922010-11-05 19:27:46 +00008015 APInt SplatBits, SplatUndef;
8016 unsigned SplatBitSize;
8017 bool HasAnyUndefs;
8018 if (BVN &&
8019 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8020 if (SplatBitSize <= 64) {
8021 EVT VbicVT;
8022 SDValue Val = isNEONModifiedImm((~SplatBits).getZExtValue(),
8023 SplatUndef.getZExtValue(), SplatBitSize,
Wesley Peck527da1b2010-11-23 03:31:01 +00008024 DAG, VbicVT, VT.is128BitVector(),
Owen Andersona4076922010-11-05 21:57:54 +00008025 OtherModImm);
Owen Anderson30c48922010-11-05 19:27:46 +00008026 if (Val.getNode()) {
8027 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008028 DAG.getNode(ISD::BITCAST, dl, VbicVT, N->getOperand(0));
Owen Anderson30c48922010-11-05 19:27:46 +00008029 SDValue Vbic = DAG.getNode(ARMISD::VBICIMM, dl, VbicVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008030 return DAG.getNode(ISD::BITCAST, dl, VT, Vbic);
Owen Anderson30c48922010-11-05 19:27:46 +00008031 }
8032 }
8033 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008034
Evan Chenge87681c2012-02-23 01:19:06 +00008035 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008036 // fold (and (select cc, -1, c), x) -> (select cc, x, (and, x, c))
8037 SDValue Result = combineSelectAndUseCommutative(N, true, DCI);
8038 if (Result.getNode())
8039 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008040 }
8041
Owen Anderson30c48922010-11-05 19:27:46 +00008042 return SDValue();
8043}
8044
Jim Grosbach11013ed2010-07-16 23:05:05 +00008045/// PerformORCombine - Target-specific dag combine xforms for ISD::OR
8046static SDValue PerformORCombine(SDNode *N,
8047 TargetLowering::DAGCombinerInfo &DCI,
8048 const ARMSubtarget *Subtarget) {
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008049 // Attempt to use immediate-form VORR
8050 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(N->getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008051 SDLoc dl(N);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008052 EVT VT = N->getValueType(0);
8053 SelectionDAG &DAG = DCI.DAG;
Wesley Peck527da1b2010-11-23 03:31:01 +00008054
Tanya Lattner266792a2011-04-07 15:24:20 +00008055 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8056 return SDValue();
Andrew Trick0ed57782011-04-23 03:55:32 +00008057
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008058 APInt SplatBits, SplatUndef;
8059 unsigned SplatBitSize;
8060 bool HasAnyUndefs;
8061 if (BVN && Subtarget->hasNEON() &&
8062 BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs)) {
8063 if (SplatBitSize <= 64) {
8064 EVT VorrVT;
8065 SDValue Val = isNEONModifiedImm(SplatBits.getZExtValue(),
8066 SplatUndef.getZExtValue(), SplatBitSize,
Owen Andersona4076922010-11-05 21:57:54 +00008067 DAG, VorrVT, VT.is128BitVector(),
8068 OtherModImm);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008069 if (Val.getNode()) {
8070 SDValue Input =
Wesley Peck527da1b2010-11-23 03:31:01 +00008071 DAG.getNode(ISD::BITCAST, dl, VorrVT, N->getOperand(0));
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008072 SDValue Vorr = DAG.getNode(ARMISD::VORRIMM, dl, VorrVT, Input, Val);
Wesley Peck527da1b2010-11-23 03:31:01 +00008073 return DAG.getNode(ISD::BITCAST, dl, VT, Vorr);
Owen Andersonbc9b31c2010-11-03 23:15:26 +00008074 }
8075 }
8076 }
8077
Evan Chenge87681c2012-02-23 01:19:06 +00008078 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008079 // fold (or (select cc, 0, c), x) -> (select cc, x, (or, x, c))
8080 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8081 if (Result.getNode())
8082 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008083 }
8084
Nadav Rotem3a94c542012-08-13 18:52:44 +00008085 // The code below optimizes (or (and X, Y), Z).
8086 // The AND operand needs to have a single user to make these optimizations
8087 // profitable.
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008088 SDValue N0 = N->getOperand(0);
Nadav Rotem3a94c542012-08-13 18:52:44 +00008089 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse())
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008090 return SDValue();
8091 SDValue N1 = N->getOperand(1);
8092
8093 // (or (and B, A), (and C, ~A)) => (VBSL A, B, C) when A is a constant.
8094 if (Subtarget->hasNEON() && N1.getOpcode() == ISD::AND && VT.isVector() &&
8095 DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
8096 APInt SplatUndef;
8097 unsigned SplatBitSize;
8098 bool HasAnyUndefs;
8099
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008100 APInt SplatBits0, SplatBits1;
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008101 BuildVectorSDNode *BVN0 = dyn_cast<BuildVectorSDNode>(N0->getOperand(1));
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008102 BuildVectorSDNode *BVN1 = dyn_cast<BuildVectorSDNode>(N1->getOperand(1));
8103 // Ensure that the second operand of both ands are constants
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008104 if (BVN0 && BVN0->isConstantSplat(SplatBits0, SplatUndef, SplatBitSize,
Saleem Abdulrasool0c2ee5a2013-07-30 04:43:08 +00008105 HasAnyUndefs) && !HasAnyUndefs) {
8106 if (BVN1 && BVN1->isConstantSplat(SplatBits1, SplatUndef, SplatBitSize,
8107 HasAnyUndefs) && !HasAnyUndefs) {
8108 // Ensure that the bit width of the constants are the same and that
8109 // the splat arguments are logical inverses as per the pattern we
8110 // are trying to simplify.
8111 if (SplatBits0.getBitWidth() == SplatBits1.getBitWidth() &&
8112 SplatBits0 == ~SplatBits1) {
8113 // Canonicalize the vector type to make instruction selection
8114 // simpler.
8115 EVT CanonicalVT = VT.is128BitVector() ? MVT::v4i32 : MVT::v2i32;
8116 SDValue Result = DAG.getNode(ARMISD::VBSL, dl, CanonicalVT,
8117 N0->getOperand(1),
8118 N0->getOperand(0),
8119 N1->getOperand(0));
8120 return DAG.getNode(ISD::BITCAST, dl, VT, Result);
8121 }
8122 }
Cameron Zwarich53dd03d2011-03-30 23:01:21 +00008123 }
8124 }
8125
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008126 // Try to use the ARM/Thumb2 BFI (bitfield insert) instruction when
8127 // reasonable.
8128
Jim Grosbach11013ed2010-07-16 23:05:05 +00008129 // BFI is only available on V6T2+
8130 if (Subtarget->isThumb1Only() || !Subtarget->hasV6T2Ops())
8131 return SDValue();
8132
Andrew Trickef9de2a2013-05-25 02:42:55 +00008133 SDLoc DL(N);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008134 // 1) or (and A, mask), val => ARMbfi A, val, mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008135 // iff (val & mask) == val
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008136 //
8137 // 2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008138 // 2a) iff isBitFieldInvertedMask(mask) && isBitFieldInvertedMask(~mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008139 // && mask == ~mask2
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00008140 // 2b) iff isBitFieldInvertedMask(~mask) && isBitFieldInvertedMask(mask2)
Eric Christopherd5530962011-03-26 01:21:03 +00008141 // && ~mask == mask2
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008142 // (i.e., copy a bitfield value into another bitfield of the same width)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008143
Jim Grosbach11013ed2010-07-16 23:05:05 +00008144 if (VT != MVT::i32)
8145 return SDValue();
8146
Evan Cheng2e51bb42010-12-13 20:32:54 +00008147 SDValue N00 = N0.getOperand(0);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008148
Jim Grosbach11013ed2010-07-16 23:05:05 +00008149 // The value and the mask need to be constants so we can verify this is
8150 // actually a bitfield set. If the mask is 0xffff, we can do better
8151 // via a movt instruction, so don't use BFI in that case.
Evan Cheng2e51bb42010-12-13 20:32:54 +00008152 SDValue MaskOp = N0.getOperand(1);
8153 ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(MaskOp);
8154 if (!MaskC)
Jim Grosbach11013ed2010-07-16 23:05:05 +00008155 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008156 unsigned Mask = MaskC->getZExtValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008157 if (Mask == 0xffff)
8158 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008159 SDValue Res;
8160 // Case (1): or (and A, mask), val => ARMbfi A, val, mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008161 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
8162 if (N1C) {
8163 unsigned Val = N1C->getZExtValue();
Evan Cheng34345752010-12-11 04:11:38 +00008164 if ((Val & ~Mask) != Val)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008165 return SDValue();
Jim Grosbach11013ed2010-07-16 23:05:05 +00008166
Evan Cheng34345752010-12-11 04:11:38 +00008167 if (ARM::isBitFieldInvertedMask(Mask)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008168 Val >>= countTrailingZeros(~Mask);
Jim Grosbach11013ed2010-07-16 23:05:05 +00008169
Evan Cheng2e51bb42010-12-13 20:32:54 +00008170 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00,
Evan Cheng34345752010-12-11 04:11:38 +00008171 DAG.getConstant(Val, MVT::i32),
8172 DAG.getConstant(Mask, MVT::i32));
8173
8174 // Do not add new nodes to DAG combiner worklist.
8175 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008176 return SDValue();
Evan Cheng34345752010-12-11 04:11:38 +00008177 }
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008178 } else if (N1.getOpcode() == ISD::AND) {
8179 // case (2) or (and A, mask), (and B, mask2) => ARMbfi A, (lsr B, amt), mask
Evan Cheng2e51bb42010-12-13 20:32:54 +00008180 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8181 if (!N11C)
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008182 return SDValue();
Evan Cheng2e51bb42010-12-13 20:32:54 +00008183 unsigned Mask2 = N11C->getZExtValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008184
Eric Christopherd5530962011-03-26 01:21:03 +00008185 // Mask and ~Mask2 (or reverse) must be equivalent for the BFI pattern
8186 // as is to match.
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008187 if (ARM::isBitFieldInvertedMask(Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008188 (Mask == ~Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008189 // The pack halfword instruction works better for masks that fit it,
8190 // so use that when it's available.
8191 if (Subtarget->hasT2ExtractPack() &&
8192 (Mask == 0xffff || Mask == 0xffff0000))
8193 return SDValue();
8194 // 2a
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008195 unsigned amt = countTrailingZeros(Mask2);
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008196 Res = DAG.getNode(ISD::SRL, DL, VT, N1.getOperand(0),
Eric Christopherd5530962011-03-26 01:21:03 +00008197 DAG.getConstant(amt, MVT::i32));
Evan Cheng2e51bb42010-12-13 20:32:54 +00008198 Res = DAG.getNode(ARMISD::BFI, DL, VT, N00, Res,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008199 DAG.getConstant(Mask, MVT::i32));
8200 // Do not add new nodes to DAG combiner worklist.
8201 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008202 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008203 } else if (ARM::isBitFieldInvertedMask(~Mask) &&
Eric Christopherd5530962011-03-26 01:21:03 +00008204 (~Mask == Mask2)) {
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008205 // The pack halfword instruction works better for masks that fit it,
8206 // so use that when it's available.
8207 if (Subtarget->hasT2ExtractPack() &&
8208 (Mask2 == 0xffff || Mask2 == 0xffff0000))
8209 return SDValue();
8210 // 2b
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008211 unsigned lsb = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008212 Res = DAG.getNode(ISD::SRL, DL, VT, N00,
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008213 DAG.getConstant(lsb, MVT::i32));
8214 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1.getOperand(0), Res,
Eric Christopherd5530962011-03-26 01:21:03 +00008215 DAG.getConstant(Mask2, MVT::i32));
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008216 // Do not add new nodes to DAG combiner worklist.
8217 DCI.CombineTo(N, Res, false);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008218 return SDValue();
Jim Grosbachb97e2bb2010-07-17 03:30:54 +00008219 }
8220 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008221
Evan Cheng2e51bb42010-12-13 20:32:54 +00008222 if (DAG.MaskedValueIsZero(N1, MaskC->getAPIntValue()) &&
8223 N00.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N00.getOperand(1)) &&
8224 ARM::isBitFieldInvertedMask(~Mask)) {
8225 // Case (3): or (and (shl A, #shamt), mask), B => ARMbfi B, A, ~mask
8226 // where lsb(mask) == #shamt and masked bits of B are known zero.
8227 SDValue ShAmt = N00.getOperand(1);
8228 unsigned ShAmtC = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008229 unsigned LSB = countTrailingZeros(Mask);
Evan Cheng2e51bb42010-12-13 20:32:54 +00008230 if (ShAmtC != LSB)
8231 return SDValue();
8232
8233 Res = DAG.getNode(ARMISD::BFI, DL, VT, N1, N00.getOperand(0),
8234 DAG.getConstant(~Mask, MVT::i32));
8235
8236 // Do not add new nodes to DAG combiner worklist.
8237 DCI.CombineTo(N, Res, false);
8238 }
8239
Jim Grosbach11013ed2010-07-16 23:05:05 +00008240 return SDValue();
8241}
8242
Evan Chenge87681c2012-02-23 01:19:06 +00008243static SDValue PerformXORCombine(SDNode *N,
8244 TargetLowering::DAGCombinerInfo &DCI,
8245 const ARMSubtarget *Subtarget) {
8246 EVT VT = N->getValueType(0);
8247 SelectionDAG &DAG = DCI.DAG;
8248
8249 if(!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8250 return SDValue();
8251
8252 if (!Subtarget->isThumb1Only()) {
Jakob Stoklund Olesenaab43db2012-08-18 21:25:16 +00008253 // fold (xor (select cc, 0, c), x) -> (select cc, x, (xor, x, c))
8254 SDValue Result = combineSelectAndUseCommutative(N, false, DCI);
8255 if (Result.getNode())
8256 return Result;
Evan Chenge87681c2012-02-23 01:19:06 +00008257 }
8258
8259 return SDValue();
8260}
8261
Evan Cheng6d02d902011-06-15 01:12:31 +00008262/// PerformBFICombine - (bfi A, (and B, Mask1), Mask2) -> (bfi A, B, Mask2) iff
8263/// the bits being cleared by the AND are not demanded by the BFI.
Evan Chengc1778132010-12-14 03:22:07 +00008264static SDValue PerformBFICombine(SDNode *N,
8265 TargetLowering::DAGCombinerInfo &DCI) {
8266 SDValue N1 = N->getOperand(1);
8267 if (N1.getOpcode() == ISD::AND) {
8268 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
8269 if (!N11C)
8270 return SDValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008271 unsigned InvMask = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008272 unsigned LSB = countTrailingZeros(~InvMask);
8273 unsigned Width = (32 - countLeadingZeros(~InvMask)) - LSB;
Evan Cheng6d02d902011-06-15 01:12:31 +00008274 unsigned Mask = (1 << Width)-1;
Evan Chengc1778132010-12-14 03:22:07 +00008275 unsigned Mask2 = N11C->getZExtValue();
Evan Cheng6d02d902011-06-15 01:12:31 +00008276 if ((Mask & (~Mask2)) == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008277 return DCI.DAG.getNode(ARMISD::BFI, SDLoc(N), N->getValueType(0),
Evan Chengc1778132010-12-14 03:22:07 +00008278 N->getOperand(0), N1.getOperand(0),
8279 N->getOperand(2));
8280 }
8281 return SDValue();
8282}
8283
Bob Wilson22806742010-09-22 22:09:21 +00008284/// PerformVMOVRRDCombine - Target-specific dag combine xforms for
8285/// ARMISD::VMOVRRD.
8286static SDValue PerformVMOVRRDCombine(SDNode *N,
8287 TargetLowering::DAGCombinerInfo &DCI) {
8288 // vmovrrd(vmovdrr x, y) -> x,y
8289 SDValue InDouble = N->getOperand(0);
8290 if (InDouble.getOpcode() == ARMISD::VMOVDRR)
8291 return DCI.CombineTo(N, InDouble.getOperand(0), InDouble.getOperand(1));
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008292
8293 // vmovrrd(load f64) -> (load i32), (load i32)
8294 SDNode *InNode = InDouble.getNode();
8295 if (ISD::isNormalLoad(InNode) && InNode->hasOneUse() &&
8296 InNode->getValueType(0) == MVT::f64 &&
8297 InNode->getOperand(1).getOpcode() == ISD::FrameIndex &&
8298 !cast<LoadSDNode>(InNode)->isVolatile()) {
8299 // TODO: Should this be done for non-FrameIndex operands?
8300 LoadSDNode *LD = cast<LoadSDNode>(InNode);
8301
8302 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008303 SDLoc DL(LD);
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008304 SDValue BasePtr = LD->getBasePtr();
8305 SDValue NewLD1 = DAG.getLoad(MVT::i32, DL, LD->getChain(), BasePtr,
8306 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008307 LD->isNonTemporal(), LD->isInvariant(),
8308 LD->getAlignment());
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008309
8310 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8311 DAG.getConstant(4, MVT::i32));
8312 SDValue NewLD2 = DAG.getLoad(MVT::i32, DL, NewLD1.getValue(1), OffsetPtr,
8313 LD->getPointerInfo(), LD->isVolatile(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008314 LD->isNonTemporal(), LD->isInvariant(),
Cameron Zwarich6fe5c292011-04-02 02:40:43 +00008315 std::min(4U, LD->getAlignment() / 2));
8316
8317 DAG.ReplaceAllUsesOfValueWith(SDValue(LD, 1), NewLD2.getValue(1));
8318 SDValue Result = DCI.CombineTo(N, NewLD1, NewLD2);
8319 DCI.RemoveFromWorklist(LD);
8320 DAG.DeleteNode(LD);
8321 return Result;
8322 }
8323
Bob Wilson22806742010-09-22 22:09:21 +00008324 return SDValue();
8325}
8326
8327/// PerformVMOVDRRCombine - Target-specific dag combine xforms for
8328/// ARMISD::VMOVDRR. This is also used for BUILD_VECTORs with 2 operands.
8329static SDValue PerformVMOVDRRCombine(SDNode *N, SelectionDAG &DAG) {
8330 // N=vmovrrd(X); vmovdrr(N:0, N:1) -> bit_convert(X)
8331 SDValue Op0 = N->getOperand(0);
8332 SDValue Op1 = N->getOperand(1);
Wesley Peck527da1b2010-11-23 03:31:01 +00008333 if (Op0.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008334 Op0 = Op0.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008335 if (Op1.getOpcode() == ISD::BITCAST)
Bob Wilson22806742010-09-22 22:09:21 +00008336 Op1 = Op1.getOperand(0);
8337 if (Op0.getOpcode() == ARMISD::VMOVRRD &&
8338 Op0.getNode() == Op1.getNode() &&
8339 Op0.getResNo() == 0 && Op1.getResNo() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008340 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bob Wilson22806742010-09-22 22:09:21 +00008341 N->getValueType(0), Op0.getOperand(0));
8342 return SDValue();
8343}
8344
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008345/// PerformSTORECombine - Target-specific dag combine xforms for
8346/// ISD::STORE.
8347static SDValue PerformSTORECombine(SDNode *N,
8348 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008349 StoreSDNode *St = cast<StoreSDNode>(N);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008350 if (St->isVolatile())
8351 return SDValue();
8352
Andrew Trickbc325162012-07-18 18:34:24 +00008353 // Optimize trunc store (of multiple scalars) to shuffle and store. First,
Chad Rosiere0e38f62012-04-09 20:32:02 +00008354 // pack all of the elements in one place. Next, store to memory in fewer
8355 // chunks.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008356 SDValue StVal = St->getValue();
Chad Rosiere0e38f62012-04-09 20:32:02 +00008357 EVT VT = StVal.getValueType();
8358 if (St->isTruncatingStore() && VT.isVector()) {
8359 SelectionDAG &DAG = DCI.DAG;
8360 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8361 EVT StVT = St->getMemoryVT();
8362 unsigned NumElems = VT.getVectorNumElements();
8363 assert(StVT != VT && "Cannot truncate to the same type");
8364 unsigned FromEltSz = VT.getVectorElementType().getSizeInBits();
8365 unsigned ToEltSz = StVT.getVectorElementType().getSizeInBits();
8366
8367 // From, To sizes and ElemCount must be pow of two
8368 if (!isPowerOf2_32(NumElems * FromEltSz * ToEltSz)) return SDValue();
8369
8370 // We are going to use the original vector elt for storing.
8371 // Accumulated smaller vector elements must be a multiple of the store size.
8372 if (0 != (NumElems * FromEltSz) % ToEltSz) return SDValue();
8373
8374 unsigned SizeRatio = FromEltSz / ToEltSz;
8375 assert(SizeRatio * NumElems * ToEltSz == VT.getSizeInBits());
8376
8377 // Create a type on which we perform the shuffle.
8378 EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), StVT.getScalarType(),
8379 NumElems*SizeRatio);
8380 assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
8381
Andrew Trickef9de2a2013-05-25 02:42:55 +00008382 SDLoc DL(St);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008383 SDValue WideVec = DAG.getNode(ISD::BITCAST, DL, WideVecVT, StVal);
8384 SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
8385 for (unsigned i = 0; i < NumElems; ++i) ShuffleVec[i] = i * SizeRatio;
8386
8387 // Can't shuffle using an illegal type.
8388 if (!TLI.isTypeLegal(WideVecVT)) return SDValue();
8389
8390 SDValue Shuff = DAG.getVectorShuffle(WideVecVT, DL, WideVec,
8391 DAG.getUNDEF(WideVec.getValueType()),
8392 ShuffleVec.data());
8393 // At this point all of the data is stored at the bottom of the
8394 // register. We now need to save it to mem.
8395
8396 // Find the largest store unit
8397 MVT StoreType = MVT::i8;
8398 for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE;
8399 tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) {
8400 MVT Tp = (MVT::SimpleValueType)tp;
8401 if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToEltSz)
8402 StoreType = Tp;
8403 }
8404 // Didn't find a legal store type.
8405 if (!TLI.isTypeLegal(StoreType))
8406 return SDValue();
8407
8408 // Bitcast the original vector into a vector of store-size units
8409 EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
8410 StoreType, VT.getSizeInBits()/EVT(StoreType).getSizeInBits());
8411 assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
8412 SDValue ShuffWide = DAG.getNode(ISD::BITCAST, DL, StoreVecVT, Shuff);
8413 SmallVector<SDValue, 8> Chains;
8414 SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8,
8415 TLI.getPointerTy());
8416 SDValue BasePtr = St->getBasePtr();
8417
8418 // Perform one or more big stores into memory.
8419 unsigned E = (ToEltSz*NumElems)/StoreType.getSizeInBits();
8420 for (unsigned I = 0; I < E; I++) {
8421 SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL,
8422 StoreType, ShuffWide,
8423 DAG.getIntPtrConstant(I));
8424 SDValue Ch = DAG.getStore(St->getChain(), DL, SubVec, BasePtr,
8425 St->getPointerInfo(), St->isVolatile(),
8426 St->isNonTemporal(), St->getAlignment());
8427 BasePtr = DAG.getNode(ISD::ADD, DL, BasePtr.getValueType(), BasePtr,
8428 Increment);
8429 Chains.push_back(Ch);
8430 }
Craig Topper48d114b2014-04-26 18:35:24 +00008431 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Chad Rosiere0e38f62012-04-09 20:32:02 +00008432 }
8433
8434 if (!ISD::isNormalStore(St))
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008435 return SDValue();
8436
Chad Rosier99cbde92012-04-09 19:38:15 +00008437 // Split a store of a VMOVDRR into two integer stores to avoid mixing NEON and
8438 // ARM stores of arguments in the same cache line.
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008439 if (StVal.getNode()->getOpcode() == ARMISD::VMOVDRR &&
Chad Rosier99cbde92012-04-09 19:38:15 +00008440 StVal.getNode()->hasOneUse()) {
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008441 SelectionDAG &DAG = DCI.DAG;
Christian Pirkerb5728192014-05-08 14:06:24 +00008442 bool isBigEndian = DAG.getTargetLoweringInfo().isBigEndian();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008443 SDLoc DL(St);
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008444 SDValue BasePtr = St->getBasePtr();
8445 SDValue NewST1 = DAG.getStore(St->getChain(), DL,
Christian Pirkerb5728192014-05-08 14:06:24 +00008446 StVal.getNode()->getOperand(isBigEndian ? 1 : 0 ),
8447 BasePtr, St->getPointerInfo(), St->isVolatile(),
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008448 St->isNonTemporal(), St->getAlignment());
8449
8450 SDValue OffsetPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, BasePtr,
8451 DAG.getConstant(4, MVT::i32));
Christian Pirkerb5728192014-05-08 14:06:24 +00008452 return DAG.getStore(NewST1.getValue(0), DL,
8453 StVal.getNode()->getOperand(isBigEndian ? 0 : 1),
Cameron Zwarichfbcd69b2011-04-12 02:24:17 +00008454 OffsetPtr, St->getPointerInfo(), St->isVolatile(),
8455 St->isNonTemporal(),
8456 std::min(4U, St->getAlignment() / 2));
8457 }
8458
8459 if (StVal.getValueType() != MVT::i64 ||
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008460 StVal.getNode()->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8461 return SDValue();
8462
Chad Rosier99cbde92012-04-09 19:38:15 +00008463 // Bitcast an i64 store extracted from a vector to f64.
8464 // Otherwise, the i64 value will be legalized to a pair of i32 values.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008465 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008466 SDLoc dl(StVal);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008467 SDValue IntVec = StVal.getOperand(0);
8468 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8469 IntVec.getValueType().getVectorNumElements());
8470 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, IntVec);
8471 SDValue ExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
8472 Vec, StVal.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008473 dl = SDLoc(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008474 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ExtElt);
8475 // Make the DAGCombiner fold the bitcasts.
8476 DCI.AddToWorklist(Vec.getNode());
8477 DCI.AddToWorklist(ExtElt.getNode());
8478 DCI.AddToWorklist(V.getNode());
8479 return DAG.getStore(St->getChain(), dl, V, St->getBasePtr(),
8480 St->getPointerInfo(), St->isVolatile(),
8481 St->isNonTemporal(), St->getAlignment(),
8482 St->getTBAAInfo());
8483}
8484
8485/// hasNormalLoadOperand - Check if any of the operands of a BUILD_VECTOR node
8486/// are normal, non-volatile loads. If so, it is profitable to bitcast an
8487/// i64 vector to have f64 elements, since the value can then be loaded
8488/// directly into a VFP register.
8489static bool hasNormalLoadOperand(SDNode *N) {
8490 unsigned NumElts = N->getValueType(0).getVectorNumElements();
8491 for (unsigned i = 0; i < NumElts; ++i) {
8492 SDNode *Elt = N->getOperand(i).getNode();
8493 if (ISD::isNormalLoad(Elt) && !cast<LoadSDNode>(Elt)->isVolatile())
8494 return true;
8495 }
8496 return false;
8497}
8498
Bob Wilsoncb6db982010-09-17 22:59:05 +00008499/// PerformBUILD_VECTORCombine - Target-specific dag combine xforms for
8500/// ISD::BUILD_VECTOR.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008501static SDValue PerformBUILD_VECTORCombine(SDNode *N,
8502 TargetLowering::DAGCombinerInfo &DCI){
Bob Wilsoncb6db982010-09-17 22:59:05 +00008503 // build_vector(N=ARMISD::VMOVRRD(X), N:1) -> bit_convert(X):
8504 // VMOVRRD is introduced when legalizing i64 types. It forces the i64 value
8505 // into a pair of GPRs, which is fine when the value is used as a scalar,
8506 // but if the i64 value is converted to a vector, we need to undo the VMOVRRD.
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008507 SelectionDAG &DAG = DCI.DAG;
8508 if (N->getNumOperands() == 2) {
8509 SDValue RV = PerformVMOVDRRCombine(N, DAG);
8510 if (RV.getNode())
8511 return RV;
8512 }
Bob Wilsoncb6db982010-09-17 22:59:05 +00008513
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008514 // Load i64 elements as f64 values so that type legalization does not split
8515 // them up into i32 values.
8516 EVT VT = N->getValueType(0);
8517 if (VT.getVectorElementType() != MVT::i64 || !hasNormalLoadOperand(N))
8518 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00008519 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008520 SmallVector<SDValue, 8> Ops;
8521 unsigned NumElts = VT.getVectorNumElements();
8522 for (unsigned i = 0; i < NumElts; ++i) {
8523 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(i));
8524 Ops.push_back(V);
8525 // Make the DAGCombiner fold the bitcast.
8526 DCI.AddToWorklist(V.getNode());
8527 }
8528 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, NumElts);
Craig Topper48d114b2014-04-26 18:35:24 +00008529 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, FloatVT, Ops);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008530 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8531}
8532
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008533/// \brief Target-specific dag combine xforms for ARMISD::BUILD_VECTOR.
8534static SDValue
8535PerformARMBUILD_VECTORCombine(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8536 // ARMISD::BUILD_VECTOR is introduced when legalizing ISD::BUILD_VECTOR.
8537 // At that time, we may have inserted bitcasts from integer to float.
8538 // If these bitcasts have survived DAGCombine, change the lowering of this
8539 // BUILD_VECTOR in something more vector friendly, i.e., that does not
8540 // force to use floating point types.
8541
8542 // Make sure we can change the type of the vector.
8543 // This is possible iff:
8544 // 1. The vector is only used in a bitcast to a integer type. I.e.,
8545 // 1.1. Vector is used only once.
8546 // 1.2. Use is a bit convert to an integer type.
8547 // 2. The size of its operands are 32-bits (64-bits are not legal).
8548 EVT VT = N->getValueType(0);
8549 EVT EltVT = VT.getVectorElementType();
8550
8551 // Check 1.1. and 2.
8552 if (EltVT.getSizeInBits() != 32 || !N->hasOneUse())
8553 return SDValue();
8554
8555 // By construction, the input type must be float.
8556 assert(EltVT == MVT::f32 && "Unexpected type!");
8557
8558 // Check 1.2.
8559 SDNode *Use = *N->use_begin();
8560 if (Use->getOpcode() != ISD::BITCAST ||
8561 Use->getValueType(0).isFloatingPoint())
8562 return SDValue();
8563
8564 // Check profitability.
8565 // Model is, if more than half of the relevant operands are bitcast from
8566 // i32, turn the build_vector into a sequence of insert_vector_elt.
8567 // Relevant operands are everything that is not statically
8568 // (i.e., at compile time) bitcasted.
8569 unsigned NumOfBitCastedElts = 0;
8570 unsigned NumElts = VT.getVectorNumElements();
8571 unsigned NumOfRelevantElts = NumElts;
8572 for (unsigned Idx = 0; Idx < NumElts; ++Idx) {
8573 SDValue Elt = N->getOperand(Idx);
8574 if (Elt->getOpcode() == ISD::BITCAST) {
8575 // Assume only bit cast to i32 will go away.
8576 if (Elt->getOperand(0).getValueType() == MVT::i32)
8577 ++NumOfBitCastedElts;
8578 } else if (Elt.getOpcode() == ISD::UNDEF || isa<ConstantSDNode>(Elt))
8579 // Constants are statically casted, thus do not count them as
8580 // relevant operands.
8581 --NumOfRelevantElts;
8582 }
8583
8584 // Check if more than half of the elements require a non-free bitcast.
8585 if (NumOfBitCastedElts <= NumOfRelevantElts / 2)
8586 return SDValue();
8587
8588 SelectionDAG &DAG = DCI.DAG;
8589 // Create the new vector type.
8590 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts);
8591 // Check if the type is legal.
8592 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8593 if (!TLI.isTypeLegal(VecVT))
8594 return SDValue();
8595
8596 // Combine:
8597 // ARMISD::BUILD_VECTOR E1, E2, ..., EN.
8598 // => BITCAST INSERT_VECTOR_ELT
8599 // (INSERT_VECTOR_ELT (...), (BITCAST EN-1), N-1),
8600 // (BITCAST EN), N.
8601 SDValue Vec = DAG.getUNDEF(VecVT);
8602 SDLoc dl(N);
8603 for (unsigned Idx = 0 ; Idx < NumElts; ++Idx) {
8604 SDValue V = N->getOperand(Idx);
8605 if (V.getOpcode() == ISD::UNDEF)
8606 continue;
8607 if (V.getOpcode() == ISD::BITCAST &&
8608 V->getOperand(0).getValueType() == MVT::i32)
8609 // Fold obvious case.
8610 V = V.getOperand(0);
8611 else {
Jim Grosbach1a597112014-04-03 23:43:18 +00008612 V = DAG.getNode(ISD::BITCAST, SDLoc(V), MVT::i32, V);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00008613 // Make the DAGCombiner fold the bitcasts.
8614 DCI.AddToWorklist(V.getNode());
8615 }
8616 SDValue LaneIdx = DAG.getConstant(Idx, MVT::i32);
8617 Vec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecVT, Vec, V, LaneIdx);
8618 }
8619 Vec = DAG.getNode(ISD::BITCAST, dl, VT, Vec);
8620 // Make the DAGCombiner fold the bitcasts.
8621 DCI.AddToWorklist(Vec.getNode());
8622 return Vec;
8623}
8624
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008625/// PerformInsertEltCombine - Target-specific dag combine xforms for
8626/// ISD::INSERT_VECTOR_ELT.
8627static SDValue PerformInsertEltCombine(SDNode *N,
8628 TargetLowering::DAGCombinerInfo &DCI) {
8629 // Bitcast an i64 load inserted into a vector to f64.
8630 // Otherwise, the i64 value will be legalized to a pair of i32 values.
8631 EVT VT = N->getValueType(0);
8632 SDNode *Elt = N->getOperand(1).getNode();
8633 if (VT.getVectorElementType() != MVT::i64 ||
8634 !ISD::isNormalLoad(Elt) || cast<LoadSDNode>(Elt)->isVolatile())
8635 return SDValue();
8636
8637 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008638 SDLoc dl(N);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00008639 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64,
8640 VT.getVectorNumElements());
8641 SDValue Vec = DAG.getNode(ISD::BITCAST, dl, FloatVT, N->getOperand(0));
8642 SDValue V = DAG.getNode(ISD::BITCAST, dl, MVT::f64, N->getOperand(1));
8643 // Make the DAGCombiner fold the bitcasts.
8644 DCI.AddToWorklist(Vec.getNode());
8645 DCI.AddToWorklist(V.getNode());
8646 SDValue InsElt = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, FloatVT,
8647 Vec, V, N->getOperand(2));
8648 return DAG.getNode(ISD::BITCAST, dl, VT, InsElt);
Bob Wilsoncb6db982010-09-17 22:59:05 +00008649}
8650
Bob Wilsonc7334a12010-10-27 20:38:28 +00008651/// PerformVECTOR_SHUFFLECombine - Target-specific dag combine xforms for
8652/// ISD::VECTOR_SHUFFLE.
8653static SDValue PerformVECTOR_SHUFFLECombine(SDNode *N, SelectionDAG &DAG) {
8654 // The LLVM shufflevector instruction does not require the shuffle mask
8655 // length to match the operand vector length, but ISD::VECTOR_SHUFFLE does
8656 // have that requirement. When translating to ISD::VECTOR_SHUFFLE, if the
8657 // operands do not match the mask length, they are extended by concatenating
8658 // them with undef vectors. That is probably the right thing for other
8659 // targets, but for NEON it is better to concatenate two double-register
8660 // size vector operands into a single quad-register size vector. Do that
8661 // transformation here:
8662 // shuffle(concat(v1, undef), concat(v2, undef)) ->
8663 // shuffle(concat(v1, v2), undef)
8664 SDValue Op0 = N->getOperand(0);
8665 SDValue Op1 = N->getOperand(1);
8666 if (Op0.getOpcode() != ISD::CONCAT_VECTORS ||
8667 Op1.getOpcode() != ISD::CONCAT_VECTORS ||
8668 Op0.getNumOperands() != 2 ||
8669 Op1.getNumOperands() != 2)
8670 return SDValue();
8671 SDValue Concat0Op1 = Op0.getOperand(1);
8672 SDValue Concat1Op1 = Op1.getOperand(1);
8673 if (Concat0Op1.getOpcode() != ISD::UNDEF ||
8674 Concat1Op1.getOpcode() != ISD::UNDEF)
8675 return SDValue();
8676 // Skip the transformation if any of the types are illegal.
8677 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8678 EVT VT = N->getValueType(0);
8679 if (!TLI.isTypeLegal(VT) ||
8680 !TLI.isTypeLegal(Concat0Op1.getValueType()) ||
8681 !TLI.isTypeLegal(Concat1Op1.getValueType()))
8682 return SDValue();
8683
Andrew Trickef9de2a2013-05-25 02:42:55 +00008684 SDValue NewConcat = DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008685 Op0.getOperand(0), Op1.getOperand(0));
8686 // Translate the shuffle mask.
8687 SmallVector<int, 16> NewMask;
8688 unsigned NumElts = VT.getVectorNumElements();
8689 unsigned HalfElts = NumElts/2;
8690 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8691 for (unsigned n = 0; n < NumElts; ++n) {
8692 int MaskElt = SVN->getMaskElt(n);
8693 int NewElt = -1;
Bob Wilson6c550072010-10-27 23:49:00 +00008694 if (MaskElt < (int)HalfElts)
Bob Wilsonc7334a12010-10-27 20:38:28 +00008695 NewElt = MaskElt;
Bob Wilson6c550072010-10-27 23:49:00 +00008696 else if (MaskElt >= (int)NumElts && MaskElt < (int)(NumElts + HalfElts))
Bob Wilsonc7334a12010-10-27 20:38:28 +00008697 NewElt = HalfElts + MaskElt - NumElts;
8698 NewMask.push_back(NewElt);
8699 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00008700 return DAG.getVectorShuffle(VT, SDLoc(N), NewConcat,
Bob Wilsonc7334a12010-10-27 20:38:28 +00008701 DAG.getUNDEF(VT), NewMask.data());
8702}
8703
Bob Wilson06fce872011-02-07 17:43:21 +00008704/// CombineBaseUpdate - Target-specific DAG combine function for VLDDUP and
8705/// NEON load/store intrinsics to merge base address updates.
8706static SDValue CombineBaseUpdate(SDNode *N,
8707 TargetLowering::DAGCombinerInfo &DCI) {
8708 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8709 return SDValue();
8710
8711 SelectionDAG &DAG = DCI.DAG;
8712 bool isIntrinsic = (N->getOpcode() == ISD::INTRINSIC_VOID ||
8713 N->getOpcode() == ISD::INTRINSIC_W_CHAIN);
8714 unsigned AddrOpIdx = (isIntrinsic ? 2 : 1);
8715 SDValue Addr = N->getOperand(AddrOpIdx);
8716
8717 // Search for a use of the address operand that is an increment.
8718 for (SDNode::use_iterator UI = Addr.getNode()->use_begin(),
8719 UE = Addr.getNode()->use_end(); UI != UE; ++UI) {
8720 SDNode *User = *UI;
8721 if (User->getOpcode() != ISD::ADD ||
8722 UI.getUse().getResNo() != Addr.getResNo())
8723 continue;
8724
8725 // Check that the add is independent of the load/store. Otherwise, folding
8726 // it would create a cycle.
8727 if (User->isPredecessorOf(N) || N->isPredecessorOf(User))
8728 continue;
8729
8730 // Find the new opcode for the updating load/store.
8731 bool isLoad = true;
8732 bool isLaneOp = false;
8733 unsigned NewOpc = 0;
8734 unsigned NumVecs = 0;
8735 if (isIntrinsic) {
8736 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
8737 switch (IntNo) {
Craig Toppere55c5562012-02-07 02:50:20 +00008738 default: llvm_unreachable("unexpected intrinsic for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008739 case Intrinsic::arm_neon_vld1: NewOpc = ARMISD::VLD1_UPD;
8740 NumVecs = 1; break;
8741 case Intrinsic::arm_neon_vld2: NewOpc = ARMISD::VLD2_UPD;
8742 NumVecs = 2; break;
8743 case Intrinsic::arm_neon_vld3: NewOpc = ARMISD::VLD3_UPD;
8744 NumVecs = 3; break;
8745 case Intrinsic::arm_neon_vld4: NewOpc = ARMISD::VLD4_UPD;
8746 NumVecs = 4; break;
8747 case Intrinsic::arm_neon_vld2lane: NewOpc = ARMISD::VLD2LN_UPD;
8748 NumVecs = 2; isLaneOp = true; break;
8749 case Intrinsic::arm_neon_vld3lane: NewOpc = ARMISD::VLD3LN_UPD;
8750 NumVecs = 3; isLaneOp = true; break;
8751 case Intrinsic::arm_neon_vld4lane: NewOpc = ARMISD::VLD4LN_UPD;
8752 NumVecs = 4; isLaneOp = true; break;
8753 case Intrinsic::arm_neon_vst1: NewOpc = ARMISD::VST1_UPD;
8754 NumVecs = 1; isLoad = false; break;
8755 case Intrinsic::arm_neon_vst2: NewOpc = ARMISD::VST2_UPD;
8756 NumVecs = 2; isLoad = false; break;
8757 case Intrinsic::arm_neon_vst3: NewOpc = ARMISD::VST3_UPD;
8758 NumVecs = 3; isLoad = false; break;
8759 case Intrinsic::arm_neon_vst4: NewOpc = ARMISD::VST4_UPD;
8760 NumVecs = 4; isLoad = false; break;
8761 case Intrinsic::arm_neon_vst2lane: NewOpc = ARMISD::VST2LN_UPD;
8762 NumVecs = 2; isLoad = false; isLaneOp = true; break;
8763 case Intrinsic::arm_neon_vst3lane: NewOpc = ARMISD::VST3LN_UPD;
8764 NumVecs = 3; isLoad = false; isLaneOp = true; break;
8765 case Intrinsic::arm_neon_vst4lane: NewOpc = ARMISD::VST4LN_UPD;
8766 NumVecs = 4; isLoad = false; isLaneOp = true; break;
8767 }
8768 } else {
8769 isLaneOp = true;
8770 switch (N->getOpcode()) {
Craig Toppere55c5562012-02-07 02:50:20 +00008771 default: llvm_unreachable("unexpected opcode for Neon base update");
Bob Wilson06fce872011-02-07 17:43:21 +00008772 case ARMISD::VLD2DUP: NewOpc = ARMISD::VLD2DUP_UPD; NumVecs = 2; break;
8773 case ARMISD::VLD3DUP: NewOpc = ARMISD::VLD3DUP_UPD; NumVecs = 3; break;
8774 case ARMISD::VLD4DUP: NewOpc = ARMISD::VLD4DUP_UPD; NumVecs = 4; break;
8775 }
8776 }
8777
8778 // Find the size of memory referenced by the load/store.
8779 EVT VecTy;
8780 if (isLoad)
8781 VecTy = N->getValueType(0);
Owen Anderson77aa2662011-04-05 21:48:57 +00008782 else
Bob Wilson06fce872011-02-07 17:43:21 +00008783 VecTy = N->getOperand(AddrOpIdx+1).getValueType();
8784 unsigned NumBytes = NumVecs * VecTy.getSizeInBits() / 8;
8785 if (isLaneOp)
8786 NumBytes /= VecTy.getVectorNumElements();
8787
8788 // If the increment is a constant, it must match the memory ref size.
8789 SDValue Inc = User->getOperand(User->getOperand(0) == Addr ? 1 : 0);
8790 if (ConstantSDNode *CInc = dyn_cast<ConstantSDNode>(Inc.getNode())) {
8791 uint64_t IncVal = CInc->getZExtValue();
8792 if (IncVal != NumBytes)
8793 continue;
8794 } else if (NumBytes >= 3 * 16) {
8795 // VLD3/4 and VST3/4 for 128-bit vectors are implemented with two
8796 // separate instructions that make it harder to use a non-constant update.
8797 continue;
8798 }
8799
8800 // Create the new updating load/store node.
8801 EVT Tys[6];
8802 unsigned NumResultVecs = (isLoad ? NumVecs : 0);
8803 unsigned n;
8804 for (n = 0; n < NumResultVecs; ++n)
8805 Tys[n] = VecTy;
8806 Tys[n++] = MVT::i32;
8807 Tys[n] = MVT::Other;
Craig Topperabb4ac72014-04-16 06:10:51 +00008808 SDVTList SDTys = DAG.getVTList(ArrayRef<EVT>(Tys, NumResultVecs+2));
Bob Wilson06fce872011-02-07 17:43:21 +00008809 SmallVector<SDValue, 8> Ops;
8810 Ops.push_back(N->getOperand(0)); // incoming chain
8811 Ops.push_back(N->getOperand(AddrOpIdx));
8812 Ops.push_back(Inc);
8813 for (unsigned i = AddrOpIdx + 1; i < N->getNumOperands(); ++i) {
8814 Ops.push_back(N->getOperand(i));
8815 }
8816 MemIntrinsicSDNode *MemInt = cast<MemIntrinsicSDNode>(N);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008817 SDValue UpdN = DAG.getMemIntrinsicNode(NewOpc, SDLoc(N), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00008818 Ops, MemInt->getMemoryVT(),
Bob Wilson06fce872011-02-07 17:43:21 +00008819 MemInt->getMemOperand());
8820
8821 // Update the uses.
8822 std::vector<SDValue> NewResults;
8823 for (unsigned i = 0; i < NumResultVecs; ++i) {
8824 NewResults.push_back(SDValue(UpdN.getNode(), i));
8825 }
8826 NewResults.push_back(SDValue(UpdN.getNode(), NumResultVecs+1)); // chain
8827 DCI.CombineTo(N, NewResults);
8828 DCI.CombineTo(User, SDValue(UpdN.getNode(), NumResultVecs));
8829
8830 break;
Owen Anderson77aa2662011-04-05 21:48:57 +00008831 }
Bob Wilson06fce872011-02-07 17:43:21 +00008832 return SDValue();
8833}
8834
Bob Wilson2d790df2010-11-28 06:51:26 +00008835/// CombineVLDDUP - For a VDUPLANE node N, check if its source operand is a
8836/// vldN-lane (N > 1) intrinsic, and if all the other uses of that intrinsic
8837/// are also VDUPLANEs. If so, combine them to a vldN-dup operation and
8838/// return true.
8839static bool CombineVLDDUP(SDNode *N, TargetLowering::DAGCombinerInfo &DCI) {
8840 SelectionDAG &DAG = DCI.DAG;
8841 EVT VT = N->getValueType(0);
8842 // vldN-dup instructions only support 64-bit vectors for N > 1.
8843 if (!VT.is64BitVector())
8844 return false;
8845
8846 // Check if the VDUPLANE operand is a vldN-dup intrinsic.
8847 SDNode *VLD = N->getOperand(0).getNode();
8848 if (VLD->getOpcode() != ISD::INTRINSIC_W_CHAIN)
8849 return false;
8850 unsigned NumVecs = 0;
8851 unsigned NewOpc = 0;
8852 unsigned IntNo = cast<ConstantSDNode>(VLD->getOperand(1))->getZExtValue();
8853 if (IntNo == Intrinsic::arm_neon_vld2lane) {
8854 NumVecs = 2;
8855 NewOpc = ARMISD::VLD2DUP;
8856 } else if (IntNo == Intrinsic::arm_neon_vld3lane) {
8857 NumVecs = 3;
8858 NewOpc = ARMISD::VLD3DUP;
8859 } else if (IntNo == Intrinsic::arm_neon_vld4lane) {
8860 NumVecs = 4;
8861 NewOpc = ARMISD::VLD4DUP;
8862 } else {
8863 return false;
8864 }
8865
8866 // First check that all the vldN-lane uses are VDUPLANEs and that the lane
8867 // numbers match the load.
8868 unsigned VLDLaneNo =
8869 cast<ConstantSDNode>(VLD->getOperand(NumVecs+3))->getZExtValue();
8870 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8871 UI != UE; ++UI) {
8872 // Ignore uses of the chain result.
8873 if (UI.getUse().getResNo() == NumVecs)
8874 continue;
8875 SDNode *User = *UI;
8876 if (User->getOpcode() != ARMISD::VDUPLANE ||
8877 VLDLaneNo != cast<ConstantSDNode>(User->getOperand(1))->getZExtValue())
8878 return false;
8879 }
8880
8881 // Create the vldN-dup node.
8882 EVT Tys[5];
8883 unsigned n;
8884 for (n = 0; n < NumVecs; ++n)
8885 Tys[n] = VT;
8886 Tys[n] = MVT::Other;
Craig Topperabb4ac72014-04-16 06:10:51 +00008887 SDVTList SDTys = DAG.getVTList(ArrayRef<EVT>(Tys, NumVecs+1));
Bob Wilson2d790df2010-11-28 06:51:26 +00008888 SDValue Ops[] = { VLD->getOperand(0), VLD->getOperand(2) };
8889 MemIntrinsicSDNode *VLDMemInt = cast<MemIntrinsicSDNode>(VLD);
Andrew Trickef9de2a2013-05-25 02:42:55 +00008890 SDValue VLDDup = DAG.getMemIntrinsicNode(NewOpc, SDLoc(VLD), SDTys,
Craig Topper206fcd42014-04-26 19:29:41 +00008891 Ops, VLDMemInt->getMemoryVT(),
Bob Wilson2d790df2010-11-28 06:51:26 +00008892 VLDMemInt->getMemOperand());
8893
8894 // Update the uses.
8895 for (SDNode::use_iterator UI = VLD->use_begin(), UE = VLD->use_end();
8896 UI != UE; ++UI) {
8897 unsigned ResNo = UI.getUse().getResNo();
8898 // Ignore uses of the chain result.
8899 if (ResNo == NumVecs)
8900 continue;
8901 SDNode *User = *UI;
8902 DCI.CombineTo(User, SDValue(VLDDup.getNode(), ResNo));
8903 }
8904
8905 // Now the vldN-lane intrinsic is dead except for its chain result.
8906 // Update uses of the chain.
8907 std::vector<SDValue> VLDDupResults;
8908 for (unsigned n = 0; n < NumVecs; ++n)
8909 VLDDupResults.push_back(SDValue(VLDDup.getNode(), n));
8910 VLDDupResults.push_back(SDValue(VLDDup.getNode(), NumVecs));
8911 DCI.CombineTo(VLD, VLDDupResults);
8912
8913 return true;
8914}
8915
Bob Wilson103a0dc2010-07-14 01:22:12 +00008916/// PerformVDUPLANECombine - Target-specific dag combine xforms for
8917/// ARMISD::VDUPLANE.
Bob Wilson2d790df2010-11-28 06:51:26 +00008918static SDValue PerformVDUPLANECombine(SDNode *N,
8919 TargetLowering::DAGCombinerInfo &DCI) {
Bob Wilson103a0dc2010-07-14 01:22:12 +00008920 SDValue Op = N->getOperand(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00008921
Bob Wilson2d790df2010-11-28 06:51:26 +00008922 // If the source is a vldN-lane (N > 1) intrinsic, and all the other uses
8923 // of that intrinsic are also VDUPLANEs, combine them to a vldN-dup operation.
8924 if (CombineVLDDUP(N, DCI))
8925 return SDValue(N, 0);
8926
8927 // If the source is already a VMOVIMM or VMVNIMM splat, the VDUPLANE is
8928 // redundant. Ignore bit_converts for now; element sizes are checked below.
Wesley Peck527da1b2010-11-23 03:31:01 +00008929 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson103a0dc2010-07-14 01:22:12 +00008930 Op = Op.getOperand(0);
Bob Wilsonbad47f62010-07-14 06:31:50 +00008931 if (Op.getOpcode() != ARMISD::VMOVIMM && Op.getOpcode() != ARMISD::VMVNIMM)
Bob Wilson103a0dc2010-07-14 01:22:12 +00008932 return SDValue();
8933
8934 // Make sure the VMOV element size is not bigger than the VDUPLANE elements.
8935 unsigned EltSize = Op.getValueType().getVectorElementType().getSizeInBits();
8936 // The canonical VMOV for a zero vector uses a 32-bit element size.
8937 unsigned Imm = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8938 unsigned EltBits;
8939 if (ARM_AM::decodeNEONModImm(Imm, EltBits) == 0)
8940 EltSize = 8;
Bob Wilson2d790df2010-11-28 06:51:26 +00008941 EVT VT = N->getValueType(0);
Bob Wilson103a0dc2010-07-14 01:22:12 +00008942 if (EltSize > VT.getVectorElementType().getSizeInBits())
8943 return SDValue();
8944
Andrew Trickef9de2a2013-05-25 02:42:55 +00008945 return DCI.DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Bob Wilson103a0dc2010-07-14 01:22:12 +00008946}
8947
Eric Christopher1b8b94192011-06-29 21:10:36 +00008948// isConstVecPow2 - Return true if each vector element is a power of 2, all
Chad Rosierfa8d8932011-06-24 19:23:04 +00008949// elements are the same constant, C, and Log2(C) ranges from 1 to 32.
8950static bool isConstVecPow2(SDValue ConstVec, bool isSigned, uint64_t &C)
8951{
Chad Rosier6b610b32011-06-28 17:26:57 +00008952 integerPart cN;
8953 integerPart c0 = 0;
Chad Rosierfa8d8932011-06-24 19:23:04 +00008954 for (unsigned I = 0, E = ConstVec.getValueType().getVectorNumElements();
8955 I != E; I++) {
8956 ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(ConstVec.getOperand(I));
8957 if (!C)
8958 return false;
8959
Eric Christopher1b8b94192011-06-29 21:10:36 +00008960 bool isExact;
Chad Rosierfa8d8932011-06-24 19:23:04 +00008961 APFloat APF = C->getValueAPF();
8962 if (APF.convertToInteger(&cN, 64, isSigned, APFloat::rmTowardZero, &isExact)
8963 != APFloat::opOK || !isExact)
8964 return false;
8965
8966 c0 = (I == 0) ? cN : c0;
8967 if (!isPowerOf2_64(cN) || c0 != cN || Log2_64(c0) < 1 || Log2_64(c0) > 32)
8968 return false;
8969 }
8970 C = c0;
8971 return true;
8972}
8973
8974/// PerformVCVTCombine - VCVT (floating-point to fixed-point, Advanced SIMD)
8975/// can replace combinations of VMUL and VCVT (floating-point to integer)
8976/// when the VMUL has a constant operand that is a power of 2.
8977///
8978/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
8979/// vmul.f32 d16, d17, d16
8980/// vcvt.s32.f32 d16, d16
8981/// becomes:
8982/// vcvt.s32.f32 d16, d16, #3
8983static SDValue PerformVCVTCombine(SDNode *N,
8984 TargetLowering::DAGCombinerInfo &DCI,
8985 const ARMSubtarget *Subtarget) {
8986 SelectionDAG &DAG = DCI.DAG;
8987 SDValue Op = N->getOperand(0);
8988
8989 if (!Subtarget->hasNEON() || !Op.getValueType().isVector() ||
8990 Op.getOpcode() != ISD::FMUL)
8991 return SDValue();
8992
8993 uint64_t C;
8994 SDValue N0 = Op->getOperand(0);
8995 SDValue ConstVec = Op->getOperand(1);
8996 bool isSigned = N->getOpcode() == ISD::FP_TO_SINT;
8997
Eric Christopher1b8b94192011-06-29 21:10:36 +00008998 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
Chad Rosierfa8d8932011-06-24 19:23:04 +00008999 !isConstVecPow2(ConstVec, isSigned, C))
9000 return SDValue();
9001
Tim Northover7cbc2152013-06-28 15:29:25 +00009002 MVT FloatTy = Op.getSimpleValueType().getVectorElementType();
9003 MVT IntTy = N->getSimpleValueType(0).getVectorElementType();
9004 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9005 // These instructions only exist converting from f32 to i32. We can handle
9006 // smaller integers by generating an extra truncate, but larger ones would
9007 // be lossy.
9008 return SDValue();
9009 }
9010
Chad Rosierfa8d8932011-06-24 19:23:04 +00009011 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfp2fxs :
9012 Intrinsic::arm_neon_vcvtfp2fxu;
Tim Northover7cbc2152013-06-28 15:29:25 +00009013 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9014 SDValue FixConv = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
9015 NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9016 DAG.getConstant(IntrinsicOpcode, MVT::i32), N0,
9017 DAG.getConstant(Log2_64(C), MVT::i32));
9018
9019 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9020 FixConv = DAG.getNode(ISD::TRUNCATE, SDLoc(N), N->getValueType(0), FixConv);
9021
9022 return FixConv;
Chad Rosierfa8d8932011-06-24 19:23:04 +00009023}
9024
9025/// PerformVDIVCombine - VCVT (fixed-point to floating-point, Advanced SIMD)
9026/// can replace combinations of VCVT (integer to floating-point) and VDIV
9027/// when the VDIV has a constant operand that is a power of 2.
9028///
9029/// Example (assume d17 = <float 8.000000e+00, float 8.000000e+00>):
9030/// vcvt.f32.s32 d16, d16
9031/// vdiv.f32 d16, d17, d16
9032/// becomes:
9033/// vcvt.f32.s32 d16, d16, #3
9034static SDValue PerformVDIVCombine(SDNode *N,
9035 TargetLowering::DAGCombinerInfo &DCI,
9036 const ARMSubtarget *Subtarget) {
9037 SelectionDAG &DAG = DCI.DAG;
9038 SDValue Op = N->getOperand(0);
9039 unsigned OpOpcode = Op.getNode()->getOpcode();
9040
9041 if (!Subtarget->hasNEON() || !N->getValueType(0).isVector() ||
9042 (OpOpcode != ISD::SINT_TO_FP && OpOpcode != ISD::UINT_TO_FP))
9043 return SDValue();
9044
9045 uint64_t C;
9046 SDValue ConstVec = N->getOperand(1);
9047 bool isSigned = OpOpcode == ISD::SINT_TO_FP;
9048
9049 if (ConstVec.getOpcode() != ISD::BUILD_VECTOR ||
9050 !isConstVecPow2(ConstVec, isSigned, C))
9051 return SDValue();
9052
Tim Northover7cbc2152013-06-28 15:29:25 +00009053 MVT FloatTy = N->getSimpleValueType(0).getVectorElementType();
9054 MVT IntTy = Op.getOperand(0).getSimpleValueType().getVectorElementType();
9055 if (FloatTy.getSizeInBits() != 32 || IntTy.getSizeInBits() > 32) {
9056 // These instructions only exist converting from i32 to f32. We can handle
9057 // smaller integers by generating an extra extend, but larger ones would
9058 // be lossy.
9059 return SDValue();
9060 }
9061
9062 SDValue ConvInput = Op.getOperand(0);
9063 unsigned NumLanes = Op.getValueType().getVectorNumElements();
9064 if (IntTy.getSizeInBits() < FloatTy.getSizeInBits())
9065 ConvInput = DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
9066 SDLoc(N), NumLanes == 2 ? MVT::v2i32 : MVT::v4i32,
9067 ConvInput);
9068
Eric Christopher1b8b94192011-06-29 21:10:36 +00009069 unsigned IntrinsicOpcode = isSigned ? Intrinsic::arm_neon_vcvtfxs2fp :
Chad Rosierfa8d8932011-06-24 19:23:04 +00009070 Intrinsic::arm_neon_vcvtfxu2fp;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009071 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, SDLoc(N),
Chad Rosierfa8d8932011-06-24 19:23:04 +00009072 Op.getValueType(),
Eric Christopher1b8b94192011-06-29 21:10:36 +00009073 DAG.getConstant(IntrinsicOpcode, MVT::i32),
Tim Northover7cbc2152013-06-28 15:29:25 +00009074 ConvInput, DAG.getConstant(Log2_64(C), MVT::i32));
Chad Rosierfa8d8932011-06-24 19:23:04 +00009075}
9076
9077/// Getvshiftimm - Check if this is a valid build_vector for the immediate
Bob Wilson2e076c42009-06-22 23:27:02 +00009078/// operand of a vector shift operation, where all the elements of the
9079/// build_vector must have the same constant integer value.
9080static bool getVShiftImm(SDValue Op, unsigned ElementBits, int64_t &Cnt) {
9081 // Ignore bit_converts.
Wesley Peck527da1b2010-11-23 03:31:01 +00009082 while (Op.getOpcode() == ISD::BITCAST)
Bob Wilson2e076c42009-06-22 23:27:02 +00009083 Op = Op.getOperand(0);
9084 BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
9085 APInt SplatBits, SplatUndef;
9086 unsigned SplatBitSize;
9087 bool HasAnyUndefs;
9088 if (! BVN || ! BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize,
9089 HasAnyUndefs, ElementBits) ||
9090 SplatBitSize > ElementBits)
9091 return false;
9092 Cnt = SplatBits.getSExtValue();
9093 return true;
9094}
9095
9096/// isVShiftLImm - Check if this is a valid build_vector for the immediate
9097/// operand of a vector shift left operation. That value must be in the range:
9098/// 0 <= Value < ElementBits for a left shift; or
9099/// 0 <= Value <= ElementBits for a long left shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009100static bool isVShiftLImm(SDValue Op, EVT VT, bool isLong, int64_t &Cnt) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009101 assert(VT.isVector() && "vector shift count is not a vector type");
9102 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9103 if (! getVShiftImm(Op, ElementBits, Cnt))
9104 return false;
9105 return (Cnt >= 0 && (isLong ? Cnt-1 : Cnt) < ElementBits);
9106}
9107
9108/// isVShiftRImm - Check if this is a valid build_vector for the immediate
9109/// operand of a vector shift right operation. For a shift opcode, the value
9110/// is positive, but for an intrinsic the value count must be negative. The
9111/// absolute value must be in the range:
9112/// 1 <= |Value| <= ElementBits for a right shift; or
9113/// 1 <= |Value| <= ElementBits/2 for a narrow right shift.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009114static bool isVShiftRImm(SDValue Op, EVT VT, bool isNarrow, bool isIntrinsic,
Bob Wilson2e076c42009-06-22 23:27:02 +00009115 int64_t &Cnt) {
9116 assert(VT.isVector() && "vector shift count is not a vector type");
9117 unsigned ElementBits = VT.getVectorElementType().getSizeInBits();
9118 if (! getVShiftImm(Op, ElementBits, Cnt))
9119 return false;
9120 if (isIntrinsic)
9121 Cnt = -Cnt;
9122 return (Cnt >= 1 && Cnt <= (isNarrow ? ElementBits/2 : ElementBits));
9123}
9124
9125/// PerformIntrinsicCombine - ARM-specific DAG combining for intrinsics.
9126static SDValue PerformIntrinsicCombine(SDNode *N, SelectionDAG &DAG) {
9127 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
9128 switch (IntNo) {
9129 default:
9130 // Don't do anything for most intrinsics.
9131 break;
9132
9133 // Vector shifts: check for immediate versions and lower them.
9134 // Note: This is done during DAG combining instead of DAG legalizing because
9135 // the build_vectors for 64-bit vector element shift counts are generally
9136 // not legal, and it is hard to see their values after they get legalized to
9137 // loads from a constant pool.
9138 case Intrinsic::arm_neon_vshifts:
9139 case Intrinsic::arm_neon_vshiftu:
Bob Wilson2e076c42009-06-22 23:27:02 +00009140 case Intrinsic::arm_neon_vrshifts:
9141 case Intrinsic::arm_neon_vrshiftu:
9142 case Intrinsic::arm_neon_vrshiftn:
9143 case Intrinsic::arm_neon_vqshifts:
9144 case Intrinsic::arm_neon_vqshiftu:
9145 case Intrinsic::arm_neon_vqshiftsu:
9146 case Intrinsic::arm_neon_vqshiftns:
9147 case Intrinsic::arm_neon_vqshiftnu:
9148 case Intrinsic::arm_neon_vqshiftnsu:
9149 case Intrinsic::arm_neon_vqrshiftns:
9150 case Intrinsic::arm_neon_vqrshiftnu:
9151 case Intrinsic::arm_neon_vqrshiftnsu: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009152 EVT VT = N->getOperand(1).getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009153 int64_t Cnt;
9154 unsigned VShiftOpc = 0;
9155
9156 switch (IntNo) {
9157 case Intrinsic::arm_neon_vshifts:
9158 case Intrinsic::arm_neon_vshiftu:
9159 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt)) {
9160 VShiftOpc = ARMISD::VSHL;
9161 break;
9162 }
9163 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt)) {
9164 VShiftOpc = (IntNo == Intrinsic::arm_neon_vshifts ?
9165 ARMISD::VSHRs : ARMISD::VSHRu);
9166 break;
9167 }
9168 return SDValue();
9169
Bob Wilson2e076c42009-06-22 23:27:02 +00009170 case Intrinsic::arm_neon_vrshifts:
9171 case Intrinsic::arm_neon_vrshiftu:
9172 if (isVShiftRImm(N->getOperand(2), VT, false, true, Cnt))
9173 break;
9174 return SDValue();
9175
9176 case Intrinsic::arm_neon_vqshifts:
9177 case Intrinsic::arm_neon_vqshiftu:
9178 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9179 break;
9180 return SDValue();
9181
9182 case Intrinsic::arm_neon_vqshiftsu:
9183 if (isVShiftLImm(N->getOperand(2), VT, false, Cnt))
9184 break;
Torok Edwinfbcc6632009-07-14 16:55:14 +00009185 llvm_unreachable("invalid shift count for vqshlu intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009186
Bob Wilson2e076c42009-06-22 23:27:02 +00009187 case Intrinsic::arm_neon_vrshiftn:
9188 case Intrinsic::arm_neon_vqshiftns:
9189 case Intrinsic::arm_neon_vqshiftnu:
9190 case Intrinsic::arm_neon_vqshiftnsu:
9191 case Intrinsic::arm_neon_vqrshiftns:
9192 case Intrinsic::arm_neon_vqrshiftnu:
9193 case Intrinsic::arm_neon_vqrshiftnsu:
9194 // Narrowing shifts require an immediate right shift.
9195 if (isVShiftRImm(N->getOperand(2), VT, true, true, Cnt))
9196 break;
Jim Grosbach84511e12010-06-02 21:53:11 +00009197 llvm_unreachable("invalid shift count for narrowing vector shift "
9198 "intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009199
9200 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00009201 llvm_unreachable("unhandled vector shift");
Bob Wilson2e076c42009-06-22 23:27:02 +00009202 }
9203
9204 switch (IntNo) {
9205 case Intrinsic::arm_neon_vshifts:
9206 case Intrinsic::arm_neon_vshiftu:
9207 // Opcode already set above.
9208 break;
Bob Wilson2e076c42009-06-22 23:27:02 +00009209 case Intrinsic::arm_neon_vrshifts:
9210 VShiftOpc = ARMISD::VRSHRs; break;
9211 case Intrinsic::arm_neon_vrshiftu:
9212 VShiftOpc = ARMISD::VRSHRu; break;
9213 case Intrinsic::arm_neon_vrshiftn:
9214 VShiftOpc = ARMISD::VRSHRN; break;
9215 case Intrinsic::arm_neon_vqshifts:
9216 VShiftOpc = ARMISD::VQSHLs; break;
9217 case Intrinsic::arm_neon_vqshiftu:
9218 VShiftOpc = ARMISD::VQSHLu; break;
9219 case Intrinsic::arm_neon_vqshiftsu:
9220 VShiftOpc = ARMISD::VQSHLsu; break;
9221 case Intrinsic::arm_neon_vqshiftns:
9222 VShiftOpc = ARMISD::VQSHRNs; break;
9223 case Intrinsic::arm_neon_vqshiftnu:
9224 VShiftOpc = ARMISD::VQSHRNu; break;
9225 case Intrinsic::arm_neon_vqshiftnsu:
9226 VShiftOpc = ARMISD::VQSHRNsu; break;
9227 case Intrinsic::arm_neon_vqrshiftns:
9228 VShiftOpc = ARMISD::VQRSHRNs; break;
9229 case Intrinsic::arm_neon_vqrshiftnu:
9230 VShiftOpc = ARMISD::VQRSHRNu; break;
9231 case Intrinsic::arm_neon_vqrshiftnsu:
9232 VShiftOpc = ARMISD::VQRSHRNsu; break;
9233 }
9234
Andrew Trickef9de2a2013-05-25 02:42:55 +00009235 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009236 N->getOperand(1), DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009237 }
9238
9239 case Intrinsic::arm_neon_vshiftins: {
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 if (isVShiftLImm(N->getOperand(3), VT, false, Cnt))
9245 VShiftOpc = ARMISD::VSLI;
9246 else if (isVShiftRImm(N->getOperand(3), VT, false, true, Cnt))
9247 VShiftOpc = ARMISD::VSRI;
9248 else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009249 llvm_unreachable("invalid shift count for vsli/vsri intrinsic");
Bob Wilson2e076c42009-06-22 23:27:02 +00009250 }
9251
Andrew Trickef9de2a2013-05-25 02:42:55 +00009252 return DAG.getNode(VShiftOpc, SDLoc(N), N->getValueType(0),
Bob Wilson2e076c42009-06-22 23:27:02 +00009253 N->getOperand(1), N->getOperand(2),
Owen Anderson9f944592009-08-11 20:47:22 +00009254 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009255 }
9256
9257 case Intrinsic::arm_neon_vqrshifts:
9258 case Intrinsic::arm_neon_vqrshiftu:
9259 // No immediate versions of these to check for.
9260 break;
9261 }
9262
9263 return SDValue();
9264}
9265
9266/// PerformShiftCombine - Checks for immediate versions of vector shifts and
9267/// lowers them. As with the vector shift intrinsics, this is done during DAG
9268/// combining instead of DAG legalizing because the build_vectors for 64-bit
9269/// vector element shift counts are generally not legal, and it is hard to see
9270/// their values after they get legalized to loads from a constant pool.
9271static SDValue PerformShiftCombine(SDNode *N, SelectionDAG &DAG,
9272 const ARMSubtarget *ST) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009273 EVT VT = N->getValueType(0);
Evan Chengf258a152012-02-23 02:58:19 +00009274 if (N->getOpcode() == ISD::SRL && VT == MVT::i32 && ST->hasV6Ops()) {
9275 // Canonicalize (srl (bswap x), 16) to (rotr (bswap x), 16) if the high
9276 // 16-bits of x is zero. This optimizes rev + lsr 16 to rev16.
9277 SDValue N1 = N->getOperand(1);
9278 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
9279 SDValue N0 = N->getOperand(0);
9280 if (C->getZExtValue() == 16 && N0.getOpcode() == ISD::BSWAP &&
9281 DAG.MaskedValueIsZero(N0.getOperand(0),
9282 APInt::getHighBitsSet(32, 16)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009283 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, N0, N1);
Evan Chengf258a152012-02-23 02:58:19 +00009284 }
9285 }
Bob Wilson2e076c42009-06-22 23:27:02 +00009286
9287 // Nothing to be done for scalar shifts.
Tanya Lattnercd680952010-11-18 22:06:46 +00009288 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9289 if (!VT.isVector() || !TLI.isTypeLegal(VT))
Bob Wilson2e076c42009-06-22 23:27:02 +00009290 return SDValue();
9291
9292 assert(ST->hasNEON() && "unexpected vector shift");
9293 int64_t Cnt;
9294
9295 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009296 default: llvm_unreachable("unexpected shift opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009297
9298 case ISD::SHL:
9299 if (isVShiftLImm(N->getOperand(1), VT, false, Cnt))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009300 return DAG.getNode(ARMISD::VSHL, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009301 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009302 break;
9303
9304 case ISD::SRA:
9305 case ISD::SRL:
9306 if (isVShiftRImm(N->getOperand(1), VT, false, false, Cnt)) {
9307 unsigned VShiftOpc = (N->getOpcode() == ISD::SRA ?
9308 ARMISD::VSHRs : ARMISD::VSHRu);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009309 return DAG.getNode(VShiftOpc, SDLoc(N), VT, N->getOperand(0),
Owen Anderson9f944592009-08-11 20:47:22 +00009310 DAG.getConstant(Cnt, MVT::i32));
Bob Wilson2e076c42009-06-22 23:27:02 +00009311 }
9312 }
9313 return SDValue();
9314}
9315
9316/// PerformExtendCombine - Target-specific DAG combining for ISD::SIGN_EXTEND,
9317/// ISD::ZERO_EXTEND, and ISD::ANY_EXTEND.
9318static SDValue PerformExtendCombine(SDNode *N, SelectionDAG &DAG,
9319 const ARMSubtarget *ST) {
9320 SDValue N0 = N->getOperand(0);
9321
9322 // Check for sign- and zero-extensions of vector extract operations of 8-
9323 // and 16-bit vector elements. NEON supports these directly. They are
9324 // handled during DAG combining because type legalization will promote them
9325 // to 32-bit types and it is messy to recognize the operations after that.
9326 if (ST->hasNEON() && N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
9327 SDValue Vec = N0.getOperand(0);
9328 SDValue Lane = N0.getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009329 EVT VT = N->getValueType(0);
9330 EVT EltVT = N0.getValueType();
Bob Wilson2e076c42009-06-22 23:27:02 +00009331 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9332
Owen Anderson9f944592009-08-11 20:47:22 +00009333 if (VT == MVT::i32 &&
9334 (EltVT == MVT::i8 || EltVT == MVT::i16) &&
Bob Wilsonceb49292010-11-03 16:24:50 +00009335 TLI.isTypeLegal(Vec.getValueType()) &&
9336 isa<ConstantSDNode>(Lane)) {
Bob Wilson2e076c42009-06-22 23:27:02 +00009337
9338 unsigned Opc = 0;
9339 switch (N->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009340 default: llvm_unreachable("unexpected opcode");
Bob Wilson2e076c42009-06-22 23:27:02 +00009341 case ISD::SIGN_EXTEND:
9342 Opc = ARMISD::VGETLANEs;
9343 break;
9344 case ISD::ZERO_EXTEND:
9345 case ISD::ANY_EXTEND:
9346 Opc = ARMISD::VGETLANEu;
9347 break;
9348 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00009349 return DAG.getNode(Opc, SDLoc(N), VT, Vec, Lane);
Bob Wilson2e076c42009-06-22 23:27:02 +00009350 }
9351 }
9352
9353 return SDValue();
9354}
9355
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009356/// PerformSELECT_CCCombine - Target-specific DAG combining for ISD::SELECT_CC
9357/// to match f32 max/min patterns to use NEON vmax/vmin instructions.
9358static SDValue PerformSELECT_CCCombine(SDNode *N, SelectionDAG &DAG,
9359 const ARMSubtarget *ST) {
9360 // If the target supports NEON, try to use vmax/vmin instructions for f32
Evan Cheng55f0c6b2010-07-15 22:07:12 +00009361 // selects like "x < y ? x : y". Unless the NoNaNsFPMath option is set,
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009362 // be careful about NaNs: NEON's vmax/vmin return NaN if either operand is
9363 // a NaN; only do the transformation when it matches that behavior.
9364
9365 // For now only do this when using NEON for FP operations; if using VFP, it
9366 // is not obvious that the benefit outweighs the cost of switching to the
9367 // NEON pipeline.
9368 if (!ST->hasNEON() || !ST->useNEONForSinglePrecisionFP() ||
9369 N->getValueType(0) != MVT::f32)
9370 return SDValue();
9371
9372 SDValue CondLHS = N->getOperand(0);
9373 SDValue CondRHS = N->getOperand(1);
9374 SDValue LHS = N->getOperand(2);
9375 SDValue RHS = N->getOperand(3);
9376 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
9377
9378 unsigned Opcode = 0;
9379 bool IsReversed;
Bob Wilsonba8ac742010-02-24 22:15:53 +00009380 if (DAG.isEqualTo(LHS, CondLHS) && DAG.isEqualTo(RHS, CondRHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009381 IsReversed = false; // x CC y ? x : y
Bob Wilsonba8ac742010-02-24 22:15:53 +00009382 } else if (DAG.isEqualTo(LHS, CondRHS) && DAG.isEqualTo(RHS, CondLHS)) {
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009383 IsReversed = true ; // x CC y ? y : x
9384 } else {
9385 return SDValue();
9386 }
9387
Bob Wilsonba8ac742010-02-24 22:15:53 +00009388 bool IsUnordered;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009389 switch (CC) {
9390 default: break;
9391 case ISD::SETOLT:
9392 case ISD::SETOLE:
9393 case ISD::SETLT:
9394 case ISD::SETLE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009395 case ISD::SETULT:
9396 case ISD::SETULE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009397 // If LHS is NaN, an ordered comparison will be false and the result will
9398 // be the RHS, but vmin(NaN, RHS) = NaN. Avoid this by checking that LHS
9399 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9400 IsUnordered = (CC == ISD::SETULT || CC == ISD::SETULE);
9401 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9402 break;
9403 // For less-than-or-equal comparisons, "+0 <= -0" will be true but vmin
9404 // will return -0, so vmin can only be used for unsafe math or if one of
9405 // the operands is known to be nonzero.
9406 if ((CC == ISD::SETLE || CC == ISD::SETOLE || CC == ISD::SETULE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009407 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009408 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9409 break;
9410 Opcode = IsReversed ? ARMISD::FMAX : ARMISD::FMIN;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009411 break;
9412
9413 case ISD::SETOGT:
9414 case ISD::SETOGE:
9415 case ISD::SETGT:
9416 case ISD::SETGE:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009417 case ISD::SETUGT:
9418 case ISD::SETUGE:
Bob Wilsonba8ac742010-02-24 22:15:53 +00009419 // If LHS is NaN, an ordered comparison will be false and the result will
9420 // be the RHS, but vmax(NaN, RHS) = NaN. Avoid this by checking that LHS
9421 // != NaN. Likewise, for unordered comparisons, check for RHS != NaN.
9422 IsUnordered = (CC == ISD::SETUGT || CC == ISD::SETUGE);
9423 if (!DAG.isKnownNeverNaN(IsUnordered ? RHS : LHS))
9424 break;
9425 // For greater-than-or-equal comparisons, "-0 >= +0" will be true but vmax
9426 // will return +0, so vmax can only be used for unsafe math or if one of
9427 // the operands is known to be nonzero.
9428 if ((CC == ISD::SETGE || CC == ISD::SETOGE || CC == ISD::SETUGE) &&
Nick Lewycky50f02cb2011-12-02 22:16:29 +00009429 !DAG.getTarget().Options.UnsafeFPMath &&
Bob Wilsonba8ac742010-02-24 22:15:53 +00009430 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9431 break;
9432 Opcode = IsReversed ? ARMISD::FMIN : ARMISD::FMAX;
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009433 break;
9434 }
9435
9436 if (!Opcode)
9437 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009438 return DAG.getNode(Opcode, SDLoc(N), N->getValueType(0), LHS, RHS);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009439}
9440
Evan Chengf863e3f2011-07-13 00:42:17 +00009441/// PerformCMOVCombine - Target-specific DAG combining for ARMISD::CMOV.
9442SDValue
9443ARMTargetLowering::PerformCMOVCombine(SDNode *N, SelectionDAG &DAG) const {
9444 SDValue Cmp = N->getOperand(4);
9445 if (Cmp.getOpcode() != ARMISD::CMPZ)
9446 // Only looking at EQ and NE cases.
9447 return SDValue();
9448
9449 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009450 SDLoc dl(N);
Evan Chengf863e3f2011-07-13 00:42:17 +00009451 SDValue LHS = Cmp.getOperand(0);
9452 SDValue RHS = Cmp.getOperand(1);
9453 SDValue FalseVal = N->getOperand(0);
9454 SDValue TrueVal = N->getOperand(1);
9455 SDValue ARMcc = N->getOperand(2);
Jim Grosbache7e2aca2011-09-13 20:30:37 +00009456 ARMCC::CondCodes CC =
9457 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
Evan Chengf863e3f2011-07-13 00:42:17 +00009458
9459 // Simplify
9460 // mov r1, r0
9461 // cmp r1, x
9462 // mov r0, y
9463 // moveq r0, x
9464 // to
9465 // cmp r0, x
9466 // movne r0, y
9467 //
9468 // mov r1, r0
9469 // cmp r1, x
9470 // mov r0, x
9471 // movne r0, y
9472 // to
9473 // cmp r0, x
9474 // movne r0, y
9475 /// FIXME: Turn this into a target neutral optimization?
9476 SDValue Res;
Evan Cheng81563762011-09-28 23:16:31 +00009477 if (CC == ARMCC::NE && FalseVal == RHS && FalseVal != LHS) {
Evan Chengf863e3f2011-07-13 00:42:17 +00009478 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, TrueVal, ARMcc,
9479 N->getOperand(3), Cmp);
9480 } else if (CC == ARMCC::EQ && TrueVal == RHS) {
9481 SDValue ARMcc;
9482 SDValue NewCmp = getARMCmp(LHS, RHS, ISD::SETNE, ARMcc, DAG, dl);
9483 Res = DAG.getNode(ARMISD::CMOV, dl, VT, LHS, FalseVal, ARMcc,
9484 N->getOperand(3), NewCmp);
9485 }
9486
9487 if (Res.getNode()) {
9488 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +00009489 DAG.computeKnownBits(SDValue(N,0), KnownZero, KnownOne);
Evan Chengf863e3f2011-07-13 00:42:17 +00009490 // Capture demanded bits information that would be otherwise lost.
9491 if (KnownZero == 0xfffffffe)
9492 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9493 DAG.getValueType(MVT::i1));
9494 else if (KnownZero == 0xffffff00)
9495 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9496 DAG.getValueType(MVT::i8));
9497 else if (KnownZero == 0xffff0000)
9498 Res = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Res,
9499 DAG.getValueType(MVT::i16));
9500 }
9501
9502 return Res;
9503}
9504
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009505SDValue ARMTargetLowering::PerformDAGCombine(SDNode *N,
Bob Wilson7117a912009-03-20 22:42:55 +00009506 DAGCombinerInfo &DCI) const {
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009507 switch (N->getOpcode()) {
9508 default: break;
Arnold Schwaighoferf00fb1c2012-09-04 14:37:49 +00009509 case ISD::ADDC: return PerformADDCCombine(N, DCI, Subtarget);
Tanya Lattnere9e67052011-06-14 23:48:48 +00009510 case ISD::ADD: return PerformADDCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009511 case ISD::SUB: return PerformSUBCombine(N, DCI);
Anton Korobeynikov1bf28a12010-05-15 18:16:59 +00009512 case ISD::MUL: return PerformMULCombine(N, DCI, Subtarget);
Jim Grosbach11013ed2010-07-16 23:05:05 +00009513 case ISD::OR: return PerformORCombine(N, DCI, Subtarget);
Evan Chenge87681c2012-02-23 01:19:06 +00009514 case ISD::XOR: return PerformXORCombine(N, DCI, Subtarget);
9515 case ISD::AND: return PerformANDCombine(N, DCI, Subtarget);
Evan Chengc1778132010-12-14 03:22:07 +00009516 case ARMISD::BFI: return PerformBFICombine(N, DCI);
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00009517 case ARMISD::VMOVRRD: return PerformVMOVRRDCombine(N, DCI);
Bob Wilson22806742010-09-22 22:09:21 +00009518 case ARMISD::VMOVDRR: return PerformVMOVDRRCombine(N, DCI.DAG);
Bob Wilson1a20c2a2010-12-21 06:43:19 +00009519 case ISD::STORE: return PerformSTORECombine(N, DCI);
9520 case ISD::BUILD_VECTOR: return PerformBUILD_VECTORCombine(N, DCI);
9521 case ISD::INSERT_VECTOR_ELT: return PerformInsertEltCombine(N, DCI);
Bob Wilsonc7334a12010-10-27 20:38:28 +00009522 case ISD::VECTOR_SHUFFLE: return PerformVECTOR_SHUFFLECombine(N, DCI.DAG);
Bob Wilson2d790df2010-11-28 06:51:26 +00009523 case ARMISD::VDUPLANE: return PerformVDUPLANECombine(N, DCI);
Chad Rosierfa8d8932011-06-24 19:23:04 +00009524 case ISD::FP_TO_SINT:
9525 case ISD::FP_TO_UINT: return PerformVCVTCombine(N, DCI, Subtarget);
9526 case ISD::FDIV: return PerformVDIVCombine(N, DCI, Subtarget);
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009527 case ISD::INTRINSIC_WO_CHAIN: return PerformIntrinsicCombine(N, DCI.DAG);
Bob Wilson2e076c42009-06-22 23:27:02 +00009528 case ISD::SHL:
9529 case ISD::SRA:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009530 case ISD::SRL: return PerformShiftCombine(N, DCI.DAG, Subtarget);
Bob Wilson2e076c42009-06-22 23:27:02 +00009531 case ISD::SIGN_EXTEND:
9532 case ISD::ZERO_EXTEND:
Bob Wilsonc6c13a32010-02-18 06:05:53 +00009533 case ISD::ANY_EXTEND: return PerformExtendCombine(N, DCI.DAG, Subtarget);
9534 case ISD::SELECT_CC: return PerformSELECT_CCCombine(N, DCI.DAG, Subtarget);
Evan Chengf863e3f2011-07-13 00:42:17 +00009535 case ARMISD::CMOV: return PerformCMOVCombine(N, DCI.DAG);
Bob Wilson06fce872011-02-07 17:43:21 +00009536 case ARMISD::VLD2DUP:
9537 case ARMISD::VLD3DUP:
9538 case ARMISD::VLD4DUP:
9539 return CombineBaseUpdate(N, DCI);
Quentin Colombet04b3a0f2013-07-03 21:42:57 +00009540 case ARMISD::BUILD_VECTOR:
9541 return PerformARMBUILD_VECTORCombine(N, DCI);
Bob Wilson06fce872011-02-07 17:43:21 +00009542 case ISD::INTRINSIC_VOID:
9543 case ISD::INTRINSIC_W_CHAIN:
9544 switch (cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()) {
9545 case Intrinsic::arm_neon_vld1:
9546 case Intrinsic::arm_neon_vld2:
9547 case Intrinsic::arm_neon_vld3:
9548 case Intrinsic::arm_neon_vld4:
9549 case Intrinsic::arm_neon_vld2lane:
9550 case Intrinsic::arm_neon_vld3lane:
9551 case Intrinsic::arm_neon_vld4lane:
9552 case Intrinsic::arm_neon_vst1:
9553 case Intrinsic::arm_neon_vst2:
9554 case Intrinsic::arm_neon_vst3:
9555 case Intrinsic::arm_neon_vst4:
9556 case Intrinsic::arm_neon_vst2lane:
9557 case Intrinsic::arm_neon_vst3lane:
9558 case Intrinsic::arm_neon_vst4lane:
9559 return CombineBaseUpdate(N, DCI);
9560 default: break;
9561 }
9562 break;
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009563 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009564 return SDValue();
Chris Lattnerf3f4ad92007-11-27 22:36:16 +00009565}
9566
Evan Chengd42641c2011-02-02 01:06:55 +00009567bool ARMTargetLowering::isDesirableToTransformToIntegerOp(unsigned Opc,
9568 EVT VT) const {
9569 return (VT == MVT::f32) && (Opc == ISD::LOAD || Opc == ISD::STORE);
9570}
9571
Matt Arsenault25793a32014-02-05 23:15:53 +00009572bool ARMTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, unsigned,
9573 bool *Fast) const {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009574 // The AllowsUnaliged flag models the SCTLR.A setting in ARM cpus
Chad Rosier66bb1782012-11-09 18:25:27 +00009575 bool AllowsUnaligned = Subtarget->allowsUnalignedMem();
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009576
9577 switch (VT.getSimpleVT().SimpleTy) {
9578 default:
9579 return false;
9580 case MVT::i8:
9581 case MVT::i16:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009582 case MVT::i32: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009583 // Unaligned access can use (for example) LRDB, LRDH, LDR
Evan Cheng79e2ca92012-12-10 23:21:26 +00009584 if (AllowsUnaligned) {
9585 if (Fast)
9586 *Fast = Subtarget->hasV7Ops();
9587 return true;
9588 }
9589 return false;
9590 }
Evan Chengeec6bc62012-08-15 17:44:53 +00009591 case MVT::f64:
Evan Cheng79e2ca92012-12-10 23:21:26 +00009592 case MVT::v2f64: {
Evan Cheng90ae8f82012-09-18 01:42:45 +00009593 // For any little-endian targets with neon, we can support unaligned ld/st
9594 // of D and Q (e.g. {D0,D1}) registers by using vld1.i8/vst1.i8.
Alp Tokercb402912014-01-24 17:20:08 +00009595 // A big-endian target may also explicitly support unaligned accesses
Evan Cheng79e2ca92012-12-10 23:21:26 +00009596 if (Subtarget->hasNEON() && (AllowsUnaligned || isLittleEndian())) {
9597 if (Fast)
9598 *Fast = true;
9599 return true;
9600 }
9601 return false;
9602 }
Bill Wendlingbae6b2c2009-08-15 21:21:19 +00009603 }
9604}
9605
Lang Hames9929c422011-11-02 22:52:45 +00009606static bool memOpAlign(unsigned DstAlign, unsigned SrcAlign,
9607 unsigned AlignCheck) {
9608 return ((SrcAlign == 0 || SrcAlign % AlignCheck == 0) &&
9609 (DstAlign == 0 || DstAlign % AlignCheck == 0));
9610}
9611
9612EVT ARMTargetLowering::getOptimalMemOpType(uint64_t Size,
9613 unsigned DstAlign, unsigned SrcAlign,
Evan Cheng962711e2012-12-12 02:34:41 +00009614 bool IsMemset, bool ZeroMemset,
Lang Hames9929c422011-11-02 22:52:45 +00009615 bool MemcpyStrSrc,
9616 MachineFunction &MF) const {
9617 const Function *F = MF.getFunction();
9618
9619 // See if we can use NEON instructions for this...
Evan Cheng962711e2012-12-12 02:34:41 +00009620 if ((!IsMemset || ZeroMemset) &&
Evan Cheng79e2ca92012-12-10 23:21:26 +00009621 Subtarget->hasNEON() &&
Bill Wendling698e84f2012-12-30 10:32:01 +00009622 !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
9623 Attribute::NoImplicitFloat)) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009624 bool Fast;
Evan Chengc2bd6202012-12-11 02:31:57 +00009625 if (Size >= 16 &&
9626 (memOpAlign(SrcAlign, DstAlign, 16) ||
Matt Arsenault25793a32014-02-05 23:15:53 +00009627 (allowsUnalignedMemoryAccesses(MVT::v2f64, 0, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009628 return MVT::v2f64;
Evan Chengc2bd6202012-12-11 02:31:57 +00009629 } else if (Size >= 8 &&
9630 (memOpAlign(SrcAlign, DstAlign, 8) ||
Matt Arsenault25793a32014-02-05 23:15:53 +00009631 (allowsUnalignedMemoryAccesses(MVT::f64, 0, &Fast) && Fast))) {
Evan Cheng79e2ca92012-12-10 23:21:26 +00009632 return MVT::f64;
Lang Hames9929c422011-11-02 22:52:45 +00009633 }
9634 }
9635
Lang Hamesb85fcd02011-11-08 18:56:23 +00009636 // Lowering to i32/i16 if the size permits.
Evan Chengc2bd6202012-12-11 02:31:57 +00009637 if (Size >= 4)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009638 return MVT::i32;
Evan Chengc2bd6202012-12-11 02:31:57 +00009639 else if (Size >= 2)
Lang Hamesb85fcd02011-11-08 18:56:23 +00009640 return MVT::i16;
Lang Hamesb85fcd02011-11-08 18:56:23 +00009641
Lang Hames9929c422011-11-02 22:52:45 +00009642 // Let the target-independent logic figure it out.
9643 return MVT::Other;
9644}
9645
Evan Cheng9ec512d2012-12-06 19:13:27 +00009646bool ARMTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
9647 if (Val.getOpcode() != ISD::LOAD)
9648 return false;
9649
9650 EVT VT1 = Val.getValueType();
9651 if (!VT1.isSimple() || !VT1.isInteger() ||
9652 !VT2.isSimple() || !VT2.isInteger())
9653 return false;
9654
9655 switch (VT1.getSimpleVT().SimpleTy) {
9656 default: break;
9657 case MVT::i1:
9658 case MVT::i8:
9659 case MVT::i16:
9660 // 8-bit and 16-bit loads implicitly zero-extend to 32-bits.
9661 return true;
9662 }
9663
9664 return false;
9665}
9666
Tim Northovercc2e9032013-08-06 13:58:03 +00009667bool ARMTargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
9668 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
9669 return false;
9670
9671 if (!isTypeLegal(EVT::getEVT(Ty1)))
9672 return false;
9673
9674 assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
9675
9676 // Assuming the caller doesn't have a zeroext or signext return parameter,
9677 // truncation all the way down to i1 is valid.
9678 return true;
9679}
9680
9681
Evan Chengdc49a8d2009-08-14 20:09:37 +00009682static bool isLegalT1AddressImmediate(int64_t V, EVT VT) {
9683 if (V < 0)
9684 return false;
9685
9686 unsigned Scale = 1;
9687 switch (VT.getSimpleVT().SimpleTy) {
9688 default: return false;
9689 case MVT::i1:
9690 case MVT::i8:
9691 // Scale == 1;
9692 break;
9693 case MVT::i16:
9694 // Scale == 2;
9695 Scale = 2;
9696 break;
9697 case MVT::i32:
9698 // Scale == 4;
9699 Scale = 4;
9700 break;
9701 }
9702
9703 if ((V & (Scale - 1)) != 0)
9704 return false;
9705 V /= Scale;
9706 return V == (V & ((1LL << 5) - 1));
9707}
9708
9709static bool isLegalT2AddressImmediate(int64_t V, EVT VT,
9710 const ARMSubtarget *Subtarget) {
9711 bool isNeg = false;
9712 if (V < 0) {
9713 isNeg = true;
9714 V = - V;
9715 }
9716
9717 switch (VT.getSimpleVT().SimpleTy) {
9718 default: return false;
9719 case MVT::i1:
9720 case MVT::i8:
9721 case MVT::i16:
9722 case MVT::i32:
9723 // + imm12 or - imm8
9724 if (isNeg)
9725 return V == (V & ((1LL << 8) - 1));
9726 return V == (V & ((1LL << 12) - 1));
9727 case MVT::f32:
9728 case MVT::f64:
9729 // Same as ARM mode. FIXME: NEON?
9730 if (!Subtarget->hasVFP2())
9731 return false;
9732 if ((V & 3) != 0)
9733 return false;
9734 V >>= 2;
9735 return V == (V & ((1LL << 8) - 1));
9736 }
9737}
9738
Evan Cheng2150b922007-03-12 23:30:29 +00009739/// isLegalAddressImmediate - Return true if the integer value can be used
9740/// as the offset of the target addressing mode for load / store of the
9741/// given type.
Owen Anderson53aa7a92009-08-10 22:56:29 +00009742static bool isLegalAddressImmediate(int64_t V, EVT VT,
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009743 const ARMSubtarget *Subtarget) {
Evan Cheng507eefa2007-03-13 20:37:59 +00009744 if (V == 0)
9745 return true;
9746
Evan Chengce5dfb62009-03-09 19:15:00 +00009747 if (!VT.isSimple())
9748 return false;
9749
Evan Chengdc49a8d2009-08-14 20:09:37 +00009750 if (Subtarget->isThumb1Only())
9751 return isLegalT1AddressImmediate(V, VT);
9752 else if (Subtarget->isThumb2())
9753 return isLegalT2AddressImmediate(V, VT, Subtarget);
Evan Cheng2150b922007-03-12 23:30:29 +00009754
Evan Chengdc49a8d2009-08-14 20:09:37 +00009755 // ARM mode.
Evan Cheng2150b922007-03-12 23:30:29 +00009756 if (V < 0)
9757 V = - V;
Owen Anderson9f944592009-08-11 20:47:22 +00009758 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng2150b922007-03-12 23:30:29 +00009759 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +00009760 case MVT::i1:
9761 case MVT::i8:
9762 case MVT::i32:
Evan Cheng2150b922007-03-12 23:30:29 +00009763 // +- imm12
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009764 return V == (V & ((1LL << 12) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +00009765 case MVT::i16:
Evan Cheng2150b922007-03-12 23:30:29 +00009766 // +- imm8
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009767 return V == (V & ((1LL << 8) - 1));
Owen Anderson9f944592009-08-11 20:47:22 +00009768 case MVT::f32:
9769 case MVT::f64:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009770 if (!Subtarget->hasVFP2()) // FIXME: NEON?
Evan Cheng2150b922007-03-12 23:30:29 +00009771 return false;
Evan Chengbef131de2007-05-03 02:00:18 +00009772 if ((V & 3) != 0)
Evan Cheng2150b922007-03-12 23:30:29 +00009773 return false;
9774 V >>= 2;
Anton Korobeynikov40d67c52008-02-20 11:22:39 +00009775 return V == (V & ((1LL << 8) - 1));
Evan Cheng2150b922007-03-12 23:30:29 +00009776 }
Evan Cheng10043e22007-01-19 07:51:42 +00009777}
9778
Evan Chengdc49a8d2009-08-14 20:09:37 +00009779bool ARMTargetLowering::isLegalT2ScaledAddressingMode(const AddrMode &AM,
9780 EVT VT) const {
9781 int Scale = AM.Scale;
9782 if (Scale < 0)
9783 return false;
9784
9785 switch (VT.getSimpleVT().SimpleTy) {
9786 default: return false;
9787 case MVT::i1:
9788 case MVT::i8:
9789 case MVT::i16:
9790 case MVT::i32:
9791 if (Scale == 1)
9792 return true;
9793 // r + r << imm
9794 Scale = Scale & ~1;
9795 return Scale == 2 || Scale == 4 || Scale == 8;
9796 case MVT::i64:
9797 // r + r
9798 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
9799 return true;
9800 return false;
9801 case MVT::isVoid:
9802 // Note, we allow "void" uses (basically, uses that aren't loads or
9803 // stores), because arm allows folding a scale into many arithmetic
9804 // operations. This should be made more precise and revisited later.
9805
9806 // Allow r << imm, but the imm has to be a multiple of two.
9807 if (Scale & 1) return false;
9808 return isPowerOf2_32(Scale);
9809 }
9810}
9811
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009812/// isLegalAddressingMode - Return true if the addressing mode represented
9813/// by AM is legal for this target, for a load/store of the specified type.
Bob Wilson7117a912009-03-20 22:42:55 +00009814bool ARMTargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattner229907c2011-07-18 04:54:35 +00009815 Type *Ty) const {
Owen Anderson53aa7a92009-08-10 22:56:29 +00009816 EVT VT = getValueType(Ty, true);
Bob Wilson866c1742009-04-08 17:55:28 +00009817 if (!isLegalAddressImmediate(AM.BaseOffs, VT, Subtarget))
Evan Cheng2150b922007-03-12 23:30:29 +00009818 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009819
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009820 // Can never fold addr of global into load/store.
Bob Wilson7117a912009-03-20 22:42:55 +00009821 if (AM.BaseGV)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009822 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009823
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009824 switch (AM.Scale) {
9825 case 0: // no scale reg, must be "r+i" or "r", or "i".
9826 break;
9827 case 1:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009828 if (Subtarget->isThumb1Only())
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009829 return false;
Chris Lattner502c3f42007-04-13 06:50:55 +00009830 // FALL THROUGH.
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009831 default:
Chris Lattner502c3f42007-04-13 06:50:55 +00009832 // ARM doesn't support any R+R*scale+imm addr modes.
9833 if (AM.BaseOffs)
9834 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009835
Bob Wilson866c1742009-04-08 17:55:28 +00009836 if (!VT.isSimple())
9837 return false;
9838
Evan Chengdc49a8d2009-08-14 20:09:37 +00009839 if (Subtarget->isThumb2())
9840 return isLegalT2ScaledAddressingMode(AM, VT);
9841
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009842 int Scale = AM.Scale;
Owen Anderson9f944592009-08-11 20:47:22 +00009843 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009844 default: return false;
Owen Anderson9f944592009-08-11 20:47:22 +00009845 case MVT::i1:
9846 case MVT::i8:
9847 case MVT::i32:
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009848 if (Scale < 0) Scale = -Scale;
9849 if (Scale == 1)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009850 return true;
9851 // r + r << imm
Chris Lattnerfe926e22007-04-11 16:17:12 +00009852 return isPowerOf2_32(Scale & ~1);
Owen Anderson9f944592009-08-11 20:47:22 +00009853 case MVT::i16:
Evan Chengdc49a8d2009-08-14 20:09:37 +00009854 case MVT::i64:
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009855 // r + r
Chris Lattner9b6d69e2007-04-10 03:48:29 +00009856 if (((unsigned)AM.HasBaseReg + Scale) <= 2)
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009857 return true;
Chris Lattnerfe926e22007-04-11 16:17:12 +00009858 return false;
Bob Wilson7117a912009-03-20 22:42:55 +00009859
Owen Anderson9f944592009-08-11 20:47:22 +00009860 case MVT::isVoid:
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009861 // Note, we allow "void" uses (basically, uses that aren't loads or
9862 // stores), because arm allows folding a scale into many arithmetic
9863 // operations. This should be made more precise and revisited later.
Bob Wilson7117a912009-03-20 22:42:55 +00009864
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009865 // Allow r << imm, but the imm has to be a multiple of two.
Evan Chengdc49a8d2009-08-14 20:09:37 +00009866 if (Scale & 1) return false;
9867 return isPowerOf2_32(Scale);
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009868 }
Evan Cheng2150b922007-03-12 23:30:29 +00009869 }
Chris Lattnerd44e24c2007-04-09 23:33:39 +00009870 return true;
Evan Cheng2150b922007-03-12 23:30:29 +00009871}
9872
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009873/// isLegalICmpImmediate - Return true if the specified immediate is legal
9874/// icmp immediate, that is the target has icmp instructions which can compare
9875/// a register against the immediate without having to materialize the
9876/// immediate into a register.
Evan Cheng15b80e42009-11-12 07:13:11 +00009877bool ARMTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +00009878 // Thumb2 and ARM modes can use cmn for negative immediates.
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009879 if (!Subtarget->isThumb())
Chandler Carruth8a102c22012-04-06 20:10:52 +00009880 return ARM_AM::getSOImmVal(llvm::abs64(Imm)) != -1;
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009881 if (Subtarget->isThumb2())
Chandler Carruth8a102c22012-04-06 20:10:52 +00009882 return ARM_AM::getT2SOImmVal(llvm::abs64(Imm)) != -1;
Jakob Stoklund Olesen967b86a2012-04-06 17:45:04 +00009883 // Thumb1 doesn't have cmn, and only 8-bit immediates.
Evan Cheng15b80e42009-11-12 07:13:11 +00009884 return Imm >= 0 && Imm <= 255;
Evan Cheng3d3c24a2009-11-11 19:05:52 +00009885}
9886
Andrew Tricka22cdb72012-07-18 18:34:27 +00009887/// isLegalAddImmediate - Return true if the specified immediate is a legal add
9888/// *or sub* immediate, that is the target has add or sub instructions which can
9889/// add a register with the immediate without having to materialize the
Dan Gohman6136e942011-05-03 00:46:49 +00009890/// immediate into a register.
9891bool ARMTargetLowering::isLegalAddImmediate(int64_t Imm) const {
Andrew Tricka22cdb72012-07-18 18:34:27 +00009892 // Same encoding for add/sub, just flip the sign.
9893 int64_t AbsImm = llvm::abs64(Imm);
9894 if (!Subtarget->isThumb())
9895 return ARM_AM::getSOImmVal(AbsImm) != -1;
9896 if (Subtarget->isThumb2())
9897 return ARM_AM::getT2SOImmVal(AbsImm) != -1;
9898 // Thumb1 only has 8-bit unsigned immediate.
9899 return AbsImm >= 0 && AbsImm <= 255;
Dan Gohman6136e942011-05-03 00:46:49 +00009900}
9901
Owen Anderson53aa7a92009-08-10 22:56:29 +00009902static bool getARMIndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +00009903 bool isSEXTLoad, SDValue &Base,
9904 SDValue &Offset, bool &isInc,
9905 SelectionDAG &DAG) {
Evan Cheng10043e22007-01-19 07:51:42 +00009906 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9907 return false;
9908
Owen Anderson9f944592009-08-11 20:47:22 +00009909 if (VT == MVT::i16 || ((VT == MVT::i8 || VT == MVT::i1) && isSEXTLoad)) {
Evan Cheng10043e22007-01-19 07:51:42 +00009910 // AddressingMode 3
9911 Base = Ptr->getOperand(0);
9912 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00009913 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +00009914 if (RHSC < 0 && RHSC > -256) {
Evan Cheng84c6cda2009-07-02 07:28:31 +00009915 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +00009916 isInc = false;
9917 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9918 return true;
9919 }
9920 }
9921 isInc = (Ptr->getOpcode() == ISD::ADD);
9922 Offset = Ptr->getOperand(1);
9923 return true;
Owen Anderson9f944592009-08-11 20:47:22 +00009924 } else if (VT == MVT::i32 || VT == MVT::i8 || VT == MVT::i1) {
Evan Cheng10043e22007-01-19 07:51:42 +00009925 // AddressingMode 2
9926 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00009927 int RHSC = (int)RHS->getZExtValue();
Evan Cheng10043e22007-01-19 07:51:42 +00009928 if (RHSC < 0 && RHSC > -0x1000) {
Evan Cheng84c6cda2009-07-02 07:28:31 +00009929 assert(Ptr->getOpcode() == ISD::ADD);
Evan Cheng10043e22007-01-19 07:51:42 +00009930 isInc = false;
9931 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9932 Base = Ptr->getOperand(0);
9933 return true;
9934 }
9935 }
9936
9937 if (Ptr->getOpcode() == ISD::ADD) {
9938 isInc = true;
Evan Chenga20cde32011-07-20 23:34:39 +00009939 ARM_AM::ShiftOpc ShOpcVal=
9940 ARM_AM::getShiftOpcForNode(Ptr->getOperand(0).getOpcode());
Evan Cheng10043e22007-01-19 07:51:42 +00009941 if (ShOpcVal != ARM_AM::no_shift) {
9942 Base = Ptr->getOperand(1);
9943 Offset = Ptr->getOperand(0);
9944 } else {
9945 Base = Ptr->getOperand(0);
9946 Offset = Ptr->getOperand(1);
9947 }
9948 return true;
9949 }
9950
9951 isInc = (Ptr->getOpcode() == ISD::ADD);
9952 Base = Ptr->getOperand(0);
9953 Offset = Ptr->getOperand(1);
9954 return true;
9955 }
9956
Jim Grosbachd7cf55c2009-11-09 00:11:35 +00009957 // FIXME: Use VLDM / VSTM to emulate indexed FP load / store.
Evan Cheng10043e22007-01-19 07:51:42 +00009958 return false;
9959}
9960
Owen Anderson53aa7a92009-08-10 22:56:29 +00009961static bool getT2IndexedAddressParts(SDNode *Ptr, EVT VT,
Evan Cheng84c6cda2009-07-02 07:28:31 +00009962 bool isSEXTLoad, SDValue &Base,
9963 SDValue &Offset, bool &isInc,
9964 SelectionDAG &DAG) {
9965 if (Ptr->getOpcode() != ISD::ADD && Ptr->getOpcode() != ISD::SUB)
9966 return false;
9967
9968 Base = Ptr->getOperand(0);
9969 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ptr->getOperand(1))) {
9970 int RHSC = (int)RHS->getZExtValue();
9971 if (RHSC < 0 && RHSC > -0x100) { // 8 bits.
9972 assert(Ptr->getOpcode() == ISD::ADD);
9973 isInc = false;
9974 Offset = DAG.getConstant(-RHSC, RHS->getValueType(0));
9975 return true;
9976 } else if (RHSC > 0 && RHSC < 0x100) { // 8 bit, no zero.
9977 isInc = Ptr->getOpcode() == ISD::ADD;
9978 Offset = DAG.getConstant(RHSC, RHS->getValueType(0));
9979 return true;
9980 }
9981 }
9982
9983 return false;
9984}
9985
Evan Cheng10043e22007-01-19 07:51:42 +00009986/// getPreIndexedAddressParts - returns true by value, base pointer and
9987/// offset pointer and addressing mode by reference if the node's address
9988/// can be legally represented as pre-indexed load / store address.
9989bool
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009990ARMTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
9991 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +00009992 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +00009993 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +00009994 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +00009995 return false;
9996
Owen Anderson53aa7a92009-08-10 22:56:29 +00009997 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009998 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +00009999 bool isSEXTLoad = false;
10000 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
10001 Ptr = LD->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010002 VT = LD->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010003 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10004 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
10005 Ptr = ST->getBasePtr();
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010006 VT = ST->getMemoryVT();
Evan Cheng10043e22007-01-19 07:51:42 +000010007 } else
10008 return false;
10009
10010 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010011 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010012 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010013 isLegal = getT2IndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
10014 Offset, isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010015 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010016 isLegal = getARMIndexedAddressParts(Ptr.getNode(), VT, isSEXTLoad, Base,
Evan Cheng844f0b42009-07-02 06:44:30 +000010017 Offset, isInc, DAG);
Evan Cheng84c6cda2009-07-02 07:28:31 +000010018 if (!isLegal)
10019 return false;
10020
10021 AM = isInc ? ISD::PRE_INC : ISD::PRE_DEC;
10022 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010023}
10024
10025/// getPostIndexedAddressParts - returns true by value, base pointer and
10026/// offset pointer and addressing mode by reference if this node can be
10027/// combined with a load / store to form a post-indexed load / store.
10028bool ARMTargetLowering::getPostIndexedAddressParts(SDNode *N, SDNode *Op,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010029 SDValue &Base,
10030 SDValue &Offset,
Evan Cheng10043e22007-01-19 07:51:42 +000010031 ISD::MemIndexedMode &AM,
Dan Gohman02b93132009-01-15 16:29:45 +000010032 SelectionDAG &DAG) const {
Evan Cheng84c6cda2009-07-02 07:28:31 +000010033 if (Subtarget->isThumb1Only())
Evan Cheng10043e22007-01-19 07:51:42 +000010034 return false;
10035
Owen Anderson53aa7a92009-08-10 22:56:29 +000010036 EVT VT;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010037 SDValue Ptr;
Evan Cheng10043e22007-01-19 07:51:42 +000010038 bool isSEXTLoad = false;
10039 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010040 VT = LD->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010041 Ptr = LD->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010042 isSEXTLoad = LD->getExtensionType() == ISD::SEXTLOAD;
10043 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +000010044 VT = ST->getMemoryVT();
Evan Chengf19384d2010-05-18 21:31:17 +000010045 Ptr = ST->getBasePtr();
Evan Cheng10043e22007-01-19 07:51:42 +000010046 } else
10047 return false;
10048
10049 bool isInc;
Evan Cheng84c6cda2009-07-02 07:28:31 +000010050 bool isLegal = false;
Evan Chengdc49a8d2009-08-14 20:09:37 +000010051 if (Subtarget->isThumb2())
Evan Cheng84c6cda2009-07-02 07:28:31 +000010052 isLegal = getT2IndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
Evan Chengf19384d2010-05-18 21:31:17 +000010053 isInc, DAG);
Jim Grosbachf24f9d92009-08-11 15:33:49 +000010054 else
Evan Cheng84c6cda2009-07-02 07:28:31 +000010055 isLegal = getARMIndexedAddressParts(Op, VT, isSEXTLoad, Base, Offset,
10056 isInc, DAG);
10057 if (!isLegal)
10058 return false;
10059
Evan Chengf19384d2010-05-18 21:31:17 +000010060 if (Ptr != Base) {
10061 // Swap base ptr and offset to catch more post-index load / store when
10062 // it's legal. In Thumb2 mode, offset must be an immediate.
10063 if (Ptr == Offset && Op->getOpcode() == ISD::ADD &&
10064 !Subtarget->isThumb2())
10065 std::swap(Base, Offset);
10066
10067 // Post-indexed load / store update the base pointer.
10068 if (Ptr != Base)
10069 return false;
10070 }
10071
Evan Cheng84c6cda2009-07-02 07:28:31 +000010072 AM = isInc ? ISD::POST_INC : ISD::POST_DEC;
10073 return true;
Evan Cheng10043e22007-01-19 07:51:42 +000010074}
10075
Jay Foada0653a32014-05-14 21:14:37 +000010076void ARMTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
10077 APInt &KnownZero,
10078 APInt &KnownOne,
10079 const SelectionDAG &DAG,
10080 unsigned Depth) const {
Michael Gottesman696e44e2013-06-18 20:49:45 +000010081 unsigned BitWidth = KnownOne.getBitWidth();
10082 KnownZero = KnownOne = APInt(BitWidth, 0);
Evan Cheng10043e22007-01-19 07:51:42 +000010083 switch (Op.getOpcode()) {
10084 default: break;
Michael Gottesman696e44e2013-06-18 20:49:45 +000010085 case ARMISD::ADDC:
10086 case ARMISD::ADDE:
10087 case ARMISD::SUBC:
10088 case ARMISD::SUBE:
10089 // These nodes' second result is a boolean
10090 if (Op.getResNo() == 0)
10091 break;
10092 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
10093 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010094 case ARMISD::CMOV: {
10095 // Bits are known zero/one if known on the LHS and RHS.
Jay Foada0653a32014-05-14 21:14:37 +000010096 DAG.computeKnownBits(Op.getOperand(0), KnownZero, KnownOne, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010097 if (KnownZero == 0 && KnownOne == 0) return;
10098
Dan Gohmanf990faf2008-02-13 00:35:47 +000010099 APInt KnownZeroRHS, KnownOneRHS;
Jay Foada0653a32014-05-14 21:14:37 +000010100 DAG.computeKnownBits(Op.getOperand(1), KnownZeroRHS, KnownOneRHS, Depth+1);
Evan Cheng10043e22007-01-19 07:51:42 +000010101 KnownZero &= KnownZeroRHS;
10102 KnownOne &= KnownOneRHS;
10103 return;
10104 }
Tim Northover01b4aa92014-04-03 15:10:35 +000010105 case ISD::INTRINSIC_W_CHAIN: {
10106 ConstantSDNode *CN = cast<ConstantSDNode>(Op->getOperand(1));
10107 Intrinsic::ID IntID = static_cast<Intrinsic::ID>(CN->getZExtValue());
10108 switch (IntID) {
10109 default: return;
10110 case Intrinsic::arm_ldaex:
10111 case Intrinsic::arm_ldrex: {
10112 EVT VT = cast<MemIntrinsicSDNode>(Op)->getMemoryVT();
10113 unsigned MemBits = VT.getScalarType().getSizeInBits();
10114 KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - MemBits);
10115 return;
10116 }
10117 }
10118 }
Evan Cheng10043e22007-01-19 07:51:42 +000010119 }
10120}
10121
10122//===----------------------------------------------------------------------===//
10123// ARM Inline Assembly Support
10124//===----------------------------------------------------------------------===//
10125
Evan Cheng078b0b02011-01-08 01:24:27 +000010126bool ARMTargetLowering::ExpandInlineAsm(CallInst *CI) const {
10127 // Looking for "rev" which is V6+.
10128 if (!Subtarget->hasV6Ops())
10129 return false;
10130
10131 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10132 std::string AsmStr = IA->getAsmString();
10133 SmallVector<StringRef, 4> AsmPieces;
10134 SplitString(AsmStr, AsmPieces, ";\n");
10135
10136 switch (AsmPieces.size()) {
10137 default: return false;
10138 case 1:
10139 AsmStr = AsmPieces[0];
10140 AsmPieces.clear();
10141 SplitString(AsmStr, AsmPieces, " \t,");
10142
10143 // rev $0, $1
10144 if (AsmPieces.size() == 3 &&
10145 AsmPieces[0] == "rev" && AsmPieces[1] == "$0" && AsmPieces[2] == "$1" &&
10146 IA->getConstraintString().compare(0, 4, "=l,l") == 0) {
Chris Lattner229907c2011-07-18 04:54:35 +000010147 IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
Evan Cheng078b0b02011-01-08 01:24:27 +000010148 if (Ty && Ty->getBitWidth() == 32)
10149 return IntrinsicLowering::LowerToByteSwap(CI);
10150 }
10151 break;
10152 }
10153
10154 return false;
10155}
10156
Evan Cheng10043e22007-01-19 07:51:42 +000010157/// getConstraintType - Given a constraint letter, return the type of
10158/// constraint it is for this target.
10159ARMTargetLowering::ConstraintType
Chris Lattnerd6855142007-03-25 02:14:49 +000010160ARMTargetLowering::getConstraintType(const std::string &Constraint) const {
10161 if (Constraint.size() == 1) {
10162 switch (Constraint[0]) {
10163 default: break;
10164 case 'l': return C_RegisterClass;
Chris Lattner6223e832007-04-02 17:24:08 +000010165 case 'w': return C_RegisterClass;
Eric Christopherf45daac2011-06-30 23:23:01 +000010166 case 'h': return C_RegisterClass;
Eric Christopherf1c74592011-07-01 00:14:47 +000010167 case 'x': return C_RegisterClass;
Eric Christopherc011d312011-07-01 00:30:46 +000010168 case 't': return C_RegisterClass;
Eric Christopher29f1db82011-07-01 01:00:07 +000010169 case 'j': return C_Other; // Constant for movw.
Eric Christopheraa503002011-07-29 21:18:58 +000010170 // An address with a single base register. Due to the way we
10171 // currently handle addresses it is the same as an 'r' memory constraint.
10172 case 'Q': return C_Memory;
Chris Lattnerd6855142007-03-25 02:14:49 +000010173 }
Eric Christophere256cd02011-06-21 22:10:57 +000010174 } else if (Constraint.size() == 2) {
10175 switch (Constraint[0]) {
10176 default: break;
10177 // All 'U+' constraints are addresses.
10178 case 'U': return C_Memory;
10179 }
Evan Cheng10043e22007-01-19 07:51:42 +000010180 }
Chris Lattnerd6855142007-03-25 02:14:49 +000010181 return TargetLowering::getConstraintType(Constraint);
Evan Cheng10043e22007-01-19 07:51:42 +000010182}
10183
John Thompsone8360b72010-10-29 17:29:13 +000010184/// Examine constraint type and operand type and determine a weight value.
10185/// This object must already have been set up with the operand type
10186/// and the current alternative constraint selected.
10187TargetLowering::ConstraintWeight
10188ARMTargetLowering::getSingleConstraintMatchWeight(
10189 AsmOperandInfo &info, const char *constraint) const {
10190 ConstraintWeight weight = CW_Invalid;
10191 Value *CallOperandVal = info.CallOperandVal;
10192 // If we don't have a value, we can't do a match,
10193 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +000010194 if (!CallOperandVal)
John Thompsone8360b72010-10-29 17:29:13 +000010195 return CW_Default;
Chris Lattner229907c2011-07-18 04:54:35 +000010196 Type *type = CallOperandVal->getType();
John Thompsone8360b72010-10-29 17:29:13 +000010197 // Look at the constraint type.
10198 switch (*constraint) {
10199 default:
10200 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
10201 break;
10202 case 'l':
10203 if (type->isIntegerTy()) {
10204 if (Subtarget->isThumb())
10205 weight = CW_SpecificReg;
10206 else
10207 weight = CW_Register;
10208 }
10209 break;
10210 case 'w':
10211 if (type->isFloatingPointTy())
10212 weight = CW_Register;
10213 break;
10214 }
10215 return weight;
10216}
10217
Eric Christophercf2007c2011-06-30 23:50:52 +000010218typedef std::pair<unsigned, const TargetRegisterClass*> RCPair;
10219RCPair
Evan Cheng10043e22007-01-19 07:51:42 +000010220ARMTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +000010221 MVT VT) const {
Evan Cheng10043e22007-01-19 07:51:42 +000010222 if (Constraint.size() == 1) {
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010223 // GCC ARM Constraint Letters
Evan Cheng10043e22007-01-19 07:51:42 +000010224 switch (Constraint[0]) {
Eric Christopherf45daac2011-06-30 23:23:01 +000010225 case 'l': // Low regs or general regs.
Jakob Stoklund Olesen0ca14e42010-01-14 18:19:56 +000010226 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010227 return RCPair(0U, &ARM::tGPRRegClass);
10228 return RCPair(0U, &ARM::GPRRegClass);
Eric Christopherf45daac2011-06-30 23:23:01 +000010229 case 'h': // High regs or no regs.
10230 if (Subtarget->isThumb())
Craig Topperc7242e02012-04-20 07:30:17 +000010231 return RCPair(0U, &ARM::hGPRRegClass);
Eric Christopherf09b0f12011-07-01 00:19:27 +000010232 break;
Chris Lattner6223e832007-04-02 17:24:08 +000010233 case 'r':
Craig Topperc7242e02012-04-20 07:30:17 +000010234 return RCPair(0U, &ARM::GPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010235 case 'w':
Tim Northover28adfbb2013-11-14 17:15:39 +000010236 if (VT == MVT::Other)
10237 break;
Owen Anderson9f944592009-08-11 20:47:22 +000010238 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010239 return RCPair(0U, &ARM::SPRRegClass);
Bob Wilson3152b0472009-12-18 01:03:29 +000010240 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010241 return RCPair(0U, &ARM::DPRRegClass);
Evan Cheng0c2544f2009-12-08 23:06:22 +000010242 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010243 return RCPair(0U, &ARM::QPRRegClass);
Chris Lattner6223e832007-04-02 17:24:08 +000010244 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010245 case 'x':
Tim Northover28adfbb2013-11-14 17:15:39 +000010246 if (VT == MVT::Other)
10247 break;
Eric Christopherf1c74592011-07-01 00:14:47 +000010248 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010249 return RCPair(0U, &ARM::SPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010250 if (VT.getSizeInBits() == 64)
Craig Topperc7242e02012-04-20 07:30:17 +000010251 return RCPair(0U, &ARM::DPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010252 if (VT.getSizeInBits() == 128)
Craig Topperc7242e02012-04-20 07:30:17 +000010253 return RCPair(0U, &ARM::QPR_8RegClass);
Eric Christopherf1c74592011-07-01 00:14:47 +000010254 break;
Eric Christopherc011d312011-07-01 00:30:46 +000010255 case 't':
10256 if (VT == MVT::f32)
Craig Topperc7242e02012-04-20 07:30:17 +000010257 return RCPair(0U, &ARM::SPRRegClass);
Eric Christopherc011d312011-07-01 00:30:46 +000010258 break;
Evan Cheng10043e22007-01-19 07:51:42 +000010259 }
10260 }
Bob Wilson3f2293b2010-03-15 23:09:18 +000010261 if (StringRef("{cc}").equals_lower(Constraint))
Craig Topperc7242e02012-04-20 07:30:17 +000010262 return std::make_pair(unsigned(ARM::CPSR), &ARM::CCRRegClass);
Bob Wilson3f2293b2010-03-15 23:09:18 +000010263
Evan Cheng10043e22007-01-19 07:51:42 +000010264 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
10265}
10266
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010267/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10268/// vector. If it is invalid, don't add anything to Ops.
10269void ARMTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Eric Christopherde9399b2011-06-02 23:16:42 +000010270 std::string &Constraint,
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010271 std::vector<SDValue>&Ops,
10272 SelectionDAG &DAG) const {
Craig Topper062a2ba2014-04-25 05:30:21 +000010273 SDValue Result;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010274
Eric Christopherde9399b2011-06-02 23:16:42 +000010275 // Currently only support length 1 constraints.
10276 if (Constraint.length() != 1) return;
Eric Christopher0713a9d2011-06-08 23:55:35 +000010277
Eric Christopherde9399b2011-06-02 23:16:42 +000010278 char ConstraintLetter = Constraint[0];
10279 switch (ConstraintLetter) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010280 default: break;
Eric Christopher29f1db82011-07-01 01:00:07 +000010281 case 'j':
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010282 case 'I': case 'J': case 'K': case 'L':
10283 case 'M': case 'N': case 'O':
10284 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
10285 if (!C)
10286 return;
10287
10288 int64_t CVal64 = C->getSExtValue();
10289 int CVal = (int) CVal64;
10290 // None of these constraints allow values larger than 32 bits. Check
10291 // that the value fits in an int.
10292 if (CVal != CVal64)
10293 return;
10294
Eric Christopherde9399b2011-06-02 23:16:42 +000010295 switch (ConstraintLetter) {
Eric Christopher29f1db82011-07-01 01:00:07 +000010296 case 'j':
Andrew Trick53df4b62011-09-20 03:06:13 +000010297 // Constant suitable for movw, must be between 0 and
10298 // 65535.
10299 if (Subtarget->hasV6T2Ops())
10300 if (CVal >= 0 && CVal <= 65535)
10301 break;
10302 return;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010303 case 'I':
David Goodwin22c2fba2009-07-08 23:10:31 +000010304 if (Subtarget->isThumb1Only()) {
10305 // This must be a constant between 0 and 255, for ADD
10306 // immediates.
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010307 if (CVal >= 0 && CVal <= 255)
10308 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010309 } else if (Subtarget->isThumb2()) {
10310 // A constant that can be used as an immediate value in a
10311 // data-processing instruction.
10312 if (ARM_AM::getT2SOImmVal(CVal) != -1)
10313 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010314 } else {
10315 // A constant that can be used as an immediate value in a
10316 // data-processing instruction.
10317 if (ARM_AM::getSOImmVal(CVal) != -1)
10318 break;
10319 }
10320 return;
10321
10322 case 'J':
David Goodwin22c2fba2009-07-08 23:10:31 +000010323 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010324 // This must be a constant between -255 and -1, for negated ADD
10325 // immediates. This can be used in GCC with an "n" modifier that
10326 // prints the negated value, for use with SUB instructions. It is
10327 // not useful otherwise but is implemented for compatibility.
10328 if (CVal >= -255 && CVal <= -1)
10329 break;
10330 } else {
10331 // This must be a constant between -4095 and 4095. It is not clear
10332 // what this constraint is intended for. Implemented for
10333 // compatibility with GCC.
10334 if (CVal >= -4095 && CVal <= 4095)
10335 break;
10336 }
10337 return;
10338
10339 case 'K':
David Goodwin22c2fba2009-07-08 23:10:31 +000010340 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010341 // A 32-bit value where only one byte has a nonzero value. Exclude
10342 // zero to match GCC. This constraint is used by GCC internally for
10343 // constants that can be loaded with a move/shift combination.
10344 // It is not useful otherwise but is implemented for compatibility.
10345 if (CVal != 0 && ARM_AM::isThumbImmShiftedVal(CVal))
10346 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010347 } else if (Subtarget->isThumb2()) {
10348 // A constant whose bitwise inverse can be used as an immediate
10349 // value in a data-processing instruction. This can be used in GCC
10350 // with a "B" modifier that prints the inverted value, for use with
10351 // BIC and MVN instructions. It is not useful otherwise but is
10352 // implemented for compatibility.
10353 if (ARM_AM::getT2SOImmVal(~CVal) != -1)
10354 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010355 } else {
10356 // A constant whose bitwise inverse can be used as an immediate
10357 // value in a data-processing instruction. This can be used in GCC
10358 // with a "B" modifier that prints the inverted value, for use with
10359 // BIC and MVN instructions. It is not useful otherwise but is
10360 // implemented for compatibility.
10361 if (ARM_AM::getSOImmVal(~CVal) != -1)
10362 break;
10363 }
10364 return;
10365
10366 case 'L':
David Goodwin22c2fba2009-07-08 23:10:31 +000010367 if (Subtarget->isThumb1Only()) {
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010368 // This must be a constant between -7 and 7,
10369 // for 3-operand ADD/SUB immediate instructions.
10370 if (CVal >= -7 && CVal < 7)
10371 break;
David Goodwin22c2fba2009-07-08 23:10:31 +000010372 } else if (Subtarget->isThumb2()) {
10373 // A constant whose negation can be used as an immediate value in a
10374 // data-processing instruction. This can be used in GCC with an "n"
10375 // modifier that prints the negated value, for use with SUB
10376 // instructions. It is not useful otherwise but is implemented for
10377 // compatibility.
10378 if (ARM_AM::getT2SOImmVal(-CVal) != -1)
10379 break;
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010380 } else {
10381 // A constant whose negation can be used as an immediate value in a
10382 // data-processing instruction. This can be used in GCC with an "n"
10383 // modifier that prints the negated value, for use with SUB
10384 // instructions. It is not useful otherwise but is implemented for
10385 // compatibility.
10386 if (ARM_AM::getSOImmVal(-CVal) != -1)
10387 break;
10388 }
10389 return;
10390
10391 case 'M':
David Goodwin22c2fba2009-07-08 23:10:31 +000010392 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010393 // This must be a multiple of 4 between 0 and 1020, for
10394 // ADD sp + immediate.
10395 if ((CVal >= 0 && CVal <= 1020) && ((CVal & 3) == 0))
10396 break;
10397 } else {
10398 // A power of two or a constant between 0 and 32. This is used in
10399 // GCC for the shift amount on shifted register operands, but it is
10400 // useful in general for any shift amounts.
10401 if ((CVal >= 0 && CVal <= 32) || ((CVal & (CVal - 1)) == 0))
10402 break;
10403 }
10404 return;
10405
10406 case 'N':
David Goodwin22c2fba2009-07-08 23:10:31 +000010407 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010408 // This must be a constant between 0 and 31, for shift amounts.
10409 if (CVal >= 0 && CVal <= 31)
10410 break;
10411 }
10412 return;
10413
10414 case 'O':
David Goodwin22c2fba2009-07-08 23:10:31 +000010415 if (Subtarget->isThumb()) { // FIXME thumb2
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010416 // This must be a multiple of 4 between -508 and 508, for
10417 // ADD/SUB sp = sp + immediate.
10418 if ((CVal >= -508 && CVal <= 508) && ((CVal & 3) == 0))
10419 break;
10420 }
10421 return;
10422 }
10423 Result = DAG.getTargetConstant(CVal, Op.getValueType());
10424 break;
10425 }
10426
10427 if (Result.getNode()) {
10428 Ops.push_back(Result);
10429 return;
10430 }
Dale Johannesence97d552010-06-25 21:55:36 +000010431 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Bob Wilsoncf1ec2c2009-04-01 17:58:54 +000010432}
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010433
Renato Golin87610692013-07-16 09:32:17 +000010434SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
10435 assert(Subtarget->isTargetAEABI() && "Register-based DivRem lowering only");
10436 unsigned Opcode = Op->getOpcode();
10437 assert((Opcode == ISD::SDIVREM || Opcode == ISD::UDIVREM) &&
10438 "Invalid opcode for Div/Rem lowering");
10439 bool isSigned = (Opcode == ISD::SDIVREM);
10440 EVT VT = Op->getValueType(0);
10441 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
10442
10443 RTLIB::Libcall LC;
10444 switch (VT.getSimpleVT().SimpleTy) {
10445 default: llvm_unreachable("Unexpected request for libcall!");
10446 case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break;
10447 case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
10448 case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
10449 case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
10450 }
10451
10452 SDValue InChain = DAG.getEntryNode();
10453
10454 TargetLowering::ArgListTy Args;
10455 TargetLowering::ArgListEntry Entry;
10456 for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
10457 EVT ArgVT = Op->getOperand(i).getValueType();
10458 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
10459 Entry.Node = Op->getOperand(i);
10460 Entry.Ty = ArgTy;
10461 Entry.isSExt = isSigned;
10462 Entry.isZExt = !isSigned;
10463 Args.push_back(Entry);
10464 }
10465
10466 SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
10467 getPointerTy());
10468
10469 Type *RetTy = (Type*)StructType::get(Ty, Ty, NULL);
10470
10471 SDLoc dl(Op);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010472 TargetLowering::CallLoweringInfo CLI(DAG);
10473 CLI.setDebugLoc(dl).setChain(InChain)
10474 .setCallee(getLibcallCallingConv(LC), RetTy, Callee, &Args, 0)
10475 .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
Renato Golin87610692013-07-16 09:32:17 +000010476
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000010477 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
Renato Golin87610692013-07-16 09:32:17 +000010478 return CallInfo.first;
10479}
10480
Anton Korobeynikov29a44df2009-09-23 19:04:09 +000010481bool
10482ARMTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
10483 // The ARM target isn't yet aware of offsets.
10484 return false;
10485}
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010486
Jim Grosbach11013ed2010-07-16 23:05:05 +000010487bool ARM::isBitFieldInvertedMask(unsigned v) {
10488 if (v == 0xffffffff)
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010489 return false;
10490
Jim Grosbach11013ed2010-07-16 23:05:05 +000010491 // there can be 1's on either or both "outsides", all the "inside"
10492 // bits must be 0's
Benjamin Kramer8bad66e2013-05-19 22:01:57 +000010493 unsigned TO = CountTrailingOnes_32(v);
10494 unsigned LO = CountLeadingOnes_32(v);
10495 v = (v >> TO) << TO;
10496 v = (v << LO) >> LO;
10497 return v == 0;
Jim Grosbach11013ed2010-07-16 23:05:05 +000010498}
10499
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010500/// isFPImmLegal - Returns true if the target can instruction select the
10501/// specified FP immediate natively. If false, the legalizer will
10502/// materialize the FP immediate as a load from a constant pool.
10503bool ARMTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
10504 if (!Subtarget->hasVFP3())
10505 return false;
10506 if (VT == MVT::f32)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010507 return ARM_AM::getFP32Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010508 if (VT == MVT::f64)
Jim Grosbachefc761a2011-09-30 00:50:06 +000010509 return ARM_AM::getFP64Imm(Imm) != -1;
Evan Cheng4a609f3c2009-10-28 01:44:26 +000010510 return false;
10511}
Bob Wilson5549d492010-09-21 17:56:22 +000010512
Wesley Peck527da1b2010-11-23 03:31:01 +000010513/// getTgtMemIntrinsic - Represent NEON load and store intrinsics as
Bob Wilson5549d492010-09-21 17:56:22 +000010514/// MemIntrinsicNodes. The associated MachineMemOperands record the alignment
10515/// specified in the intrinsic calls.
10516bool ARMTargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
10517 const CallInst &I,
10518 unsigned Intrinsic) const {
10519 switch (Intrinsic) {
10520 case Intrinsic::arm_neon_vld1:
10521 case Intrinsic::arm_neon_vld2:
10522 case Intrinsic::arm_neon_vld3:
10523 case Intrinsic::arm_neon_vld4:
10524 case Intrinsic::arm_neon_vld2lane:
10525 case Intrinsic::arm_neon_vld3lane:
10526 case Intrinsic::arm_neon_vld4lane: {
10527 Info.opc = ISD::INTRINSIC_W_CHAIN;
10528 // Conservatively set memVT to the entire set of vectors loaded.
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010529 uint64_t NumElts = getDataLayout()->getTypeAllocSize(I.getType()) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010530 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10531 Info.ptrVal = I.getArgOperand(0);
10532 Info.offset = 0;
10533 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10534 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10535 Info.vol = false; // volatile loads with NEON intrinsics not supported
10536 Info.readMem = true;
10537 Info.writeMem = false;
10538 return true;
10539 }
10540 case Intrinsic::arm_neon_vst1:
10541 case Intrinsic::arm_neon_vst2:
10542 case Intrinsic::arm_neon_vst3:
10543 case Intrinsic::arm_neon_vst4:
10544 case Intrinsic::arm_neon_vst2lane:
10545 case Intrinsic::arm_neon_vst3lane:
10546 case Intrinsic::arm_neon_vst4lane: {
10547 Info.opc = ISD::INTRINSIC_VOID;
10548 // Conservatively set memVT to the entire set of vectors stored.
10549 unsigned NumElts = 0;
10550 for (unsigned ArgI = 1, ArgE = I.getNumArgOperands(); ArgI < ArgE; ++ArgI) {
Chris Lattner229907c2011-07-18 04:54:35 +000010551 Type *ArgTy = I.getArgOperand(ArgI)->getType();
Bob Wilson5549d492010-09-21 17:56:22 +000010552 if (!ArgTy->isVectorTy())
10553 break;
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010554 NumElts += getDataLayout()->getTypeAllocSize(ArgTy) / 8;
Bob Wilson5549d492010-09-21 17:56:22 +000010555 }
10556 Info.memVT = EVT::getVectorVT(I.getType()->getContext(), MVT::i64, NumElts);
10557 Info.ptrVal = I.getArgOperand(0);
10558 Info.offset = 0;
10559 Value *AlignArg = I.getArgOperand(I.getNumArgOperands() - 1);
10560 Info.align = cast<ConstantInt>(AlignArg)->getZExtValue();
10561 Info.vol = false; // volatile stores with NEON intrinsics not supported
10562 Info.readMem = false;
10563 Info.writeMem = true;
10564 return true;
10565 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010566 case Intrinsic::arm_ldaex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010567 case Intrinsic::arm_ldrex: {
10568 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(0)->getType());
10569 Info.opc = ISD::INTRINSIC_W_CHAIN;
10570 Info.memVT = MVT::getVT(PtrTy->getElementType());
10571 Info.ptrVal = I.getArgOperand(0);
10572 Info.offset = 0;
10573 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10574 Info.vol = true;
10575 Info.readMem = true;
10576 Info.writeMem = false;
10577 return true;
10578 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010579 case Intrinsic::arm_stlex:
Tim Northovera7ecd242013-07-16 09:46:55 +000010580 case Intrinsic::arm_strex: {
10581 PointerType *PtrTy = cast<PointerType>(I.getArgOperand(1)->getType());
10582 Info.opc = ISD::INTRINSIC_W_CHAIN;
10583 Info.memVT = MVT::getVT(PtrTy->getElementType());
10584 Info.ptrVal = I.getArgOperand(1);
10585 Info.offset = 0;
10586 Info.align = getDataLayout()->getABITypeAlignment(PtrTy->getElementType());
10587 Info.vol = true;
10588 Info.readMem = false;
10589 Info.writeMem = true;
10590 return true;
10591 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010592 case Intrinsic::arm_stlexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010593 case Intrinsic::arm_strexd: {
10594 Info.opc = ISD::INTRINSIC_W_CHAIN;
10595 Info.memVT = MVT::i64;
10596 Info.ptrVal = I.getArgOperand(2);
10597 Info.offset = 0;
10598 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010599 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010600 Info.readMem = false;
10601 Info.writeMem = true;
10602 return true;
10603 }
Tim Northover1ff5f292014-03-26 14:39:31 +000010604 case Intrinsic::arm_ldaexd:
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010605 case Intrinsic::arm_ldrexd: {
10606 Info.opc = ISD::INTRINSIC_W_CHAIN;
10607 Info.memVT = MVT::i64;
10608 Info.ptrVal = I.getArgOperand(0);
10609 Info.offset = 0;
10610 Info.align = 8;
Bruno Cardoso Lopesd66ab9e2011-06-16 18:11:32 +000010611 Info.vol = true;
Bruno Cardoso Lopes325110f2011-05-28 04:07:29 +000010612 Info.readMem = true;
10613 Info.writeMem = false;
10614 return true;
10615 }
Bob Wilson5549d492010-09-21 17:56:22 +000010616 default:
10617 break;
10618 }
10619
10620 return false;
10621}
Juergen Ributzka659ce002014-01-28 01:20:14 +000010622
10623/// \brief Returns true if it is beneficial to convert a load of a constant
10624/// to just the constant itself.
10625bool ARMTargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
10626 Type *Ty) const {
10627 assert(Ty->isIntegerTy());
10628
10629 unsigned Bits = Ty->getPrimitiveSizeInBits();
10630 if (Bits == 0 || Bits > 32)
10631 return false;
10632 return true;
10633}
Tim Northover037f26f22014-04-17 18:22:47 +000010634
10635bool ARMTargetLowering::shouldExpandAtomicInIR(Instruction *Inst) const {
10636 // Loads and stores less than 64-bits are already atomic; ones above that
10637 // are doomed anyway, so defer to the default libcall and blame the OS when
10638 // things go wrong:
10639 if (StoreInst *SI = dyn_cast<StoreInst>(Inst))
10640 return SI->getValueOperand()->getType()->getPrimitiveSizeInBits() == 64;
10641 else if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
10642 return LI->getType()->getPrimitiveSizeInBits() == 64;
10643
10644 // For the real atomic operations, we have ldrex/strex up to 64 bits.
10645 return Inst->getType()->getPrimitiveSizeInBits() <= 64;
10646}
10647
10648Value *ARMTargetLowering::emitLoadLinked(IRBuilder<> &Builder, Value *Addr,
10649 AtomicOrdering Ord) const {
10650 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
10651 Type *ValTy = cast<PointerType>(Addr->getType())->getElementType();
10652 bool IsAcquire =
10653 Ord == Acquire || Ord == AcquireRelease || Ord == SequentiallyConsistent;
10654
10655 // Since i64 isn't legal and intrinsics don't get type-lowered, the ldrexd
10656 // intrinsic must return {i32, i32} and we have to recombine them into a
10657 // single i64 here.
10658 if (ValTy->getPrimitiveSizeInBits() == 64) {
10659 Intrinsic::ID Int =
10660 IsAcquire ? Intrinsic::arm_ldaexd : Intrinsic::arm_ldrexd;
10661 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int);
10662
10663 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
10664 Value *LoHi = Builder.CreateCall(Ldrex, Addr, "lohi");
10665
10666 Value *Lo = Builder.CreateExtractValue(LoHi, 0, "lo");
10667 Value *Hi = Builder.CreateExtractValue(LoHi, 1, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000010668 if (!Subtarget->isLittle())
10669 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000010670 Lo = Builder.CreateZExt(Lo, ValTy, "lo64");
10671 Hi = Builder.CreateZExt(Hi, ValTy, "hi64");
10672 return Builder.CreateOr(
10673 Lo, Builder.CreateShl(Hi, ConstantInt::get(ValTy, 32)), "val64");
10674 }
10675
10676 Type *Tys[] = { Addr->getType() };
10677 Intrinsic::ID Int = IsAcquire ? Intrinsic::arm_ldaex : Intrinsic::arm_ldrex;
10678 Function *Ldrex = llvm::Intrinsic::getDeclaration(M, Int, Tys);
10679
10680 return Builder.CreateTruncOrBitCast(
10681 Builder.CreateCall(Ldrex, Addr),
10682 cast<PointerType>(Addr->getType())->getElementType());
10683}
10684
10685Value *ARMTargetLowering::emitStoreConditional(IRBuilder<> &Builder, Value *Val,
10686 Value *Addr,
10687 AtomicOrdering Ord) const {
10688 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
10689 bool IsRelease =
10690 Ord == Release || Ord == AcquireRelease || Ord == SequentiallyConsistent;
10691
10692 // Since the intrinsics must have legal type, the i64 intrinsics take two
10693 // parameters: "i32, i32". We must marshal Val into the appropriate form
10694 // before the call.
10695 if (Val->getType()->getPrimitiveSizeInBits() == 64) {
10696 Intrinsic::ID Int =
10697 IsRelease ? Intrinsic::arm_stlexd : Intrinsic::arm_strexd;
10698 Function *Strex = Intrinsic::getDeclaration(M, Int);
10699 Type *Int32Ty = Type::getInt32Ty(M->getContext());
10700
10701 Value *Lo = Builder.CreateTrunc(Val, Int32Ty, "lo");
10702 Value *Hi = Builder.CreateTrunc(Builder.CreateLShr(Val, 32), Int32Ty, "hi");
Christian Pirkerb5728192014-05-08 14:06:24 +000010703 if (!Subtarget->isLittle())
10704 std::swap (Lo, Hi);
Tim Northover037f26f22014-04-17 18:22:47 +000010705 Addr = Builder.CreateBitCast(Addr, Type::getInt8PtrTy(M->getContext()));
10706 return Builder.CreateCall3(Strex, Lo, Hi, Addr);
10707 }
10708
10709 Intrinsic::ID Int = IsRelease ? Intrinsic::arm_stlex : Intrinsic::arm_strex;
10710 Type *Tys[] = { Addr->getType() };
10711 Function *Strex = Intrinsic::getDeclaration(M, Int, Tys);
10712
10713 return Builder.CreateCall2(
10714 Strex, Builder.CreateZExtOrBitCast(
10715 Val, Strex->getFunctionType()->getParamType(0)),
10716 Addr);
10717}
Oliver Stannardc24f2172014-05-09 14:01:47 +000010718
10719enum HABaseType {
10720 HA_UNKNOWN = 0,
10721 HA_FLOAT,
10722 HA_DOUBLE,
10723 HA_VECT64,
10724 HA_VECT128
10725};
10726
10727static bool isHomogeneousAggregate(Type *Ty, HABaseType &Base,
10728 uint64_t &Members) {
10729 if (const StructType *ST = dyn_cast<StructType>(Ty)) {
10730 for (unsigned i = 0; i < ST->getNumElements(); ++i) {
10731 uint64_t SubMembers = 0;
10732 if (!isHomogeneousAggregate(ST->getElementType(i), Base, SubMembers))
10733 return false;
10734 Members += SubMembers;
10735 }
10736 } else if (const ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
10737 uint64_t SubMembers = 0;
10738 if (!isHomogeneousAggregate(AT->getElementType(), Base, SubMembers))
10739 return false;
10740 Members += SubMembers * AT->getNumElements();
10741 } else if (Ty->isFloatTy()) {
10742 if (Base != HA_UNKNOWN && Base != HA_FLOAT)
10743 return false;
10744 Members = 1;
10745 Base = HA_FLOAT;
10746 } else if (Ty->isDoubleTy()) {
10747 if (Base != HA_UNKNOWN && Base != HA_DOUBLE)
10748 return false;
10749 Members = 1;
10750 Base = HA_DOUBLE;
10751 } else if (const VectorType *VT = dyn_cast<VectorType>(Ty)) {
10752 Members = 1;
10753 switch (Base) {
10754 case HA_FLOAT:
10755 case HA_DOUBLE:
10756 return false;
10757 case HA_VECT64:
10758 return VT->getBitWidth() == 64;
10759 case HA_VECT128:
10760 return VT->getBitWidth() == 128;
10761 case HA_UNKNOWN:
10762 switch (VT->getBitWidth()) {
10763 case 64:
10764 Base = HA_VECT64;
10765 return true;
10766 case 128:
10767 Base = HA_VECT128;
10768 return true;
10769 default:
10770 return false;
10771 }
10772 }
10773 }
10774
10775 return (Members > 0 && Members <= 4);
10776}
10777
10778/// \brief Return true if a type is an AAPCS-VFP homogeneous aggregate.
10779bool ARMTargetLowering::functionArgumentNeedsConsecutiveRegisters(
10780 Type *Ty, CallingConv::ID CallConv, bool isVarArg) const {
Tim Northover4f1909f2014-05-27 10:43:38 +000010781 if (getEffectiveCallingConv(CallConv, isVarArg) !=
10782 CallingConv::ARM_AAPCS_VFP)
Oliver Stannardc24f2172014-05-09 14:01:47 +000010783 return false;
Tim Northover4f1909f2014-05-27 10:43:38 +000010784
10785 HABaseType Base = HA_UNKNOWN;
10786 uint64_t Members = 0;
10787 bool result = isHomogeneousAggregate(Ty, Base, Members);
10788 DEBUG(dbgs() << "isHA: " << result << " "; Ty->dump(); dbgs() << "\n");
10789 return result;
Oliver Stannardc24f2172014-05-09 14:01:47 +000010790}